-
Notifications
You must be signed in to change notification settings - Fork 89
/
murmurhash32.go
55 lines (45 loc) · 864 Bytes
/
murmurhash32.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package hash
import (
"math/bits"
)
const (
c1 uint32 = 0xcc9e2d51
c2 uint32 = 0x1b873593
)
// Sum32WithSeed is a port of MurmurHash3_x86_32 function.
func Sum32WithSeed(data []byte, seed uint32) uint32 {
h1 := seed
dlen := len(data)
for len(data) >= 4 {
k1 := uint32(data[0]) | uint32(data[1])<<8 | uint32(data[2])<<16 | uint32(data[3])<<24
data = data[4:]
k1 *= c1
k1 = bits.RotateLeft32(k1, 15)
k1 *= c2
h1 ^= k1
h1 = bits.RotateLeft32(h1, 13)
h1 = h1*5 + 0xe6546b64
}
var k1 uint32
switch len(data) {
case 3:
k1 ^= uint32(data[2]) << 16
fallthrough
case 2:
k1 ^= uint32(data[1]) << 8
fallthrough
case 1:
k1 ^= uint32(data[0])
k1 *= c1
k1 = bits.RotateLeft32(k1, 15)
k1 *= c2
h1 ^= k1
}
h1 ^= uint32(dlen)
h1 ^= h1 >> 16
h1 *= 0x85ebca6b
h1 ^= h1 >> 13
h1 *= 0xc2b2ae35
h1 ^= h1 >> 16
return h1
}