Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve BCE in CH128 computing #3

Merged
merged 3 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 32 additions & 16 deletions ch_128.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func chMurmur(s []byte, seed U128) U128 {
c := uint64(0)
d := uint64(0)
l := length - 16
if l <= 0 { // length <= 16
if len(s) <= 16 { // length <= 16
a = shiftMix(a*k1) * k1
c = b*k1 + ch0to16(s, length)

Expand All @@ -25,18 +25,31 @@ func chMurmur(s []byte, seed U128) U128 {
d = ch16(b+uint64(length), c+binary.LittleEndian.Uint64(s[length-16:]))
a += d

for {
a ^= shiftMix(binary.LittleEndian.Uint64(s)*k1) * k1
{
a ^= shiftMix(binary.LittleEndian.Uint64(s[0:8:8])*k1) * k1
a *= k1
b ^= a
c ^= shiftMix(binary.LittleEndian.Uint64(s[8:])*k1) * k1
c ^= shiftMix(binary.LittleEndian.Uint64(s[8:8+8:8+8])*k1) * k1
c *= k1
d ^= c
s = s[16:]
l -= 16
}

if l <= 0 {
break
if l > 0 {
for len(s) >= 16 {
a ^= shiftMix(binary.LittleEndian.Uint64(s[0:8:8])*k1) * k1
a *= k1
b ^= a
c ^= shiftMix(binary.LittleEndian.Uint64(s[8:8+8:8+8])*k1) * k1
c *= k1
d ^= c
s = s[16:]
l -= 16

if l <= 0 {
break
}
}
}
}
Expand All @@ -49,8 +62,8 @@ func chMurmur(s []byte, seed U128) U128 {
func CH128(s []byte) U128 {
if len(s) >= 16 {
return CH128Seed(s[16:], U128{
Low: binary.LittleEndian.Uint64(s) ^ k3,
High: binary.LittleEndian.Uint64(s[8:]),
Low: binary.LittleEndian.Uint64(s[0:8:8]) ^ k3,
High: binary.LittleEndian.Uint64(s[8 : 8+8 : 8+8]),
})
}
if len(s) >= 8 {
Expand Down Expand Up @@ -79,17 +92,20 @@ func CH128Seed(s []byte, seed U128) U128 {
y := seed.High
z := uint64(len(s)) * k1

v.Low = rot64(y^k1, 49)*k1 + binary.LittleEndian.Uint64(s)
v.High = rot64(v.Low, 42)*k1 + binary.LittleEndian.Uint64(s[8:])
w.Low = rot64(y+z, 35)*k1 + x
w.High = rot64(x+binary.LittleEndian.Uint64(s[88:]), 53) * k1
{
subSlice := (*[96]byte)(s[0:])
v.Low = rot64(y^k1, 49)*k1 + binary.LittleEndian.Uint64(subSlice[0:])
v.High = rot64(v.Low, 42)*k1 + binary.LittleEndian.Uint64(subSlice[8:])
w.Low = rot64(y+z, 35)*k1 + x
w.High = rot64(x+binary.LittleEndian.Uint64(subSlice[88:]), 53) * k1
}

// This is the same inner loop as CH64(), manually unrolled.
for len(s) >= 128 {
// Roll 1.
{
x = rot64(x+y+v.Low+binary.LittleEndian.Uint64(s[16:]), 37) * k1
y = rot64(y+v.High+binary.LittleEndian.Uint64(s[48:]), 42) * k1
x = rot64(x+y+v.Low+binary.LittleEndian.Uint64(s[16:16+8:16+8]), 37) * k1
y = rot64(y+v.High+binary.LittleEndian.Uint64(s[48:48+8:48+8]), 42) * k1

x ^= w.High
y ^= v.Low
Expand All @@ -103,8 +119,8 @@ func CH128Seed(s []byte, seed U128) U128 {
// Roll 2.
{
const offset = 64
x = rot64(x+y+v.Low+binary.LittleEndian.Uint64(s[offset+16:]), 37) * k1
y = rot64(y+v.High+binary.LittleEndian.Uint64(s[offset+48:]), 42) * k1
x = rot64(x+y+v.Low+binary.LittleEndian.Uint64(s[offset+16:offset+16+8:offset+16+8]), 37) * k1
y = rot64(y+v.High+binary.LittleEndian.Uint64(s[offset+48:offset+48+8:offset+48+8]), 42) * k1
x ^= w.High
y ^= v.Low

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/go-faster/city

go 1.16
go 1.17