Cleanup: remove trailing white spaces across project
[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 * "k[2]&0xffffff" actually reads beyond the end of the string, but
113 * then masks off the part it's not allowed to read. Because the
114 * string is aligned, the masked-off tail is in the same word as the
115 * rest of the string. Every machine with memory protection I've seen
116 * does it on word boundaries, so is OK with this. But VALGRIND will
117 * still catch it and complain. The masking trick does make the hash
118 * noticably faster for short strings (like English words).
119 */
120 #ifndef VALGRIND
121
122 switch (length) {
123 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
124 case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
125 case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
126 case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
127 case 8 : b+=k[1]; a+=k[0]; break;
128 case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
129 case 6 : b+=k[1]&0xffff; a+=k[0]; break;
130 case 5 : b+=k[1]&0xff; a+=k[0]; break;
131 case 4 : a+=k[0]; break;
132 case 3 : a+=k[0]&0xffffff; break;
133 case 2 : a+=k[0]&0xffff; break;
134 case 1 : a+=k[0]&0xff; break;
135 case 0 : return c; /* zero length strings require no mixing */
136 }
137
138 #else /* make valgrind happy */
139 {
140 const uint8_t *k8;
141
142 k8 = (const uint8_t *) k;
143 switch (length) {
144 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
145 case 11: c+=((uint32_t) k8[10])<<16; /* fall through */
146 case 10: c+=((uint32_t) k8[9])<<8; /* fall through */
147 case 9 : c+=k8[8]; /* fall through */
148 case 8 : b+=k[1]; a+=k[0]; break;
149 case 7 : b+=((uint32_t) k8[6])<<16; /* fall through */
150 case 6 : b+=((uint32_t) k8[5])<<8; /* fall through */
151 case 5 : b+=k8[4]; /* fall through */
152 case 4 : a+=k[0]; break;
153 case 3 : a+=((uint32_t) k8[2])<<16; /* fall through */
154 case 2 : a+=((uint32_t) k8[1])<<8; /* fall through */
155 case 1 : a+=k8[0]; break;
156 case 0 : return c;
157 }
158 }
159 #endif /* !valgrind */
160
161 } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
162 const uint16_t *k = (const uint16_t *) key; /* read 16-bit chunks */
163 const uint8_t *k8;
164
165 /*--------------- all but last block: aligned reads and different mixing */
166 while (length > 12)
167 {
168 a += k[0] + (((uint32_t) k[1])<<16);
169 b += k[2] + (((uint32_t) k[3])<<16);
170 c += k[4] + (((uint32_t) k[5])<<16);
171 mix(a, b, c);
172 length -= 12;
173 k += 6;
174 }
175
176 /*----------------------------- handle the last (probably partial) block */
177 k8 = (const uint8_t *) k;
178 switch(length)
179 {
180 case 12: c+=k[4]+(((uint32_t) k[5])<<16);
181 b+=k[2]+(((uint32_t) k[3])<<16);
182 a+=k[0]+(((uint32_t) k[1])<<16);
183 break;
184 case 11: c+=((uint32_t) k8[10])<<16; /* fall through */
185 case 10: c+=k[4];
186 b+=k[2]+(((uint32_t) k[3])<<16);
187 a+=k[0]+(((uint32_t) k[1])<<16);
188 break;
189 case 9 : c+=k8[8]; /* fall through */
190 case 8 : b+=k[2]+(((uint32_t) k[3])<<16);
191 a+=k[0]+(((uint32_t) k[1])<<16);
192 break;
193 case 7 : b+=((uint32_t) k8[6])<<16; /* fall through */
194 case 6 : b+=k[2];
195 a+=k[0]+(((uint32_t) k[1])<<16);
196 break;
197 case 5 : b+=k8[4]; /* fall through */
198 case 4 : a+=k[0]+(((uint32_t) k[1])<<16);
199 break;
200 case 3 : a+=((uint32_t) k8[2])<<16; /* fall through */
201 case 2 : a+=k[0];
202 break;
203 case 1 : a+=k8[0];
204 break;
205 case 0 : return c; /* zero length requires no mixing */
206 }
207
208 } else { /* need to read the key one byte at a time */
209 const uint8_t *k = (const uint8_t *)key;
210
211 /*--------------- all but the last block: affect some 32 bits of (a, b, c) */
212 while (length > 12) {
213 a += k[0];
214 a += ((uint32_t) k[1])<<8;
215 a += ((uint32_t) k[2])<<16;
216 a += ((uint32_t) k[3])<<24;
217 b += k[4];
218 b += ((uint32_t) k[5])<<8;
219 b += ((uint32_t) k[6])<<16;
220 b += ((uint32_t) k[7])<<24;
221 c += k[8];
222 c += ((uint32_t) k[9])<<8;
223 c += ((uint32_t) k[10])<<16;
224 c += ((uint32_t) k[11])<<24;
225 mix(a,b,c);
226 length -= 12;
227 k += 12;
228 }
229
230 /*-------------------------------- last block: affect all 32 bits of (c) */
231 switch (length) { /* all the case statements fall through */
232 case 12: c+=((uint32_t) k[11])<<24;
233 case 11: c+=((uint32_t) k[10])<<16;
234 case 10: c+=((uint32_t) k[9])<<8;
235 case 9 : c+=k[8];
236 case 8 : b+=((uint32_t) k[7])<<24;
237 case 7 : b+=((uint32_t) k[6])<<16;
238 case 6 : b+=((uint32_t) k[5])<<8;
239 case 5 : b+=k[4];
240 case 4 : a+=((uint32_t) k[3])<<24;
241 case 3 : a+=((uint32_t) k[2])<<16;
242 case 2 : a+=((uint32_t) k[1])<<8;
243 case 1 : a+=k[0];
244 break;
245 case 0 : return c;
246 }
247 }
248
249 final(a, b, c);
250 return c;
251 }
252
253 static inline
254 uint32_t jhash(const void *key, size_t length, uint32_t seed)
255 {
256 return hashlittle(key, length, seed);
257 }
This page took 0.035266 seconds and 4 git commands to generate.