1
0
mirror of https://github.com/golang/go synced 2024-11-14 20:00:31 -07:00

hash/maphash: sync wyhash with runtime implementation

Fixes #69940

Change-Id: I40535d2647f9456d2196241bf7414b1e92b53c2c
Reviewed-on: https://go-review.googlesource.com/c/go/+/621756
Reviewed-by: Keith Randall <khr@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Michael Pratt <mpratt@google.com>
This commit is contained in:
Cuong Manh Le 2024-10-22 14:56:17 +07:00
parent cb69354de3
commit 0f2353997a

View File

@ -13,6 +13,14 @@ import (
"reflect"
)
var hashkey [4]uint64
func init() {
for i := range hashkey {
hashkey[i] = randUint64()
}
}
func rthash(buf []byte, seed uint64) uint64 {
if len(buf) == 0 {
return seed
@ -33,34 +41,28 @@ func randUint64() uint64 {
// This is a port of wyhash implementation in runtime/hash64.go,
// without using unsafe for purego.
const (
m1 = 0xa0761d6478bd642f
m2 = 0xe7037ed1a0b428db
m3 = 0x8ebc6af09c88c6e3
m4 = 0x589965cc75374cc3
m5 = 0x1d8e4e27c47d124f
)
const m5 = 0x1d8e4e27c47d124f
func wyhash(key []byte, seed, len uint64) uint64 {
p := key
i := len
var a, b uint64
seed ^= m1
seed ^= hashkey[0]
if i > 16 {
if i > 48 {
seed1 := seed
seed2 := seed
for ; i > 48; i -= 48 {
seed = mix(r8(p)^m2, r8(p[8:])^seed)
seed1 = mix(r8(p[16:])^m3, r8(p[24:])^seed1)
seed2 = mix(r8(p[32:])^m4, r8(p[40:])^seed2)
seed = mix(r8(p)^hashkey[1], r8(p[8:])^seed)
seed1 = mix(r8(p[16:])^hashkey[2], r8(p[24:])^seed1)
seed2 = mix(r8(p[32:])^hashkey[3], r8(p[40:])^seed2)
p = p[48:]
}
seed ^= seed1 ^ seed2
}
for ; i > 16; i -= 16 {
seed = mix(r8(p)^m2, r8(p[8:])^seed)
seed = mix(r8(p)^hashkey[1], r8(p[8:])^seed)
p = p[16:]
}
}
@ -74,7 +76,7 @@ func wyhash(key []byte, seed, len uint64) uint64 {
a = r4(p)<<32 | r4(p[n:])
b = r4(p[i-4:])<<32 | r4(p[i-4-n:])
}
return mix(m5^len, mix(a^m2, b^seed))
return mix(m5^len, mix(a^hashkey[1], b^seed))
}
func r3(p []byte, k uint64) uint64 {