Debian Codegraph @

murmurhash/MurmurHash2.cpp

raw
1 //-----------------------------------------------------------------------------
2 // MurmurHash2 was written by Austin Appleby, and is placed in the public
3 // domain. The author hereby disclaims copyright to this source code.
4
5 // Note - This code makes a few assumptions about how your machine behaves -
6
7 // 1. We can read a 4-byte value from any address without crashing
8 // 2. sizeof(int) == 4
9
10 // And it has a few limitations -
11
12 // 1. It will not work incrementally.
13 // 2. It will not produce the same results on little-endian and big-endian
14 // machines.
15
16 #include "murmurhash/MurmurHash2.h"
17
18 //-----------------------------------------------------------------------------
19 // Platform-specific functions and macros
20
21 // Microsoft Visual Studio
22
23 #if defined(_MSC_VER)
24
25 #define BIG_CONSTANT(x) (x)
26
27 // Other compilers
28
29 #else // defined(_MSC_VER)
30
31 #define BIG_CONSTANT(x) (x##LLU)
32
33 #endif // !defined(_MSC_VER)
34 //
35 //-----------------------------------------------------------------------------
36 // Block read - on little-endian machines this is a single load,
37 // while on big-endian or unknown machines the byte accesses should
38 // still get optimized into the most efficient instruction.
39 static inline uint32_t getblock ( const uint32_t * p )
40 {
41 #if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
42 return *p;
43 #else
44 const uint8_t *c = (const uint8_t *)p;
45 return (uint32_t)c[0] |
46 (uint32_t)c[1] << 8 |
47 (uint32_t)c[2] << 16 |
48 (uint32_t)c[3] << 24;
49 #endif
50 }
51
52 static inline uint64_t getblock ( const uint64_t * p )
53 {
54 #if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
55 return *p;
56 #else
57 const uint8_t *c = (const uint8_t *)p;
58 return (uint64_t)c[0] |
59 (uint64_t)c[1] << 8 |
60 (uint64_t)c[2] << 16 |
61 (uint64_t)c[3] << 24 |
62 (uint64_t)c[4] << 32 |
63 (uint64_t)c[5] << 40 |
64 (uint64_t)c[6] << 48 |
65 (uint64_t)c[7] << 56;
66 #endif
67 }
68
69 //-----------------------------------------------------------------------------
70
71 uint32_t MurmurHash2 ( const void * key, int len, uint32_t seed )
72 {
73 // 'm' and 'r' are mixing constants generated offline.
74 // They're not really 'magic', they just happen to work well.
75
76 const uint32_t m = 0x5bd1e995;
77 const int r = 24;
78
79 // Initialize the hash to a 'random' value
80
81 uint32_t h = seed ^ len;
82
83 // Mix 4 bytes at a time into the hash
84
85 const unsigned char * data = (const unsigned char *)key;
86
87 while(len >= 4)
88 {
89 uint32_t k = getblock((const uint32_t *)data);
90
91 k *= m;
92 k ^= k >> r;
93 k *= m;
94
95 h *= m;
96 h ^= k;
97
98 data += 4;
99 len -= 4;
100 }
101
102 // Handle the last few bytes of the input array
103
104 switch(len)
105 {
106 case 3: h ^= data[2] << 16;
107 case 2: h ^= data[1] << 8;
108 case 1: h ^= data[0];
109 h *= m;
110 };
111
112 // Do a few final mixes of the hash to ensure the last few
113 // bytes are well-incorporated.
114
115 h ^= h >> 13;
116 h *= m;
117 h ^= h >> 15;
118
119 return h;
120 }
121
122 //-----------------------------------------------------------------------------
123 // MurmurHash2, 64-bit versions, by Austin Appleby
124
125 // The same caveats as 32-bit MurmurHash2 apply here - beware of alignment
126 // and endian-ness issues if used across multiple platforms.
127
128 // 64-bit hash for 64-bit platforms
129
130 uint64_t MurmurHash64A ( const void * key, int len, uint64_t seed )
131 {
132 const uint64_t m = BIG_CONSTANT(0xc6a4a7935bd1e995);
133 const int r = 47;
134
135 uint64_t h = seed ^ (len * m);
136
137 const uint64_t * data = (const uint64_t *)key;
138 const uint64_t * end = data + (len/8);
139
140 while(data != end)
141 {
142 uint64_t k = getblock(data++);
143
144 k *= m;
145 k ^= k >> r;
146 k *= m;
147
148 h ^= k;
149 h *= m;
150 }
151
152 const unsigned char * data2 = (const unsigned char*)data;
153
154 switch(len & 7)
155 {
156 case 7: h ^= uint64_t(data2[6]) << 48;
157 case 6: h ^= uint64_t(data2[5]) << 40;
158 case 5: h ^= uint64_t(data2[4]) << 32;
159 case 4: h ^= uint64_t(data2[3]) << 24;
160 case 3: h ^= uint64_t(data2[2]) << 16;
161 case 2: h ^= uint64_t(data2[1]) << 8;
162 case 1: h ^= uint64_t(data2[0]);
163 h *= m;
164 };
165
166 h ^= h >> r;
167 h *= m;
168 h ^= h >> r;
169
170 return h;
171 }
172
173
174 // 64-bit hash for 32-bit platforms
175
176 uint64_t MurmurHash64B ( const void * key, int len, uint64_t seed )
177 {
178 const uint32_t m = 0x5bd1e995;
179 const int r = 24;
180
181 uint32_t h1 = uint32_t(seed) ^ len;
182 uint32_t h2 = uint32_t(seed >> 32);
183
184 const uint32_t * data = (const uint32_t *)key;
185
186 while(len >= 8)
187 {
188 uint32_t k1 = getblock(data++);
189 k1 *= m; k1 ^= k1 >> r; k1 *= m;
190 h1 *= m; h1 ^= k1;
191 len -= 4;
192
193 uint32_t k2 = getblock(data++);
194 k2 *= m; k2 ^= k2 >> r; k2 *= m;
195 h2 *= m; h2 ^= k2;
196 len -= 4;
197 }
198
199 if(len >= 4)
200 {
201 uint32_t k1 = getblock(data++);
202 k1 *= m; k1 ^= k1 >> r; k1 *= m;
203 h1 *= m; h1 ^= k1;
204 len -= 4;
205 }
206
207 switch(len)
208 {
209 case 3: h2 ^= ((unsigned char*)data)[2] << 16;
210 case 2: h2 ^= ((unsigned char*)data)[1] << 8;
211 case 1: h2 ^= ((unsigned char*)data)[0];
212 h2 *= m;
213 };
214
215 h1 ^= h2 >> 18; h1 *= m;
216 h2 ^= h1 >> 22; h2 *= m;
217 h1 ^= h2 >> 17; h1 *= m;
218 h2 ^= h1 >> 19; h2 *= m;
219
220 uint64_t h = h1;
221
222 h = (h << 32) | h2;
223
224 return h;
225 }
226
227 //-----------------------------------------------------------------------------
228 // MurmurHash2A, by Austin Appleby
229
230 // This is a variant of MurmurHash2 modified to use the Merkle-Damgard
231 // construction. Bulk speed should be identical to Murmur2, small-key speed
232 // will be 10%-20% slower due to the added overhead at the end of the hash.
233
234 // This variant fixes a minor issue where null keys were more likely to
235 // collide with each other than expected, and also makes the function
236 // more amenable to incremental implementations.
237
238 #define mmix(h,k) { k *= m; k ^= k >> r; k *= m; h *= m; h ^= k; }
239
240 uint32_t MurmurHash2A ( const void * key, int len, uint32_t seed )
241 {
242 const uint32_t m = 0x5bd1e995;
243 const int r = 24;
244 uint32_t l = len;
245
246 const unsigned char * data = (const unsigned char *)key;
247
248 uint32_t h = seed;
249
250 while(len >= 4)
251 {
252 uint32_t k = getblock((const uint32_t *)data);
253
254 mmix(h,k);
255
256 data += 4;
257 len -= 4;
258 }
259
260 uint32_t t = 0;
261
262 switch(len)
263 {
264 case 3: t ^= data[2] << 16;
265 case 2: t ^= data[1] << 8;
266 case 1: t ^= data[0];
267 };
268
269 mmix(h,t);
270 mmix(h,l);
271
272 h ^= h >> 13;
273 h *= m;
274 h ^= h >> 15;
275
276 return h;
277 }
278
279 //-----------------------------------------------------------------------------
280 // CMurmurHash2A, by Austin Appleby
281
282 // This is a sample implementation of MurmurHash2A designed to work
283 // incrementally.
284
285 // Usage -
286
287 // CMurmurHash2A hasher
288 // hasher.Begin(seed);
289 // hasher.Add(data1,size1);
290 // hasher.Add(data2,size2);
291 // ...
292 // hasher.Add(dataN,sizeN);
293 // uint32_t hash = hasher.End()
294
295 class CMurmurHash2A
296 {
297 public:
298
299 void Begin ( uint32_t seed = 0 )
300 {
301 m_hash = seed;
302 m_tail = 0;
303 m_count = 0;
304 m_size = 0;
305 }
306
307 void Add ( const unsigned char * data, int len )
308 {
309 m_size += len;
310
311 MixTail(data,len);
312
313 while(len >= 4)
314 {
315 uint32_t k = getblock((const uint32_t *)data);
316
317 mmix(m_hash,k);
318
319 data += 4;
320 len -= 4;
321 }
322
323 MixTail(data,len);
324 }
325
326 uint32_t End ( void )
327 {
328 mmix(m_hash,m_tail);
329 mmix(m_hash,m_size);
330
331 m_hash ^= m_hash >> 13;
332 m_hash *= m;
333 m_hash ^= m_hash >> 15;
334
335 return m_hash;
336 }
337
338 private:
339
340 static const uint32_t m = 0x5bd1e995;
341 static const int r = 24;
342
343 void MixTail ( const unsigned char * & data, int & len )
344 {
345 while( len && ((len<4) || m_count) )
346 {
347 m_tail |= *data++ << (m_count * 8);
348
349 m_count++;
350 len--;
351
352 if(m_count == 4)
353 {
354 mmix(m_hash,m_tail);
355 m_tail = 0;
356 m_count = 0;
357 }
358 }
359 }
360
361 uint32_t m_hash;
362 uint32_t m_tail;
363 uint32_t m_count;
364 uint32_t m_size;
365 };
366
367 //-----------------------------------------------------------------------------
368 // MurmurHashNeutral2, by Austin Appleby
369
370 // Same as MurmurHash2, but endian- and alignment-neutral.
371 // Half the speed though, alas.
372
373 uint32_t MurmurHashNeutral2 ( const void * key, int len, uint32_t seed )
374 {
375 const uint32_t m = 0x5bd1e995;
376 const int r = 24;
377
378 uint32_t h = seed ^ len;
379
380 const unsigned char * data = (const unsigned char *)key;
381
382 while(len >= 4)
383 {
384 uint32_t k;
385
386 k = data[0];
387 k |= data[1] << 8;
388 k |= data[2] << 16;
389 k |= data[3] << 24;
390
391 k *= m;
392 k ^= k >> r;
393 k *= m;
394
395 h *= m;
396 h ^= k;
397
398 data += 4;
399 len -= 4;
400 }
401
402 switch(len)
403 {
404 case 3: h ^= data[2] << 16;
405 case 2: h ^= data[1] << 8;
406 case 1: h ^= data[0];
407 h *= m;
408 };
409
410 h ^= h >> 13;
411 h *= m;
412 h ^= h >> 15;
413
414 return h;
415 }
416
417 //-----------------------------------------------------------------------------
418 // MurmurHashAligned2, by Austin Appleby
419
420 // Same algorithm as MurmurHash2, but only does aligned reads - should be safer
421 // on certain platforms.
422
423 // Performance will be lower than MurmurHash2
424
425 #define MIX(h,k,m) { k *= m; k ^= k >> r; k *= m; h *= m; h ^= k; }
426
427
428 uint32_t MurmurHashAligned2 ( const void * key, int len, uint32_t seed )
429 {
430 const uint32_t m = 0x5bd1e995;
431 const int r = 24;
432
433 const unsigned char * data = (const unsigned char *)key;
434
435 uint32_t h = seed ^ len;
436
437 int align = (uint64_t)data & 3;
438
439 if(align && (len >= 4))
440 {
441 // Pre-load the temp registers
442
443 uint32_t t = 0, d = 0;
444
445 switch(align)
446 {
447 case 1: t |= data[2] << 16;
448 case 2: t |= data[1] << 8;
449 case 3: t |= data[0];
450 }
451
452 t <<= (8 * align);
453
454 data += 4-align;
455 len -= 4-align;
456
457 int sl = 8 * (4-align);
458 int sr = 8 * align;
459
460 // Mix
461
462 while(len >= 4)
463 {
464 d = getblock((const uint32_t *)data);
465 t = (t >> sr) | (d << sl);
466
467 uint32_t k = t;
468
469 MIX(h,k,m);
470
471 t = d;
472
473 data += 4;
474 len -= 4;
475 }
476
477 // Handle leftover data in temp registers
478
479 d = 0;
480
481 if(len >= align)
482 {
483 switch(align)
484 {
485 case 3: d |= data[2] << 16;
486 case 2: d |= data[1] << 8;
487 case 1: d |= data[0];
488 }
489
490 uint32_t k = (t >> sr) | (d << sl);
491 MIX(h,k,m);
492
493 data += align;
494 len -= align;
495
496 //----------
497 // Handle tail bytes
498
499 switch(len)
500 {
501 case 3: h ^= data[2] << 16;
502 case 2: h ^= data[1] << 8;
503 case 1: h ^= data[0];
504 h *= m;
505 };
506 }
507 else
508 {
509 switch(len)
510 {
511 case 3: d |= data[2] << 16;
512 case 2: d |= data[1] << 8;
513 case 1: d |= data[0];
514 case 0: h ^= (t >> sr) | (d << sl);
515 h *= m;
516 }
517 }
518
519 h ^= h >> 13;
520 h *= m;
521 h ^= h >> 15;
522
523 return h;
524 }
525 else
526 {
527 while(len >= 4)
528 {
529 uint32_t k = getblock((const uint32_t *)data);
530
531 MIX(h,k,m);
532
533 data += 4;
534 len -= 4;
535 }
536
537 //----------
538 // Handle tail bytes
539
540 switch(len)
541 {
542 case 3: h ^= data[2] << 16;
543 case 2: h ^= data[1] << 8;
544 case 1: h ^= data[0];
545 h *= m;
546 };
547
548 h ^= h >> 13;
549 h *= m;
550 h ^= h >> 15;
551
552 return h;
553 }
554 }
555
556 //-----------------------------------------------------------------------------
557