|
0 |
//-----------------------------------------------------------------------------
|
|
1 |
// MurmurHash3 was written by Austin Appleby, and is placed in the public
|
|
2 |
// domain. The author hereby disclaims copyright to this source code.
|
|
3 |
|
|
4 |
// Note - The x86 and x64 versions do _not_ produce the same results, as the
|
|
5 |
// algorithms are optimized for their respective platforms. You can still
|
|
6 |
// compile and run any of them on any platform, but your performance with the
|
|
7 |
// non-native version will be less than optimal.
|
|
8 |
|
|
9 |
#include "MurmurHash3.h"
|
|
10 |
|
|
11 |
//-----------------------------------------------------------------------------
|
|
12 |
// Platform-specific functions and macros
|
|
13 |
|
|
14 |
// Microsoft Visual Studio
|
|
15 |
|
|
16 |
#if defined(_MSC_VER)
|
|
17 |
|
|
18 |
#define FORCE_INLINE __forceinline
|
|
19 |
|
|
20 |
#include <stdlib.h>
|
|
21 |
|
|
22 |
#define ROTL32(x,y) _rotl(x,y)
|
|
23 |
#define ROTL64(x,y) _rotl64(x,y)
|
|
24 |
|
|
25 |
#define BIG_CONSTANT(x) (x)
|
|
26 |
|
|
27 |
// Other compilers
|
|
28 |
|
|
29 |
#else // defined(_MSC_VER)
|
|
30 |
|
|
31 |
#define FORCE_INLINE inline __attribute__((always_inline))
|
|
32 |
|
|
33 |
inline uint32_t rotl32 ( uint32_t x, int8_t r )
|
|
34 |
{
|
|
35 |
return (x << r) | (x >> (32 - r));
|
|
36 |
}
|
|
37 |
|
|
38 |
inline uint64_t rotl64 ( uint64_t x, int8_t r )
|
|
39 |
{
|
|
40 |
return (x << r) | (x >> (64 - r));
|
|
41 |
}
|
|
42 |
|
|
43 |
#define ROTL32(x,y) rotl32(x,y)
|
|
44 |
#define ROTL64(x,y) rotl64(x,y)
|
|
45 |
|
|
46 |
#define BIG_CONSTANT(x) (x##LLU)
|
|
47 |
|
|
48 |
#endif // !defined(_MSC_VER)
|
|
49 |
|
|
50 |
//-----------------------------------------------------------------------------
|
|
51 |
// Block read - if your platform needs to do endian-swapping or can only
|
|
52 |
// handle aligned reads, do the conversion here
|
|
53 |
|
|
54 |
FORCE_INLINE uint32_t getblock32 ( const uint32_t * p, int i )
|
|
55 |
{
|
|
56 |
return p[i];
|
|
57 |
}
|
|
58 |
|
|
59 |
FORCE_INLINE uint64_t getblock64 ( const uint64_t * p, int i )
|
|
60 |
{
|
|
61 |
return p[i];
|
|
62 |
}
|
|
63 |
|
|
64 |
//-----------------------------------------------------------------------------
|
|
65 |
// Finalization mix - force all bits of a hash block to avalanche
|
|
66 |
|
|
67 |
FORCE_INLINE uint32_t fmix32 ( uint32_t h )
|
|
68 |
{
|
|
69 |
h ^= h >> 16;
|
|
70 |
h *= 0x85ebca6b;
|
|
71 |
h ^= h >> 13;
|
|
72 |
h *= 0xc2b2ae35;
|
|
73 |
h ^= h >> 16;
|
|
74 |
|
|
75 |
return h;
|
|
76 |
}
|
|
77 |
|
|
78 |
//----------
|
|
79 |
|
|
80 |
FORCE_INLINE uint64_t fmix64 ( uint64_t k )
|
|
81 |
{
|
|
82 |
k ^= k >> 33;
|
|
83 |
k *= BIG_CONSTANT(0xff51afd7ed558ccd);
|
|
84 |
k ^= k >> 33;
|
|
85 |
k *= BIG_CONSTANT(0xc4ceb9fe1a85ec53);
|
|
86 |
k ^= k >> 33;
|
|
87 |
|
|
88 |
return k;
|
|
89 |
}
|
|
90 |
|
|
91 |
//-----------------------------------------------------------------------------
|
|
92 |
|
|
93 |
void MurmurHash3_x86_32 ( const void * key, int len,
|
|
94 |
uint32_t seed, void * out )
|
|
95 |
{
|
|
96 |
const uint8_t * data = (const uint8_t*)key;
|
|
97 |
const int nblocks = len / 4;
|
|
98 |
|
|
99 |
uint32_t h1 = seed;
|
|
100 |
|
|
101 |
const uint32_t c1 = 0xcc9e2d51;
|
|
102 |
const uint32_t c2 = 0x1b873593;
|
|
103 |
|
|
104 |
//----------
|
|
105 |
// body
|
|
106 |
|
|
107 |
const uint32_t * blocks = (const uint32_t *)(data + nblocks*4);
|
|
108 |
|
|
109 |
for(int i = -nblocks; i; i++)
|
|
110 |
{
|
|
111 |
uint32_t k1 = getblock32(blocks,i);
|
|
112 |
|
|
113 |
k1 *= c1;
|
|
114 |
k1 = ROTL32(k1,15);
|
|
115 |
k1 *= c2;
|
|
116 |
|
|
117 |
h1 ^= k1;
|
|
118 |
h1 = ROTL32(h1,13);
|
|
119 |
h1 = h1*5+0xe6546b64;
|
|
120 |
}
|
|
121 |
|
|
122 |
//----------
|
|
123 |
// tail
|
|
124 |
|
|
125 |
const uint8_t * tail = (const uint8_t*)(data + nblocks*4);
|
|
126 |
|
|
127 |
uint32_t k1 = 0;
|
|
128 |
|
|
129 |
switch(len & 3)
|
|
130 |
{
|
|
131 |
case 3: k1 ^= tail[2] << 16;
|
|
132 |
case 2: k1 ^= tail[1] << 8;
|
|
133 |
case 1: k1 ^= tail[0];
|
|
134 |
k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
|
|
135 |
};
|
|
136 |
|
|
137 |
//----------
|
|
138 |
// finalization
|
|
139 |
|
|
140 |
h1 ^= len;
|
|
141 |
|
|
142 |
h1 = fmix32(h1);
|
|
143 |
|
|
144 |
*(uint32_t*)out = h1;
|
|
145 |
}
|
|
146 |
|
|
147 |
//-----------------------------------------------------------------------------
|
|
148 |
|
|
149 |
void MurmurHash3_x86_128 ( const void * key, const int len,
|
|
150 |
uint32_t seed, void * out )
|
|
151 |
{
|
|
152 |
const uint8_t * data = (const uint8_t*)key;
|
|
153 |
const int nblocks = len / 16;
|
|
154 |
|
|
155 |
uint32_t h1 = seed;
|
|
156 |
uint32_t h2 = seed;
|
|
157 |
uint32_t h3 = seed;
|
|
158 |
uint32_t h4 = seed;
|
|
159 |
|
|
160 |
const uint32_t c1 = 0x239b961b;
|
|
161 |
const uint32_t c2 = 0xab0e9789;
|
|
162 |
const uint32_t c3 = 0x38b34ae5;
|
|
163 |
const uint32_t c4 = 0xa1e38b93;
|
|
164 |
|
|
165 |
//----------
|
|
166 |
// body
|
|
167 |
|
|
168 |
const uint32_t * blocks = (const uint32_t *)(data + nblocks*16);
|
|
169 |
|
|
170 |
for(int i = -nblocks; i; i++)
|
|
171 |
{
|
|
172 |
uint32_t k1 = getblock32(blocks,i*4+0);
|
|
173 |
uint32_t k2 = getblock32(blocks,i*4+1);
|
|
174 |
uint32_t k3 = getblock32(blocks,i*4+2);
|
|
175 |
uint32_t k4 = getblock32(blocks,i*4+3);
|
|
176 |
|
|
177 |
k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
|
|
178 |
|
|
179 |
h1 = ROTL32(h1,19); h1 += h2; h1 = h1*5+0x561ccd1b;
|
|
180 |
|
|
181 |
k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2;
|
|
182 |
|
|
183 |
h2 = ROTL32(h2,17); h2 += h3; h2 = h2*5+0x0bcaa747;
|
|
184 |
|
|
185 |
k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3;
|
|
186 |
|
|
187 |
h3 = ROTL32(h3,15); h3 += h4; h3 = h3*5+0x96cd1c35;
|
|
188 |
|
|
189 |
k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4;
|
|
190 |
|
|
191 |
h4 = ROTL32(h4,13); h4 += h1; h4 = h4*5+0x32ac3b17;
|
|
192 |
}
|
|
193 |
|
|
194 |
//----------
|
|
195 |
// tail
|
|
196 |
|
|
197 |
const uint8_t * tail = (const uint8_t*)(data + nblocks*16);
|
|
198 |
|
|
199 |
uint32_t k1 = 0;
|
|
200 |
uint32_t k2 = 0;
|
|
201 |
uint32_t k3 = 0;
|
|
202 |
uint32_t k4 = 0;
|
|
203 |
|
|
204 |
switch(len & 15)
|
|
205 |
{
|
|
206 |
case 15: k4 ^= tail[14] << 16;
|
|
207 |
case 14: k4 ^= tail[13] << 8;
|
|
208 |
case 13: k4 ^= tail[12] << 0;
|
|
209 |
k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4;
|
|
210 |
|
|
211 |
case 12: k3 ^= tail[11] << 24;
|
|
212 |
case 11: k3 ^= tail[10] << 16;
|
|
213 |
case 10: k3 ^= tail[ 9] << 8;
|
|
214 |
case 9: k3 ^= tail[ 8] << 0;
|
|
215 |
k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3;
|
|
216 |
|
|
217 |
case 8: k2 ^= tail[ 7] << 24;
|
|
218 |
case 7: k2 ^= tail[ 6] << 16;
|
|
219 |
case 6: k2 ^= tail[ 5] << 8;
|
|
220 |
case 5: k2 ^= tail[ 4] << 0;
|
|
221 |
k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2;
|
|
222 |
|
|
223 |
case 4: k1 ^= tail[ 3] << 24;
|
|
224 |
case 3: k1 ^= tail[ 2] << 16;
|
|
225 |
case 2: k1 ^= tail[ 1] << 8;
|
|
226 |
case 1: k1 ^= tail[ 0] << 0;
|
|
227 |
k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
|
|
228 |
};
|
|
229 |
|
|
230 |
//----------
|
|
231 |
// finalization
|
|
232 |
|
|
233 |
h1 ^= len; h2 ^= len; h3 ^= len; h4 ^= len;
|
|
234 |
|
|
235 |
h1 += h2; h1 += h3; h1 += h4;
|
|
236 |
h2 += h1; h3 += h1; h4 += h1;
|
|
237 |
|
|
238 |
h1 = fmix32(h1);
|
|
239 |
h2 = fmix32(h2);
|
|
240 |
h3 = fmix32(h3);
|
|
241 |
h4 = fmix32(h4);
|
|
242 |
|
|
243 |
h1 += h2; h1 += h3; h1 += h4;
|
|
244 |
h2 += h1; h3 += h1; h4 += h1;
|
|
245 |
|
|
246 |
((uint32_t*)out)[0] = h1;
|
|
247 |
((uint32_t*)out)[1] = h2;
|
|
248 |
((uint32_t*)out)[2] = h3;
|
|
249 |
((uint32_t*)out)[3] = h4;
|
|
250 |
}
|
|
251 |
|
|
252 |
//-----------------------------------------------------------------------------
|
|
253 |
|
|
254 |
void MurmurHash3_x64_128 ( const void * key, const int len,
|
|
255 |
const uint32_t seed, void * out )
|
|
256 |
{
|
|
257 |
const uint8_t * data = (const uint8_t*)key;
|
|
258 |
const int nblocks = len / 16;
|
|
259 |
|
|
260 |
uint64_t h1 = seed;
|
|
261 |
uint64_t h2 = seed;
|
|
262 |
|
|
263 |
const uint64_t c1 = BIG_CONSTANT(0x87c37b91114253d5);
|
|
264 |
const uint64_t c2 = BIG_CONSTANT(0x4cf5ad432745937f);
|
|
265 |
|
|
266 |
//----------
|
|
267 |
// body
|
|
268 |
|
|
269 |
const uint64_t * blocks = (const uint64_t *)(data);
|
|
270 |
|
|
271 |
for(int i = 0; i < nblocks; i++)
|
|
272 |
{
|
|
273 |
uint64_t k1 = getblock64(blocks,i*2+0);
|
|
274 |
uint64_t k2 = getblock64(blocks,i*2+1);
|
|
275 |
|
|
276 |
k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
|
|
277 |
|
|
278 |
h1 = ROTL64(h1,27); h1 += h2; h1 = h1*5+0x52dce729;
|
|
279 |
|
|
280 |
k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
|
|
281 |
|
|
282 |
h2 = ROTL64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5;
|
|
283 |
}
|
|
284 |
|
|
285 |
//----------
|
|
286 |
// tail
|
|
287 |
|
|
288 |
const uint8_t * tail = (const uint8_t*)(data + nblocks*16);
|
|
289 |
|
|
290 |
uint64_t k1 = 0;
|
|
291 |
uint64_t k2 = 0;
|
|
292 |
|
|
293 |
switch(len & 15)
|
|
294 |
{
|
|
295 |
case 15: k2 ^= ((uint64_t)tail[14]) << 48;
|
|
296 |
case 14: k2 ^= ((uint64_t)tail[13]) << 40;
|
|
297 |
case 13: k2 ^= ((uint64_t)tail[12]) << 32;
|
|
298 |
case 12: k2 ^= ((uint64_t)tail[11]) << 24;
|
|
299 |
case 11: k2 ^= ((uint64_t)tail[10]) << 16;
|
|
300 |
case 10: k2 ^= ((uint64_t)tail[ 9]) << 8;
|
|
301 |
case 9: k2 ^= ((uint64_t)tail[ 8]) << 0;
|
|
302 |
k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
|
|
303 |
|
|
304 |
case 8: k1 ^= ((uint64_t)tail[ 7]) << 56;
|
|
305 |
case 7: k1 ^= ((uint64_t)tail[ 6]) << 48;
|
|
306 |
case 6: k1 ^= ((uint64_t)tail[ 5]) << 40;
|
|
307 |
case 5: k1 ^= ((uint64_t)tail[ 4]) << 32;
|
|
308 |
case 4: k1 ^= ((uint64_t)tail[ 3]) << 24;
|
|
309 |
case 3: k1 ^= ((uint64_t)tail[ 2]) << 16;
|
|
310 |
case 2: k1 ^= ((uint64_t)tail[ 1]) << 8;
|
|
311 |
case 1: k1 ^= ((uint64_t)tail[ 0]) << 0;
|
|
312 |
k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
|
|
313 |
};
|
|
314 |
|
|
315 |
//----------
|
|
316 |
// finalization
|
|
317 |
|
|
318 |
h1 ^= len; h2 ^= len;
|
|
319 |
|
|
320 |
h1 += h2;
|
|
321 |
h2 += h1;
|
|
322 |
|
|
323 |
h1 = fmix64(h1);
|
|
324 |
h2 = fmix64(h2);
|
|
325 |
|
|
326 |
h1 += h2;
|
|
327 |
h2 += h1;
|
|
328 |
|
|
329 |
((uint64_t*)out)[0] = h1;
|
|
330 |
((uint64_t*)out)[1] = h2;
|
|
331 |
}
|
|
332 |
|
|
333 |
//-----------------------------------------------------------------------------
|
|
334 |
|