fef55a1f9599c5938348a09ab2f43f8b2d4d7ccf
[lttng-ust.git] / liblttng-ust / jhash.h
1 /*
2 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation;
7 * version 2.1 of the License.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <stddef.h>
20 #include <stdint.h>
21 #include <urcu/compiler.h>
22 #include <lttng/ust-endian.h>
23
24 /*
25 * Hash function
26 * Source: http://burtleburtle.net/bob/c/lookup3.c
27 * Originally Public Domain
28 */
29
30 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
31
32 #define mix(a, b, c) \
33 do { \
34 a -= c; a ^= rot(c, 4); c += b; \
35 b -= a; b ^= rot(a, 6); a += c; \
36 c -= b; c ^= rot(b, 8); b += a; \
37 a -= c; a ^= rot(c, 16); c += b; \
38 b -= a; b ^= rot(a, 19); a += c; \
39 c -= b; c ^= rot(b, 4); b += a; \
40 } while (0)
41
42 #define final(a, b, c) \
43 { \
44 c ^= b; c -= rot(b, 14); \
45 a ^= c; a -= rot(c, 11); \
46 b ^= a; b -= rot(a, 25); \
47 c ^= b; c -= rot(b, 16); \
48 a ^= c; a -= rot(c, 4);\
49 b ^= a; b -= rot(a, 14); \
50 c ^= b; c -= rot(b, 24); \
51 }
52
53 #if (BYTE_ORDER == LITTLE_ENDIAN)
54 #define HASH_LITTLE_ENDIAN 1
55 #else
56 #define HASH_LITTLE_ENDIAN 0
57 #endif
58
59 /*
60 *
61 * hashlittle() -- hash a variable-length key into a 32-bit value
62 * k : the key (the unaligned variable-length array of bytes)
63 * length : the length of the key, counting by bytes
64 * initval : can be any 4-byte value
65 * Returns a 32-bit value. Every bit of the key affects every bit of
66 * the return value. Two keys differing by one or two bits will have
67 * totally different hash values.
68 *
69 * The best hash table sizes are powers of 2. There is no need to do
70 * mod a prime (mod is sooo slow!). If you need less than 32 bits,
71 * use a bitmask. For example, if you need only 10 bits, do
72 * h = (h & hashmask(10));
73 * In which case, the hash table should have hashsize(10) elements.
74 *
75 * If you are hashing n strings (uint8_t **)k, do it like this:
76 * for (i = 0, h = 0; i < n; ++i) h = hashlittle(k[i], len[i], h);
77 *
78 * By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this
79 * code any way you wish, private, educational, or commercial. It's free.
80 *
81 * Use for hash table lookup, or anything where one collision in 2^^32 is
82 * acceptable. Do NOT use for cryptographic purposes.
83 */
84 static
85 uint32_t hashlittle(const void *key, size_t length, uint32_t initval)
86 {
87 uint32_t a, b, c; /* internal state */
88 union {
89 const void *ptr;
90 size_t i;
91 } u;
92
93 /* Set up the internal state */
94 a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
95
96 u.ptr = key;
97 if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
98 const uint32_t *k = (const uint32_t *) key; /* read 32-bit chunks */
99
100 /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
101 while (length > 12) {
102 a += k[0];
103 b += k[1];
104 c += k[2];
105 mix(a, b, c);
106 length -= 12;
107 k += 3;
108 }
109
110 /*----------------------------- handle the last (probably partial) block */
111 /*
112 * The original jhash.h reads beyond the end of string, and implements
113 * a special code path for VALGRIND. It seems to make ASan unhappy too
114 * though, so considering that hashing event names is not a fast-path
115 * in lttng-ust, remove the "fast" code entirely and use the slower
116 * but verifiable VALGRIND version of the code which does not issue
117 * out-of-bound reads.
118 */
119 {
120 const uint8_t *k8;
121
122 k8 = (const uint8_t *) k;
123 switch (length) {
124 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
125 case 11: c+=((uint32_t) k8[10])<<16; /* fall through */
126 case 10: c+=((uint32_t) k8[9])<<8; /* fall through */
127 case 9 : c+=k8[8]; /* fall through */
128 case 8 : b+=k[1]; a+=k[0]; break;
129 case 7 : b+=((uint32_t) k8[6])<<16; /* fall through */
130 case 6 : b+=((uint32_t) k8[5])<<8; /* fall through */
131 case 5 : b+=k8[4]; /* fall through */
132 case 4 : a+=k[0]; break;
133 case 3 : a+=((uint32_t) k8[2])<<16; /* fall through */
134 case 2 : a+=((uint32_t) k8[1])<<8; /* fall through */
135 case 1 : a+=k8[0]; break;
136 case 0 : return c;
137 }
138 }
139
140 } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
141 const uint16_t *k = (const uint16_t *) key; /* read 16-bit chunks */
142 const uint8_t *k8;
143
144 /*--------------- all but last block: aligned reads and different mixing */
145 while (length > 12)
146 {
147 a += k[0] + (((uint32_t) k[1])<<16);
148 b += k[2] + (((uint32_t) k[3])<<16);
149 c += k[4] + (((uint32_t) k[5])<<16);
150 mix(a, b, c);
151 length -= 12;
152 k += 6;
153 }
154
155 /*----------------------------- handle the last (probably partial) block */
156 k8 = (const uint8_t *) k;
157 switch(length)
158 {
159 case 12: c+=k[4]+(((uint32_t) k[5])<<16);
160 b+=k[2]+(((uint32_t) k[3])<<16);
161 a+=k[0]+(((uint32_t) k[1])<<16);
162 break;
163 case 11: c+=((uint32_t) k8[10])<<16; /* fall through */
164 case 10: c+=k[4];
165 b+=k[2]+(((uint32_t) k[3])<<16);
166 a+=k[0]+(((uint32_t) k[1])<<16);
167 break;
168 case 9 : c+=k8[8]; /* fall through */
169 case 8 : b+=k[2]+(((uint32_t) k[3])<<16);
170 a+=k[0]+(((uint32_t) k[1])<<16);
171 break;
172 case 7 : b+=((uint32_t) k8[6])<<16; /* fall through */
173 case 6 : b+=k[2];
174 a+=k[0]+(((uint32_t) k[1])<<16);
175 break;
176 case 5 : b+=k8[4]; /* fall through */
177 case 4 : a+=k[0]+(((uint32_t) k[1])<<16);
178 break;
179 case 3 : a+=((uint32_t) k8[2])<<16; /* fall through */
180 case 2 : a+=k[0];
181 break;
182 case 1 : a+=k8[0];
183 break;
184 case 0 : return c; /* zero length requires no mixing */
185 }
186
187 } else { /* need to read the key one byte at a time */
188 const uint8_t *k = (const uint8_t *)key;
189
190 /*--------------- all but the last block: affect some 32 bits of (a, b, c) */
191 while (length > 12) {
192 a += k[0];
193 a += ((uint32_t) k[1])<<8;
194 a += ((uint32_t) k[2])<<16;
195 a += ((uint32_t) k[3])<<24;
196 b += k[4];
197 b += ((uint32_t) k[5])<<8;
198 b += ((uint32_t) k[6])<<16;
199 b += ((uint32_t) k[7])<<24;
200 c += k[8];
201 c += ((uint32_t) k[9])<<8;
202 c += ((uint32_t) k[10])<<16;
203 c += ((uint32_t) k[11])<<24;
204 mix(a,b,c);
205 length -= 12;
206 k += 12;
207 }
208
209 /*-------------------------------- last block: affect all 32 bits of (c) */
210 switch (length) { /* all the case statements fall through */
211 case 12: c+=((uint32_t) k[11])<<24;
212 case 11: c+=((uint32_t) k[10])<<16;
213 case 10: c+=((uint32_t) k[9])<<8;
214 case 9 : c+=k[8];
215 case 8 : b+=((uint32_t) k[7])<<24;
216 case 7 : b+=((uint32_t) k[6])<<16;
217 case 6 : b+=((uint32_t) k[5])<<8;
218 case 5 : b+=k[4];
219 case 4 : a+=((uint32_t) k[3])<<24;
220 case 3 : a+=((uint32_t) k[2])<<16;
221 case 2 : a+=((uint32_t) k[1])<<8;
222 case 1 : a+=k[0];
223 break;
224 case 0 : return c;
225 }
226 }
227
228 final(a, b, c);
229 return c;
230 }
231
232 static inline
233 uint32_t jhash(const void *key, size_t length, uint32_t seed)
234 {
235 return hashlittle(key, length, seed);
236 }
This page took 0.036572 seconds and 3 git commands to generate.