3 // http://code.google.com/p/guava-libraries/source/browse/guava/src/com/google/common/hash/Murmur3_32HashFunction.java
10 // Make sure interfaces are correctly implemented.
12 _ hash.Hash = new(digest32)
13 _ hash.Hash32 = new(digest32)
17 c1_32 uint32 = 0xcc9e2d51
18 c2_32 uint32 = 0x1b873593
21 // digest32 represents a partial evaluation of a 32 bites hash.
22 type digest32 struct {
24 h1 uint32 // Unfinalized running hash.
27 func New32() hash.Hash32 {
34 func (d *digest32) Size() int { return 4 }
36 func (d *digest32) reset() { d.h1 = 0 }
38 func (d *digest32) Sum(b []byte) []byte {
40 return append(b, byte(h>>24), byte(h>>16), byte(h>>8), byte(h))
43 // Digest as many blocks as possible.
44 func (d *digest32) bmix(p []byte) (tail []byte) {
48 for i := 0; i < nblocks; i++ {
49 k1 := *(*uint32)(unsafe.Pointer(&p[i*4]))
52 k1 = (k1 << 15) | (k1 >> 17) // rotl32(k1, 15)
56 h1 = (h1 << 13) | (h1 >> 19) // rotl32(h1, 13)
57 h1 = h1*5 + 0xe6546b64
60 return p[nblocks*d.Size():]
63 func (d *digest32) Sum32() (h1 uint32) {
68 switch len(d.tail) & 3 {
70 k1 ^= uint32(d.tail[2]) << 16
73 k1 ^= uint32(d.tail[1]) << 8
76 k1 ^= uint32(d.tail[0])
78 k1 = (k1 << 15) | (k1 >> 17) // rotl32(k1, 15)
95 func rotl32(x uint32, r byte) uint32 {
96 return (x << r) | (x >> (32 - r))
100 // Sum32 returns the MurmurHash3 sum of data. It is equivalent to the
101 // following sequence (without the extra burden and the extra allocation):
103 // hasher.Write(data)
104 // return hasher.Sum32()
105 func Sum32(data []byte) uint32 {
109 nblocks := len(data) / 4
112 p = uintptr(unsafe.Pointer(&data[0]))
114 p1 := p + uintptr(4*nblocks)
115 for ; p < p1; p += 4 {
116 k1 := *(*uint32)(unsafe.Pointer(p))
119 k1 = (k1 << 15) | (k1 >> 17) // rotl32(k1, 15)
123 h1 = (h1 << 13) | (h1 >> 19) // rotl32(h1, 13)
124 h1 = h1*5 + 0xe6546b64
127 tail := data[nblocks*4:]
130 switch len(tail) & 3 {
132 k1 ^= uint32(tail[2]) << 16
135 k1 ^= uint32(tail[1]) << 8
138 k1 ^= uint32(tail[0])
140 k1 = (k1 << 15) | (k1 >> 17) // rotl32(k1, 15)
145 h1 ^= uint32(len(data))