|
| 1 | +#include "hash.h" |
| 2 | + |
| 3 | +#if defined(_MSC_VER) |
| 4 | +# define ROTL32(x,y) _rotl(x,y) |
| 5 | +# define ROTL64(x,y) _rotl64(x,y) |
| 6 | + |
| 7 | +#else |
| 8 | +inline uint32_t rotl32(uint32_t x, int8_t r) { |
| 9 | + return (x << r) | (x >> (32 - r)); |
| 10 | +} |
| 11 | + |
| 12 | +inline uint64_t rotl64(uint64_t x, int8_t r) { |
| 13 | + return (x << r) | (x >> (64 - r)); |
| 14 | +} |
| 15 | + |
| 16 | +# define ROTL32(x,y) rotl32(x,y) |
| 17 | +# define ROTL64(x,y) rotl64(x,y) |
| 18 | +#endif |
| 19 | + |
| 20 | +//----------------------------------------------------------------------------- |
| 21 | + |
| 22 | +uint64_t MurmurHash3_x64_64(const void *key, const size_t len, |
| 23 | + const uint32_t seed) { |
| 24 | + const uint8_t *data = (const uint8_t *) key; |
| 25 | + const size_t nblocks = len / 16; |
| 26 | + |
| 27 | + uint64_t h1 = seed; |
| 28 | + uint64_t h2 = seed; |
| 29 | + |
| 30 | + const uint64_t c1 = (uint64_t) 0x87c37b91114253d5ull; |
| 31 | + const uint64_t c2 = (uint64_t) 0x4cf5ad432745937full; |
| 32 | + |
| 33 | + //---------- |
| 34 | + // body |
| 35 | + |
| 36 | + const uint64_t * blocks = (const uint64_t *)(data); |
| 37 | + |
| 38 | + for(size_t i = 0; i < nblocks; i++) { |
| 39 | + uint64_t k1 = blocks[i*2+0]; |
| 40 | + uint64_t k2 = blocks[i*2+1]; |
| 41 | + |
| 42 | + k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1; |
| 43 | + |
| 44 | + h1 = ROTL64(h1,27); h1 += h2; h1 = h1*5+0x52dce729; |
| 45 | + |
| 46 | + k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2; |
| 47 | + |
| 48 | + h2 = ROTL64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5; |
| 49 | + } |
| 50 | + |
| 51 | + //---------- |
| 52 | + // tail |
| 53 | + |
| 54 | + const uint8_t *tail = (const uint8_t *) (data + nblocks * 16); |
| 55 | + |
| 56 | + uint64_t k1 = 0; |
| 57 | + uint64_t k2 = 0; |
| 58 | + |
| 59 | + switch(len & 15) { |
| 60 | + case 15: k2 ^= ((uint64_t)tail[14]) << 48; |
| 61 | + case 14: k2 ^= ((uint64_t)tail[13]) << 40; |
| 62 | + case 13: k2 ^= ((uint64_t)tail[12]) << 32; |
| 63 | + case 12: k2 ^= ((uint64_t)tail[11]) << 24; |
| 64 | + case 11: k2 ^= ((uint64_t)tail[10]) << 16; |
| 65 | + case 10: k2 ^= ((uint64_t)tail[ 9]) << 8; |
| 66 | + case 9: k2 ^= ((uint64_t)tail[ 8]) << 0; |
| 67 | + k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2; |
| 68 | + |
| 69 | + case 8: k1 ^= ((uint64_t)tail[ 7]) << 56; |
| 70 | + case 7: k1 ^= ((uint64_t)tail[ 6]) << 48; |
| 71 | + case 6: k1 ^= ((uint64_t)tail[ 5]) << 40; |
| 72 | + case 5: k1 ^= ((uint64_t)tail[ 4]) << 32; |
| 73 | + case 4: k1 ^= ((uint64_t)tail[ 3]) << 24; |
| 74 | + case 3: k1 ^= ((uint64_t)tail[ 2]) << 16; |
| 75 | + case 2: k1 ^= ((uint64_t)tail[ 1]) << 8; |
| 76 | + case 1: k1 ^= ((uint64_t)tail[ 0]) << 0; |
| 77 | + k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1; |
| 78 | + }; |
| 79 | + |
| 80 | + //---------- |
| 81 | + // finalization |
| 82 | + |
| 83 | + h1 ^= len; h2 ^= len; |
| 84 | + |
| 85 | + h1 += h2; |
| 86 | + h2 += h1; |
| 87 | + |
| 88 | + h1 = fmix64(h1); |
| 89 | + h2 = fmix64(h2); |
| 90 | + |
| 91 | + h1 += h2; |
| 92 | + |
| 93 | + return h1; |
| 94 | +} |
0 commit comments