Visualization Library 2.0.0-b3

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

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