Debian Codegraph @

src/OVAL/probes/SEAP/MurmurHash3.c

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