Debian Codegraph @

external/murmur3/murmur32.go

raw
1 package murmur3
2
3 // http://code.google.com/p/guava-libraries/source/browse/guava/src/com/google/common/hash/Murmur3_32HashFunction.java
4
5 import (
6 "encoding/binary"
7 "hash"
8 )
9
10 // Make sure interfaces are correctly implemented.
11 var (
12 _ hash.Hash = new(digest32)
13 _ hash.Hash32 = new(digest32)
14 )
15
16 const (
17 c1_32 uint32 = 0xcc9e2d51
18 c2_32 uint32 = 0x1b873593
19 )
20
21 // digest32 represents a partial evaluation of a 32 bites hash.
22 type digest32 struct {
23 digest
24 h1 uint32 // Unfinalized running hash.
25 }
26
27 func New32() hash.Hash32 {
28 d := new(digest32)
29 d.bmixer = d
30 d.Reset()
31 return d
32 }
33
34 func (d *digest32) Size() int { return 4 }
35
36 func (d *digest32) reset() { d.h1 = 0 }
37
38 func (d *digest32) Sum(b []byte) []byte {
39 h := d.h1
40 return append(b, byte(h>>24), byte(h>>16), byte(h>>8), byte(h))
41 }
42
43 // Digest as many blocks as possible.
44 func (d *digest32) bmix(p []byte) (tail []byte) {
45 h1 := d.h1
46
47 nblocks := len(p) / 4
48 for i := 0; i < nblocks; i++ {
49 offset := i * 4
50 k1 := binary.LittleEndian.Uint32(p[offset : offset+4])
51
52 k1 *= c1_32
53 k1 = (k1 << 15) | (k1 >> 17) // rotl32(k1, 15)
54 k1 *= c2_32
55
56 h1 ^= k1
57 h1 = (h1 << 13) | (h1 >> 19) // rotl32(h1, 13)
58 h1 = h1*5 + 0xe6546b64
59 }
60 d.h1 = h1
61 return p[nblocks*d.Size():]
62 }
63
64 func (d *digest32) Sum32() (h1 uint32) {
65
66 h1 = d.h1
67
68 var k1 uint32
69 switch len(d.tail) & 3 {
70 case 3:
71 k1 ^= uint32(d.tail[2]) << 16
72 fallthrough
73 case 2:
74 k1 ^= uint32(d.tail[1]) << 8
75 fallthrough
76 case 1:
77 k1 ^= uint32(d.tail[0])
78 k1 *= c1_32
79 k1 = (k1 << 15) | (k1 >> 17) // rotl32(k1, 15)
80 k1 *= c2_32
81 h1 ^= k1
82 }
83
84 h1 ^= uint32(d.clen)
85
86 h1 ^= h1 >> 16
87 h1 *= 0x85ebca6b
88 h1 ^= h1 >> 13
89 h1 *= 0xc2b2ae35
90 h1 ^= h1 >> 16
91
92 return h1
93 }
94
95 /*
96 func rotl32(x uint32, r byte) uint32 {
97 return (x << r) | (x >> (32 - r))
98 }
99 */
100
101 // Sum32 returns the MurmurHash3 sum of data. It is equivalent to the
102 // following sequence (without the extra burden and the extra allocation):
103 // hasher := New32()
104 // hasher.Write(data)
105 // return hasher.Sum32()
106 func Sum32(data []byte) uint32 {
107
108 var h1 uint32 = 0
109
110 nblocks := len(data) / 4
111 for i := 0; i < nblocks; i++ {
112 offset := i * 4
113 k1 := binary.LittleEndian.Uint32(data[offset : offset+4])
114
115 k1 *= c1_32
116 k1 = (k1 << 15) | (k1 >> 17) // rotl32(k1, 15)
117 k1 *= c2_32
118
119 h1 ^= k1
120 h1 = (h1 << 13) | (h1 >> 19) // rotl32(h1, 13)
121 h1 = h1*5 + 0xe6546b64
122 }
123
124 tail := data[nblocks*4:]
125
126 var k1 uint32
127 switch len(tail) & 3 {
128 case 3:
129 k1 ^= uint32(tail[2]) << 16
130 fallthrough
131 case 2:
132 k1 ^= uint32(tail[1]) << 8
133 fallthrough
134 case 1:
135 k1 ^= uint32(tail[0])
136 k1 *= c1_32
137 k1 = (k1 << 15) | (k1 >> 17) // rotl32(k1, 15)
138 k1 *= c2_32
139 h1 ^= k1
140 }
141
142 h1 ^= uint32(len(data))
143
144 h1 ^= h1 >> 16
145 h1 *= 0x85ebca6b
146 h1 ^= h1 >> 13
147 h1 *= 0xc2b2ae35
148 h1 ^= h1 >> 16
149
150 return h1
151 }