summaryrefslogtreecommitdiffstats
path: root/src/runtime/hash32.go
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-16 19:19:13 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-16 19:19:13 +0000
commitccd992355df7192993c666236047820244914598 (patch)
treef00fea65147227b7743083c6148396f74cd66935 /src/runtime/hash32.go
parentInitial commit. (diff)
downloadgolang-1.21-ccd992355df7192993c666236047820244914598.tar.xz
golang-1.21-ccd992355df7192993c666236047820244914598.zip
Adding upstream version 1.21.8.upstream/1.21.8
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/runtime/hash32.go')
-rw-r--r--src/runtime/hash32.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/runtime/hash32.go b/src/runtime/hash32.go
new file mode 100644
index 0000000..0616c7d
--- /dev/null
+++ b/src/runtime/hash32.go
@@ -0,0 +1,62 @@
+// Copyright 2014 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Hashing algorithm inspired by
+// wyhash: https://github.com/wangyi-fudan/wyhash/blob/ceb019b530e2c1c14d70b79bfa2bc49de7d95bc1/Modern%20Non-Cryptographic%20Hash%20Function%20and%20Pseudorandom%20Number%20Generator.pdf
+
+//go:build 386 || arm || mips || mipsle
+
+package runtime
+
+import "unsafe"
+
+func memhash32Fallback(p unsafe.Pointer, seed uintptr) uintptr {
+ a, b := mix32(uint32(seed), uint32(4^hashkey[0]))
+ t := readUnaligned32(p)
+ a ^= t
+ b ^= t
+ a, b = mix32(a, b)
+ a, b = mix32(a, b)
+ return uintptr(a ^ b)
+}
+
+func memhash64Fallback(p unsafe.Pointer, seed uintptr) uintptr {
+ a, b := mix32(uint32(seed), uint32(8^hashkey[0]))
+ a ^= readUnaligned32(p)
+ b ^= readUnaligned32(add(p, 4))
+ a, b = mix32(a, b)
+ a, b = mix32(a, b)
+ return uintptr(a ^ b)
+}
+
+func memhashFallback(p unsafe.Pointer, seed, s uintptr) uintptr {
+
+ a, b := mix32(uint32(seed), uint32(s^hashkey[0]))
+ if s == 0 {
+ return uintptr(a ^ b)
+ }
+ for ; s > 8; s -= 8 {
+ a ^= readUnaligned32(p)
+ b ^= readUnaligned32(add(p, 4))
+ a, b = mix32(a, b)
+ p = add(p, 8)
+ }
+ if s >= 4 {
+ a ^= readUnaligned32(p)
+ b ^= readUnaligned32(add(p, s-4))
+ } else {
+ t := uint32(*(*byte)(p))
+ t |= uint32(*(*byte)(add(p, s>>1))) << 8
+ t |= uint32(*(*byte)(add(p, s-1))) << 16
+ b ^= t
+ }
+ a, b = mix32(a, b)
+ a, b = mix32(a, b)
+ return uintptr(a ^ b)
+}
+
+func mix32(a, b uint32) (uint32, uint32) {
+ c := uint64(a^uint32(hashkey[1])) * uint64(b^uint32(hashkey[2]))
+ return uint32(c), uint32(c >> 32)
+}