Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Apr 21, 2024
1 parent 0cd637e commit 013d7d3
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 29 deletions.
16 changes: 6 additions & 10 deletions xxhash/xxhash32/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (d *digest) Sum32() uint32 {
p := 0
n := d.nx
for n := n - 4; p <= n; p += 4 {
tmp += getu32(d.x[p:p+4]) * prime[2]
tmp += getu32(d.x[p:]) * prime[2]
tmp = rotl(tmp, 17) * prime[3]
}

Expand All @@ -106,22 +106,18 @@ func (d *digest) Sum32() uint32 {
tmp = rotl(tmp, 11) * prime[0]
}

tmp ^= tmp >> 15
tmp *= prime[1]
tmp ^= tmp >> 13
tmp *= prime[2]
tmp ^= tmp >> 16
tmp = avalanche(tmp)

return tmp
}

func (d *digest) compress(data []byte) {
datas := bytesToUint32s(data)

d.s[0] = rotl(d.s[0] + datas[0] * prime[1], 13) * prime[0]
d.s[1] = rotl(d.s[1] + datas[1] * prime[1], 13) * prime[0]
d.s[2] = rotl(d.s[2] + datas[2] * prime[1], 13) * prime[0]
d.s[3] = rotl(d.s[3] + datas[3] * prime[1], 13) * prime[0]
d.s[0] = round(d.s[0], datas[0])
d.s[1] = round(d.s[1], datas[1])
d.s[2] = round(d.s[2], datas[2])
d.s[3] = round(d.s[3], datas[3])
}

// checksum returns the 32bits Hash value.
Expand Down
18 changes: 18 additions & 0 deletions xxhash/xxhash32/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,21 @@ func uint32sToBytes(w []uint32) []byte {
func rotl(x, n uint32) uint32 {
return bits.RotateLeft32(x, int(n))
}

func round(acc, input uint32) uint32 {
acc += input * prime[1]
acc = rotl(acc, 13)
acc *= prime[0]

return acc
}

func avalanche(h32 uint32) uint32 {
h32 ^= h32 >> 15
h32 *= prime[1]
h32 ^= h32 >> 13
h32 *= prime[2]
h32 ^= h32 >> 16

return h32
}
29 changes: 10 additions & 19 deletions xxhash/xxhash64/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,10 @@ func (d *digest) Sum64() uint64 {
if d.len >= 32 {
tmp = rotl(d.s[0], 1) + rotl(d.s[1], 7) + rotl(d.s[2], 12) + rotl(d.s[3], 18)

d.s[0] *= prime[1]
d.s[1] *= prime[1]
d.s[2] *= prime[1]
d.s[3] *= prime[1]

tmp = (tmp ^ (rotl(d.s[0], 31) * prime[0])) * prime[0] + prime[3]
tmp = (tmp ^ (rotl(d.s[1], 31) * prime[0])) * prime[0] + prime[3]
tmp = (tmp ^ (rotl(d.s[2], 31) * prime[0])) * prime[0] + prime[3]
tmp = (tmp ^ (rotl(d.s[3], 31) * prime[0])) * prime[0] + prime[3]
tmp = mergeRound(tmp, d.s[0])
tmp = mergeRound(tmp, d.s[1])
tmp = mergeRound(tmp, d.s[2])
tmp = mergeRound(tmp, d.s[3])

tmp += d.len
} else {
Expand All @@ -117,7 +112,7 @@ func (d *digest) Sum64() uint64 {
if p + 4 <= n {
sub := d.x[p : p+4]
tmp ^= uint64(getu32(sub)) * prime[0]
tmp = rotl(tmp, 23)*prime[1] + prime[2]
tmp = rotl(tmp, 23) * prime[1] + prime[2]
p += 4
}

Expand All @@ -126,22 +121,18 @@ func (d *digest) Sum64() uint64 {
tmp = rotl(tmp, 11) * prime[0]
}

tmp ^= tmp >> 33
tmp *= prime[1]
tmp ^= tmp >> 29
tmp *= prime[2]
tmp ^= tmp >> 32
tmp = avalanche(tmp)

return tmp
}

func (d *digest) compress(data []byte) {
datas := bytesToUint64s(data)

d.s[0] = rotl(d.s[0] + datas[0] * prime[1], 31) * prime[0]
d.s[1] = rotl(d.s[1] + datas[1] * prime[1], 31) * prime[0]
d.s[2] = rotl(d.s[2] + datas[2] * prime[1], 31) * prime[0]
d.s[3] = rotl(d.s[3] + datas[3] * prime[1], 31) * prime[0]
d.s[0] = round(d.s[0], datas[0])
d.s[1] = round(d.s[1], datas[1])
d.s[2] = round(d.s[2], datas[2])
d.s[3] = round(d.s[3], datas[3])
}

// checksum returns the 64bits Hash value.
Expand Down
23 changes: 23 additions & 0 deletions xxhash/xxhash64/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,26 @@ func uint64sToBytes(w []uint64) []byte {
func rotl(x, n uint64) uint64 {
return bits.RotateLeft64(x, int(n))
}

func round(acc, input uint64) uint64 {
acc += input * prime[1]
acc = rotl(acc, 31)
acc *= prime[0]
return acc
}

func mergeRound(acc, val uint64) uint64 {
val = round(0, val)
acc ^= val
acc = acc * prime[0] + prime[3]
return acc
}

func avalanche(h64 uint64) uint64 {
h64 ^= h64 >> 33
h64 *= prime[1]
h64 ^= h64 >> 29
h64 *= prime[2]
h64 ^= h64 >> 32
return h64
}

0 comments on commit 013d7d3

Please sign in to comment.