2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 #ifndef _UST_COMMON_JHASH_H
8 #define _UST_COMMON_JHASH_H
12 #include <urcu/compiler.h>
13 #include <lttng/ust-endian.h>
17 * Source: http://burtleburtle.net/bob/c/lookup3.c
18 * Originally Public Domain
21 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
23 #define mix(a, b, c) \
25 a -= c; a ^= rot(c, 4); c += b; \
26 b -= a; b ^= rot(a, 6); a += c; \
27 c -= b; c ^= rot(b, 8); b += a; \
28 a -= c; a ^= rot(c, 16); c += b; \
29 b -= a; b ^= rot(a, 19); a += c; \
30 c -= b; c ^= rot(b, 4); b += a; \
33 #define final(a, b, c) \
35 c ^= b; c -= rot(b, 14); \
36 a ^= c; a -= rot(c, 11); \
37 b ^= a; b -= rot(a, 25); \
38 c ^= b; c -= rot(b, 16); \
39 a ^= c; a -= rot(c, 4);\
40 b ^= a; b -= rot(a, 14); \
41 c ^= b; c -= rot(b, 24); \
44 #if (BYTE_ORDER == LITTLE_ENDIAN)
45 #define HASH_LITTLE_ENDIAN 1
47 #define HASH_LITTLE_ENDIAN 0
52 * hashlittle() -- hash a variable-length key into a 32-bit value
53 * k : the key (the unaligned variable-length array of bytes)
54 * length : the length of the key, counting by bytes
55 * initval : can be any 4-byte value
56 * Returns a 32-bit value. Every bit of the key affects every bit of
57 * the return value. Two keys differing by one or two bits will have
58 * totally different hash values.
60 * The best hash table sizes are powers of 2. There is no need to do
61 * mod a prime (mod is sooo slow!). If you need less than 32 bits,
62 * use a bitmask. For example, if you need only 10 bits, do
63 * h = (h & hashmask(10));
64 * In which case, the hash table should have hashsize(10) elements.
66 * If you are hashing n strings (uint8_t **)k, do it like this:
67 * for (i = 0, h = 0; i < n; ++i) h = hashlittle(k[i], len[i], h);
69 * By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this
70 * code any way you wish, private, educational, or commercial. It's free.
72 * Use for hash table lookup, or anything where one collision in 2^^32 is
73 * acceptable. Do NOT use for cryptographic purposes.
76 uint32_t hashlittle(const void *key
, size_t length
, uint32_t initval
)
78 uint32_t a
, b
, c
; /* internal state */
84 /* Set up the internal state */
85 a
= b
= c
= 0xdeadbeef + ((uint32_t)length
) + initval
;
88 if (HASH_LITTLE_ENDIAN
&& ((u
.i
& 0x3) == 0)) {
89 const uint32_t *k
= (const uint32_t *) key
; /* read 32-bit chunks */
91 /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
101 /*----------------------------- handle the last (probably partial) block */
103 * The original jhash.h reads beyond the end of string, and implements
104 * a special code path for VALGRIND. It seems to make ASan unhappy too
105 * though, so considering that hashing event names is not a fast-path
106 * in lttng-ust, remove the "fast" code entirely and use the slower
107 * but verifiable VALGRIND version of the code which does not issue
108 * out-of-bound reads.
113 k8
= (const uint8_t *) k
;
115 case 12: c
+=k
[2]; b
+=k
[1]; a
+=k
[0]; break;
116 case 11: c
+=((uint32_t) k8
[10])<<16; /* fall through */
117 case 10: c
+=((uint32_t) k8
[9])<<8; /* fall through */
118 case 9 : c
+=k8
[8]; /* fall through */
119 case 8 : b
+=k
[1]; a
+=k
[0]; break;
120 case 7 : b
+=((uint32_t) k8
[6])<<16; /* fall through */
121 case 6 : b
+=((uint32_t) k8
[5])<<8; /* fall through */
122 case 5 : b
+=k8
[4]; /* fall through */
123 case 4 : a
+=k
[0]; break;
124 case 3 : a
+=((uint32_t) k8
[2])<<16; /* fall through */
125 case 2 : a
+=((uint32_t) k8
[1])<<8; /* fall through */
126 case 1 : a
+=k8
[0]; break;
131 } else if (HASH_LITTLE_ENDIAN
&& ((u
.i
& 0x1) == 0)) {
132 const uint16_t *k
= (const uint16_t *) key
; /* read 16-bit chunks */
135 /*--------------- all but last block: aligned reads and different mixing */
138 a
+= k
[0] + (((uint32_t) k
[1])<<16);
139 b
+= k
[2] + (((uint32_t) k
[3])<<16);
140 c
+= k
[4] + (((uint32_t) k
[5])<<16);
146 /*----------------------------- handle the last (probably partial) block */
147 k8
= (const uint8_t *) k
;
150 case 12: c
+=k
[4]+(((uint32_t) k
[5])<<16);
151 b
+=k
[2]+(((uint32_t) k
[3])<<16);
152 a
+=k
[0]+(((uint32_t) k
[1])<<16);
154 case 11: c
+=((uint32_t) k8
[10])<<16; /* fall through */
156 b
+=k
[2]+(((uint32_t) k
[3])<<16);
157 a
+=k
[0]+(((uint32_t) k
[1])<<16);
159 case 9 : c
+=k8
[8]; /* fall through */
160 case 8 : b
+=k
[2]+(((uint32_t) k
[3])<<16);
161 a
+=k
[0]+(((uint32_t) k
[1])<<16);
163 case 7 : b
+=((uint32_t) k8
[6])<<16; /* fall through */
165 a
+=k
[0]+(((uint32_t) k
[1])<<16);
167 case 5 : b
+=k8
[4]; /* fall through */
168 case 4 : a
+=k
[0]+(((uint32_t) k
[1])<<16);
170 case 3 : a
+=((uint32_t) k8
[2])<<16; /* fall through */
175 case 0 : return c
; /* zero length requires no mixing */
178 } else { /* need to read the key one byte at a time */
179 const uint8_t *k
= (const uint8_t *)key
;
181 /*--------------- all but the last block: affect some 32 bits of (a, b, c) */
182 while (length
> 12) {
184 a
+= ((uint32_t) k
[1])<<8;
185 a
+= ((uint32_t) k
[2])<<16;
186 a
+= ((uint32_t) k
[3])<<24;
188 b
+= ((uint32_t) k
[5])<<8;
189 b
+= ((uint32_t) k
[6])<<16;
190 b
+= ((uint32_t) k
[7])<<24;
192 c
+= ((uint32_t) k
[9])<<8;
193 c
+= ((uint32_t) k
[10])<<16;
194 c
+= ((uint32_t) k
[11])<<24;
200 /*-------------------------------- last block: affect all 32 bits of (c) */
201 switch (length
) { /* all the case statements fall through */
202 case 12: c
+=((uint32_t) k
[11])<<24; /* fall through */
203 case 11: c
+=((uint32_t) k
[10])<<16; /* fall through */
204 case 10: c
+=((uint32_t) k
[9])<<8; /* fall through */
205 case 9 : c
+=k
[8]; /* fall through */
206 case 8 : b
+=((uint32_t) k
[7])<<24; /* fall through */
207 case 7 : b
+=((uint32_t) k
[6])<<16; /* fall through */
208 case 6 : b
+=((uint32_t) k
[5])<<8; /* fall through */
209 case 5 : b
+=k
[4]; /* fall through */
210 case 4 : a
+=((uint32_t) k
[3])<<24; /* fall through */
211 case 3 : a
+=((uint32_t) k
[2])<<16; /* fall through */
212 case 2 : a
+=((uint32_t) k
[1])<<8; /* fall through */
224 uint32_t jhash(const void *key
, size_t length
, uint32_t seed
)
226 return hashlittle(key
, length
, seed
);
229 #endif /* _UST_COMMON_JHASH_H */
This page took 0.038017 seconds and 4 git commands to generate.