runtime: use hash32, not hash64, for amd64p32, mips64p32, mips64p32le
[official-gcc.git] / libgo / go / runtime / hash32.go
blob0df73035c051d754e8a62e5295078dd4d8df7a1c
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.
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
8 //go:build 386 || arm || mips || mipsle || amd64p32 || armbe || m68k || mips64p32 || mips64p32le || nios2 || ppc || riscv || s390 || sh || shbe || sparc
9 // +build 386 arm mips mipsle amd64p32 armbe m68k mips64p32 mips64p32le nios2 ppc riscv s390 sh shbe sparc
11 package runtime
13 import "unsafe"
15 // For gccgo, use go:linkname to export compiler-called functions.
17 //go:linkname memhash
19 func memhash32(p unsafe.Pointer, seed uintptr) uintptr {
20 a, b := mix32(uint32(seed), uint32(4^hashkey[0]))
21 t := readUnaligned32(p)
22 a ^= t
23 b ^= t
24 a, b = mix32(a, b)
25 a, b = mix32(a, b)
26 return uintptr(a ^ b)
29 func memhash64(p unsafe.Pointer, seed uintptr) uintptr {
30 a, b := mix32(uint32(seed), uint32(8^hashkey[0]))
31 a ^= readUnaligned32(p)
32 b ^= readUnaligned32(add(p, 4))
33 a, b = mix32(a, b)
34 a, b = mix32(a, b)
35 return uintptr(a ^ b)
38 func memhash(p unsafe.Pointer, seed, s uintptr) uintptr {
39 if GOARCH == "386" && GOOS != "nacl" && useAeshash {
40 return aeshash(p, seed, s)
42 a, b := mix32(uint32(seed), uint32(s^hashkey[0]))
43 if s == 0 {
44 return uintptr(a ^ b)
46 for ; s > 8; s -= 8 {
47 a ^= readUnaligned32(p)
48 b ^= readUnaligned32(add(p, 4))
49 a, b = mix32(a, b)
50 p = add(p, 8)
52 if s >= 4 {
53 a ^= readUnaligned32(p)
54 b ^= readUnaligned32(add(p, s-4))
55 } else {
56 t := uint32(*(*byte)(p))
57 t |= uint32(*(*byte)(add(p, s>>1))) << 8
58 t |= uint32(*(*byte)(add(p, s-1))) << 16
59 b ^= t
61 a, b = mix32(a, b)
62 a, b = mix32(a, b)
63 return uintptr(a ^ b)
66 func mix32(a, b uint32) (uint32, uint32) {
67 c := uint64(a^uint32(hashkey[1])) * uint64(b^uint32(hashkey[2]))
68 return uint32(c), uint32(c >> 32)