416836adbf294c997414a292e60596dbf1045ec9
[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 <urcu/compiler.h>
21 #include <lttng/ust-endian.h>
22
23 /*
24 * Hash function
25 * Source: http://burtleburtle.net/bob/c/lookup3.c
26 * Originally Public Domain
27 */
28
29 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
30
31 #define mix(a, b, c) \
32 do { \
33 a -= c; a ^= rot(c, 4); c += b; \
34 b -= a; b ^= rot(a, 6); a += c; \
35 c -= b; c ^= rot(b, 8); b += a; \
36 a -= c; a ^= rot(c, 16); c += b; \
37 b -= a; b ^= rot(a, 19); a += c; \
38 c -= b; c ^= rot(b, 4); b += a; \
39 } while (0)
40
41 #define final(a, b, c) \
42 { \
43 c ^= b; c -= rot(b, 14); \
44 a ^= c; a -= rot(c, 11); \
45 b ^= a; b -= rot(a, 25); \
46 c ^= b; c -= rot(b, 16); \
47 a ^= c; a -= rot(c, 4);\
48 b ^= a; b -= rot(a, 14); \
49 c ^= b; c -= rot(b, 24); \
50 }
51
52 #if (BYTE_ORDER == LITTLE_ENDIAN)
53 #define HASH_LITTLE_ENDIAN 1
54 #else
55 #define HASH_LITTLE_ENDIAN 0
56 #endif
57
58 /*
59 *
60 * hashlittle() -- hash a variable-length key into a 32-bit value
61 * k : the key (the unaligned variable-length array of bytes)
62 * length : the length of the key, counting by bytes
63 * initval : can be any 4-byte value
64 * Returns a 32-bit value. Every bit of the key affects every bit of
65 * the return value. Two keys differing by one or two bits will have
66 * totally different hash values.
67 *
68 * The best hash table sizes are powers of 2. There is no need to do
69 * mod a prime (mod is sooo slow!). If you need less than 32 bits,
70 * use a bitmask. For example, if you need only 10 bits, do
71 * h = (h & hashmask(10));
72 * In which case, the hash table should have hashsize(10) elements.
73 *
74 * If you are hashing n strings (uint8_t **)k, do it like this:
75 * for (i = 0, h = 0; i < n; ++i) h = hashlittle(k[i], len[i], h);
76 *
77 * By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this
78 * code any way you wish, private, educational, or commercial. It's free.
79 *
80 * Use for hash table lookup, or anything where one collision in 2^^32 is
81 * acceptable. Do NOT use for cryptographic purposes.
82 */
83 static
84 uint32_t hashlittle(const void *key, size_t length, uint32_t initval)
85 {
86 uint32_t a, b, c; /* internal state */
87 union {
88 const void *ptr;
89 size_t i;
90 } u;
91
92 /* Set up the internal state */
93 a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
94
95 u.ptr = key;
96 if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
97 const uint32_t *k = (const uint32_t *) key; /* read 32-bit chunks */
98
99 /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
100 while (length > 12) {
101 a += k[0];
102 b += k[1];
103 c += k[2];
104 mix(a, b, c);
105 length -= 12;
106 k += 3;
107 }
108
109 /*----------------------------- handle the last (probably partial) block */
110 /*
111 * "k[2]&0xffffff" actually reads beyond the end of the string, but
112 * then masks off the part it's not allowed to read. Because the
113 * string is aligned, the masked-off tail is in the same word as the
114 * rest of the string. Every machine with memory protection I've seen
115 * does it on word boundaries, so is OK with this. But VALGRIND will
116 * still catch it and complain. The masking trick does make the hash
117 * noticably faster for short strings (like English words).
118 */
119 #ifndef VALGRIND
120
121 switch (length) {
122 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
123 case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
124 case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
125 case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
126 case 8 : b+=k[1]; a+=k[0]; break;
127 case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
128 case 6 : b+=k[1]&0xffff; a+=k[0]; break;
129 case 5 : b+=k[1]&0xff; a+=k[0]; break;
130 case 4 : a+=k[0]; break;
131 case 3 : a+=k[0]&0xffffff; break;
132 case 2 : a+=k[0]&0xffff; break;
133 case 1 : a+=k[0]&0xff; break;
134 case 0 : return c; /* zero length strings require no mixing */
135 }
136
137 #else /* make valgrind happy */
138 {
139 const uint8_t *k8;
140
141 k8 = (const uint8_t *) k;
142 switch (length) {
143 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
144 case 11: c+=((uint32_t) k8[10])<<16; /* fall through */
145 case 10: c+=((uint32_t) k8[9])<<8; /* fall through */
146 case 9 : c+=k8[8]; /* fall through */
147 case 8 : b+=k[1]; a+=k[0]; break;
148 case 7 : b+=((uint32_t) k8[6])<<16; /* fall through */
149 case 6 : b+=((uint32_t) k8[5])<<8; /* fall through */
150 case 5 : b+=k8[4]; /* fall through */
151 case 4 : a+=k[0]; break;
152 case 3 : a+=((uint32_t) k8[2])<<16; /* fall through */
153 case 2 : a+=((uint32_t) k8[1])<<8; /* fall through */
154 case 1 : a+=k8[0]; break;
155 case 0 : return c;
156 }
157 }
158 #endif /* !valgrind */
159
160 } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
161 const uint16_t *k = (const uint16_t *) key; /* read 16-bit chunks */
162 const uint8_t *k8;
163
164 /*--------------- all but last block: aligned reads and different mixing */
165 while (length > 12)
166 {
167 a += k[0] + (((uint32_t) k[1])<<16);
168 b += k[2] + (((uint32_t) k[3])<<16);
169 c += k[4] + (((uint32_t) k[5])<<16);
170 mix(a, b, c);
171 length -= 12;
172 k += 6;
173 }
174
175 /*----------------------------- handle the last (probably partial) block */
176 k8 = (const uint8_t *) k;
177 switch(length)
178 {
179 case 12: c+=k[4]+(((uint32_t) k[5])<<16);
180 b+=k[2]+(((uint32_t) k[3])<<16);
181 a+=k[0]+(((uint32_t) k[1])<<16);
182 break;
183 case 11: c+=((uint32_t) k8[10])<<16; /* fall through */
184 case 10: c+=k[4];
185 b+=k[2]+(((uint32_t) k[3])<<16);
186 a+=k[0]+(((uint32_t) k[1])<<16);
187 break;
188 case 9 : c+=k8[8]; /* fall through */
189 case 8 : b+=k[2]+(((uint32_t) k[3])<<16);
190 a+=k[0]+(((uint32_t) k[1])<<16);
191 break;
192 case 7 : b+=((uint32_t) k8[6])<<16; /* fall through */
193 case 6 : b+=k[2];
194 a+=k[0]+(((uint32_t) k[1])<<16);
195 break;
196 case 5 : b+=k8[4]; /* fall through */
197 case 4 : a+=k[0]+(((uint32_t) k[1])<<16);
198 break;
199 case 3 : a+=((uint32_t) k8[2])<<16; /* fall through */
200 case 2 : a+=k[0];
201 break;
202 case 1 : a+=k8[0];
203 break;
204 case 0 : return c; /* zero length requires no mixing */
205 }
206
207 } else { /* need to read the key one byte at a time */
208 const uint8_t *k = (const uint8_t *)key;
209
210 /*--------------- all but the last block: affect some 32 bits of (a, b, c) */
211 while (length > 12) {
212 a += k[0];
213 a += ((uint32_t) k[1])<<8;
214 a += ((uint32_t) k[2])<<16;
215 a += ((uint32_t) k[3])<<24;
216 b += k[4];
217 b += ((uint32_t) k[5])<<8;
218 b += ((uint32_t) k[6])<<16;
219 b += ((uint32_t) k[7])<<24;
220 c += k[8];
221 c += ((uint32_t) k[9])<<8;
222 c += ((uint32_t) k[10])<<16;
223 c += ((uint32_t) k[11])<<24;
224 mix(a,b,c);
225 length -= 12;
226 k += 12;
227 }
228
229 /*-------------------------------- last block: affect all 32 bits of (c) */
230 switch (length) { /* all the case statements fall through */
231 case 12: c+=((uint32_t) k[11])<<24;
232 case 11: c+=((uint32_t) k[10])<<16;
233 case 10: c+=((uint32_t) k[9])<<8;
234 case 9 : c+=k[8];
235 case 8 : b+=((uint32_t) k[7])<<24;
236 case 7 : b+=((uint32_t) k[6])<<16;
237 case 6 : b+=((uint32_t) k[5])<<8;
238 case 5 : b+=k[4];
239 case 4 : a+=((uint32_t) k[3])<<24;
240 case 3 : a+=((uint32_t) k[2])<<16;
241 case 2 : a+=((uint32_t) k[1])<<8;
242 case 1 : a+=k[0];
243 break;
244 case 0 : return c;
245 }
246 }
247
248 final(a, b, c);
249 return c;
250 }
251
252 static inline
253 uint32_t jhash(const void *key, size_t length, uint32_t seed)
254 {
255 return hashlittle(key, length, seed);
256 }
This page took 0.035109 seconds and 3 git commands to generate.