| 1 |
// Copyright 2014 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
// Hashing algorithm inspired by |
| 6 |
// wyhash: https://github.com/wangyi-fudan/wyhash |
| 7 |
|
| 8 |
//go:build amd64 || arm64 || mips64 || mips64le || ppc64 || ppc64le || riscv64 || s390x || wasm || alpha || arm64be || ia64 || sparc64 |
| 9 |
|
| 10 |
package runtime |
| 11 |
|
| 12 |
import ( |
| 13 |
"runtime/internal/math" |
| 14 |
"unsafe" |
| 15 |
) |
| 16 |
|
| 17 |
// For gccgo, use go:linkname to export compiler-called functions. |
| 18 |
// |
| 19 |
//go:linkname memhash |
| 20 |
|
| 21 |
const ( |
| 22 |
m1 = 0xa0761d6478bd642f |
| 23 |
m2 = 0xe7037ed1a0b428db |
| 24 |
m3 = 0x8ebc6af09c88c6e3 |
| 25 |
m4 = 0x589965cc75374cc3 |
| 26 |
m5 = 0x1d8e4e27c47d124f |
| 27 |
) |
| 28 |
|
| 29 |
func memhash(p unsafe.Pointer, seed, s uintptr) uintptr { |
| 30 |
if (GOARCH == "amd64" || GOARCH == "arm64") && useAeshash { |
| 31 |
return aeshash(p, seed, s) |
| 32 |
} |
| 33 |
var a, b uintptr |
| 34 |
seed ^= hashkey[0] ^ m1 |
| 35 |
switch { |
| 36 |
case s == 0: |
| 37 |
return seed |
| 38 |
case s < 4: |
| 39 |
a = uintptr(*(*byte)(p)) |
| 40 |
a |= uintptr(*(*byte)(add(p, s>>1))) << 8 |
| 41 |
a |= uintptr(*(*byte)(add(p, s-1))) << 16 |
| 42 |
case s == 4: |
| 43 |
a = r4(p) |
| 44 |
b = a |
| 45 |
case s < 8: |
| 46 |
a = r4(p) |
| 47 |
b = r4(add(p, s-4)) |
| 48 |
case s == 8: |
| 49 |
a = r8(p) |
| 50 |
b = a |
| 51 |
case s <= 16: |
| 52 |
a = r8(p) |
| 53 |
b = r8(add(p, s-8)) |
| 54 |
default: |
| 55 |
l := s |
| 56 |
if l > 48 { |
| 57 |
seed1 := seed |
| 58 |
seed2 := seed |
| 59 |
for ; l > 48; l -= 48 { |
| 60 |
seed = mix(r8(p)^m2, r8(add(p, 8))^seed) |
| 61 |
seed1 = mix(r8(add(p, 16))^m3, r8(add(p, 24))^seed1) |
| 62 |
seed2 = mix(r8(add(p, 32))^m4, r8(add(p, 40))^seed2) |
| 63 |
p = add(p, 48) |
| 64 |
} |
| 65 |
seed ^= seed1 ^ seed2 |
| 66 |
} |
| 67 |
for ; l > 16; l -= 16 { |
| 68 |
seed = mix(r8(p)^m2, r8(add(p, 8))^seed) |
| 69 |
p = add(p, 16) |
| 70 |
} |
| 71 |
a = r8(add(p, l-16)) |
| 72 |
b = r8(add(p, l-8)) |
| 73 |
} |
| 74 |
|
| 75 |
return mix(m5^s, mix(a^m2, b^seed)) |
| 76 |
} |
| 77 |
|
| 78 |
func memhash32(p unsafe.Pointer, seed uintptr) uintptr { |
| 79 |
a := r4(p) |
| 80 |
return mix(m5^4, mix(a^m2, a^seed^hashkey[0]^m1)) |
| 81 |
} |
| 82 |
|
| 83 |
func memhash64(p unsafe.Pointer, seed uintptr) uintptr { |
| 84 |
a := r8(p) |
| 85 |
return mix(m5^8, mix(a^m2, a^seed^hashkey[0]^m1)) |
| 86 |
} |
| 87 |
|
| 88 |
func mix(a, b uintptr) uintptr { |
| 89 |
hi, lo := math.Mul64(uint64(a), uint64(b)) |
| 90 |
return uintptr(hi ^ lo) |
| 91 |
} |
| 92 |
|
| 93 |
func r4(p unsafe.Pointer) uintptr { |
| 94 |
return uintptr(readUnaligned32(p)) |
| 95 |
} |
| 96 |
|
| 97 |
func r8(p unsafe.Pointer) uintptr { |
| 98 |
return uintptr(readUnaligned64(p)) |
| 99 |
} |