2014-12-10 18:02:58 -07:00
|
|
|
// 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
|
|
|
|
// xxhash: https://code.google.com/p/xxhash/
|
|
|
|
// cityhash: https://code.google.com/p/cityhash/
|
|
|
|
|
2016-03-18 17:02:52 -06:00
|
|
|
// +build amd64 amd64p32 arm64 mips64 mips64le ppc64 ppc64le s390x
|
2014-12-10 18:02:58 -07:00
|
|
|
|
|
|
|
package runtime
|
|
|
|
|
|
|
|
import "unsafe"
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Constants for multiplication: four random odd 64-bit numbers.
|
|
|
|
m1 = 16877499708836156737
|
|
|
|
m2 = 2820277070424839065
|
|
|
|
m3 = 9497967016996688599
|
|
|
|
m4 = 15839092249703872147
|
|
|
|
)
|
|
|
|
|
2015-01-06 17:42:48 -07:00
|
|
|
func memhash(p unsafe.Pointer, seed, s uintptr) uintptr {
|
2018-04-03 23:15:22 -06:00
|
|
|
if (GOARCH == "amd64" || GOARCH == "arm64") &&
|
|
|
|
GOOS != "nacl" && useAeshash {
|
2015-01-06 17:42:48 -07:00
|
|
|
return aeshash(p, seed, s)
|
2014-12-10 18:02:58 -07:00
|
|
|
}
|
2015-01-06 10:06:44 -07:00
|
|
|
h := uint64(seed + s*hashkey[0])
|
2014-12-10 18:02:58 -07:00
|
|
|
tail:
|
|
|
|
switch {
|
|
|
|
case s == 0:
|
|
|
|
case s < 4:
|
2015-01-06 10:06:44 -07:00
|
|
|
h ^= uint64(*(*byte)(p))
|
|
|
|
h ^= uint64(*(*byte)(add(p, s>>1))) << 8
|
|
|
|
h ^= uint64(*(*byte)(add(p, s-1))) << 16
|
|
|
|
h = rotl_31(h*m1) * m2
|
2014-12-10 18:02:58 -07:00
|
|
|
case s <= 8:
|
2015-01-06 10:06:44 -07:00
|
|
|
h ^= uint64(readUnaligned32(p))
|
|
|
|
h ^= uint64(readUnaligned32(add(p, s-4))) << 32
|
|
|
|
h = rotl_31(h*m1) * m2
|
2014-12-10 18:02:58 -07:00
|
|
|
case s <= 16:
|
2015-01-06 10:06:44 -07:00
|
|
|
h ^= readUnaligned64(p)
|
|
|
|
h = rotl_31(h*m1) * m2
|
|
|
|
h ^= readUnaligned64(add(p, s-8))
|
|
|
|
h = rotl_31(h*m1) * m2
|
2014-12-10 18:02:58 -07:00
|
|
|
case s <= 32:
|
2015-01-06 10:06:44 -07:00
|
|
|
h ^= readUnaligned64(p)
|
|
|
|
h = rotl_31(h*m1) * m2
|
|
|
|
h ^= readUnaligned64(add(p, 8))
|
|
|
|
h = rotl_31(h*m1) * m2
|
|
|
|
h ^= readUnaligned64(add(p, s-16))
|
|
|
|
h = rotl_31(h*m1) * m2
|
|
|
|
h ^= readUnaligned64(add(p, s-8))
|
|
|
|
h = rotl_31(h*m1) * m2
|
2014-12-10 18:02:58 -07:00
|
|
|
default:
|
|
|
|
v1 := h
|
2015-08-31 17:26:12 -06:00
|
|
|
v2 := uint64(seed * hashkey[1])
|
|
|
|
v3 := uint64(seed * hashkey[2])
|
|
|
|
v4 := uint64(seed * hashkey[3])
|
2014-12-10 18:02:58 -07:00
|
|
|
for s >= 32 {
|
2015-01-06 10:06:44 -07:00
|
|
|
v1 ^= readUnaligned64(p)
|
|
|
|
v1 = rotl_31(v1*m1) * m2
|
2014-12-10 18:02:58 -07:00
|
|
|
p = add(p, 8)
|
2015-01-06 10:06:44 -07:00
|
|
|
v2 ^= readUnaligned64(p)
|
|
|
|
v2 = rotl_31(v2*m2) * m3
|
2014-12-10 18:02:58 -07:00
|
|
|
p = add(p, 8)
|
2015-01-06 10:06:44 -07:00
|
|
|
v3 ^= readUnaligned64(p)
|
|
|
|
v3 = rotl_31(v3*m3) * m4
|
2014-12-10 18:02:58 -07:00
|
|
|
p = add(p, 8)
|
2015-01-06 10:06:44 -07:00
|
|
|
v4 ^= readUnaligned64(p)
|
|
|
|
v4 = rotl_31(v4*m4) * m1
|
2014-12-10 18:02:58 -07:00
|
|
|
p = add(p, 8)
|
|
|
|
s -= 32
|
|
|
|
}
|
2015-01-06 10:06:44 -07:00
|
|
|
h = v1 ^ v2 ^ v3 ^ v4
|
2014-12-10 18:02:58 -07:00
|
|
|
goto tail
|
|
|
|
}
|
|
|
|
|
|
|
|
h ^= h >> 29
|
|
|
|
h *= m3
|
|
|
|
h ^= h >> 32
|
|
|
|
return uintptr(h)
|
|
|
|
}
|
|
|
|
|
2017-08-27 06:05:11 -06:00
|
|
|
func memhash32(p unsafe.Pointer, seed uintptr) uintptr {
|
|
|
|
h := uint64(seed + 4*hashkey[0])
|
|
|
|
v := uint64(readUnaligned32(p))
|
|
|
|
h ^= v
|
|
|
|
h ^= v << 32
|
|
|
|
h = rotl_31(h*m1) * m2
|
|
|
|
h ^= h >> 29
|
|
|
|
h *= m3
|
|
|
|
h ^= h >> 32
|
|
|
|
return uintptr(h)
|
|
|
|
}
|
|
|
|
|
|
|
|
func memhash64(p unsafe.Pointer, seed uintptr) uintptr {
|
|
|
|
h := uint64(seed + 8*hashkey[0])
|
2017-08-28 22:10:17 -06:00
|
|
|
h ^= uint64(readUnaligned32(p)) | uint64(readUnaligned32(add(p, 4)))<<32
|
2017-08-27 06:05:11 -06:00
|
|
|
h = rotl_31(h*m1) * m2
|
|
|
|
h ^= h >> 29
|
|
|
|
h *= m3
|
|
|
|
h ^= h >> 32
|
|
|
|
return uintptr(h)
|
|
|
|
}
|
|
|
|
|
2014-12-10 18:02:58 -07:00
|
|
|
// Note: in order to get the compiler to issue rotl instructions, we
|
|
|
|
// need to constant fold the shift amount by hand.
|
|
|
|
// TODO: convince the compiler to issue rotl instructions after inlining.
|
|
|
|
func rotl_31(x uint64) uint64 {
|
|
|
|
return (x << 31) | (x >> (64 - 31))
|
|
|
|
}
|