| 1 |
#ifndef MURMUR_HASH_H |
| 2 |
#define MURMUR_HASH_H |
| 3 |
|
| 4 |
#include <stdint.h> |
| 5 |
|
| 6 |
//----------------------------------------------------------------------------- |
| 7 |
// MurmurHash2, 64-bit versions, by Austin Appleby |
| 8 |
|
| 9 |
// The same caveats as 32-bit MurmurHash2 apply here - beware of alignment |
| 10 |
// and endian-ness issues if used across multiple platforms. |
| 11 |
|
| 12 |
// 64-bit hash for 64-bit platforms |
| 13 |
|
| 14 |
uint64_t MurmurHash64A ( const void * key, int len, unsigned int seed ) { |
| 15 |
const uint64_t m = 0xc6a4a7935bd1e995; |
| 16 |
const int r = 47; |
| 17 |
|
| 18 |
uint64_t h = seed ^ (len * m); |
| 19 |
|
| 20 |
const uint64_t * data = (const uint64_t *)key; |
| 21 |
const uint64_t * end = data + (len/8); |
| 22 |
|
| 23 |
while (data != end) |
| 24 |
{ |
| 25 |
uint64_t k = *data++; |
| 26 |
|
| 27 |
k *= m; |
| 28 |
k ^= k >> r; |
| 29 |
k *= m; |
| 30 |
|
| 31 |
h ^= k; |
| 32 |
h *= m; |
| 33 |
} |
| 34 |
|
| 35 |
const unsigned char * data2 = (const unsigned char*)data; |
| 36 |
|
| 37 |
switch(len & 7) |
| 38 |
{ |
| 39 |
case 7: |
| 40 |
h ^= uint64_t(data2[6]) << 48; // fallthrough |
| 41 |
case 6: |
| 42 |
h ^= uint64_t(data2[5]) << 40; // fallthrough |
| 43 |
case 5: |
| 44 |
h ^= uint64_t(data2[4]) << 32; // fallthrough |
| 45 |
case 4: |
| 46 |
h ^= uint64_t(data2[3]) << 24; // fallthrough |
| 47 |
case 3: |
| 48 |
h ^= uint64_t(data2[2]) << 16; // fallthrough |
| 49 |
case 2: |
| 50 |
h ^= uint64_t(data2[1]) << 8; // fallthrough |
| 51 |
case 1: |
| 52 |
h ^= uint64_t(data2[0]); |
| 53 |
h *= m; |
| 54 |
}; |
| 55 |
|
| 56 |
h ^= h >> r; |
| 57 |
h *= m; |
| 58 |
h ^= h >> r; |
| 59 |
|
| 60 |
return h; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
#endif |