10 #include "MurmurHash3.hpp"
18 #define FORCE_INLINE __forceinline
22 #define ROTL32(x,y) _rotl(x,y)
23 #define ROTL64(x,y) _rotl64(x,y)
25 #define BIG_CONSTANT(x) (x)
29 #else // not defined(_MSC_VER)
33 inline uint32_t rotl32 ( uint32_t x, int8_t r )
35 return (x << r) | (x >> (32 - r));
38 inline uint64_t rotl64 ( uint64_t x, int8_t r )
40 return (x << r) | (x >> (64 - r));
45 #define ROTL32(x,y) rotl32(x,y)
46 #define ROTL64(x,y) rotl64(x,y)
48 #define BIG_CONSTANT(x) (x##LLU)
50 #endif // !defined(_MSC_VER)
56 #define GETBLOCK(lhs, p, i ) \
65 #define FMIX_32( h ) \
82 t_k *= BIG_CONSTANT(0xff51afd7ed558ccd);\
84 t_k *= BIG_CONSTANT(0xc4ceb9fe1a85ec53);\
94 void MurmurHash3_x86_32 (
const void * key,
int len,
95 uint32_t seed,
void * out )
97 const uint8_t * data = (
const uint8_t*)key;
98 const int nblocks = len / 4;
102 const uint32_t c1 = 0xcc9e2d51;
103 const uint32_t c2 = 0x1b873593;
108 const uint32_t * blocks = (
const uint32_t *)(data + nblocks*4);
110 for(
int i = -nblocks; i; i++)
113 GETBLOCK(k1, blocks,i);
121 h1 = h1*5+0xe6546b64;
127 const uint8_t * tail = (
const uint8_t*)(data + nblocks*4);
133 case 3: k1 ^= tail[2] << 16;
134 case 2: k1 ^= tail[1] << 8;
135 case 1: k1 ^= tail[0];
136 k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
146 *(uint32_t*)out = h1;
151 void MurmurHash3_x86_128 (
const void * key,
const int len,
152 uint32_t seed,
void * out )
154 const uint8_t * data = (
const uint8_t*)key;
155 const int nblocks = len / 16;
162 const uint32_t c1 = 0x239b961b;
163 const uint32_t c2 = 0xab0e9789;
164 const uint32_t c3 = 0x38b34ae5;
165 const uint32_t c4 = 0xa1e38b93;
170 const uint32_t * blocks = (
const uint32_t *)(data + nblocks*16);
172 for(
int i = -nblocks; i; i++)
174 uint32_t k1, k2, k3, k4;
175 GETBLOCK(k1, blocks,i*4+0);
176 GETBLOCK(k2, blocks,i*4+1);
177 GETBLOCK(k3, blocks,i*4+2);
178 GETBLOCK(k4, blocks,i*4+3);
180 k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
182 h1 = ROTL32(h1,19); h1 += h2; h1 = h1*5+0x561ccd1b;
184 k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2;
186 h2 = ROTL32(h2,17); h2 += h3; h2 = h2*5+0x0bcaa747;
188 k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3;
190 h3 = ROTL32(h3,15); h3 += h4; h3 = h3*5+0x96cd1c35;
192 k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4;
194 h4 = ROTL32(h4,13); h4 += h1; h4 = h4*5+0x32ac3b17;
200 const uint8_t * tail = (
const uint8_t*)(data + nblocks*16);
209 case 15: k4 ^= tail[14] << 16;
210 case 14: k4 ^= tail[13] << 8;
211 case 13: k4 ^= tail[12] << 0;
212 k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4;
214 case 12: k3 ^= tail[11] << 24;
215 case 11: k3 ^= tail[10] << 16;
216 case 10: k3 ^= tail[ 9] << 8;
217 case 9: k3 ^= tail[ 8] << 0;
218 k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3;
220 case 8: k2 ^= tail[ 7] << 24;
221 case 7: k2 ^= tail[ 6] << 16;
222 case 6: k2 ^= tail[ 5] << 8;
223 case 5: k2 ^= tail[ 4] << 0;
224 k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2;
226 case 4: k1 ^= tail[ 3] << 24;
227 case 3: k1 ^= tail[ 2] << 16;
228 case 2: k1 ^= tail[ 1] << 8;
229 case 1: k1 ^= tail[ 0] << 0;
230 k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
236 h1 ^= len; h2 ^= len; h3 ^= len; h4 ^= len;
238 h1 += h2; h1 += h3; h1 += h4;
239 h2 += h1; h3 += h1; h4 += h1;
246 h1 += h2; h1 += h3; h1 += h4;
247 h2 += h1; h3 += h1; h4 += h1;
249 ((uint32_t*)out)[0] = h1;
250 ((uint32_t*)out)[1] = h2;
251 ((uint32_t*)out)[2] = h3;
252 ((uint32_t*)out)[3] = h4;
257 void MurmurHash3_x64_128 (
const void * key,
const int len,
258 const uint32_t seed,
void * out )
260 const uint8_t * data = (
const uint8_t*)key;
261 const int nblocks = len / 16;
266 const uint64_t c1 = BIG_CONSTANT(0x87c37b91114253d5);
267 const uint64_t c2 = BIG_CONSTANT(0x4cf5ad432745937f);
272 const uint64_t * blocks = (
const uint64_t *)(data);
274 for(
int i = 0; i < nblocks; i++)
277 GETBLOCK(k1, blocks,i*2+0);
278 GETBLOCK(k2, blocks,i*2+1);
280 k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
282 h1 = ROTL64(h1,27); h1 += h2; h1 = h1*5+0x52dce729;
284 k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
286 h2 = ROTL64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5;
292 const uint8_t * tail = (
const uint8_t*)(data + nblocks*16);
299 case 15: k2 ^= uint64_t(tail[14]) << 48;
300 case 14: k2 ^= uint64_t(tail[13]) << 40;
301 case 13: k2 ^= uint64_t(tail[12]) << 32;
302 case 12: k2 ^= uint64_t(tail[11]) << 24;
303 case 11: k2 ^= uint64_t(tail[10]) << 16;
304 case 10: k2 ^= uint64_t(tail[ 9]) << 8;
305 case 9: k2 ^= uint64_t(tail[ 8]) << 0;
306 k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
308 case 8: k1 ^= uint64_t(tail[ 7]) << 56;
309 case 7: k1 ^= uint64_t(tail[ 6]) << 48;
310 case 6: k1 ^= uint64_t(tail[ 5]) << 40;
311 case 5: k1 ^= uint64_t(tail[ 4]) << 32;
312 case 4: k1 ^= uint64_t(tail[ 3]) << 24;
313 case 3: k1 ^= uint64_t(tail[ 2]) << 16;
314 case 2: k1 ^= uint64_t(tail[ 1]) << 8;
315 case 1: k1 ^= uint64_t(tail[ 0]) << 0;
316 k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
322 h1 ^= len; h2 ^= len;
333 ((uint64_t*)out)[0] = h1;
334 ((uint64_t*)out)[1] = h2;