16 #ifndef _MURMURHASH3_H_
17 #define _MURMURHASH3_H_
28 typedef unsigned char uint8_t;
29 typedef unsigned int uint32_t;
30 typedef unsigned __int64 uint64_t;
32 #define MURMUR3_FORCE_INLINE __forceinline
36 #define MURMUR3_ROTL64(x,y) _rotl64(x,y)
38 #define MURMUR3_BIG_CONSTANT(x) (x)
46 #define MURMUR3_FORCE_INLINE inline __attribute__((always_inline))
48 inline uint64_t rotl64 ( uint64_t x, int8_t r )
50 return (x << r) | (x >> (64 - r));
53 #define MURMUR3_ROTL64(x,y) rotl64(x,y)
55 #define MURMUR3_BIG_CONSTANT(x) (x##LLU)
74 MURMUR3_FORCE_INLINE uint64_t getblock64 (
const uint8_t * p,
size_t i )
77 memcpy(&res, p + i *
sizeof(uint64_t),
sizeof(res));
84 MURMUR3_FORCE_INLINE uint64_t fmix64 ( uint64_t k )
87 k *= MURMUR3_BIG_CONSTANT(0xff51afd7ed558ccd);
89 k *= MURMUR3_BIG_CONSTANT(0xc4ceb9fe1a85ec53);
95 MURMUR3_FORCE_INLINE
void MurmurHash3_x64_128(
const void* key,
size_t lenBytes,
96 uint64_t seed, HashState& out) {
97 static const uint64_t c1 = MURMUR3_BIG_CONSTANT(0x87c37b91114253d5);
98 static const uint64_t c2 = MURMUR3_BIG_CONSTANT(0x4cf5ad432745937f);
100 const uint8_t* data = (
const uint8_t*)key;
107 const size_t nblocks = lenBytes >> 4;
110 for (
size_t i = 0; i < nblocks; ++i) {
111 uint64_t k1 = getblock64(data, i * 2 + 0);
112 uint64_t k2 = getblock64(data, i * 2 + 1);
114 k1 *= c1; k1 = MURMUR3_ROTL64(k1,31); k1 *= c2; out.h1 ^= k1;
115 out.h1 = MURMUR3_ROTL64(out.h1,27);
117 out.h1 = out.h1*5+0x52dce729;
119 k2 *= c2; k2 = MURMUR3_ROTL64(k2,33); k2 *= c1; out.h2 ^= k2;
120 out.h2 = MURMUR3_ROTL64(out.h2,31);
122 out.h2 = out.h2*5+0x38495ab5;
126 const uint8_t * tail = (
const uint8_t*)(data + (nblocks << 4));
131 switch(lenBytes & 15)
133 case 15: k2 ^= ((uint64_t)tail[14]) << 48;
134 case 14: k2 ^= ((uint64_t)tail[13]) << 40;
135 case 13: k2 ^= ((uint64_t)tail[12]) << 32;
136 case 12: k2 ^= ((uint64_t)tail[11]) << 24;
137 case 11: k2 ^= ((uint64_t)tail[10]) << 16;
138 case 10: k2 ^= ((uint64_t)tail[ 9]) << 8;
139 case 9: k2 ^= ((uint64_t)tail[ 8]) << 0;
140 k2 *= c2; k2 = MURMUR3_ROTL64(k2,33); k2 *= c1; out.h2 ^= k2;
142 case 8: k1 ^= ((uint64_t)tail[ 7]) << 56;
143 case 7: k1 ^= ((uint64_t)tail[ 6]) << 48;
144 case 6: k1 ^= ((uint64_t)tail[ 5]) << 40;
145 case 5: k1 ^= ((uint64_t)tail[ 4]) << 32;
146 case 4: k1 ^= ((uint64_t)tail[ 3]) << 24;
147 case 3: k1 ^= ((uint64_t)tail[ 2]) << 16;
148 case 2: k1 ^= ((uint64_t)tail[ 1]) << 8;
149 case 1: k1 ^= ((uint64_t)tail[ 0]) << 0;
150 k1 *= c1; k1 = MURMUR3_ROTL64(k1,31); k1 *= c2; out.h1 ^= k1;
162 out.h1 = fmix64(out.h1);
163 out.h2 = fmix64(out.h2);
171 MURMUR3_FORCE_INLINE uint16_t compute_seed_hash(uint64_t seed) {
173 MurmurHash3_x64_128(&seed,
sizeof(seed), 0, hashes);
174 return static_cast<uint16_t
>(hashes.h1 & 0xffff);
177 #undef MURMUR3_FORCE_INLINE
178 #undef MURMUR3_ROTL64
179 #undef MURMUR3_BIG_CONSTANT