| 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/blob/ceb019b530e2c1c14d70b79bfa2bc49de7d95bc1/Modern%20Non-Cryptographic%20Hash%20Function%20and%20Pseudorandom%20Number%20Generator.pdf |
| 7 |
|
| 8 |
//go:build 386 || arm || mips || mipsle || amd64p32 || armbe || m68k || mips64p32 || mips64p32le || nios2 || ppc || riscv || s390 || sh || shbe || sparc |
| 9 |
|
| 10 |
package runtime |
| 11 |
|
| 12 |
import "unsafe" |
| 13 |
|
| 14 |
// For gccgo, use go:linkname to export compiler-called functions. |
| 15 |
// |
| 16 |
//go:linkname memhash |
| 17 |
|
| 18 |
func memhash32(p unsafe.Pointer, seed uintptr) uintptr { |
| 19 |
a, b := mix32(uint32(seed), uint32(4^hashkey[0])) |
| 20 |
t := readUnaligned32(p) |
| 21 |
a ^= t |
| 22 |
b ^= t |
| 23 |
a, b = mix32(a, b) |
| 24 |
a, b = mix32(a, b) |
| 25 |
return uintptr(a ^ b) |
| 26 |
} |
| 27 |
|
| 28 |
func memhash64(p unsafe.Pointer, seed uintptr) uintptr { |
| 29 |
a, b := mix32(uint32(seed), uint32(8^hashkey[0])) |
| 30 |
a ^= readUnaligned32(p) |
| 31 |
b ^= readUnaligned32(add(p, 4)) |
| 32 |
a, b = mix32(a, b) |
| 33 |
a, b = mix32(a, b) |
| 34 |
return uintptr(a ^ b) |
| 35 |
} |
| 36 |
|
| 37 |
func memhash(p unsafe.Pointer, seed, s uintptr) uintptr { |
| 38 |
if GOARCH == "386" && GOOS != "nacl" && useAeshash { |
| 39 |
return aeshash(p, seed, s) |
| 40 |
} |
| 41 |
a, b := mix32(uint32(seed), uint32(s^hashkey[0])) |
| 42 |
if s == 0 { |
| 43 |
return uintptr(a ^ b) |
| 44 |
} |
| 45 |
for ; s > 8; s -= 8 { |
| 46 |
a ^= readUnaligned32(p) |
| 47 |
b ^= readUnaligned32(add(p, 4)) |
| 48 |
a, b = mix32(a, b) |
| 49 |
p = add(p, 8) |
| 50 |
} |
| 51 |
if s >= 4 { |
| 52 |
a ^= readUnaligned32(p) |
| 53 |
b ^= readUnaligned32(add(p, s-4)) |
| 54 |
} else { |
| 55 |
t := uint32(*(*byte)(p)) |
| 56 |
t |= uint32(*(*byte)(add(p, s>>1))) << 8 |
| 57 |
t |= uint32(*(*byte)(add(p, s-1))) << 16 |
| 58 |
b ^= t |
| 59 |
} |
| 60 |
a, b = mix32(a, b) |
| 61 |
a, b = mix32(a, b) |
| 62 |
return uintptr(a ^ b) |
| 63 |
} |
| 64 |
|
| 65 |
func mix32(a, b uint32) (uint32, uint32) { |
| 66 |
c := uint64(a^uint32(hashkey[1])) * uint64(b^uint32(hashkey[2])) |
| 67 |
return uint32(c), uint32(c >> 32) |
| 68 |
} |