blob: 1eff74ac526888cef3a13167a8e77006be1180a3 [file] [log] [blame]
jjako52c24142002-12-16 13:33:51 +00001/*
2--------------------------------------------------------------------
3lookupa.c, by Bob Jenkins, December 1996. Same as lookup2.c
4Use this code however you wish. Public Domain. No warranty.
5Source is http://burtleburtle.net/bob/c/lookupa.c
6--------------------------------------------------------------------
7*/
8#ifndef STANDARD
9/*
10#include "standard.h"
11*/
12#endif
13#ifndef LOOKUPA
14#include "lookupa.h"
15#endif
16
17/*
18--------------------------------------------------------------------
19mix -- mix 3 32-bit values reversibly.
20For 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.)
27mix() 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; a -= c; a ^= (c>>13); \
46 b -= c; b -= a; b ^= (a<<8); \
47 c -= a; c -= b; c ^= (b>>13); \
48 a -= b; a -= c; a ^= (c>>12); \
49 b -= c; b -= a; b ^= (a<<16); \
50 c -= a; c -= b; c ^= (b>>5); \
51 a -= b; a -= c; a ^= (c>>3); \
52 b -= c; b -= a; b ^= (a<<10); \
53 c -= a; c -= b; c ^= (b>>15); \
54}
55
56/*
57--------------------------------------------------------------------
58lookup() -- hash a variable-length key into a 32-bit value
59 k : the key (the unaligned variable-length array of bytes)
60 len : the length of the key, counting by bytes
61 level : can be any 4-byte value
62Returns a 32-bit value. Every bit of the key affects every bit of
63the return value. Every 1-bit and 2-bit delta achieves avalanche.
64About 6len+35 instructions.
65
66The best hash table sizes are powers of 2. There is no need to do
67mod a prime (mod is sooo slow!). If you need less than 32 bits,
68use a bitmask. For example, if you need only 10 bits, do
69 h = (h & hashmask(10));
70In which case, the hash table should have hashsize(10) elements.
71
72If you are hashing n strings (ub1 **)k, do it like this:
73 for (i=0, h=0; i<n; ++i) h = lookup( k[i], len[i], h);
74
75By Bob Jenkins, 1996. bob_jenkins@burtleburtle.net. You may use this
76code any way you wish, private, educational, or commercial.
77
78See http://burtleburtle.net/bob/hash/evahash.html
79Use for hash table lookup, or anything where one collision in 2^32 is
80acceptable. Do NOT use for cryptographic purposes.
81--------------------------------------------------------------------
82*/
83
Harald Weltebed35df2011-11-02 13:06:18 +010084ub4 lookup(k, length, level)
85register ub1 *k; /* the key */
86register ub4 length; /* the length of the key */
87register ub4 level; /* the previous hash, or an arbitrary value */
jjako52c24142002-12-16 13:33:51 +000088{
Harald Weltebed35df2011-11-02 13:06:18 +010089 register ub4 a, b, c, len;
jjako52c24142002-12-16 13:33:51 +000090
Harald Weltebed35df2011-11-02 13:06:18 +010091 /* Set up the internal state */
92 len = length;
93 a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
94 c = level; /* the previous hash value */
jjako52c24142002-12-16 13:33:51 +000095
96 /*---------------------------------------- handle most of the key */
Harald Weltebed35df2011-11-02 13:06:18 +010097 while (len >= 12) {
98 a += (k[0] + ((ub4) k[1] << 8) + ((ub4) k[2] << 16) +
99 ((ub4) k[3] << 24));
100 b += (k[4] + ((ub4) k[5] << 8) + ((ub4) k[6] << 16) +
101 ((ub4) k[7] << 24));
102 c += (k[8] + ((ub4) k[9] << 8) + ((ub4) k[10] << 16) +
103 ((ub4) k[11] << 24));
104 mix(a, b, c);
105 k += 12;
106 len -= 12;
107 }
jjako52c24142002-12-16 13:33:51 +0000108
109 /*------------------------------------- handle the last 11 bytes */
Harald Weltebed35df2011-11-02 13:06:18 +0100110 c += length;
111 switch (len) { /* all the case statements fall through */
112 case 11:
113 c += ((ub4) k[10] << 24);
114 case 10:
115 c += ((ub4) k[9] << 16);
116 case 9:
117 c += ((ub4) k[8] << 8);
118 /* the first byte of c is reserved for the length */
119 case 8:
120 b += ((ub4) k[7] << 24);
121 case 7:
122 b += ((ub4) k[6] << 16);
123 case 6:
124 b += ((ub4) k[5] << 8);
125 case 5:
126 b += k[4];
127 case 4:
128 a += ((ub4) k[3] << 24);
129 case 3:
130 a += ((ub4) k[2] << 16);
131 case 2:
132 a += ((ub4) k[1] << 8);
133 case 1:
134 a += k[0];
135 /* case 0: nothing left to add */
136 }
137 mix(a, b, c);
jjako52c24142002-12-16 13:33:51 +0000138 /*-------------------------------------------- report the result */
Harald Weltebed35df2011-11-02 13:06:18 +0100139 return c;
jjako52c24142002-12-16 13:33:51 +0000140}
141
jjako52c24142002-12-16 13:33:51 +0000142/*
143--------------------------------------------------------------------
144mixc -- mixc 8 4-bit values as quickly and thoroughly as possible.
145Repeating mix() three times achieves avalanche.
146Repeating mix() four times eliminates all funnels and all
147 characteristics stronger than 2^{-11}.
148--------------------------------------------------------------------
149*/
150#define mixc(a,b,c,d,e,f,g,h) \
151{ \
152 a^=b<<11; d+=a; b+=c; \
153 b^=c>>2; e+=b; c+=d; \
154 c^=d<<8; f+=c; d+=e; \
155 d^=e>>16; g+=d; e+=f; \
156 e^=f<<10; h+=e; f+=g; \
157 f^=g>>4; a+=f; g+=h; \
158 g^=h<<8; b+=g; h+=a; \
159 h^=a>>9; c+=h; a+=b; \
160}
161
162/*
163--------------------------------------------------------------------
164checksum() -- hash a variable-length key into a 256-bit value
165 k : the key (the unaligned variable-length array of bytes)
166 len : the length of the key, counting by bytes
167 state : an array of CHECKSTATE 4-byte values (256 bits)
168The state is the checksum. Every bit of the key affects every bit of
169the state. There are no funnels. About 112+6.875len instructions.
170
171If you are hashing n strings (ub1 **)k, do it like this:
172 for (i=0; i<8; ++i) state[i] = 0x9e3779b9;
173 for (i=0, h=0; i<n; ++i) checksum( k[i], len[i], state);
174
175(c) Bob Jenkins, 1996. bob_jenkins@burtleburtle.net. You may use this
176code any way you wish, private, educational, or commercial, as long
177as this whole comment accompanies it.
178
179See http://burtleburtle.net/bob/hash/evahash.html
180Use to detect changes between revisions of documents, assuming nobody
181is trying to cause collisions. Do NOT use for cryptography.
182--------------------------------------------------------------------
183*/
Harald Weltebed35df2011-11-02 13:06:18 +0100184void checksum(k, len, state)
jjako52c24142002-12-16 13:33:51 +0000185register ub1 *k;
Harald Weltebed35df2011-11-02 13:06:18 +0100186register ub4 len;
jjako52c24142002-12-16 13:33:51 +0000187register ub4 *state;
188{
Harald Weltebed35df2011-11-02 13:06:18 +0100189 register ub4 a, b, c, d, e, f, g, h, length;
jjako52c24142002-12-16 13:33:51 +0000190
Harald Weltebed35df2011-11-02 13:06:18 +0100191 /* Use the length and level; add in the golden ratio. */
192 length = len;
193 a = state[0];
194 b = state[1];
195 c = state[2];
196 d = state[3];
197 e = state[4];
198 f = state[5];
199 g = state[6];
200 h = state[7];
jjako52c24142002-12-16 13:33:51 +0000201
202 /*---------------------------------------- handle most of the key */
Harald Weltebed35df2011-11-02 13:06:18 +0100203 while (len >= 32) {
204 a += (k[0] + (k[1] << 8) + (k[2] << 16) + (k[3] << 24));
205 b += (k[4] + (k[5] << 8) + (k[6] << 16) + (k[7] << 24));
206 c += (k[8] + (k[9] << 8) + (k[10] << 16) + (k[11] << 24));
207 d += (k[12] + (k[13] << 8) + (k[14] << 16) + (k[15] << 24));
208 e += (k[16] + (k[17] << 8) + (k[18] << 16) + (k[19] << 24));
209 f += (k[20] + (k[21] << 8) + (k[22] << 16) + (k[23] << 24));
210 g += (k[24] + (k[25] << 8) + (k[26] << 16) + (k[27] << 24));
211 h += (k[28] + (k[29] << 8) + (k[30] << 16) + (k[31] << 24));
212 mixc(a, b, c, d, e, f, g, h);
213 mixc(a, b, c, d, e, f, g, h);
214 mixc(a, b, c, d, e, f, g, h);
215 mixc(a, b, c, d, e, f, g, h);
216 k += 32;
217 len -= 32;
218 }
jjako52c24142002-12-16 13:33:51 +0000219
220 /*------------------------------------- handle the last 31 bytes */
Harald Weltebed35df2011-11-02 13:06:18 +0100221 h += length;
222 switch (len) {
223 case 31:
224 h += (k[30] << 24);
225 case 30:
226 h += (k[29] << 16);
227 case 29:
228 h += (k[28] << 8);
229 case 28:
230 g += (k[27] << 24);
231 case 27:
232 g += (k[26] << 16);
233 case 26:
234 g += (k[25] << 8);
235 case 25:
236 g += k[24];
237 case 24:
238 f += (k[23] << 24);
239 case 23:
240 f += (k[22] << 16);
241 case 22:
242 f += (k[21] << 8);
243 case 21:
244 f += k[20];
245 case 20:
246 e += (k[19] << 24);
247 case 19:
248 e += (k[18] << 16);
249 case 18:
250 e += (k[17] << 8);
251 case 17:
252 e += k[16];
253 case 16:
254 d += (k[15] << 24);
255 case 15:
256 d += (k[14] << 16);
257 case 14:
258 d += (k[13] << 8);
259 case 13:
260 d += k[12];
261 case 12:
262 c += (k[11] << 24);
263 case 11:
264 c += (k[10] << 16);
265 case 10:
266 c += (k[9] << 8);
267 case 9:
268 c += k[8];
269 case 8:
270 b += (k[7] << 24);
271 case 7:
272 b += (k[6] << 16);
273 case 6:
274 b += (k[5] << 8);
275 case 5:
276 b += k[4];
277 case 4:
278 a += (k[3] << 24);
279 case 3:
280 a += (k[2] << 16);
281 case 2:
282 a += (k[1] << 8);
283 case 1:
284 a += k[0];
285 }
286 mixc(a, b, c, d, e, f, g, h);
287 mixc(a, b, c, d, e, f, g, h);
288 mixc(a, b, c, d, e, f, g, h);
289 mixc(a, b, c, d, e, f, g, h);
jjako52c24142002-12-16 13:33:51 +0000290
291 /*-------------------------------------------- report the result */
Harald Weltebed35df2011-11-02 13:06:18 +0100292 state[0] = a;
293 state[1] = b;
294 state[2] = c;
295 state[3] = d;
296 state[4] = e;
297 state[5] = f;
298 state[6] = g;
299 state[7] = h;
jjako52c24142002-12-16 13:33:51 +0000300}