8d86cfd859cca7e11d6afa72055572d75d6965e5
[awesomized/libmemcached] / libhashkit / murmur3.cc
1 //-----------------------------------------------------------------------------
2 //MurmurHash3 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 - The x86 and x64 versions do _not_ produce the same results, as the
6 // algorithms are optimized for their respective platforms. You can still
7 // compile and run any of them on any platform, but your performance with the
8 // non-native version will be less than optimal.
9
10 #include "libhashkit/hashkitcon.h"
11
12 #include "libhashkit/murmur3.h"
13
14 //-----------------------------------------------------------------------------
15 // Platform-specific functions and macros
16
17 #ifdef __GNUC__
18 #define FORCE_INLINE __attribute__((always_inline)) inline
19 #else
20 #define FORCE_INLINE inline
21 #endif
22
23 static FORCE_INLINE uint32_t rotl32 ( uint32_t x, int8_t r )
24 {
25 return (x << r) | (x >> (32 - r));
26 }
27
28 static FORCE_INLINE uint64_t rotl64 ( uint64_t x, int8_t r )
29 {
30 return (x << r) | (x >> (64 - r));
31 }
32
33 #define ROTL32(x,y) rotl32(x,y)
34 #define ROTL64(x,y) rotl64(x,y)
35
36 #define BIG_CONSTANT(x) (x##LLU)
37
38 //-----------------------------------------------------------------------------
39 // Block read - if your platform needs to do endian-swapping or can only
40 // handle aligned reads, do the conversion here
41
42 #include <cstring>
43 template <typename T>
44 static inline T getblock(const T *blocks, int i) {
45 T b;
46 memcpy(&b, ((const uint8_t *) blocks) + i * sizeof(T), sizeof(T));
47 return b;
48 }
49
50 //-----------------------------------------------------------------------------
51 // Finalization mix - force all bits of a hash block to avalanche
52
53 static FORCE_INLINE uint32_t fmix32 ( uint32_t h )
54 {
55 h ^= h >> 16;
56 h *= 0x85ebca6b;
57 h ^= h >> 13;
58 h *= 0xc2b2ae35;
59 h ^= h >> 16;
60
61 return h;
62 }
63
64 //----------
65
66 static FORCE_INLINE uint64_t fmix64 ( uint64_t k )
67 {
68 k ^= k >> 33;
69 k *= BIG_CONSTANT(0xff51afd7ed558ccd);
70 k ^= k >> 33;
71 k *= BIG_CONSTANT(0xc4ceb9fe1a85ec53);
72 k ^= k >> 33;
73
74 return k;
75 }
76
77 //-----------------------------------------------------------------------------
78
79 void MurmurHash3_x86_32 ( const void * key, int len,
80 uint32_t seed, void * out )
81 {
82 const uint8_t * data = (const uint8_t*)key;
83 const int nblocks = len / 4;
84 int i;
85
86 uint32_t h1 = seed;
87
88 uint32_t c1 = 0xcc9e2d51;
89 uint32_t c2 = 0x1b873593;
90
91 //----------
92 // body
93
94 const uint32_t * blocks = (const uint32_t *)(data + nblocks*4);
95
96 for(i = -nblocks; i; i++)
97 {
98 uint32_t k1 = getblock(blocks,i);
99
100 k1 *= c1;
101 k1 = ROTL32(k1,15);
102 k1 *= c2;
103
104 h1 ^= k1;
105 h1 = ROTL32(h1,13);
106 h1 = h1*5+0xe6546b64;
107 }
108
109 //----------
110 // tail
111
112 const uint8_t * tail = (const uint8_t*)(data + nblocks*4);
113
114 uint32_t k1 = 0;
115
116 switch(len & 3)
117 {
118 case 3: k1 ^= tail[2] << 16; /* fall through */
119 case 2: k1 ^= tail[1] << 8; /* fall through */
120 case 1: k1 ^= tail[0];
121 k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
122 };
123
124 //----------
125 // finalization
126
127 h1 ^= len;
128
129 h1 = fmix32(h1);
130
131 *(uint32_t*)out = h1;
132 }
133
134 //-----------------------------------------------------------------------------
135
136 void MurmurHash3_x86_128 ( const void * key, const int len,
137 uint32_t seed, void * out )
138 {
139 const uint8_t * data = (const uint8_t*)key;
140 const int nblocks = len / 16;
141 int i;
142
143 uint32_t h1 = seed;
144 uint32_t h2 = seed;
145 uint32_t h3 = seed;
146 uint32_t h4 = seed;
147
148 uint32_t c1 = 0x239b961b;
149 uint32_t c2 = 0xab0e9789;
150 uint32_t c3 = 0x38b34ae5;
151 uint32_t c4 = 0xa1e38b93;
152
153 //----------
154 // body
155
156 const uint32_t * blocks = (const uint32_t *)(data + nblocks*16);
157
158 for(i = -nblocks; i; i++)
159 {
160 uint32_t k1 = getblock(blocks,i*4+0);
161 uint32_t k2 = getblock(blocks,i*4+1);
162 uint32_t k3 = getblock(blocks,i*4+2);
163 uint32_t k4 = getblock(blocks,i*4+3);
164
165 k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
166
167 h1 = ROTL32(h1,19); h1 += h2; h1 = h1*5+0x561ccd1b;
168
169 k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2;
170
171 h2 = ROTL32(h2,17); h2 += h3; h2 = h2*5+0x0bcaa747;
172
173 k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3;
174
175 h3 = ROTL32(h3,15); h3 += h4; h3 = h3*5+0x96cd1c35;
176
177 k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4;
178
179 h4 = ROTL32(h4,13); h4 += h1; h4 = h4*5+0x32ac3b17;
180 }
181
182 //----------
183 // tail
184
185 const uint8_t * tail = (const uint8_t*)(data + nblocks*16);
186
187 uint32_t k1 = 0;
188 uint32_t k2 = 0;
189 uint32_t k3 = 0;
190 uint32_t k4 = 0;
191
192 switch(len & 15)
193 {
194 case 15: k4 ^= tail[14] << 16; /* fall through */
195 case 14: k4 ^= tail[13] << 8; /* fall through */
196 case 13: k4 ^= tail[12] << 0;
197 k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4;
198 /* fall through */
199 case 12: k3 ^= tail[11] << 24; /* fall through */
200 case 11: k3 ^= tail[10] << 16; /* fall through */
201 case 10: k3 ^= tail[ 9] << 8; /* fall through */
202 case 9: k3 ^= tail[ 8] << 0; /* fall through */
203 k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3;
204 /* fall through */
205 case 8: k2 ^= tail[ 7] << 24; /* fall through */
206 case 7: k2 ^= tail[ 6] << 16; /* fall through */
207 case 6: k2 ^= tail[ 5] << 8; /* fall through */
208 case 5: k2 ^= tail[ 4] << 0; /* fall through */
209 k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2;
210 /* fall through */
211 case 4: k1 ^= tail[ 3] << 24; /* fall through */
212 case 3: k1 ^= tail[ 2] << 16; /* fall through */
213 case 2: k1 ^= tail[ 1] << 8; /* fall through */
214 case 1: k1 ^= tail[ 0] << 0; /* fall through */
215 k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
216 };
217
218 //----------
219 // finalization
220
221 h1 ^= len; h2 ^= len; h3 ^= len; h4 ^= len;
222
223 h1 += h2; h1 += h3; h1 += h4;
224 h2 += h1; h3 += h1; h4 += h1;
225
226 h1 = fmix32(h1);
227 h2 = fmix32(h2);
228 h3 = fmix32(h3);
229 h4 = fmix32(h4);
230
231 h1 += h2; h1 += h3; h1 += h4;
232 h2 += h1; h3 += h1; h4 += h1;
233
234 ((uint32_t*)out)[0] = h1;
235 ((uint32_t*)out)[1] = h2;
236 ((uint32_t*)out)[2] = h3;
237 ((uint32_t*)out)[3] = h4;
238 }
239
240 //-----------------------------------------------------------------------------
241
242 void MurmurHash3_x64_128 ( const void * key, const int len,
243 const uint32_t seed, void * out )
244 {
245 const uint8_t * data = (const uint8_t*)key;
246 const int nblocks = len / 16;
247 int i;
248
249 uint64_t h1 = seed;
250 uint64_t h2 = seed;
251
252 uint64_t c1 = BIG_CONSTANT(0x87c37b91114253d5);
253 uint64_t c2 = BIG_CONSTANT(0x4cf5ad432745937f);
254
255 //----------
256 // body
257
258 const uint64_t * blocks = (const uint64_t *)(data);
259
260 for(i = 0; i < nblocks; i++)
261 {
262 uint64_t k1 = getblock(blocks,i*2+0);
263 uint64_t k2 = getblock(blocks,i*2+1);
264
265 k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
266
267 h1 = ROTL64(h1,27); h1 += h2; h1 = h1*5+0x52dce729;
268
269 k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
270
271 h2 = ROTL64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5;
272 }
273
274 //----------
275 // tail
276
277 const uint8_t * tail = (const uint8_t*)(data + nblocks*16);
278
279 uint64_t k1 = 0;
280 uint64_t k2 = 0;
281
282 switch(len & 15)
283 {
284 case 15: k2 ^= (uint64_t)(tail[14]) << 48; /* fall through */
285 case 14: k2 ^= (uint64_t)(tail[13]) << 40; /* fall through */
286 case 13: k2 ^= (uint64_t)(tail[12]) << 32; /* fall through */
287 case 12: k2 ^= (uint64_t)(tail[11]) << 24; /* fall through */
288 case 11: k2 ^= (uint64_t)(tail[10]) << 16; /* fall through */
289 case 10: k2 ^= (uint64_t)(tail[ 9]) << 8; /* fall through */
290 case 9: k2 ^= (uint64_t)(tail[ 8]) << 0; /* fall through */
291 k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
292 /* fall through */
293 case 8: k1 ^= (uint64_t)(tail[ 7]) << 56; /* fall through */
294 case 7: k1 ^= (uint64_t)(tail[ 6]) << 48; /* fall through */
295 case 6: k1 ^= (uint64_t)(tail[ 5]) << 40; /* fall through */
296 case 5: k1 ^= (uint64_t)(tail[ 4]) << 32; /* fall through */
297 case 4: k1 ^= (uint64_t)(tail[ 3]) << 24; /* fall through */
298 case 3: k1 ^= (uint64_t)(tail[ 2]) << 16; /* fall through */
299 case 2: k1 ^= (uint64_t)(tail[ 1]) << 8; /* fall through */
300 case 1: k1 ^= (uint64_t)(tail[ 0]) << 0; /* fall through */
301 k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
302 };
303
304 //----------
305 // finalization
306
307 h1 ^= len; h2 ^= len;
308
309 h1 += h2;
310 h2 += h1;
311
312 h1 = fmix64(h1);
313 h2 = fmix64(h2);
314
315 h1 += h2;
316 h2 += h1;
317
318 ((uint64_t*)out)[0] = h1;
319 ((uint64_t*)out)[1] = h2;
320 }
321
322 //-----------------------------------------------------------------------------
323