Merge branch 'master' of git://git.lttng.org/lttng-tools
[lttng-tools.git] / liblttng-ht / utils.c
CommitLineData
819dc7d4
DG
1/*
2 * Copyright (C) - Bob Jenkins, May 2006, Public Domain.
3 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
bec39940 4 * Copyright (C) 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
819dc7d4
DG
5 *
6 * These are functions for producing 32-bit hashes for hash table lookup.
7 * hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final() are
8 * externally useful functions. Routines to test the hash are included if
9 * SELF_TEST is defined. You can use this free for any purpose. It's in the
10 * public domain. It has no warranty.
11 *
12 * You probably want to use hashlittle(). hashlittle() and hashbig() hash byte
13 * arrays. hashlittle() is is faster than hashbig() on little-endian machines.
14 * Intel and AMD are little-endian machines. On second thought, you probably
15 * want hashlittle2(), which is identical to hashlittle() except it returns two
16 * 32-bit hashes for the price of one. You could implement hashbig2() if you
17 * wanted but I haven't bothered here.
18 *
19 * If you want to find a hash of, say, exactly 7 integers, do
20 * a = i1; b = i2; c = i3;
21 * mix(a,b,c);
22 * a += i4; b += i5; c += i6;
23 * mix(a,b,c);
24 * a += i7;
25 * final(a,b,c);
26 * then use c as the hash value. If you have a variable length array of
27 * 4-byte integers to hash, use hashword(). If you have a byte array (like
28 * a character string), use hashlittle(). If you have several byte arrays, or
29 * a mix of things, see the comments above hashlittle().
30 *
31 * Why is this so big? I read 12 bytes at a time into 3 4-byte integers, then
32 * mix those integers. This is fast (you can do a lot more thorough mixing
33 * with 12*3 instructions on 3 integers than you can with 3 instructions on 1
34 * byte), but shoehorning those bytes into integers efficiently is messy.
35 */
36
bec39940 37#include <assert.h>
819dc7d4 38#include <endian.h> /* attempt to define endianness */
bec39940
DG
39#include <stdint.h> /* defines uint32_t etc */
40#include <stdio.h> /* defines printf for tests */
819dc7d4 41#include <string.h>
bec39940
DG
42#include <sys/param.h> /* attempt to define endianness */
43#include <time.h> /* defines time_t for timings in the test */
0df502fd 44#include <urcu/compiler.h>
819dc7d4 45
bec39940
DG
46#include "utils.h"
47
819dc7d4
DG
48/*
49 * My best guess at if you are big-endian or little-endian. This may
50 * need adjustment.
51 */
52#if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \
53 __BYTE_ORDER == __LITTLE_ENDIAN) || \
54 (defined(i386) || defined(__i386__) || defined(__i486__) || \
55 defined(__i586__) || defined(__i686__) || defined(vax) || defined(MIPSEL))
56# define HASH_LITTLE_ENDIAN 1
57# define HASH_BIG_ENDIAN 0
58#elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
59 __BYTE_ORDER == __BIG_ENDIAN) || \
60 (defined(sparc) || defined(POWERPC) || defined(mc68000) || defined(sel))
61# define HASH_LITTLE_ENDIAN 0
62# define HASH_BIG_ENDIAN 1
63#else
64# define HASH_LITTLE_ENDIAN 0
65# define HASH_BIG_ENDIAN 0
66#endif
67
68#define hashsize(n) ((uint32_t)1<<(n))
69#define hashmask(n) (hashsize(n)-1)
70#define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
71
72/*
73 * mix -- mix 3 32-bit values reversibly.
74 *
75 * This is reversible, so any information in (a,b,c) before mix() is
76 * still in (a,b,c) after mix().
77 *
78 * If four pairs of (a,b,c) inputs are run through mix(), or through
79 * mix() in reverse, there are at least 32 bits of the output that
80 * are sometimes the same for one pair and different for another pair.
81 * This was tested for:
82 * * pairs that differed by one bit, by two bits, in any combination
83 * of top bits of (a,b,c), or in any combination of bottom bits of
84 * (a,b,c).
85 * * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
86 * the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
87 * is commonly produced by subtraction) look like a single 1-bit
88 * difference.
89 * * the base values were pseudorandom, all zero but one bit set, or
90 * all zero plus a counter that starts at zero.
91 *
92 * Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
93 * satisfy this are
94 * 4 6 8 16 19 4
95 * 9 15 3 18 27 15
96 * 14 9 3 7 17 3
97 * Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
98 * for "differ" defined as + with a one-bit base and a two-bit delta. I
99 * used http://burtleburtle.net/bob/hash/avalanche.html to choose
100 * the operations, constants, and arrangements of the variables.
101 *
102 * This does not achieve avalanche. There are input bits of (a,b,c)
103 * that fail to affect some output bits of (a,b,c), especially of a. The
104 * most thoroughly mixed value is c, but it doesn't really even achieve
105 * avalanche in c.
106 *
107 * This allows some parallelism. Read-after-writes are good at doubling
108 * the number of bits affected, so the goal of mixing pulls in the opposite
109 * direction as the goal of parallelism. I did what I could. Rotates
110 * seem to cost as much as shifts on every machine I could lay my hands
111 * on, and rotates are much kinder to the top and bottom bits, so I used
112 * rotates.
113 */
114#define mix(a,b,c) \
115{ \
116 a -= c; a ^= rot(c, 4); c += b; \
117 b -= a; b ^= rot(a, 6); a += c; \
118 c -= b; c ^= rot(b, 8); b += a; \
119 a -= c; a ^= rot(c,16); c += b; \
120 b -= a; b ^= rot(a,19); a += c; \
121 c -= b; c ^= rot(b, 4); b += a; \
122}
123
124/*
125 * final -- final mixing of 3 32-bit values (a,b,c) into c
126 *
127 * Pairs of (a,b,c) values differing in only a few bits will usually
128 * produce values of c that look totally different. This was tested for
129 * * pairs that differed by one bit, by two bits, in any combination
130 * of top bits of (a,b,c), or in any combination of bottom bits of
131 * (a,b,c).
132 * * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
133 * the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
134 * is commonly produced by subtraction) look like a single 1-bit
135 * difference.
136 * * the base values were pseudorandom, all zero but one bit set, or
137 * all zero plus a counter that starts at zero.
138 *
139 * These constants passed:
140 * 14 11 25 16 4 14 24
141 * 12 14 25 16 4 14 24
142 * and these came close:
143 * 4 8 15 26 3 22 24
144 * 10 8 15 26 3 22 24
145 * 11 8 15 26 3 22 24
146 */
147#define final(a,b,c) \
148{ \
149 c ^= b; c -= rot(b,14); \
150 a ^= c; a -= rot(c,11); \
151 b ^= a; b -= rot(a,25); \
152 c ^= b; c -= rot(b,16); \
153 a ^= c; a -= rot(c,4); \
154 b ^= a; b -= rot(a,14); \
155 c ^= b; c -= rot(b,24); \
156}
157
bec39940
DG
158/*
159 * k - the key, an array of uint32_t values
160 * length - the length of the key, in uint32_ts
161 * initval - the previous hash, or an arbitrary value
162 */
163static uint32_t __attribute__((unused)) hashword(const uint32_t *k,
164 size_t length, uint32_t initval)
0df502fd
MD
165{
166 uint32_t a, b, c;
167
168 /* Set up the internal state */
169 a = b = c = 0xdeadbeef + (((uint32_t) length) << 2) + initval;
170
171 /*----------------------------------------- handle most of the key */
172 while (length > 3) {
173 a += k[0];
174 b += k[1];
175 c += k[2];
176 mix(a, b, c);
177 length -= 3;
178 k += 3;
179 }
180
181 /*----------------------------------- handle the last 3 uint32_t's */
182 switch (length) { /* all the case statements fall through */
183 case 3: c += k[2];
184 case 2: b += k[1];
185 case 1: a += k[0];
186 final(a, b, c);
187 case 0: /* case 0: nothing left to add */
188 break;
189 }
190 /*---------------------------------------------- report the result */
191 return c;
192}
193
194
819dc7d4
DG
195/*
196 * hashword2() -- same as hashword(), but take two seeds and return two 32-bit
197 * values. pc and pb must both be nonnull, and *pc and *pb must both be
198 * initialized with seeds. If you pass in (*pb)==0, the output (*pc) will be
199 * the same as the return value from hashword().
200 */
bec39940 201static void __attribute__((unused)) hashword2(const uint32_t *k, size_t length,
819dc7d4
DG
202 uint32_t *pc, uint32_t *pb)
203{
204 uint32_t a, b, c;
205
206 /* Set up the internal state */
207 a = b = c = 0xdeadbeef + ((uint32_t) (length << 2)) + *pc;
208 c += *pb;
209
210 while (length > 3) {
211 a += k[0];
212 b += k[1];
213 c += k[2];
214 mix(a, b, c);
215 length -= 3;
216 k += 3;
217 }
218
219 switch (length) {
220 case 3 :
221 c += k[2];
222 case 2 :
223 b += k[1];
224 case 1 :
225 a += k[0];
226 final(a, b, c);
227 case 0: /* case 0: nothing left to add */
228 break;
229 }
230
231 *pc = c;
232 *pb = b;
233}
234
235/*
236 * hashlittle() -- hash a variable-length key into a 32-bit value
237 * k : the key (the unaligned variable-length array of bytes)
238 * length : the length of the key, counting by bytes
239 * initval : can be any 4-byte value
240 * Returns a 32-bit value. Every bit of the key affects every bit of
241 * the return value. Two keys differing by one or two bits will have
242 * totally different hash values.
243 *
244 * The best hash table sizes are powers of 2. There is no need to do
245 * mod a prime (mod is sooo slow!). If you need less than 32 bits,
246 * use a bitmask. For example, if you need only 10 bits, do
247 * h = (h & hashmask(10));
248 * In which case, the hash table should have hashsize(10) elements.
249 *
250 * If you are hashing n strings (uint8_t **)k, do it like this:
251 * for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);
252 *
253 * By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this
254 * code any way you wish, private, educational, or commercial. It's free.
255 *
256 * Use for hash table lookup, or anything where one collision in 2^^32 is
257 * acceptable. Do NOT use for cryptographic purposes.
258 */
bec39940
DG
259static uint32_t __attribute__((unused)) hashlittle(const void *key,
260 size_t length, uint32_t initval)
819dc7d4
DG
261{
262 uint32_t a,b,c;
263 union {
264 const void *ptr;
265 size_t i;
266 } u; /* needed for Mac Powerbook G4 */
267
268 /* Set up the internal state */
269 a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
270
271 u.ptr = key;
272 if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
273 const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
274
275 /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
276 while (length > 12) {
277 a += k[0];
278 b += k[1];
279 c += k[2];
280 mix(a,b,c);
281 length -= 12;
282 k += 3;
283 }
284
285 /*
286 * "k[2]&0xffffff" actually reads beyond the end of the string, but
287 * then masks off the part it's not allowed to read. Because the
288 * string is aligned, the masked-off tail is in the same word as the
289 * rest of the string. Every machine with memory protection I've seen
290 * does it on word boundaries, so is OK with this. But VALGRIND will
291 * still catch it and complain. The masking trick does make the hash
292 * noticably faster for short strings (like English words).
293 */
294#ifndef VALGRIND
295
296 switch (length) {
297 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
298 case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
299 case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
300 case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
301 case 8 : b+=k[1]; a+=k[0]; break;
302 case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
303 case 6 : b+=k[1]&0xffff; a+=k[0]; break;
304 case 5 : b+=k[1]&0xff; a+=k[0]; break;
305 case 4 : a+=k[0]; break;
306 case 3 : a+=k[0]&0xffffff; break;
307 case 2 : a+=k[0]&0xffff; break;
308 case 1 : a+=k[0]&0xff; break;
309 case 0 : return c; /* zero length strings require no mixing */
310 }
311#else /* make valgrind happy */
312 const uint8_t *k8;
313
314 k8 = (const uint8_t *)k;
315 switch (length) {
316 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
317 case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
318 case 10: c+=((uint32_t)k8[9])<<8; /* fall through */
319 case 9 : c+=k8[8]; /* fall through */
320 case 8 : b+=k[1]; a+=k[0]; break;
321 case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
322 case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */
323 case 5 : b+=k8[4]; /* fall through */
324 case 4 : a+=k[0]; break;
325 case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
326 case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */
327 case 1 : a+=k8[0]; break;
328 case 0 : return c;
329 }
330#endif /* !valgrind */
331 } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
332 const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
333 const uint8_t *k8;
334
335 /*--------------- all but last block: aligned reads and different mixing */
336 while (length > 12) {
337 a += k[0] + (((uint32_t)k[1])<<16);
338 b += k[2] + (((uint32_t)k[3])<<16);
339 c += k[4] + (((uint32_t)k[5])<<16);
340 mix(a,b,c);
341 length -= 12;
342 k += 6;
343 }
344
345 k8 = (const uint8_t *)k;
346 switch (length) {
347 case 12:
348 c+=k[4]+(((uint32_t)k[5])<<16);
349 b+=k[2]+(((uint32_t)k[3])<<16);
350 a+=k[0]+(((uint32_t)k[1])<<16);
351 break;
352 case 11:
353 c+=((uint32_t)k8[10])<<16; /* fall through */
354 case 10:
355 c+=k[4];
356 b+=k[2]+(((uint32_t)k[3])<<16);
357 a+=k[0]+(((uint32_t)k[1])<<16);
358 break;
359 case 9:
360 c+=k8[8]; /* fall through */
361 case 8:
362 b+=k[2]+(((uint32_t)k[3])<<16);
363 a+=k[0]+(((uint32_t)k[1])<<16);
364 break;
365 case 7:
366 b+=((uint32_t)k8[6])<<16; /* fall through */
367 case 6:
368 b+=k[2];
369 a+=k[0]+(((uint32_t)k[1])<<16);
370 break;
371 case 5:
372 b+=k8[4]; /* fall through */
373 case 4:
374 a+=k[0]+(((uint32_t)k[1])<<16);
375 break;
376 case 3:
377 a+=((uint32_t)k8[2])<<16; /* fall through */
378 case 2:
379 a+=k[0];
380 break;
381 case 1:
382 a+=k8[0];
383 break;
384 case 0:
385 return c; /* zero length requires no mixing */
386 }
387
388 } else { /* need to read the key one byte at a time */
389 const uint8_t *k = (const uint8_t *)key;
390
391 while (length > 12) {
392 a += k[0];
393 a += ((uint32_t)k[1])<<8;
394 a += ((uint32_t)k[2])<<16;
395 a += ((uint32_t)k[3])<<24;
396 b += k[4];
397 b += ((uint32_t)k[5])<<8;
398 b += ((uint32_t)k[6])<<16;
399 b += ((uint32_t)k[7])<<24;
400 c += k[8];
401 c += ((uint32_t)k[9])<<8;
402 c += ((uint32_t)k[10])<<16;
403 c += ((uint32_t)k[11])<<24;
404 mix(a,b,c);
405 length -= 12;
406 k += 12;
407 }
408
409 switch(length) { /* all the case statements fall through */
410 case 12: c+=((uint32_t)k[11])<<24;
411 case 11: c+=((uint32_t)k[10])<<16;
412 case 10: c+=((uint32_t)k[9])<<8;
413 case 9: c+=k[8];
414 case 8: b+=((uint32_t)k[7])<<24;
415 case 7: b+=((uint32_t)k[6])<<16;
416 case 6: b+=((uint32_t)k[5])<<8;
417 case 5: b+=k[4];
418 case 4: a+=((uint32_t)k[3])<<24;
419 case 3: a+=((uint32_t)k[2])<<16;
420 case 2: a+=((uint32_t)k[1])<<8;
421 case 1:
422 a+=k[0];
423 break;
424 case 0:
425 return c;
426 }
427 }
428
429 final(a,b,c);
430 return c;
431}
432
0df502fd
MD
433#if (CAA_BITS_PER_LONG == 64)
434/*
435 * Hash function for number value.
436 */
bec39940 437unsigned long hash_key_ulong(void *_key, unsigned long seed)
0df502fd
MD
438{
439 union {
440 uint64_t v64;
441 uint32_t v32[2];
442 } v;
443 union {
444 uint64_t v64;
445 uint32_t v32[2];
446 } key;
447
0df502fd
MD
448 v.v64 = (uint64_t) seed;
449 key.v64 = (uint64_t) _key;
450 hashword2(key.v32, 2, &v.v32[0], &v.v32[1]);
451 return v.v64;
452}
453#else
819dc7d4
DG
454/*
455 * Hash function for number value.
456 */
bec39940 457unsigned long hash_key_ulong(void *_key, unsigned long seed)
819dc7d4 458{
8da9ba32 459 uint32_t key = (uint32_t) _key;
0df502fd 460
0df502fd 461 return hashword(&key, 1, seed);
819dc7d4 462}
bec39940 463#endif /* CAA_BITS_PER_LONG */
819dc7d4
DG
464
465/*
466 * Hash function for string.
467 */
bec39940 468unsigned long hash_key_str(void *key, unsigned long seed)
819dc7d4 469{
bec39940 470 return hashlittle(key, strlen((char *) key), seed);
819dc7d4
DG
471}
472
473/*
474 * Hash function compare for number value.
475 */
bec39940 476int hash_match_key_ulong(void *key1, void *key2)
819dc7d4 477{
819dc7d4 478 if (key1 == key2) {
bec39940 479 return 1;
819dc7d4
DG
480 }
481
bec39940 482 return 0;
819dc7d4
DG
483}
484
485/*
486 * Hash compare function for string.
487 */
bec39940 488int hash_match_key_str(void *key1, void *key2)
819dc7d4 489{
bec39940
DG
490 if (strcmp(key1, key2) == 0) {
491 return 1;
819dc7d4
DG
492 }
493
bec39940 494 return 0;
819dc7d4 495}
This page took 0.084479 seconds and 4 git commands to generate.