Commit | Line | Data |
---|---|---|
596c4223 | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: LGPL-2.1-only |
596c4223 | 3 | * |
c0c0989a | 4 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
596c4223 MD |
5 | */ |
6 | ||
e58e5ad5 MJ |
7 | #ifndef _UST_COMMON_JHASH_H |
8 | #define _UST_COMMON_JHASH_H | |
9 | ||
b4051ad8 | 10 | #include <stddef.h> |
fb31eb73 | 11 | #include <stdint.h> |
596c4223 | 12 | #include <urcu/compiler.h> |
2ae57758 | 13 | #include <lttng/ust-endian.h> |
596c4223 MD |
14 | |
15 | /* | |
16 | * Hash function | |
17 | * Source: http://burtleburtle.net/bob/c/lookup3.c | |
18 | * Originally Public Domain | |
19 | */ | |
20 | ||
21 | #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k)))) | |
22 | ||
23 | #define mix(a, b, c) \ | |
24 | do { \ | |
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; \ | |
31 | } while (0) | |
32 | ||
33 | #define final(a, b, c) \ | |
34 | { \ | |
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); \ | |
42 | } | |
43 | ||
44 | #if (BYTE_ORDER == LITTLE_ENDIAN) | |
45 | #define HASH_LITTLE_ENDIAN 1 | |
46 | #else | |
47 | #define HASH_LITTLE_ENDIAN 0 | |
48 | #endif | |
49 | ||
50 | /* | |
51 | * | |
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. | |
a60af3a5 | 59 | * |
596c4223 MD |
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. | |
a60af3a5 | 65 | * |
596c4223 MD |
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); | |
a60af3a5 | 68 | * |
596c4223 MD |
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. | |
a60af3a5 | 71 | * |
596c4223 MD |
72 | * Use for hash table lookup, or anything where one collision in 2^^32 is |
73 | * acceptable. Do NOT use for cryptographic purposes. | |
74 | */ | |
75 | static | |
76 | uint32_t hashlittle(const void *key, size_t length, uint32_t initval) | |
77 | { | |
78 | uint32_t a, b, c; /* internal state */ | |
79 | union { | |
80 | const void *ptr; | |
81 | size_t i; | |
82 | } u; | |
83 | ||
84 | /* Set up the internal state */ | |
85 | a = b = c = 0xdeadbeef + ((uint32_t)length) + initval; | |
86 | ||
87 | u.ptr = key; | |
88 | if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) { | |
89 | const uint32_t *k = (const uint32_t *) key; /* read 32-bit chunks */ | |
90 | ||
91 | /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */ | |
92 | while (length > 12) { | |
93 | a += k[0]; | |
94 | b += k[1]; | |
95 | c += k[2]; | |
96 | mix(a, b, c); | |
97 | length -= 12; | |
98 | k += 3; | |
99 | } | |
100 | ||
101 | /*----------------------------- handle the last (probably partial) block */ | |
a60af3a5 | 102 | /* |
9ee5b088 MD |
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. | |
596c4223 | 109 | */ |
596c4223 MD |
110 | { |
111 | const uint8_t *k8; | |
112 | ||
113 | k8 = (const uint8_t *) k; | |
114 | switch (length) { | |
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; | |
127 | case 0 : return c; | |
128 | } | |
129 | } | |
596c4223 MD |
130 | |
131 | } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) { | |
132 | const uint16_t *k = (const uint16_t *) key; /* read 16-bit chunks */ | |
133 | const uint8_t *k8; | |
134 | ||
135 | /*--------------- all but last block: aligned reads and different mixing */ | |
136 | while (length > 12) | |
137 | { | |
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); | |
141 | mix(a, b, c); | |
142 | length -= 12; | |
143 | k += 6; | |
144 | } | |
145 | ||
146 | /*----------------------------- handle the last (probably partial) block */ | |
147 | k8 = (const uint8_t *) k; | |
148 | switch(length) | |
149 | { | |
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); | |
153 | break; | |
154 | case 11: c+=((uint32_t) k8[10])<<16; /* fall through */ | |
155 | case 10: c+=k[4]; | |
156 | b+=k[2]+(((uint32_t) k[3])<<16); | |
157 | a+=k[0]+(((uint32_t) k[1])<<16); | |
158 | break; | |
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); | |
162 | break; | |
163 | case 7 : b+=((uint32_t) k8[6])<<16; /* fall through */ | |
164 | case 6 : b+=k[2]; | |
165 | a+=k[0]+(((uint32_t) k[1])<<16); | |
166 | break; | |
167 | case 5 : b+=k8[4]; /* fall through */ | |
168 | case 4 : a+=k[0]+(((uint32_t) k[1])<<16); | |
169 | break; | |
170 | case 3 : a+=((uint32_t) k8[2])<<16; /* fall through */ | |
171 | case 2 : a+=k[0]; | |
172 | break; | |
173 | case 1 : a+=k8[0]; | |
174 | break; | |
175 | case 0 : return c; /* zero length requires no mixing */ | |
176 | } | |
177 | ||
178 | } else { /* need to read the key one byte at a time */ | |
179 | const uint8_t *k = (const uint8_t *)key; | |
180 | ||
181 | /*--------------- all but the last block: affect some 32 bits of (a, b, c) */ | |
182 | while (length > 12) { | |
183 | a += k[0]; | |
184 | a += ((uint32_t) k[1])<<8; | |
185 | a += ((uint32_t) k[2])<<16; | |
186 | a += ((uint32_t) k[3])<<24; | |
187 | b += k[4]; | |
188 | b += ((uint32_t) k[5])<<8; | |
189 | b += ((uint32_t) k[6])<<16; | |
190 | b += ((uint32_t) k[7])<<24; | |
191 | c += k[8]; | |
192 | c += ((uint32_t) k[9])<<8; | |
193 | c += ((uint32_t) k[10])<<16; | |
194 | c += ((uint32_t) k[11])<<24; | |
195 | mix(a,b,c); | |
196 | length -= 12; | |
197 | k += 12; | |
198 | } | |
199 | ||
200 | /*-------------------------------- last block: affect all 32 bits of (c) */ | |
201 | switch (length) { /* all the case statements fall through */ | |
60864af0 MJ |
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 */ | |
596c4223 MD |
213 | case 1 : a+=k[0]; |
214 | break; | |
215 | case 0 : return c; | |
216 | } | |
217 | } | |
218 | ||
219 | final(a, b, c); | |
220 | return c; | |
221 | } | |
222 | ||
223 | static inline | |
224 | uint32_t jhash(const void *key, size_t length, uint32_t seed) | |
225 | { | |
226 | return hashlittle(key, length, seed); | |
227 | } | |
e58e5ad5 MJ |
228 | |
229 | #endif /* _UST_COMMON_JHASH_H */ |