Codebase list golang-github-spaolacci-murmur3 / 7a8d3e9
Import upstream version 1.1.0+git20190317.1.539464a Debian Janitor 3 years ago
4 changed file(s) with 76 addition(s) and 162 deletion(s). Raw diff Collapse all Expand all
22 import (
33 //"encoding/binary"
44 "hash"
5 "math/bits"
56 "unsafe"
67 )
78
6768 k1, k2 := t[0], t[1]
6869
6970 k1 *= c1_128
70 k1 = (k1 << 31) | (k1 >> 33) // rotl64(k1, 31)
71 k1 = bits.RotateLeft64(k1, 31)
7172 k1 *= c2_128
7273 h1 ^= k1
7374
74 h1 = (h1 << 27) | (h1 >> 37) // rotl64(h1, 27)
75 h1 = bits.RotateLeft64(h1, 27)
7576 h1 += h2
7677 h1 = h1*5 + 0x52dce729
7778
7879 k2 *= c2_128
79 k2 = (k2 << 33) | (k2 >> 31) // rotl64(k2, 33)
80 k2 = bits.RotateLeft64(k2, 33)
8081 k2 *= c1_128
8182 h2 ^= k2
8283
83 h2 = (h2 << 31) | (h2 >> 33) // rotl64(h2, 31)
84 h2 = bits.RotateLeft64(h2, 31)
8485 h2 += h1
8586 h2 = h2*5 + 0x38495ab5
8687 }
116117 k2 ^= uint64(d.tail[8]) << 0
117118
118119 k2 *= c2_128
119 k2 = (k2 << 33) | (k2 >> 31) // rotl64(k2, 33)
120 k2 = bits.RotateLeft64(k2, 33)
120121 k2 *= c1_128
121122 h2 ^= k2
122123
146147 case 1:
147148 k1 ^= uint64(d.tail[0]) << 0
148149 k1 *= c1_128
149 k1 = (k1 << 31) | (k1 >> 33) // rotl64(k1, 31)
150 k1 = bits.RotateLeft64(k1, 31)
150151 k1 *= c2_128
151152 h1 ^= k1
152153 }
175176 return k
176177 }
177178
178 /*
179 func rotl64(x uint64, r byte) uint64 {
180 return (x << r) | (x >> (64 - r))
181 }
182 */
183
184179 // Sum128 returns the MurmurHash3 sum of data. It is equivalent to the
185180 // following sequence (without the extra burden and the extra allocation):
186181 // hasher := New128()
194189 // hasher.Write(data)
195190 // return hasher.Sum128()
196191 func Sum128WithSeed(data []byte, seed uint32) (h1 uint64, h2 uint64) {
197 d := &digest128{h1: uint64(seed), h2: uint64(seed)}
192 d := digest128{h1: uint64(seed), h2: uint64(seed)}
198193 d.seed = seed
199194 d.tail = d.bmix(data)
200195 d.clen = len(data)
33
44 import (
55 "hash"
6 "math/bits"
67 "unsafe"
78 )
89
5455 k1 := *(*uint32)(unsafe.Pointer(&p[i*4]))
5556
5657 k1 *= c1_32
57 k1 = (k1 << 15) | (k1 >> 17) // rotl32(k1, 15)
58 k1 = bits.RotateLeft32(k1, 15)
5859 k1 *= c2_32
5960
6061 h1 ^= k1
61 h1 = (h1 << 13) | (h1 >> 19) // rotl32(h1, 13)
62 h1 = bits.RotateLeft32(h1, 13)
6263 h1 = h1*4 + h1 + 0xe6546b64
6364 }
6465 d.h1 = h1
8081 case 1:
8182 k1 ^= uint32(d.tail[0])
8283 k1 *= c1_32
83 k1 = (k1 << 15) | (k1 >> 17) // rotl32(k1, 15)
84 k1 = bits.RotateLeft32(k1, 15)
8485 k1 *= c2_32
8586 h1 ^= k1
8687 }
9596
9697 return h1
9798 }
98
99 /*
100 func rotl32(x uint32, r byte) uint32 {
101 return (x << r) | (x >> (32 - r))
102 }
103 */
10499
105100 // Sum32 returns the MurmurHash3 sum of data. It is equivalent to the
106101 // following sequence (without the extra burden and the extra allocation):
128123 k1 := *(*uint32)(unsafe.Pointer(p))
129124
130125 k1 *= c1_32
131 k1 = (k1 << 15) | (k1 >> 17) // rotl32(k1, 15)
126 k1 = bits.RotateLeft32(k1, 15)
132127 k1 *= c2_32
133128
134129 h1 ^= k1
135 h1 = (h1 << 13) | (h1 >> 19) // rotl32(h1, 13)
130 h1 = bits.RotateLeft32(h1, 13)
136131 h1 = h1*4 + h1 + 0xe6546b64
137132 }
138133
149144 case 1:
150145 k1 ^= uint32(tail[0])
151146 k1 *= c1_32
152 k1 = (k1 << 15) | (k1 >> 17) // rotl32(k1, 15)
147 k1 = bits.RotateLeft32(k1, 15)
153148 k1 *= c2_32
154149 h1 ^= k1
155150 }
4747 // hasher.Write(data)
4848 // return hasher.Sum64()
4949 func Sum64WithSeed(data []byte, seed uint32) uint64 {
50 d := &digest128{h1: uint64(seed), h2: uint64(seed)}
50 d := digest128{h1: uint64(seed), h2: uint64(seed)}
5151 d.seed = seed
5252 d.tail = d.bmix(data)
5353 d.clen = len(data)
11
22 import (
33 "fmt"
4 "strconv"
45 "testing"
56 )
67
112113 }
113114 }
114115
115 //---
116
117 func bench32(b *testing.B, length int) {
118 buf := make([]byte, length)
119 b.SetBytes(int64(length))
120 b.ResetTimer()
121 for i := 0; i < b.N; i++ {
122 Sum32(buf)
116 func Benchmark32(b *testing.B) {
117 buf := make([]byte, 8192)
118 for length := 1; length <= cap(buf); length *= 2 {
119 b.Run(strconv.Itoa(length), func(b *testing.B) {
120 buf = buf[:length]
121 b.SetBytes(int64(length))
122 b.ReportAllocs()
123 b.ResetTimer()
124 for i := 0; i < b.N; i++ {
125 Sum32(buf)
126 }
127 })
123128 }
124129 }
125130
126 func Benchmark32_1(b *testing.B) {
127 bench32(b, 1)
128 }
129 func Benchmark32_2(b *testing.B) {
130 bench32(b, 2)
131 }
132 func Benchmark32_4(b *testing.B) {
133 bench32(b, 4)
134 }
135 func Benchmark32_8(b *testing.B) {
136 bench32(b, 8)
137 }
138 func Benchmark32_16(b *testing.B) {
139 bench32(b, 16)
140 }
141 func Benchmark32_32(b *testing.B) {
142 bench32(b, 32)
143 }
144 func Benchmark32_64(b *testing.B) {
145 bench32(b, 64)
146 }
147 func Benchmark32_128(b *testing.B) {
148 bench32(b, 128)
149 }
150 func Benchmark32_256(b *testing.B) {
151 bench32(b, 256)
152 }
153 func Benchmark32_512(b *testing.B) {
154 bench32(b, 512)
155 }
156 func Benchmark32_1024(b *testing.B) {
157 bench32(b, 1024)
158 }
159 func Benchmark32_2048(b *testing.B) {
160 bench32(b, 2048)
161 }
162 func Benchmark32_4096(b *testing.B) {
163 bench32(b, 4096)
164 }
165 func Benchmark32_8192(b *testing.B) {
166 bench32(b, 8192)
167 }
131 func BenchmarkPartial32(b *testing.B) {
132 buf := make([]byte, 128)
133 for length := 8; length <= cap(buf); length *= 2 {
134 b.Run(strconv.Itoa(length), func(b *testing.B) {
135 buf = buf[:length]
136 b.SetBytes(int64(length))
137 b.ReportAllocs()
168138
169 //---
139 start := (32 / 8) / 2
140 chunks := 7
141 k := length / chunks
142 tail := (length - start) % k
170143
171 func benchPartial32(b *testing.B, length int) {
172 buf := make([]byte, length)
173 b.SetBytes(int64(length))
144 b.ResetTimer()
145 for i := 0; i < b.N; i++ {
146 hasher := New32()
147 hasher.Write(buf[0:start])
174148
175 start := (32 / 8) / 2
176 chunks := 7
177 k := length / chunks
178 tail := (length - start) % k
149 for j := start; j+k <= length; j += k {
150 hasher.Write(buf[j : j+k])
151 }
179152
180 b.ResetTimer()
181 for i := 0; i < b.N; i++ {
182 hasher := New32()
183 hasher.Write(buf[0:start])
184
185 for j := start; j+k <= length; j += k {
186 hasher.Write(buf[j : j+k])
187 }
188
189 hasher.Write(buf[length-tail:])
190 hasher.Sum32()
153 hasher.Write(buf[length-tail:])
154 hasher.Sum32()
155 }
156 })
191157 }
192158 }
193159
194 func BenchmarkPartial32_8(b *testing.B) {
195 benchPartial32(b, 8)
196 }
197 func BenchmarkPartial32_16(b *testing.B) {
198 benchPartial32(b, 16)
199 }
200 func BenchmarkPartial32_32(b *testing.B) {
201 benchPartial32(b, 32)
202 }
203 func BenchmarkPartial32_64(b *testing.B) {
204 benchPartial32(b, 64)
205 }
206 func BenchmarkPartial32_128(b *testing.B) {
207 benchPartial32(b, 128)
208 }
209
210 //---
211
212 func bench128(b *testing.B, length int) {
213 buf := make([]byte, length)
214 b.SetBytes(int64(length))
215 b.ResetTimer()
216 for i := 0; i < b.N; i++ {
217 Sum128(buf)
160 func Benchmark64(b *testing.B) {
161 buf := make([]byte, 8192)
162 for length := 1; length <= cap(buf); length *= 2 {
163 b.Run(strconv.Itoa(length), func(b *testing.B) {
164 buf = buf[:length]
165 b.SetBytes(int64(length))
166 b.ReportAllocs()
167 b.ResetTimer()
168 for i := 0; i < b.N; i++ {
169 Sum64(buf)
170 }
171 })
218172 }
219173 }
220174
221 func Benchmark128_1(b *testing.B) {
222 bench128(b, 1)
175 func Benchmark128(b *testing.B) {
176 buf := make([]byte, 8192)
177 for length := 1; length <= cap(buf); length *= 2 {
178 b.Run(strconv.Itoa(length), func(b *testing.B) {
179 buf = buf[:length]
180 b.SetBytes(int64(length))
181 b.ReportAllocs()
182 b.ResetTimer()
183 for i := 0; i < b.N; i++ {
184 Sum128(buf)
185 }
186 })
187 }
223188 }
224 func Benchmark128_2(b *testing.B) {
225 bench128(b, 2)
226 }
227 func Benchmark128_4(b *testing.B) {
228 bench128(b, 4)
229 }
230 func Benchmark128_8(b *testing.B) {
231 bench128(b, 8)
232 }
233 func Benchmark128_16(b *testing.B) {
234 bench128(b, 16)
235 }
236 func Benchmark128_32(b *testing.B) {
237 bench128(b, 32)
238 }
239 func Benchmark128_64(b *testing.B) {
240 bench128(b, 64)
241 }
242 func Benchmark128_128(b *testing.B) {
243 bench128(b, 128)
244 }
245 func Benchmark128_256(b *testing.B) {
246 bench128(b, 256)
247 }
248 func Benchmark128_512(b *testing.B) {
249 bench128(b, 512)
250 }
251 func Benchmark128_1024(b *testing.B) {
252 bench128(b, 1024)
253 }
254 func Benchmark128_2048(b *testing.B) {
255 bench128(b, 2048)
256 }
257 func Benchmark128_4096(b *testing.B) {
258 bench128(b, 4096)
259 }
260 func Benchmark128_8192(b *testing.B) {
261 bench128(b, 8192)
262 }
263
264 //---