UYS  0.1
Useful Yet Simple (C libraries collection)
 All Data Structures Files Functions Typedefs Macros Pages
lookup3.c
1 /*****************************************************************
2  * lookup3.c, by Bob Jenkins, May 2006, Public Domain.
3  *
4  * These are functions for producing 32-bit hashes for hash table lookup.
5  * hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
6  * are externally useful functions. Routines to test the hash are included
7  * if SELF_TEST is defined. You can use this free for any purpose. It's in
8  * the public domain. It has no warranty.
9  *
10  * You probably want to use hashlittle(). hashlittle() and hashbig()
11  * hash byte arrays. hashlittle() is is faster than hashbig() on
12  * little-endian machines. Intel and AMD are little-endian machines.
13  * On second thought, you probably want hashlittle2(), which is identical to
14  * hashlittle() except it returns two 32-bit hashes for the price of one.
15  * You could implement hashbig2() if you wanted but I haven't bothered here.
16  * If you want to find a hash of, say, exactly 7 integers, do
17  *
18  * a = i1; b = i2; c = i3;
19  * mix(a,b,c);
20  * a += i4; b += i5; c += i6;
21  * mix(a,b,c);
22  * a += i7;
23  * final(a,b,c);
24  *
25  * then use c as the hash value. If you have a variable length array of
26  * 4-byte integers to hash, use hashword(). If you have a byte array (like
27  * a character string), use hashlittle(). If you have several byte arrays, or
28  * a mix of things, see the comments above hashlittle().
29  * Why is this so big? I read 12 bytes at a time into 3 4-byte integers,
30  * then mix those integers. This is fast (you can do a lot more thorough
31  * mixing with 12*3 instructions on 3 integers than you can with 3
32  * instructions on 1 byte), but shoehorning those bytes into integers
33  * efficiently is messy.
34  *****************************************************************/
35 #define VALGRIND 1
36 
37 #include <stdio.h> /* defines printf for tests */
38 #include <time.h> /* defines time_t for timings in the test */
39 #include <stdint.h> /* defines uint32_t etc */
40 #include <sys/param.h> /* attempt to define endianness */
41 #ifdef linux
42 # include <endian.h> /* attempt to define endianness */
43 #endif
44 
45 #include "lookup3.h"
46 
47 /*
48  * My best guess at if you are big-endian or little-endian. This may
49  * need adjustment.
50  */
51 #if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \
52  __BYTE_ORDER == __LITTLE_ENDIAN) || \
53  (defined(i386) || defined(__i386__) || defined(__i486__) || \
54  defined(__i586__) || defined(__i686__) || defined(vax) || defined(MIPSEL))
55 # define HASH_LITTLE_ENDIAN 1
56 # define HASH_BIG_ENDIAN 0
57 #elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
58  __BYTE_ORDER == __BIG_ENDIAN) || \
59  (defined(sparc) || defined(POWERPC) || defined(mc68000) || defined(sel))
60 # define HASH_LITTLE_ENDIAN 0
61 # define HASH_BIG_ENDIAN 1
62 #else
63 # define HASH_LITTLE_ENDIAN 0
64 # define HASH_BIG_ENDIAN 0
65 #endif
66 
67 #define hashsize(n) ((uint32_t)1<<(n))
68 #define hashmask(n) (hashsize(n)-1)
69 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
70 
71 
72 
73 /*
74 -------------------------------------------------------------------------------
75 mix -- mix 3 32-bit values reversibly.
76 
77 This is reversible, so any information in (a,b,c) before mix() is
78 still in (a,b,c) after mix().
79 
80 If four pairs of (a,b,c) inputs are run through mix(), or through
81 mix() in reverse, there are at least 32 bits of the output that
82 are sometimes the same for one pair and different for another pair.
83 This was tested for:
84 * pairs that differed by one bit, by two bits, in any combination
85  of top bits of (a,b,c), or in any combination of bottom bits of
86  (a,b,c).
87 * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
88  the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
89  is commonly produced by subtraction) look like a single 1-bit
90  difference.
91 * the base values were pseudorandom, all zero but one bit set, or
92  all zero plus a counter that starts at zero.
93 
94 Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
95 satisfy this are
96  4 6 8 16 19 4
97  9 15 3 18 27 15
98  14 9 3 7 17 3
99 Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
100 for "differ" defined as + with a one-bit base and a two-bit delta. I
101 used http://burtleburtle.net/bob/hash/avalanche.html to choose
102 the operations, constants, and arrangements of the variables.
103 
104 This does not achieve avalanche. There are input bits of (a,b,c)
105 that fail to affect some output bits of (a,b,c), especially of a. The
106 most thoroughly mixed value is c, but it doesn't really even achieve
107 avalanche in c.
108 
109 This allows some parallelism. Read-after-writes are good at doubling
110 the number of bits affected, so the goal of mixing pulls in the opposite
111 direction as the goal of parallelism. I did what I could. Rotates
112 seem to cost as much as shifts on every machine I could lay my hands
113 on, and rotates are much kinder to the top and bottom bits, so I used
114 rotates.
115 -------------------------------------------------------------------------------
116 */
117 #define mix(a,b,c) \
118 { \
119  a -= c; a ^= rot(c, 4); c += b; \
120  b -= a; b ^= rot(a, 6); a += c; \
121  c -= b; c ^= rot(b, 8); b += a; \
122  a -= c; a ^= rot(c,16); c += b; \
123  b -= a; b ^= rot(a,19); a += c; \
124  c -= b; c ^= rot(b, 4); b += a; \
125 }
126 
127 
128 
129 /*
130 -------------------------------------------------------------------------------
131 final -- final mixing of 3 32-bit values (a,b,c) into c
132 
133 Pairs of (a,b,c) values differing in only a few bits will usually
134 produce values of c that look totally different. This was tested for
135 * pairs that differed by one bit, by two bits, in any combination
136  of top bits of (a,b,c), or in any combination of bottom bits of
137  (a,b,c).
138 * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
139  the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
140  is commonly produced by subtraction) look like a single 1-bit
141  difference.
142 * the base values were pseudorandom, all zero but one bit set, or
143  all zero plus a counter that starts at zero.
144 
145 These constants passed:
146  14 11 25 16 4 14 24
147  12 14 25 16 4 14 24
148 and these came close:
149  4 8 15 26 3 22 24
150  10 8 15 26 3 22 24
151  11 8 15 26 3 22 24
152 -------------------------------------------------------------------------------
153 */
154 #define final(a,b,c) \
155 { \
156  c ^= b; c -= rot(b,14); \
157  a ^= c; a -= rot(c,11); \
158  b ^= a; b -= rot(a,25); \
159  c ^= b; c -= rot(b,16); \
160  a ^= c; a -= rot(c,4); \
161  b ^= a; b -= rot(a,14); \
162  c ^= b; c -= rot(b,24); \
163 }
164 
165 
166 
167 /*****************************************************************
168  * hashword
169  ****************************************************************/
170 /*****************************************************************
171  * This works on all machines. To be useful, it requires:
172  * -- that the key be an array of uint32_t's, and
173  * -- that the length be the number of uint32_t's in the key
174  *
175  * The function hashword() is identical to hashlittle() on little-endian
176  * machines, and identical to hashbig() on big-endian machines,
177  * except that the length has to be measured in uint32_ts rather than in
178  * bytes. hashlittle() is more complicated than hashword() only because
179  * hashlittle() has to dance around fitting the key bytes into registers.
180  *
181  * \param k the key, an array of uint32_t values.
182  * \param length the length of the key, in uint32_ts
183  * \param initval the previous hash, or an arbitrary value
184  ****************************************************************/
185 /* NOTE: COMMENT IT OUT
186 uint32_t hashword(const uint32_t *k, size_t length, uint32_t initval) {
187  uint32_t a,b,c;
188  // Set up the internal state
189  a = b = c = 0xdeadbeef + (((uint32_t)length)<<2) + initval;
190 
191  //------------------------------------------------- handle most of the key
192  while (length > 3)
193  {
194  a += k[0];
195  b += k[1];
196  c += k[2];
197  mix(a,b,c);
198  length -= 3;
199  k += 3;
200  }
201 
202  //------------------------------------------- handle the last 3 uint32_t's
203 
204  switch(length)
205  {
206  // *****************************************************************
207  // * NOTE: ALL THE CASE STATEMENTS DO FALL THROUGH
208  // ****************************************************************
209  case 3 : c+=k[2];
210  case 2 : b+=k[1];
211  case 1 : a+=k[0];
212  final(a,b,c);
213  case 0: // case 0: nothing left to add
214  break;
215  }
216  //------------------------------------------------------ report the result
217  return c;
218 }
219 */
220 
221 
222 
223 /*****************************************************************
224  * hashword2
225  ****************************************************************/
226 /*****************************************************************
227  * same as hashword(), but take two seeds and return two 32-bit values. pc
228  * and pb must both be nonnull, and *pc and *pb must both be initialized with
229  * seeds. If you pass in (*pb)==0, the output (*pc) will be the same as the
230  * return value from hashword().
231  *
232  * \param k the key, an array of uint32_t values.
233  * \param length the length of the key.
234  * \param pc IN: seed OUT: primary hash value.
235  * \param pb IN: more seed OUT: secondary hash value
236  ****************************************************************/
237 /* NOTE: COMMENT IT OUT
238 void hashword2 (const uint32_t *k, size_t length, uint32_t *pc, uint32_t *pb){
239  uint32_t a,b,c;
240 
241  // Set up the internal state
242  a = b = c = 0xdeadbeef + ((uint32_t)(length<<2)) + *pc;
243  c += *pb;
244 
245  //------------------------------------------------- handle most of the key
246  while (length > 3)
247  {
248  a += k[0];
249  b += k[1];
250  c += k[2];
251  mix(a,b,c);
252  length -= 3;
253  k += 3;
254  }
255 
256  //------------------------------------------- handle the last 3 uint32_t's
257  switch(length)
258  {
259  // *****************************************************************
260  // * NOTE: ALL THE CASE STATEMENTS DO FALL THROUGH
261  // ****************************************************************
262  case 3 : c+=k[2];
263  case 2 : b+=k[1];
264  case 1 : a+=k[0];
265  final(a,b,c);
266  case 0: // case 0: nothing left to add
267  break;
268  }
269  //------------------------------------------------------ report the result
270  *pc=c; *pb=b;
271 }
272 */
273 
274 
275 
276 /*
277 -------------------------------------------------------------------------------
278 hashlittle() -- hash a variable-length key into a 32-bit value
279  k : the key (the unaligned variable-length array of bytes)
280  length : the length of the key, counting by bytes
281  initval : can be any 4-byte value
282 Returns a 32-bit value. Every bit of the key affects every bit of
283 the return value. Two keys differing by one or two bits will have
284 totally different hash values.
285 
286 The best hash table sizes are powers of 2. There is no need to do
287 mod a prime (mod is sooo slow!). If you need less than 32 bits,
288 use a bitmask. For example, if you need only 10 bits, do
289  h = (h & hashmask(10));
290 In which case, the hash table should have hashsize(10) elements.
291 
292 If you are hashing n strings (uint8_t **)k, do it like this:
293  for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);
294 
295 By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this
296 code any way you wish, private, educational, or commercial. It's free.
297 
298 Use for hash table lookup, or anything where one collision in 2^^32 is
299 acceptable. Do NOT use for cryptographic purposes.
300 -------------------------------------------------------------------------------
301 */
302 uint32_t hashlittle( const void *key, size_t length, uint32_t initval)
303 {
304  uint32_t a,b,c; /* internal state */
305  union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
306 
307  /* Set up the internal state */
308  a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
309 
310  u.ptr = key;
311  if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
312  const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
313  const uint8_t *k8;
314 
315  /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
316  while (length > 12)
317  {
318  a += k[0];
319  b += k[1];
320  c += k[2];
321  mix(a,b,c);
322  length -= 12;
323  k += 3;
324  }
325 
326  /*----------------------------- handle the last (probably partial) block */
327  /*
328  * "k[2]&0xffffff" actually reads beyond the end of the string, but
329  * then masks off the part it's not allowed to read. Because the
330  * string is aligned, the masked-off tail is in the same word as the
331  * rest of the string. Every machine with memory protection I've seen
332  * does it on word boundaries, so is OK with this. But VALGRIND will
333  * still catch it and complain. The masking trick does make the hash
334  * noticably faster for short strings (like English words).
335  */
336 #ifndef VALGRIND
337 
338  switch(length)
339  {
340  case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
341  case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
342  case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
343  case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
344  case 8 : b+=k[1]; a+=k[0]; break;
345  case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
346  case 6 : b+=k[1]&0xffff; a+=k[0]; break;
347  case 5 : b+=k[1]&0xff; a+=k[0]; break;
348  case 4 : a+=k[0]; break;
349  case 3 : a+=k[0]&0xffffff; break;
350  case 2 : a+=k[0]&0xffff; break;
351  case 1 : a+=k[0]&0xff; break;
352  case 0 : return c; /* zero length strings require no mixing */
353  }
354 
355 #else /* make valgrind happy */
356 
357  k8 = (const uint8_t *)k;
358  switch(length)
359  {
360  case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
361  case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
362  case 10: c+=((uint32_t)k8[9])<<8; /* fall through */
363  case 9 : c+=k8[8]; /* fall through */
364  case 8 : b+=k[1]; a+=k[0]; break;
365  case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
366  case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */
367  case 5 : b+=k8[4]; /* fall through */
368  case 4 : a+=k[0]; break;
369  case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
370  case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */
371  case 1 : a+=k8[0]; break;
372  case 0 : return c;
373  }
374 
375 #endif /* !valgrind */
376 
377  } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
378  const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
379  const uint8_t *k8;
380 
381  /*--------------- all but last block: aligned reads and different mixing */
382  while (length > 12)
383  {
384  a += k[0] + (((uint32_t)k[1])<<16);
385  b += k[2] + (((uint32_t)k[3])<<16);
386  c += k[4] + (((uint32_t)k[5])<<16);
387  mix(a,b,c);
388  length -= 12;
389  k += 6;
390  }
391 
392  /*----------------------------- handle the last (probably partial) block */
393  k8 = (const uint8_t *)k;
394  switch(length)
395  {
396  case 12: c+=k[4]+(((uint32_t)k[5])<<16);
397  b+=k[2]+(((uint32_t)k[3])<<16);
398  a+=k[0]+(((uint32_t)k[1])<<16);
399  break;
400  case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
401  case 10: c+=k[4];
402  b+=k[2]+(((uint32_t)k[3])<<16);
403  a+=k[0]+(((uint32_t)k[1])<<16);
404  break;
405  case 9 : c+=k8[8]; /* fall through */
406  case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
407  a+=k[0]+(((uint32_t)k[1])<<16);
408  break;
409  case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
410  case 6 : b+=k[2];
411  a+=k[0]+(((uint32_t)k[1])<<16);
412  break;
413  case 5 : b+=k8[4]; /* fall through */
414  case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
415  break;
416  case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
417  case 2 : a+=k[0];
418  break;
419  case 1 : a+=k8[0];
420  break;
421  case 0 : return c; /* zero length requires no mixing */
422  }
423 
424  } else { /* need to read the key one byte at a time */
425  const uint8_t *k = (const uint8_t *)key;
426 
427  /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
428  while (length > 12)
429  {
430  a += k[0];
431  a += ((uint32_t)k[1])<<8;
432  a += ((uint32_t)k[2])<<16;
433  a += ((uint32_t)k[3])<<24;
434  b += k[4];
435  b += ((uint32_t)k[5])<<8;
436  b += ((uint32_t)k[6])<<16;
437  b += ((uint32_t)k[7])<<24;
438  c += k[8];
439  c += ((uint32_t)k[9])<<8;
440  c += ((uint32_t)k[10])<<16;
441  c += ((uint32_t)k[11])<<24;
442  mix(a,b,c);
443  length -= 12;
444  k += 12;
445  }
446 
447  /*-------------------------------- last block: affect all 32 bits of (c) */
448  switch(length)
449  {
450  /*****************************************************************
451  * NOTE: ALL THE CASE STATEMENTS DO FALL THROUGH
452  ****************************************************************/
453  case 12: c+=((uint32_t)k[11])<<24;
454  case 11: c+=((uint32_t)k[10])<<16;
455  case 10: c+=((uint32_t)k[9])<<8;
456  case 9 : c+=k[8];
457  case 8 : b+=((uint32_t)k[7])<<24;
458  case 7 : b+=((uint32_t)k[6])<<16;
459  case 6 : b+=((uint32_t)k[5])<<8;
460  case 5 : b+=k[4];
461  case 4 : a+=((uint32_t)k[3])<<24;
462  case 3 : a+=((uint32_t)k[2])<<16;
463  case 2 : a+=((uint32_t)k[1])<<8;
464  case 1 : a+=k[0];
465  break;
466  case 0 : return c;
467  }
468  }
469 
470  final(a,b,c);
471  return c;
472 }
473 
474 
475 
476 /*
477  * hashlittle2: return 2 32-bit hash values
478  *
479  * This is identical to hashlittle(), except it returns two 32-bit hash
480  * values instead of just one. This is good enough for hash table
481  * lookup with 2^^64 buckets, or if you want a second hash if you're not
482  * happy with the first, or if you want a probably-unique 64-bit ID for
483  * the key. *pc is better mixed than *pb, so use *pc first. If you want
484  * a 64-bit value do something like "*pc + (((uint64_t)*pb)<<32)".
485  */
486 void hashlittle2(
487  const void *key, /* the key to hash */
488  size_t length, /* length of the key */
489  uint32_t *pc, /* IN: primary initval, OUT: primary hash */
490  uint32_t *pb) /* IN: secondary initval, OUT: secondary hash */
491 {
492  uint32_t a,b,c; /* internal state */
493  union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
494 
495  /* Set up the internal state */
496  a = b = c = 0xdeadbeef + ((uint32_t)length) + *pc;
497  c += *pb;
498 
499  u.ptr = key;
500  if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
501  const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
502  const uint8_t *k8;
503 
504  /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
505  while (length > 12)
506  {
507  a += k[0];
508  b += k[1];
509  c += k[2];
510  mix(a,b,c);
511  length -= 12;
512  k += 3;
513  }
514 
515  /*----------------------------- handle the last (probably partial) block */
516  /*
517  * "k[2]&0xffffff" actually reads beyond the end of the string, but
518  * then masks off the part it's not allowed to read. Because the
519  * string is aligned, the masked-off tail is in the same word as the
520  * rest of the string. Every machine with memory protection I've seen
521  * does it on word boundaries, so is OK with this. But VALGRIND will
522  * still catch it and complain. The masking trick does make the hash
523  * noticably faster for short strings (like English words).
524  */
525 #ifndef VALGRIND
526 
527  switch(length)
528  {
529  case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
530  case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
531  case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
532  case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
533  case 8 : b+=k[1]; a+=k[0]; break;
534  case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
535  case 6 : b+=k[1]&0xffff; a+=k[0]; break;
536  case 5 : b+=k[1]&0xff; a+=k[0]; break;
537  case 4 : a+=k[0]; break;
538  case 3 : a+=k[0]&0xffffff; break;
539  case 2 : a+=k[0]&0xffff; break;
540  case 1 : a+=k[0]&0xff; break;
541  case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
542  }
543 
544 #else /* make valgrind happy */
545 
546  k8 = (const uint8_t *)k;
547  switch(length)
548  {
549  case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
550  case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
551  case 10: c+=((uint32_t)k8[9])<<8; /* fall through */
552  case 9 : c+=k8[8]; /* fall through */
553  case 8 : b+=k[1]; a+=k[0]; break;
554  case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
555  case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */
556  case 5 : b+=k8[4]; /* fall through */
557  case 4 : a+=k[0]; break;
558  case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
559  case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */
560  case 1 : a+=k8[0]; break;
561  case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
562  }
563 
564 #endif /* !valgrind */
565 
566  } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
567  const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
568  const uint8_t *k8;
569 
570  /*--------------- all but last block: aligned reads and different mixing */
571  while (length > 12)
572  {
573  a += k[0] + (((uint32_t)k[1])<<16);
574  b += k[2] + (((uint32_t)k[3])<<16);
575  c += k[4] + (((uint32_t)k[5])<<16);
576  mix(a,b,c);
577  length -= 12;
578  k += 6;
579  }
580 
581  /*----------------------------- handle the last (probably partial) block */
582  k8 = (const uint8_t *)k;
583  switch(length)
584  {
585  case 12: c+=k[4]+(((uint32_t)k[5])<<16);
586  b+=k[2]+(((uint32_t)k[3])<<16);
587  a+=k[0]+(((uint32_t)k[1])<<16);
588  break;
589  case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
590  case 10: c+=k[4];
591  b+=k[2]+(((uint32_t)k[3])<<16);
592  a+=k[0]+(((uint32_t)k[1])<<16);
593  break;
594  case 9 : c+=k8[8]; /* fall through */
595  case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
596  a+=k[0]+(((uint32_t)k[1])<<16);
597  break;
598  case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
599  case 6 : b+=k[2];
600  a+=k[0]+(((uint32_t)k[1])<<16);
601  break;
602  case 5 : b+=k8[4]; /* fall through */
603  case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
604  break;
605  case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
606  case 2 : a+=k[0];
607  break;
608  case 1 : a+=k8[0];
609  break;
610  case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
611  }
612 
613  } else { /* need to read the key one byte at a time */
614  const uint8_t *k = (const uint8_t *)key;
615 
616  /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
617  while (length > 12)
618  {
619  a += k[0];
620  a += ((uint32_t)k[1])<<8;
621  a += ((uint32_t)k[2])<<16;
622  a += ((uint32_t)k[3])<<24;
623  b += k[4];
624  b += ((uint32_t)k[5])<<8;
625  b += ((uint32_t)k[6])<<16;
626  b += ((uint32_t)k[7])<<24;
627  c += k[8];
628  c += ((uint32_t)k[9])<<8;
629  c += ((uint32_t)k[10])<<16;
630  c += ((uint32_t)k[11])<<24;
631  mix(a,b,c);
632  length -= 12;
633  k += 12;
634  }
635 
636  /*-------------------------------- last block: affect all 32 bits of (c) */
637  switch(length)
638  {
639  /*****************************************************************
640  * NOTE: ALL THE CASE STATEMENTS DO FALL THROUGH
641  ****************************************************************/
642  case 12: c+=((uint32_t)k[11])<<24;
643  case 11: c+=((uint32_t)k[10])<<16;
644  case 10: c+=((uint32_t)k[9])<<8;
645  case 9 : c+=k[8];
646  case 8 : b+=((uint32_t)k[7])<<24;
647  case 7 : b+=((uint32_t)k[6])<<16;
648  case 6 : b+=((uint32_t)k[5])<<8;
649  case 5 : b+=k[4];
650  case 4 : a+=((uint32_t)k[3])<<24;
651  case 3 : a+=((uint32_t)k[2])<<16;
652  case 2 : a+=((uint32_t)k[1])<<8;
653  case 1 : a+=k[0];
654  break;
655  case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
656  }
657  }
658 
659  final(a,b,c);
660  *pc=c; *pb=b;
661 }
662 
663 
664 /*****************************************************************
665  * hashbig
666  ****************************************************************/
667 /*****************************************************************
668  * This is the same as hashword() on big-endian machines. It is different
669  * from hashlittle() on all machines. hashbig() takes advantage of
670  * big-endian byte ordering.
671  ****************************************************************/
672 /*
673 uint32_t hashbig(const void *key, size_t length, uint32_t initval)
674 {
675  uint32_t a,b,c;
676  union { const void *ptr; size_t i; } u; // to cast key to (size_t) happily
677 
678  // Set up the internal state
679  a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
680 
681  u.ptr = key;
682  if (HASH_BIG_ENDIAN && ((u.i & 0x3) == 0)) {
683  const uint32_t *k = (const uint32_t *)key; // read 32-bit chunks
684  const uint8_t *k8;
685 
686  //------ all but last block: aligned reads and affect 32 bits of (a,b,c)
687  while (length > 12)
688  {
689  a += k[0];
690  b += k[1];
691  c += k[2];
692  mix(a,b,c);
693  length -= 12;
694  k += 3;
695  }
696 
697  //----------------------------- handle the last (probably partial) block
698  // *
699  // * "k[2]<<8" actually reads beyond the end of the string, but
700  // * then shifts out the part it's not allowed to read. Because the
701  // * string is aligned, the illegal read is in the same word as the
702  // * rest of the string. Every machine with memory protection I've seen
703  // * does it on word boundaries, so is OK with this. But VALGRIND will
704  // * still catch it and complain. The masking trick does make the hash
705  // * noticably faster for short strings (like English words).
706  // *
707 #ifndef VALGRIND
708 
709  switch(length)
710  {
711  case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
712  case 11: c+=k[2]&0xffffff00; b+=k[1]; a+=k[0]; break;
713  case 10: c+=k[2]&0xffff0000; b+=k[1]; a+=k[0]; break;
714  case 9 : c+=k[2]&0xff000000; b+=k[1]; a+=k[0]; break;
715  case 8 : b+=k[1]; a+=k[0]; break;
716  case 7 : b+=k[1]&0xffffff00; a+=k[0]; break;
717  case 6 : b+=k[1]&0xffff0000; a+=k[0]; break;
718  case 5 : b+=k[1]&0xff000000; a+=k[0]; break;
719  case 4 : a+=k[0]; break;
720  case 3 : a+=k[0]&0xffffff00; break;
721  case 2 : a+=k[0]&0xffff0000; break;
722  case 1 : a+=k[0]&0xff000000; break;
723  case 0 : return c; // zero length strings require no mixing
724  }
725 
726 #else // make valgrind happy
727 
728  k8 = (const uint8_t *)k;
729  switch(length) // all the case statements fall through
730  {
731  case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
732  case 11: c+=((uint32_t)k8[10])<<8; // fall through
733  case 10: c+=((uint32_t)k8[9])<<16; // fall through
734  case 9 : c+=((uint32_t)k8[8])<<24; // fall through
735  case 8 : b+=k[1]; a+=k[0]; break;
736  case 7 : b+=((uint32_t)k8[6])<<8; // fall through
737  case 6 : b+=((uint32_t)k8[5])<<16; // fall through
738  case 5 : b+=((uint32_t)k8[4])<<24; // fall through
739  case 4 : a+=k[0]; break;
740  case 3 : a+=((uint32_t)k8[2])<<8; // fall through
741  case 2 : a+=((uint32_t)k8[1])<<16; // fall through
742  case 1 : a+=((uint32_t)k8[0])<<24; break;
743  case 0 : return c;
744  }
745 
746 #endif // !VALGRIND
747 
748  } else { // need to read the key one byte at a time
749  const uint8_t *k = (const uint8_t *)key;
750 
751  //--------------- all but the last block: affect some 32 bits of (a,b,c)
752  while (length > 12) {
753  a += ((uint32_t)k[0])<<24;
754  a += ((uint32_t)k[1])<<16;
755  a += ((uint32_t)k[2])<<8;
756  a += ((uint32_t)k[3]);
757  b += ((uint32_t)k[4])<<24;
758  b += ((uint32_t)k[5])<<16;
759  b += ((uint32_t)k[6])<<8;
760  b += ((uint32_t)k[7]);
761  c += ((uint32_t)k[8])<<24;
762  c += ((uint32_t)k[9])<<16;
763  c += ((uint32_t)k[10])<<8;
764  c += ((uint32_t)k[11]);
765  mix(a,b,c);
766  length -= 12;
767  k += 12;
768  }
769 
770  //-------------------------------- last block: affect all 32 bits of (c)
771  switch(length) {
772  // *****************************************************************
773  // * NOTE: ALL THE CASE STATEMENTS DO FALL THROUGH
774  // *****************************************************************
775  case 12: c+=k[11];
776  case 11: c+=((uint32_t)k[10])<<8;
777  case 10: c+=((uint32_t)k[9])<<16;
778  case 9 : c+=((uint32_t)k[8])<<24;
779  case 8 : b+=k[7];
780  case 7 : b+=((uint32_t)k[6])<<8;
781  case 6 : b+=((uint32_t)k[5])<<16;
782  case 5 : b+=((uint32_t)k[4])<<24;
783  case 4 : a+=k[3];
784  case 3 : a+=((uint32_t)k[2])<<8;
785  case 2 : a+=((uint32_t)k[1])<<16;
786  case 1 : a+=((uint32_t)k[0])<<24;
787  break;
788  case 0 : return c;
789  }
790  }
791 
792  final(a,b,c);
793  return c;
794 }
795 */
796 
797 
798