Debian Codegraph @

murmurhash/MurmurHash3.cpp

raw
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 "murmurhash/MurmurHash3.h"
11
12 //-----------------------------------------------------------------------------
13 // Platform-specific functions and macros
14
15 // Microsoft Visual Studio
16
17 #if defined(_MSC_VER)
18
19 #define FORCE_INLINE __forceinline
20
21 #include <stdlib.h>
22
23 #define ROTL32(x,y) _rotl(x,y)
24 #define ROTL64(x,y) _rotl64(x,y)
25
26 #define BIG_CONSTANT(x) (x)
27
28 // Other compilers
29
30 #else // defined(_MSC_VER)
31
32 #if defined(GNUC) && ((GNUC > 4) || (GNUC == 4 && GNUC_MINOR >= 4))
33
34 /* gcc version >= 4.4 4.1 = RHEL 5, 4.4 = RHEL 6.
35 * Don't inline for RHEL 5 gcc which is 4.1 */
36 #define FORCE_INLINE attribute((always_inline))
37
38 #else
39
40 #define FORCE_INLINE
41
42 #endif
43
44
45 inline uint32_t rotl32 ( uint32_t x, int8_t r )
46 {
47 return (x << r) | (x >> (32 - r));
48 }
49
50 inline uint64_t rotl64 ( uint64_t x, int8_t r )
51 {
52 return (x << r) | (x >> (64 - r));
53 }
54
55 #define ROTL32(x,y) rotl32(x,y)
56 #define ROTL64(x,y) rotl64(x,y)
57
58 #define BIG_CONSTANT(x) (x##LLU)
59
60 #endif // !defined(_MSC_VER)
61
62 //-----------------------------------------------------------------------------
63 // Block read - on little-endian machines this is a single load,
64 // while on big-endian or unknown machines the byte accesses should
65 // still get optimized into the most efficient instruction.
66 FORCE_INLINE uint32_t getblock ( const uint32_t * p, int i )
67 {
68 #if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
69 return p[i];
70 #else
71 const uint8_t *c = (const uint8_t *)&p[i];
72 return (uint32_t)c[0] |
73 (uint32_t)c[1] << 8 |
74 (uint32_t)c[2] << 16 |
75 (uint32_t)c[3] << 24;
76 #endif
77 }
78
79 FORCE_INLINE uint64_t getblock ( const uint64_t * p, int i )
80 {
81 #if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
82 return p[i];
83 #else
84 const uint8_t *c = (const uint8_t *)&p[i];
85 return (uint64_t)c[0] |
86 (uint64_t)c[1] << 8 |
87 (uint64_t)c[2] << 16 |
88 (uint64_t)c[3] << 24 |
89 (uint64_t)c[4] << 32 |
90 (uint64_t)c[5] << 40 |
91 (uint64_t)c[6] << 48 |
92 (uint64_t)c[7] << 56;
93 #endif
94 }
95
96 //-----------------------------------------------------------------------------
97 // Finalization mix - force all bits of a hash block to avalanche
98
99 FORCE_INLINE uint32_t fmix ( uint32_t h )
100 {
101 h ^= h >> 16;
102 h *= 0x85ebca6b;
103 h ^= h >> 13;
104 h *= 0xc2b2ae35;
105 h ^= h >> 16;
106
107 return h;
108 }
109
110 //----------
111
112 FORCE_INLINE uint64_t fmix ( uint64_t k )
113 {
114 k ^= k >> 33;
115 k *= BIG_CONSTANT(0xff51afd7ed558ccd);
116 k ^= k >> 33;
117 k *= BIG_CONSTANT(0xc4ceb9fe1a85ec53);
118 k ^= k >> 33;
119
120 return k;
121 }
122
123 //-----------------------------------------------------------------------------
124
125 void MurmurHash3_x86_32 ( const void * key, int len,
126 uint32_t seed, void * out )
127 {
128 const uint8_t * data = (const uint8_t*)key;
129 const int nblocks = len / 4;
130
131 uint32_t h1 = seed;
132
133 uint32_t c1 = 0xcc9e2d51;
134 uint32_t c2 = 0x1b873593;
135
136 //----------
137 // body
138
139 const uint32_t * blocks = (const uint32_t *)(data + nblocks*4);
140
141 for(int i = -nblocks; i; i++)
142 {
143 uint32_t k1 = getblock(blocks,i);
144
145 k1 *= c1;
146 k1 = ROTL32(k1,15);
147 k1 *= c2;
148
149 h1 ^= k1;
150 h1 = ROTL32(h1,13);
151 h1 = h1*5+0xe6546b64;
152 }
153
154 //----------
155 // tail
156
157 const uint8_t * tail = (const uint8_t*)(data + nblocks*4);
158
159 uint32_t k1 = 0;
160
161 switch(len & 3)
162 {
163 case 3: k1 ^= tail[2] << 16;
164 case 2: k1 ^= tail[1] << 8;
165 case 1: k1 ^= tail[0];
166 k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
167 };
168
169 //----------
170 // finalization
171
172 h1 ^= len;
173
174 h1 = fmix(h1);
175
176 *(uint32_t*)out = h1;
177 }
178
179 //-----------------------------------------------------------------------------
180
181 void MurmurHash3_x86_128 ( const void * key, const int len,
182 uint32_t seed, void * out )
183 {
184 const uint8_t * data = (const uint8_t*)key;
185 const int nblocks = len / 16;
186
187 uint32_t h1 = seed;
188 uint32_t h2 = seed;
189 uint32_t h3 = seed;
190 uint32_t h4 = seed;
191
192 uint32_t c1 = 0x239b961b;
193 uint32_t c2 = 0xab0e9789;
194 uint32_t c3 = 0x38b34ae5;
195 uint32_t c4 = 0xa1e38b93;
196
197 //----------
198 // body
199
200 const uint32_t * blocks = (const uint32_t *)(data + nblocks*16);
201
202 for(int i = -nblocks; i; i++)
203 {
204 uint32_t k1 = getblock(blocks,i*4+0);
205 uint32_t k2 = getblock(blocks,i*4+1);
206 uint32_t k3 = getblock(blocks,i*4+2);
207 uint32_t k4 = getblock(blocks,i*4+3);
208
209 k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
210
211 h1 = ROTL32(h1,19); h1 += h2; h1 = h1*5+0x561ccd1b;
212
213 k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2;
214
215 h2 = ROTL32(h2,17); h2 += h3; h2 = h2*5+0x0bcaa747;
216
217 k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3;
218
219 h3 = ROTL32(h3,15); h3 += h4; h3 = h3*5+0x96cd1c35;
220
221 k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4;
222
223 h4 = ROTL32(h4,13); h4 += h1; h4 = h4*5+0x32ac3b17;
224 }
225
226 //----------
227 // tail
228
229 const uint8_t * tail = (const uint8_t*)(data + nblocks*16);
230
231 uint32_t k1 = 0;
232 uint32_t k2 = 0;
233 uint32_t k3 = 0;
234 uint32_t k4 = 0;
235
236 switch(len & 15)
237 {
238 case 15: k4 ^= tail[14] << 16;
239 case 14: k4 ^= tail[13] << 8;
240 case 13: k4 ^= tail[12] << 0;
241 k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4;
242
243 case 12: k3 ^= tail[11] << 24;
244 case 11: k3 ^= tail[10] << 16;
245 case 10: k3 ^= tail[ 9] << 8;
246 case 9: k3 ^= tail[ 8] << 0;
247 k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3;
248
249 case 8: k2 ^= tail[ 7] << 24;
250 case 7: k2 ^= tail[ 6] << 16;
251 case 6: k2 ^= tail[ 5] << 8;
252 case 5: k2 ^= tail[ 4] << 0;
253 k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2;
254
255 case 4: k1 ^= tail[ 3] << 24;
256 case 3: k1 ^= tail[ 2] << 16;
257 case 2: k1 ^= tail[ 1] << 8;
258 case 1: k1 ^= tail[ 0] << 0;
259 k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
260 };
261
262 //----------
263 // finalization
264
265 h1 ^= len; h2 ^= len; h3 ^= len; h4 ^= len;
266
267 h1 += h2; h1 += h3; h1 += h4;
268 h2 += h1; h3 += h1; h4 += h1;
269
270 h1 = fmix(h1);
271 h2 = fmix(h2);
272 h3 = fmix(h3);
273 h4 = fmix(h4);
274
275 h1 += h2; h1 += h3; h1 += h4;
276 h2 += h1; h3 += h1; h4 += h1;
277
278 ((uint32_t*)out)[0] = h1;
279 ((uint32_t*)out)[1] = h2;
280 ((uint32_t*)out)[2] = h3;
281 ((uint32_t*)out)[3] = h4;
282 }
283
284 //-----------------------------------------------------------------------------
285
286 void MurmurHash3_x64_128 ( const void * key, const int len,
287 const uint32_t seed, void * out )
288 {
289 const uint8_t * data = (const uint8_t*)key;
290 const int nblocks = len / 16;
291
292 uint64_t h1 = seed;
293 uint64_t h2 = seed;
294
295 uint64_t c1 = BIG_CONSTANT(0x87c37b91114253d5);
296 uint64_t c2 = BIG_CONSTANT(0x4cf5ad432745937f);
297
298 //----------
299 // body
300
301 const uint64_t * blocks = (const uint64_t *)(data);
302
303 for(int i = 0; i < nblocks; i++)
304 {
305 uint64_t k1 = getblock(blocks,i*2+0);
306 uint64_t k2 = getblock(blocks,i*2+1);
307
308 k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
309
310 h1 = ROTL64(h1,27); h1 += h2; h1 = h1*5+0x52dce729;
311
312 k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
313
314 h2 = ROTL64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5;
315 }
316
317 //----------
318 // tail
319
320 const uint8_t * tail = (const uint8_t*)(data + nblocks*16);
321
322 uint64_t k1 = 0;
323 uint64_t k2 = 0;
324
325 switch(len & 15)
326 {
327 case 15: k2 ^= uint64_t(tail[14]) << 48;
328 case 14: k2 ^= uint64_t(tail[13]) << 40;
329 case 13: k2 ^= uint64_t(tail[12]) << 32;
330 case 12: k2 ^= uint64_t(tail[11]) << 24;
331 case 11: k2 ^= uint64_t(tail[10]) << 16;
332 case 10: k2 ^= uint64_t(tail[ 9]) << 8;
333 case 9: k2 ^= uint64_t(tail[ 8]) << 0;
334 k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
335
336 case 8: k1 ^= uint64_t(tail[ 7]) << 56;
337 case 7: k1 ^= uint64_t(tail[ 6]) << 48;
338 case 6: k1 ^= uint64_t(tail[ 5]) << 40;
339 case 5: k1 ^= uint64_t(tail[ 4]) << 32;
340 case 4: k1 ^= uint64_t(tail[ 3]) << 24;
341 case 3: k1 ^= uint64_t(tail[ 2]) << 16;
342 case 2: k1 ^= uint64_t(tail[ 1]) << 8;
343 case 1: k1 ^= uint64_t(tail[ 0]) << 0;
344 k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
345 };
346
347 //----------
348 // finalization
349
350 h1 ^= len; h2 ^= len;
351
352 h1 += h2;
353 h2 += h1;
354
355 h1 = fmix(h1);
356 h2 = fmix(h2);
357
358 h1 += h2;
359 h2 += h1;
360
361 ((uint64_t*)out)[0] = h1;
362 ((uint64_t*)out)[1] = h2;
363 }
364
365 //-----------------------------------------------------------------------------
366