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/
|
|
|
|
|
|
|
|
// +build 386 arm
|
|
|
|
|
|
|
|
package runtime
|
|
|
|
|
|
|
|
import "unsafe"
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Constants for multiplication: four random odd 32-bit numbers.
|
|
|
|
m1 = 3168982561
|
|
|
|
m2 = 3339683297
|
|
|
|
m3 = 832293441
|
|
|
|
m4 = 2336365089
|
|
|
|
)
|
|
|
|
|
2015-01-06 17:42:48 -07:00
|
|
|
func memhash(p unsafe.Pointer, seed, s uintptr) uintptr {
|
2014-12-10 18:02:58 -07:00
|
|
|
if GOARCH == "386" && 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 := uint32(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 ^= uint32(*(*byte)(p))
|
|
|
|
h ^= uint32(*(*byte)(add(p, s>>1))) << 8
|
|
|
|
h ^= uint32(*(*byte)(add(p, s-1))) << 16
|
|
|
|
h = rotl_15(h*m1) * m2
|
2014-12-10 18:02:58 -07:00
|
|
|
case s == 4:
|
2015-01-06 10:06:44 -07:00
|
|
|
h ^= readUnaligned32(p)
|
|
|
|
h = rotl_15(h*m1) * m2
|
2014-12-10 18:02:58 -07:00
|
|
|
case s <= 8:
|
2015-01-06 10:06:44 -07:00
|
|
|
h ^= readUnaligned32(p)
|
|
|
|
h = rotl_15(h*m1) * m2
|
|
|
|
h ^= readUnaligned32(add(p, s-4))
|
|
|
|
h = rotl_15(h*m1) * m2
|
2014-12-10 18:02:58 -07:00
|
|
|
case s <= 16:
|
2015-01-06 10:06:44 -07:00
|
|
|
h ^= readUnaligned32(p)
|
|
|
|
h = rotl_15(h*m1) * m2
|
|
|
|
h ^= readUnaligned32(add(p, 4))
|
|
|
|
h = rotl_15(h*m1) * m2
|
|
|
|
h ^= readUnaligned32(add(p, s-8))
|
|
|
|
h = rotl_15(h*m1) * m2
|
|
|
|
h ^= readUnaligned32(add(p, s-4))
|
|
|
|
h = rotl_15(h*m1) * m2
|
2014-12-10 18:02:58 -07:00
|
|
|
default:
|
|
|
|
v1 := h
|
2015-01-06 10:06:44 -07:00
|
|
|
v2 := uint32(hashkey[1])
|
|
|
|
v3 := uint32(hashkey[2])
|
|
|
|
v4 := uint32(hashkey[3])
|
2014-12-10 18:02:58 -07:00
|
|
|
for s >= 16 {
|
2015-01-06 10:06:44 -07:00
|
|
|
v1 ^= readUnaligned32(p)
|
|
|
|
v1 = rotl_15(v1*m1) * m2
|
2014-12-10 18:02:58 -07:00
|
|
|
p = add(p, 4)
|
2015-01-06 10:06:44 -07:00
|
|
|
v2 ^= readUnaligned32(p)
|
|
|
|
v2 = rotl_15(v2*m2) * m3
|
2014-12-10 18:02:58 -07:00
|
|
|
p = add(p, 4)
|
2015-01-06 10:06:44 -07:00
|
|
|
v3 ^= readUnaligned32(p)
|
|
|
|
v3 = rotl_15(v3*m3) * m4
|
2014-12-10 18:02:58 -07:00
|
|
|
p = add(p, 4)
|
2015-01-06 10:06:44 -07:00
|
|
|
v4 ^= readUnaligned32(p)
|
|
|
|
v4 = rotl_15(v4*m4) * m1
|
2014-12-10 18:02:58 -07:00
|
|
|
p = add(p, 4)
|
|
|
|
s -= 16
|
|
|
|
}
|
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 >> 17
|
|
|
|
h *= m3
|
|
|
|
h ^= h >> 13
|
|
|
|
h *= m4
|
|
|
|
h ^= h >> 16
|
|
|
|
return uintptr(h)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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_15(x uint32) uint32 {
|
|
|
|
return (x << 15) | (x >> (32 - 15))
|
|
|
|
}
|