SDDSlib
Loading...
Searching...
No Matches
lookupa.c
1/*
2 --------------------------------------------------------------------
3 lookupa.c, by Bob Jenkins, December 1996. Public Domain. No warranty.
4 Source is http://burtleburtle.net/bob/c/lookupa.c
5 Customized for elegant for both 32 and 64 bits machines by Yusong Wang,
6 May, 2007.
7 --------------------------------------------------------------------
8*/
9#ifndef STANDARD
10# include "standard.h"
11#endif
12#ifndef LOOKUPA
13# include "lookupa.h"
14#endif
15
16#ifndef _X86_64_
17/*
18 --------------------------------------------------------------------
19 mix -- mix 3 32-bit values reversibly.
20 For every delta with one or two bit set, and the deltas of all three
21 high bits or all three low bits, whether the original value of a,b,c
22 is almost all zero or is uniformly distributed,
23 * If mix() is run forward or backward, at least 32 bits in a,b,c
24 have at least 1/4 probability of changing.
25 * If mix() is run forward, every bit of c will change between 1/3 and
26 2/3 of the time. (Well, 22/100 and 78/100 for some 2-bit deltas.)
27 mix() was built out of 36 single-cycle latency instructions in a
28 structure that could supported 2x parallelism, like so:
29 a -= b;
30 a -= c; x = (c>>13);
31 b -= c; a ^= x;
32 b -= a; x = (a<<8);
33 c -= a; b ^= x;
34 c -= b; x = (b>>13);
35 ...
36 Unfortunately, superscalar Pentiums and Sparcs can't take advantage
37 of that parallelism. They've also turned some of those single-cycle
38 latency instructions into multi-cycle latency instructions. Still,
39 this is the fastest good hash I could find. There were about 2^^68
40 to choose from. I only looked at a billion or so.
41 --------------------------------------------------------------------
42*/
43# define mix(a, b, c) \
44 { \
45 a -= b; \
46 a -= c; \
47 a ^= (c >> 13); \
48 b -= c; \
49 b -= a; \
50 b ^= (a << 8); \
51 c -= a; \
52 c -= b; \
53 c ^= (b >> 13); \
54 a -= b; \
55 a -= c; \
56 a ^= (c >> 12); \
57 b -= c; \
58 b -= a; \
59 b ^= (a << 16); \
60 c -= a; \
61 c -= b; \
62 c ^= (b >> 5); \
63 a -= b; \
64 a -= c; \
65 a ^= (c >> 3); \
66 b -= c; \
67 b -= a; \
68 b ^= (a << 10); \
69 c -= a; \
70 c -= b; \
71 c ^= (b >> 15); \
72 }
73
74/*
75 --------------------------------------------------------------------
76 lookup() -- hash a variable-length key into a 32-bit value
77 k : the key (the unaligned variable-length array of bytes)
78 len : the length of the key, counting by bytes
79 level : can be any 4-byte value
80 Returns a 32-bit value. Every bit of the key affects every bit of
81 the return value. Every 1-bit and 2-bit delta achieves avalanche.
82 About 6len+35 instructions.
83
84 The best hash table sizes are powers of 2. There is no need to do
85 mod a prime (mod is sooo slow!). If you need less than 32 bits,
86 use a bitmask. For example, if you need only 10 bits, do
87 h = (h & hashmask(10));
88 In which case, the hash table should have hashsize(10) elements.
89
90 If you are hashing n strings (ub1 **)k, do it like this:
91 for (i=0, h=0; i<n; ++i) h = lookup( k[i], len[i], h);
92
93 By Bob Jenkins, 1996. bob_jenkins@burtleburtle.net. You may use this
94 code any way you wish, private, educational, or commercial.
95
96 See http://burtleburtle.net/bob/hash/evahash.html
97 Use for hash table lookup, or anything where one collision in 2^32 is
98 acceptable. Do NOT use for cryptographic purposes.
99 --------------------------------------------------------------------
100*/
101
102ub4 lookup(k, length, level) register ub1 *k; /* the key */
103register ub4 length; /* the length of the key */
104register ub4 level; /* the previous hash, or an arbitrary value */
105{
106 register ub4 a, b, c, len;
107
108 /* Set up the internal state */
109 len = length;
110 a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
111 c = level; /* the previous hash value */
112
113 /*---------------------------------------- handle most of the key */
114 while (len >= 12) {
115 a += (k[0] + ((ub4)k[1] << 8) + ((ub4)k[2] << 16) + ((ub4)k[3] << 24));
116 b += (k[4] + ((ub4)k[5] << 8) + ((ub4)k[6] << 16) + ((ub4)k[7] << 24));
117 c += (k[8] + ((ub4)k[9] << 8) + ((ub4)k[10] << 16) + ((ub4)k[11] << 24));
118 mix(a, b, c);
119 k += 12;
120 len -= 12;
121 }
122
123 /*------------------------------------- handle the last 11 bytes */
124 c += length;
125 switch (len) /* all the case statements fall through */
126 {
127 case 11:
128 c += ((ub4)k[10] << 24);
129 case 10:
130 c += ((ub4)k[9] << 16);
131 case 9:
132 c += ((ub4)k[8] << 8);
133 /* the first byte of c is reserved for the length */
134 case 8:
135 b += ((ub4)k[7] << 24);
136 case 7:
137 b += ((ub4)k[6] << 16);
138 case 6:
139 b += ((ub4)k[5] << 8);
140 case 5:
141 b += k[4];
142 case 4:
143 a += ((ub4)k[3] << 24);
144 case 3:
145 a += ((ub4)k[2] << 16);
146 case 2:
147 a += ((ub4)k[1] << 8);
148 case 1:
149 a += k[0];
150 /* case 0: nothing left to add */
151 }
152 mix(a, b, c);
153 /*-------------------------------------------- report the result */
154 return c;
155}
156
157#else
158
159/*
160 --------------------------------------------------------------------
161 mix -- mix 3 64-bit values reversibly.
162 mix() takes 48 machine instructions, but only 24 cycles on a superscalar
163 machine (like Intel's new MMX architecture). It requires 4 64-bit
164 registers for 4::2 parallelism.
165 All 1-bit deltas, all 2-bit deltas, all deltas composed of top bits of
166 (a,b,c), and all deltas of bottom bits were tested. All deltas were
167 tested both on random keys and on keys that were nearly all zero.
168 These deltas all cause every bit of c to change between 1/3 and 2/3
169 of the time (well, only 113/400 to 287/400 of the time for some
170 2-bit delta). These deltas all cause at least 80 bits to change
171 among (a,b,c) when the mix is run either forward or backward (yes it
172 is reversible).
173 This implies that a hash using mix64 has no funnels. There may be
174 characteristics with 3-bit deltas or bigger, I didn't test for
175 those.
176 --------------------------------------------------------------------
177*/
178# define mix64(a, b, c) \
179 { \
180 a -= b; \
181 a -= c; \
182 a ^= (c >> 43); \
183 b -= c; \
184 b -= a; \
185 b ^= (a << 9); \
186 c -= a; \
187 c -= b; \
188 c ^= (b >> 8); \
189 a -= b; \
190 a -= c; \
191 a ^= (c >> 38); \
192 b -= c; \
193 b -= a; \
194 b ^= (a << 23); \
195 c -= a; \
196 c -= b; \
197 c ^= (b >> 5); \
198 a -= b; \
199 a -= c; \
200 a ^= (c >> 35); \
201 b -= c; \
202 b -= a; \
203 b ^= (a << 49); \
204 c -= a; \
205 c -= b; \
206 c ^= (b >> 11); \
207 a -= b; \
208 a -= c; \
209 a ^= (c >> 12); \
210 b -= c; \
211 b -= a; \
212 b ^= (a << 18); \
213 c -= a; \
214 c -= b; \
215 c ^= (b >> 22); \
216 }
217
218/*
219 --------------------------------------------------------------------
220 lookup() -- hash a variable-length key into a 64-bit value
221 k : the key (the unaligned variable-length array of bytes)
222 len : the length of the key, counting by bytes
223 level : can be any 8-byte value
224 Returns a 64-bit value. Every bit of the key affects every bit of
225 the return value. No funnels. Every 1-bit and 2-bit delta achieves
226 avalanche. About 41+5len instructions.
227
228 The best hash table sizes are powers of 2. There is no need to do
229 mod a prime (mod is sooo slow!). If you need less than 64 bits,
230 use a bitmask. For example, if you need only 10 bits, do
231 h = (h & hashmask(10));
232 In which case, the hash table should have hashsize(10) elements.
233
234 If you are hashing n strings (ub1 **)k, do it like this:
235 for (i=0, h=0; i<n; ++i) h = lookup( k[i], len[i], h);
236
237 By Bob Jenkins, Jan 4 1997. bob_jenkins@burtleburtle.net. You may
238 use this code any way you wish, private, educational, or commercial,
239 but I would appreciate if you give me credit.
240
241 See http://burtleburtle.net/bob/hash/evahash.html
242 Use for hash table lookup, or anything where one collision in 2^^64
243 is acceptable. Do NOT use for cryptographic purposes.
244 --------------------------------------------------------------------
245*/
246
247ub8 lookup(k, length, level) register ub1 *k; /* the key */
248register ub8 length; /* the length of the key */
249register ub8 level; /* the previous hash, or an arbitrary value */
250{
251 register ub8 a, b, c, len;
252
253 /* Set up the internal state */
254 len = length;
255 a = b = level; /* the previous hash value */
256 c = 0x9e3779b97f4a7c13LL; /* the golden ratio; an arbitrary value */
257
258 /*---------------------------------------- handle most of the key */
259 while (len >= 24) {
260 a += (k[0] + ((ub8)k[1] << 8) + ((ub8)k[2] << 16) + ((ub8)k[3] << 24) + ((ub8)k[4] << 32) + ((ub8)k[5] << 40) + ((ub8)k[6] << 48) + ((ub8)k[7] << 56));
261 b += (k[8] + ((ub8)k[9] << 8) + ((ub8)k[10] << 16) + ((ub8)k[11] << 24) + ((ub8)k[12] << 32) + ((ub8)k[13] << 40) + ((ub8)k[14] << 48) + ((ub8)k[15] << 56));
262 c += (k[16] + ((ub8)k[17] << 8) + ((ub8)k[18] << 16) + ((ub8)k[19] << 24) + ((ub8)k[20] << 32) + ((ub8)k[21] << 40) + ((ub8)k[22] << 48) + ((ub8)k[23] << 56));
263 mix64(a, b, c);
264 k += 24;
265 len -= 24;
266 }
267
268 /*------------------------------------- handle the last 23 bytes */
269 c += length;
270 switch (len) /* all the case statements fall through */
271 {
272 case 23:
273 c += ((ub8)k[22] << 56);
274 case 22:
275 c += ((ub8)k[21] << 48);
276 case 21:
277 c += ((ub8)k[20] << 40);
278 case 20:
279 c += ((ub8)k[19] << 32);
280 case 19:
281 c += ((ub8)k[18] << 24);
282 case 18:
283 c += ((ub8)k[17] << 16);
284 case 17:
285 c += ((ub8)k[16] << 8);
286 /* the first byte of c is reserved for the length */
287 case 16:
288 b += ((ub8)k[15] << 56);
289 case 15:
290 b += ((ub8)k[14] << 48);
291 case 14:
292 b += ((ub8)k[13] << 40);
293 case 13:
294 b += ((ub8)k[12] << 32);
295 case 12:
296 b += ((ub8)k[11] << 24);
297 case 11:
298 b += ((ub8)k[10] << 16);
299 case 10:
300 b += ((ub8)k[9] << 8);
301 case 9:
302 b += ((ub8)k[8]);
303 case 8:
304 a += ((ub8)k[7] << 56);
305 case 7:
306 a += ((ub8)k[6] << 48);
307 case 6:
308 a += ((ub8)k[5] << 40);
309 case 5:
310 a += ((ub8)k[4] << 32);
311 case 4:
312 a += ((ub8)k[3] << 24);
313 case 3:
314 a += ((ub8)k[2] << 16);
315 case 2:
316 a += ((ub8)k[1] << 8);
317 case 1:
318 a += ((ub8)k[0]);
319 /* case 0: nothing left to add */
320 }
321 mix64(a, b, c);
322 /*-------------------------------------------- report the result */
323 return c;
324}
325#endif