The Pedigree Project  0.1
MurmurHash3.cpp
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 //-----------------------------------------------------------------------------
21 // MurmurHash3 was written by Austin Appleby, and is placed in the public
22 // domain. The author hereby disclaims copyright to this source code.
23 
24 // Note - The x86 and x64 versions do _not_ produce the same results, as the
25 // algorithms are optimized for their respective platforms. You can still
26 // compile and run any of them on any platform, but your performance with the
27 // non-native version will be less than optimal.
28 
29 #include "pedigree/kernel/utilities/smhasher/MurmurHash3.h"
30 
31 //-----------------------------------------------------------------------------
32 // Platform-specific functions and macros
33 
34 // Microsoft Visual Studio
35 
36 #if defined(_MSC_VER)
37 
38 #define FORCE_INLINE __forceinline
39 
40 #include <stdlib.h>
41 
42 #define ROTL32(x,y) _rotl(x,y)
43 #define ROTL64(x,y) _rotl64(x,y)
44 
45 #define BIG_CONSTANT(x) (x)
46 
47 // Other compilers
48 
49 #else // defined(_MSC_VER)
50 
51 #define FORCE_INLINE inline __attribute__((always_inline))
52 
53 inline uint32_t rotl32 ( uint32_t x, int8_t r )
54 {
55  return (x << r) | (x >> (32 - r));
56 }
57 
58 inline uint64_t rotl64 ( uint64_t x, int8_t r )
59 {
60  return (x << r) | (x >> (64 - r));
61 }
62 
63 #define ROTL32(x,y) rotl32(x,y)
64 #define ROTL64(x,y) rotl64(x,y)
65 
66 #define BIG_CONSTANT(x) (x##LLU)
67 
68 #endif // !defined(_MSC_VER)
69 
70 //-----------------------------------------------------------------------------
71 // Block read - if your platform needs to do endian-swapping or can only
72 // handle aligned reads, do the conversion here
73 
74 FORCE_INLINE uint32_t getblock32 ( const uint32_t * p, int i )
75 {
76  return p[i];
77 }
78 
79 FORCE_INLINE uint64_t getblock64 ( const uint64_t * p, int i )
80 {
81  return p[i];
82 }
83 
84 //-----------------------------------------------------------------------------
85 // Finalization mix - force all bits of a hash block to avalanche
86 
87 FORCE_INLINE uint32_t fmix32 ( uint32_t h )
88 {
89  h ^= h >> 16;
90  h *= 0x85ebca6b;
91  h ^= h >> 13;
92  h *= 0xc2b2ae35;
93  h ^= h >> 16;
94 
95  return h;
96 }
97 
98 //----------
99 
100 FORCE_INLINE uint64_t fmix64 ( uint64_t k )
101 {
102  k ^= k >> 33;
103  k *= BIG_CONSTANT(0xff51afd7ed558ccd);
104  k ^= k >> 33;
105  k *= BIG_CONSTANT(0xc4ceb9fe1a85ec53);
106  k ^= k >> 33;
107 
108  return k;
109 }
110 
111 //-----------------------------------------------------------------------------
112 
113 void MurmurHash3_x86_32 ( const void * key, int len,
114  uint32_t seed, void * out )
115 {
116  const uint8_t * data = (const uint8_t*)key;
117  const int nblocks = len / 4;
118 
119  uint32_t h1 = seed;
120 
121  const uint32_t c1 = 0xcc9e2d51;
122  const uint32_t c2 = 0x1b873593;
123 
124  //----------
125  // body
126 
127  const uint32_t * blocks = (const uint32_t *)(data + nblocks*4);
128 
129  for(int i = -nblocks; i; i++)
130  {
131  uint32_t k1 = getblock32(blocks,i);
132 
133  k1 *= c1;
134  k1 = ROTL32(k1,15);
135  k1 *= c2;
136 
137  h1 ^= k1;
138  h1 = ROTL32(h1,13);
139  h1 = h1*5+0xe6546b64;
140  }
141 
142  //----------
143  // tail
144 
145  const uint8_t * tail = (const uint8_t*)(data + nblocks*4);
146 
147  uint32_t k1 = 0;
148 
149  switch(len & 3)
150  {
151  case 3: k1 ^= tail[2] << 16;
152  case 2: k1 ^= tail[1] << 8;
153  case 1: k1 ^= tail[0];
154  k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
155  };
156 
157  //----------
158  // finalization
159 
160  h1 ^= len;
161 
162  h1 = fmix32(h1);
163 
164  *(uint32_t*)out = h1;
165 }
166 
167 //-----------------------------------------------------------------------------
168 
169 void MurmurHash3_x86_128 ( const void * key, const int len,
170  uint32_t seed, void * out )
171 {
172  const uint8_t * data = (const uint8_t*)key;
173  const int nblocks = len / 16;
174 
175  uint32_t h1 = seed;
176  uint32_t h2 = seed;
177  uint32_t h3 = seed;
178  uint32_t h4 = seed;
179 
180  const uint32_t c1 = 0x239b961b;
181  const uint32_t c2 = 0xab0e9789;
182  const uint32_t c3 = 0x38b34ae5;
183  const uint32_t c4 = 0xa1e38b93;
184 
185  //----------
186  // body
187 
188  const uint32_t * blocks = (const uint32_t *)(data + nblocks*16);
189 
190  for(int i = -nblocks; i; i++)
191  {
192  uint32_t k1 = getblock32(blocks,i*4+0);
193  uint32_t k2 = getblock32(blocks,i*4+1);
194  uint32_t k3 = getblock32(blocks,i*4+2);
195  uint32_t k4 = getblock32(blocks,i*4+3);
196 
197  k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
198 
199  h1 = ROTL32(h1,19); h1 += h2; h1 = h1*5+0x561ccd1b;
200 
201  k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2;
202 
203  h2 = ROTL32(h2,17); h2 += h3; h2 = h2*5+0x0bcaa747;
204 
205  k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3;
206 
207  h3 = ROTL32(h3,15); h3 += h4; h3 = h3*5+0x96cd1c35;
208 
209  k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4;
210 
211  h4 = ROTL32(h4,13); h4 += h1; h4 = h4*5+0x32ac3b17;
212  }
213 
214  //----------
215  // tail
216 
217  const uint8_t * tail = (const uint8_t*)(data + nblocks*16);
218 
219  uint32_t k1 = 0;
220  uint32_t k2 = 0;
221  uint32_t k3 = 0;
222  uint32_t k4 = 0;
223 
224  switch(len & 15)
225  {
226  case 15: k4 ^= tail[14] << 16;
227  case 14: k4 ^= tail[13] << 8;
228  case 13: k4 ^= tail[12] << 0;
229  k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4;
230 
231  case 12: k3 ^= tail[11] << 24;
232  case 11: k3 ^= tail[10] << 16;
233  case 10: k3 ^= tail[ 9] << 8;
234  case 9: k3 ^= tail[ 8] << 0;
235  k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3;
236 
237  case 8: k2 ^= tail[ 7] << 24;
238  case 7: k2 ^= tail[ 6] << 16;
239  case 6: k2 ^= tail[ 5] << 8;
240  case 5: k2 ^= tail[ 4] << 0;
241  k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2;
242 
243  case 4: k1 ^= tail[ 3] << 24;
244  case 3: k1 ^= tail[ 2] << 16;
245  case 2: k1 ^= tail[ 1] << 8;
246  case 1: k1 ^= tail[ 0] << 0;
247  k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
248  };
249 
250  //----------
251  // finalization
252 
253  h1 ^= len; h2 ^= len; h3 ^= len; h4 ^= len;
254 
255  h1 += h2; h1 += h3; h1 += h4;
256  h2 += h1; h3 += h1; h4 += h1;
257 
258  h1 = fmix32(h1);
259  h2 = fmix32(h2);
260  h3 = fmix32(h3);
261  h4 = fmix32(h4);
262 
263  h1 += h2; h1 += h3; h1 += h4;
264  h2 += h1; h3 += h1; h4 += h1;
265 
266  ((uint32_t*)out)[0] = h1;
267  ((uint32_t*)out)[1] = h2;
268  ((uint32_t*)out)[2] = h3;
269  ((uint32_t*)out)[3] = h4;
270 }
271 
272 //-----------------------------------------------------------------------------
273 
274 void MurmurHash3_x64_128 ( const void * key, const int len,
275  const uint32_t seed, void * out )
276 {
277  const uint8_t * data = (const uint8_t*)key;
278  const int nblocks = len / 16;
279 
280  uint64_t h1 = seed;
281  uint64_t h2 = seed;
282 
283  const uint64_t c1 = BIG_CONSTANT(0x87c37b91114253d5);
284  const uint64_t c2 = BIG_CONSTANT(0x4cf5ad432745937f);
285 
286  //----------
287  // body
288 
289  const uint64_t * blocks = (const uint64_t *)(data);
290 
291  for(int i = 0; i < nblocks; i++)
292  {
293  uint64_t k1 = getblock64(blocks,i*2+0);
294  uint64_t k2 = getblock64(blocks,i*2+1);
295 
296  k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
297 
298  h1 = ROTL64(h1,27); h1 += h2; h1 = h1*5+0x52dce729;
299 
300  k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
301 
302  h2 = ROTL64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5;
303  }
304 
305  //----------
306  // tail
307 
308  const uint8_t * tail = (const uint8_t*)(data + nblocks*16);
309 
310  uint64_t k1 = 0;
311  uint64_t k2 = 0;
312 
313  switch(len & 15)
314  {
315  case 15: k2 ^= ((uint64_t)tail[14]) << 48;
316  case 14: k2 ^= ((uint64_t)tail[13]) << 40;
317  case 13: k2 ^= ((uint64_t)tail[12]) << 32;
318  case 12: k2 ^= ((uint64_t)tail[11]) << 24;
319  case 11: k2 ^= ((uint64_t)tail[10]) << 16;
320  case 10: k2 ^= ((uint64_t)tail[ 9]) << 8;
321  case 9: k2 ^= ((uint64_t)tail[ 8]) << 0;
322  k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
323 
324  case 8: k1 ^= ((uint64_t)tail[ 7]) << 56;
325  case 7: k1 ^= ((uint64_t)tail[ 6]) << 48;
326  case 6: k1 ^= ((uint64_t)tail[ 5]) << 40;
327  case 5: k1 ^= ((uint64_t)tail[ 4]) << 32;
328  case 4: k1 ^= ((uint64_t)tail[ 3]) << 24;
329  case 3: k1 ^= ((uint64_t)tail[ 2]) << 16;
330  case 2: k1 ^= ((uint64_t)tail[ 1]) << 8;
331  case 1: k1 ^= ((uint64_t)tail[ 0]) << 0;
332  k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
333  };
334 
335  //----------
336  // finalization
337 
338  h1 ^= len; h2 ^= len;
339 
340  h1 += h2;
341  h2 += h1;
342 
343  h1 = fmix64(h1);
344  h2 = fmix64(h2);
345 
346  h1 += h2;
347  h2 += h1;
348 
349  ((uint64_t*)out)[0] = h1;
350  ((uint64_t*)out)[1] = h2;
351 }
352 
353 //-----------------------------------------------------------------------------
354