1 // code taken more-or-less from Paul Hsieh's tests
10 # define CLOCKS_PER_SEC (CLK_TCK)
18 -------------------------------------------------------------------------------
19 lookup3.c, by Bob Jenkins, May 2006, Public Domain.
21 These are functions for producing 32-bit hashes for hash table lookup.
22 hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
23 are externally useful functions. Routines to test the hash are included
24 if SELF_TEST is defined. You can use this free for any purpose. It's in
25 the public domain. It has no warranty.
27 You probably want to use hashlittle(). hashlittle() and hashbig()
28 hash byte arrays. hashlittle() is is faster than hashbig() on
29 little-endian machines. Intel and AMD are little-endian machines.
30 On second thought, you probably want hashlittle2(), which is identical to
31 hashlittle() except it returns two 32-bit hashes for the price of one.
32 You could implement hashbig2() if you wanted but I haven't bothered here.
34 If you want to find a hash of, say, exactly 7 integers, do
35 a = i1; b = i2; c = i3;
37 a += i4; b += i5; c += i6;
41 then use c as the hash value. If you have a variable length array of
42 4-byte integers to hash, use hashword(). If you have a byte array (like
43 a character string), use hashlittle(). If you have several byte arrays, or
44 a mix of things, see the comments above hashlittle().
46 Why is this so big? I read 12 bytes at a time into 3 4-byte integers,
47 then mix those integers. This is fast (you can do a lot more thorough
48 mixing with 12*3 instructions on 3 integers than you can with 3 instructions
49 on 1 byte), but shoehorning those bytes into integers efficiently is messy.
50 -------------------------------------------------------------------------------
53 #include <stdio.h> /* defines printf for tests */
54 #include <time.h> /* defines time_t for timings in the test */
55 #include <stdint.h> /* defines uint32_t etc */
56 #include <sys/param.h> /* attempt to define endianness */
58 # include <endian.h> /* attempt to define endianness */
62 * My best guess at if you are big-endian or little-endian. This may
65 #if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \
66 __BYTE_ORDER == __LITTLE_ENDIAN) || \
67 (defined(i386) || defined(__i386__) || defined(__i486__) || \
68 defined(__i586__) || defined(__i686__) || defined(vax) || defined(MIPSEL))
69 # define HASH_LITTLE_ENDIAN 1
70 # define HASH_BIG_ENDIAN 0
71 #elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
72 __BYTE_ORDER == __BIG_ENDIAN) || \
73 (defined(sparc) || defined(POWERPC) || defined(mc68000) || defined(sel))
74 # define HASH_LITTLE_ENDIAN 0
75 # define HASH_BIG_ENDIAN 1
77 # define HASH_LITTLE_ENDIAN 0
78 # define HASH_BIG_ENDIAN 0
81 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
84 -------------------------------------------------------------------------------
85 mix -- mix 3 32-bit values reversibly.
87 This is reversible, so any information in (a,b,c) before mix() is
88 still in (a,b,c) after mix().
90 If four pairs of (a,b,c) inputs are run through mix(), or through
91 mix() in reverse, there are at least 32 bits of the output that
92 are sometimes the same for one pair and different for another pair.
94 * pairs that differed by one bit, by two bits, in any combination
95 of top bits of (a,b,c), or in any combination of bottom bits of
97 * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
98 the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
99 is commonly produced by subtraction) look like a single 1-bit
101 * the base values were pseudorandom, all zero but one bit set, or
102 all zero plus a counter that starts at zero.
104 Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
109 Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
110 for "differ" defined as + with a one-bit base and a two-bit delta. I
111 used http://burtleburtle.net/bob/hash/avalanche.html to choose
112 the operations, constants, and arrangements of the variables.
114 This does not achieve avalanche. There are input bits of (a,b,c)
115 that fail to affect some output bits of (a,b,c), especially of a. The
116 most thoroughly mixed value is c, but it doesn't really even achieve
119 This allows some parallelism. Read-after-writes are good at doubling
120 the number of bits affected, so the goal of mixing pulls in the opposite
121 direction as the goal of parallelism. I did what I could. Rotates
122 seem to cost as much as shifts on every machine I could lay my hands
123 on, and rotates are much kinder to the top and bottom bits, so I used
125 -------------------------------------------------------------------------------
129 a -= c; a ^= rot(c, 4); c += b; \
130 b -= a; b ^= rot(a, 6); a += c; \
131 c -= b; c ^= rot(b, 8); b += a; \
132 a -= c; a ^= rot(c,16); c += b; \
133 b -= a; b ^= rot(a,19); a += c; \
134 c -= b; c ^= rot(b, 4); b += a; \
138 -------------------------------------------------------------------------------
139 final -- final mixing of 3 32-bit values (a,b,c) into c
141 Pairs of (a,b,c) values differing in only a few bits will usually
142 produce values of c that look totally different. This was tested for
143 * pairs that differed by one bit, by two bits, in any combination
144 of top bits of (a,b,c), or in any combination of bottom bits of
146 * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
147 the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
148 is commonly produced by subtraction) look like a single 1-bit
150 * the base values were pseudorandom, all zero but one bit set, or
151 all zero plus a counter that starts at zero.
153 These constants passed:
156 and these came close:
160 -------------------------------------------------------------------------------
162 #define final(a,b,c) \
164 c ^= b; c -= rot(b, 14); \
165 a ^= c; a -= rot(c, 11); \
166 b ^= a; b -= rot(a, 25); \
167 c ^= b; c -= rot(b, 16); \
168 a ^= c; a -= rot(c, 4); \
169 b ^= a; b -= rot(a, 14); \
170 c ^= b; c -= rot(b, 24); \
174 --------------------------------------------------------------------
175 This works on all machines. To be useful, it requires
176 -- that the key be an array of uint32_t's, and
177 -- that the length be the number of uint32_t's in the key
179 The function hashword() is identical to hashlittle() on little-endian
180 machines, and identical to hashbig() on big-endian machines,
181 except that the length has to be measured in uint32_ts rather than in
182 bytes. hashlittle() is more complicated than hashword() only because
183 hashlittle() has to dance around fitting the key bytes into registers.
184 --------------------------------------------------------------------
187 const uint32_t *k, /* the key, an array of uint32_t values */
188 size_t length, /* the length of the key, in uint32_ts */
189 uint32_t initval) /* the previous hash, or an arbitrary value */
193 /* Set up the internal state */
194 a = b = c = 0xdeadbeef + (((uint32_t)length)<<2) + initval;
196 /*------------------------------------------------- handle most of the key */
207 /*------------------------------------------- handle the last 3 uint32_t's */
208 switch(length) /* all the case statements fall through */
214 case 0: /* case 0: nothing left to add */
217 /*------------------------------------------------------ report the result */
223 --------------------------------------------------------------------
224 hashword2() -- same as hashword(), but take two seeds and return two
225 32-bit values. pc and pb must both be nonnull, and *pc and *pb must
226 both be initialized with seeds. If you pass in (*pb)==0, the output
227 (*pc) will be the same as the return value from hashword().
228 --------------------------------------------------------------------
231 const uint32_t *k, /* the key, an array of uint32_t values */
232 size_t length, /* the length of the key, in uint32_ts */
233 uint32_t *pc, /* IN: seed OUT: primary hash value */
234 uint32_t *pb) /* IN: more seed OUT: secondary hash value */
238 /* Set up the internal state */
239 a = b = c = 0xdeadbeef + ((uint32_t)(length<<2)) + *pc;
242 /*------------------------------------------------- handle most of the key */
253 /*------------------------------------------- handle the last 3 uint32_t's */
254 switch(length) /* all the case statements fall through */
260 case 0: /* case 0: nothing left to add */
263 /*------------------------------------------------------ report the result */
269 -------------------------------------------------------------------------------
270 hashlittle() -- hash a variable-length key into a 32-bit value
271 k : the key (the unaligned variable-length array of bytes)
272 length : the length of the key, counting by bytes
273 initval : can be any 4-byte value
274 Returns a 32-bit value. Every bit of the key affects every bit of
275 the return value. Two keys differing by one or two bits will have
276 totally different hash values.
278 The best hash table sizes are powers of 2. There is no need to do
279 mod a prime (mod is sooo slow!). If you need less than 32 bits,
280 use a bitmask. For example, if you need only 10 bits, do
281 h = (h & hashmask(10));
282 In which case, the hash table should have hashsize(10) elements.
284 If you are hashing n strings (uint8_t **)k, do it like this:
285 for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);
287 By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this
288 code any way you wish, private, educational, or commercial. It's free.
290 Use for hash table lookup, or anything where one collision in 2^^32 is
291 acceptable. Do NOT use for cryptographic purposes.
292 -------------------------------------------------------------------------------
295 uint32_t hashlittle( const void *key, size_t length, uint32_t initval)
297 uint32_t a,b,c; /* internal state */
298 union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
300 /* Set up the internal state */
301 a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
304 if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
305 const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
308 /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
319 /*----------------------------- handle the last (probably partial) block */
321 k8 = (const uint8_t *)k;
324 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
325 case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
326 case 10: c+=((uint32_t)k8[9])<<8; /* fall through */
327 case 9 : c+=k8[8]; /* fall through */
328 case 8 : b+=k[1]; a+=k[0]; break;
329 case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
330 case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */
331 case 5 : b+=k8[4]; /* fall through */
332 case 4 : a+=k[0]; break;
333 case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
334 case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */
335 case 1 : a+=k8[0]; break;
339 } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
340 const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
343 /*--------------- all but last block: aligned reads and different mixing */
346 a += k[0] + (((uint32_t)k[1])<<16);
347 b += k[2] + (((uint32_t)k[3])<<16);
348 c += k[4] + (((uint32_t)k[5])<<16);
354 /*----------------------------- handle the last (probably partial) block */
355 k8 = (const uint8_t *)k;
358 case 12: c+=k[4]+(((uint32_t)k[5])<<16);
359 b+=k[2]+(((uint32_t)k[3])<<16);
360 a+=k[0]+(((uint32_t)k[1])<<16);
362 case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
364 b+=k[2]+(((uint32_t)k[3])<<16);
365 a+=k[0]+(((uint32_t)k[1])<<16);
367 case 9 : c+=k8[8]; /* fall through */
368 case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
369 a+=k[0]+(((uint32_t)k[1])<<16);
371 case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
373 a+=k[0]+(((uint32_t)k[1])<<16);
375 case 5 : b+=k8[4]; /* fall through */
376 case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
378 case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
383 case 0 : return c; /* zero length requires no mixing */
386 } else { /* need to read the key one byte at a time */
387 const uint8_t *k = (const uint8_t *)key;
389 /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
393 a += ((uint32_t)k[1])<<8;
394 a += ((uint32_t)k[2])<<16;
395 a += ((uint32_t)k[3])<<24;
397 b += ((uint32_t)k[5])<<8;
398 b += ((uint32_t)k[6])<<16;
399 b += ((uint32_t)k[7])<<24;
401 c += ((uint32_t)k[9])<<8;
402 c += ((uint32_t)k[10])<<16;
403 c += ((uint32_t)k[11])<<24;
409 /*-------------------------------- last block: affect all 32 bits of (c) */
410 switch(length) /* all the case statements fall through */
412 case 12: c+=((uint32_t)k[11])<<24;
413 case 11: c+=((uint32_t)k[10])<<16;
414 case 10: c+=((uint32_t)k[9])<<8;
417 case 8 : b+=((uint32_t)k[7])<<24;
418 case 7 : b+=((uint32_t)k[6])<<16;
419 case 6 : b+=((uint32_t)k[5])<<8;
422 case 4 : a+=((uint32_t)k[3])<<24;
423 case 3 : a+=((uint32_t)k[2])<<16;
424 case 2 : a+=((uint32_t)k[1])<<8;
438 * hashlittle2: return 2 32-bit hash values
440 * This is identical to hashlittle(), except it returns two 32-bit hash
441 * values instead of just one. This is good enough for hash table
442 * lookup with 2^^64 buckets, or if you want a second hash if you're not
443 * happy with the first, or if you want a probably-unique 64-bit ID for
444 * the key. *pc is better mixed than *pb, so use *pc first. If you want
445 * a 64-bit value do something like "*pc + (((uint64_t)*pb)<<32)".
448 const void *key, /* the key to hash */
449 size_t length, /* length of the key */
450 uint32_t *pc, /* IN: primary initval, OUT: primary hash */
451 uint32_t *pb) /* IN: secondary initval, OUT: secondary hash */
453 uint32_t a,b,c; /* internal state */
454 union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
456 /* Set up the internal state */
457 a = b = c = 0xdeadbeef + ((uint32_t)length) + *pc;
461 if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
462 const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
465 /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
476 /*----------------------------- handle the last (probably partial) block */
478 k8 = (const uint8_t *)k;
481 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
482 case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
483 case 10: c+=((uint32_t)k8[9])<<8; /* fall through */
484 case 9 : c+=k8[8]; /* fall through */
485 case 8 : b+=k[1]; a+=k[0]; break;
486 case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
487 case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */
488 case 5 : b+=k8[4]; /* fall through */
489 case 4 : a+=k[0]; break;
490 case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
491 case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */
492 case 1 : a+=k8[0]; break;
493 case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
496 } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
497 const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
500 /*--------------- all but last block: aligned reads and different mixing */
503 a += k[0] + (((uint32_t)k[1])<<16);
504 b += k[2] + (((uint32_t)k[3])<<16);
505 c += k[4] + (((uint32_t)k[5])<<16);
511 /*----------------------------- handle the last (probably partial) block */
512 k8 = (const uint8_t *)k;
515 case 12: c+=k[4]+(((uint32_t)k[5])<<16);
516 b+=k[2]+(((uint32_t)k[3])<<16);
517 a+=k[0]+(((uint32_t)k[1])<<16);
519 case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
521 b+=k[2]+(((uint32_t)k[3])<<16);
522 a+=k[0]+(((uint32_t)k[1])<<16);
524 case 9 : c+=k8[8]; /* fall through */
525 case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
526 a+=k[0]+(((uint32_t)k[1])<<16);
528 case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
530 a+=k[0]+(((uint32_t)k[1])<<16);
532 case 5 : b+=k8[4]; /* fall through */
533 case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
535 case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
540 case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
543 } else { /* need to read the key one byte at a time */
544 const uint8_t *k = (const uint8_t *)key;
546 /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
550 a += ((uint32_t)k[1])<<8;
551 a += ((uint32_t)k[2])<<16;
552 a += ((uint32_t)k[3])<<24;
554 b += ((uint32_t)k[5])<<8;
555 b += ((uint32_t)k[6])<<16;
556 b += ((uint32_t)k[7])<<24;
558 c += ((uint32_t)k[9])<<8;
559 c += ((uint32_t)k[10])<<16;
560 c += ((uint32_t)k[11])<<24;
566 /*-------------------------------- last block: affect all 32 bits of (c) */
567 switch(length) /* all the case statements fall through */
569 case 12: c+=((uint32_t)k[11])<<24;
570 case 11: c+=((uint32_t)k[10])<<16;
571 case 10: c+=((uint32_t)k[9])<<8;
573 case 8 : b+=((uint32_t)k[7])<<24;
574 case 7 : b+=((uint32_t)k[6])<<16;
575 case 6 : b+=((uint32_t)k[5])<<8;
577 case 4 : a+=((uint32_t)k[3])<<24;
578 case 3 : a+=((uint32_t)k[2])<<16;
579 case 2 : a+=((uint32_t)k[1])<<8;
582 case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
594 * This is the same as hashword() on big-endian machines. It is different
595 * from hashlittle() on all machines. hashbig() takes advantage of
596 * big-endian byte ordering.
598 uint32_t hashbig( const void *key, size_t length, uint32_t initval)
601 union { const void *ptr; size_t i; } u; /* to cast key to (size_t) happily */
603 /* Set up the internal state */
604 a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
607 if (HASH_BIG_ENDIAN && ((u.i & 0x3) == 0)) {
608 const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
611 /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
622 /*----------------------------- handle the last (probably partial) block */
624 k8 = (const uint8_t *)k;
625 switch(length) /* all the case statements fall through */
627 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
628 case 11: c+=((uint32_t)k8[10])<<8; /* fall through */
629 case 10: c+=((uint32_t)k8[9])<<16; /* fall through */
630 case 9 : c+=((uint32_t)k8[8])<<24; /* fall through */
631 case 8 : b+=k[1]; a+=k[0]; break;
632 case 7 : b+=((uint32_t)k8[6])<<8; /* fall through */
633 case 6 : b+=((uint32_t)k8[5])<<16; /* fall through */
634 case 5 : b+=((uint32_t)k8[4])<<24; /* fall through */
635 case 4 : a+=k[0]; break;
636 case 3 : a+=((uint32_t)k8[2])<<8; /* fall through */
637 case 2 : a+=((uint32_t)k8[1])<<16; /* fall through */
638 case 1 : a+=((uint32_t)k8[0])<<24; break;
642 } else { /* need to read the key one byte at a time */
643 const uint8_t *k = (const uint8_t *)key;
645 /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
648 a += ((uint32_t)k[0])<<24;
649 a += ((uint32_t)k[1])<<16;
650 a += ((uint32_t)k[2])<<8;
651 a += ((uint32_t)k[3]);
652 b += ((uint32_t)k[4])<<24;
653 b += ((uint32_t)k[5])<<16;
654 b += ((uint32_t)k[6])<<8;
655 b += ((uint32_t)k[7]);
656 c += ((uint32_t)k[8])<<24;
657 c += ((uint32_t)k[9])<<16;
658 c += ((uint32_t)k[10])<<8;
659 c += ((uint32_t)k[11]);
665 /*-------------------------------- last block: affect all 32 bits of (c) */
666 switch(length) /* all the case statements fall through */
669 case 11: c+=((uint32_t)k[10])<<8;
670 case 10: c+=((uint32_t)k[9])<<16;
671 case 9 : c+=((uint32_t)k[8])<<24;
673 case 7 : b+=((uint32_t)k[6])<<8;
674 case 6 : b+=((uint32_t)k[5])<<16;
675 case 5 : b+=((uint32_t)k[4])<<24;
677 case 3 : a+=((uint32_t)k[2])<<8;
678 case 2 : a+=((uint32_t)k[1])<<16;
679 case 1 : a+=((uint32_t)k[0])<<24;
690 uint32_t hashLookup3Orig (const char * k, int length) {
691 return hashlittle (k, length, 0);
694 uint32_t hashLookup3 (const char * k, int length) {
695 return Foam::Hasher(k, length, 0);
700 /* ======================================================================== */
702 static uint32_t crc_16_table[16] = {
703 0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401,
704 0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400
708 * This code was found at: http://wannabe.guru.org/alg/node191.html
709 * and still exists here: http://www.fearme.com/misc/alg/node191.html
711 * this source code is based on Rex and Binstock which, in turn,
712 * acknowledges William James Hunt.
714 * According to the site this CRC uses the polynomial x^16+x^5+x^2+1.
715 * Unfortunately, DOCSIS uses x^16+x^12+x^5+1. D'oh!
718 static uint32_t GetCRC16Update
721 const char * data_stream,
724 uint32_t crc = start_crc;
727 /* while there is more data to process */
728 while (length-- > 0) {
730 /* compute checksum of lower four bits of *data_stream */
731 r = crc_16_table[crc & 0xF];
732 crc = (crc >> 4) & 0x0FFF;
733 crc ^= r ^ crc_16_table[*data_stream & 0xF];
735 /* now compute checksum of upper four bits of *data_stream */
736 r = crc_16_table[crc & 0xF];
737 crc = (crc >> 4) & 0x0FFF;
738 crc ^= r ^ crc_16_table[(*data_stream >> 4) & 0xF];
747 uint32_t GetCRC16 (const char * data_stream, int length) {
748 return GetCRC16Update (0, data_stream, length);
751 /* ======================================================================== */
753 static uint32_t crc_table[256];
755 /* This code was found at:
756 * http://cell.onecall.net/cell-relay/publications/software/CRC/32bitCRC.c.html
760 /* crc32h.c -- package to compute 32-bit CRC one byte at a time using */
761 /* the high-bit first (Big-Endian) bit ordering convention */
764 /* gen_crc_table() -- generates a 256-word table containing all CRC */
765 /* remainders for every possible 8-bit byte. It */
766 /* must be executed (once) before any CRC updates. */
768 /* unsigned update_crc(crc_accum, data_blk_ptr, data_blk_size) */
769 /* unsigned crc_accum; char *data_blk_ptr; int data_blk_size; */
770 /* Returns the updated value of the CRC accumulator after */
771 /* processing each byte in the addressed block of data. */
773 /* It is assumed that an unsigned long is at least 32 bits wide and */
774 /* that the predefined type char occupies one 8-bit byte of storage. */
776 /* The generator polynomial used for this version of the package is */
777 /* x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x^1+x^0 */
778 /* as specified in the Autodin/Ethernet/ADCCP protocol standards. */
779 /* Other degree 32 polynomials may be substituted by re-defining the */
780 /* symbol POLYNOMIAL below. Lower degree polynomials must first be */
781 /* multiplied by an appropriate power of x. The representation used */
782 /* is that the coefficient of x^0 is stored in the LSB of the 32-bit */
783 /* word and the coefficient of x^31 is stored in the most significant */
784 /* bit. The CRC is to be appended to the data most significant byte */
785 /* first. For those protocols in which bytes are transmitted MSB */
786 /* first and in the same order as they are encountered in the block */
787 /* this convention results in the CRC remainder being transmitted with */
788 /* the coefficient of x^31 first and with that of x^0 last (just as */
789 /* would be done by a hardware shift register mechanization). */
791 /* The table lookup technique was adapted from the algorithm described */
792 /* by Avram Perez, Byte-wise CRC Calculations, IEEE Micro 3, 40 (1983).*/
794 /* generate the table of CRC remainders for all possible bytes */
796 #define CRC32POLYNOMIAL 0x04c11db7L
798 static void GenerateCRC32Table (void) {
800 register uint32_t crc_accum;
802 for ( i = 0; i < 256; i++ ) {
803 crc_accum = ( (unsigned long) i << 24 );
804 for ( j = 0; j < 8; j++ ) {
805 if ( crc_accum & 0x80000000L ) {
806 crc_accum = ( crc_accum << 1 ) ^ CRC32POLYNOMIAL;
808 crc_accum = ( crc_accum << 1 );
811 crc_table[i] = crc_accum;
816 /* update the CRC on the data block one byte at a time */
818 static uint32_t UpdateCRC32
821 const char *data_blk_ptr,
827 for (j = 0; j < data_blk_size; j++) {
828 i = (crc_accum >> 24) ^ *data_blk_ptr++;
829 crc_accum = (crc_accum << 8) ^ crc_table[i];
834 uint32_t GetCRC32 (const char * data_stream, int length) {
835 return UpdateCRC32 (0, data_stream, length);
838 /* ======================================================================== */
840 /* Performs two parallel CRC-32 on even and odd bytes of the input, then
841 combines the two in a further CRC-32 calculation */
843 uint32_t GetCRC32PH (const char *data_blk_ptr, int data_blk_size) {
846 uint32_t crc_accum0 = 0, crc_accum1 = 0x23456789u;
848 if (data_blk_size & 1) crc_accum0 ^= *data_blk_ptr++;
849 for (j = 1; j < data_blk_size; j+=2) {
850 i0 = ((crc_accum0 >> 24) ^ *data_blk_ptr++);
851 i1 = ((crc_accum1 >> 24) ^ *data_blk_ptr++);
852 crc_accum0 = (crc_accum0 << 8) ^ crc_table[i0];
853 crc_accum1 = (crc_accum1 << 8) ^ crc_table[i1];
855 return crc_accum0 + crc_accum1;
858 /* ======================================================================== */
860 /* Fowler / Noll / Vo (FNV) Hash
862 http://www.isthe.com/chongo/tech/comp/fnv/ */
864 uint32_t FNVHash (const char * data, int len) {
869 for (i=0; i < len; i++) {
870 hash = (16777619u * hash) ^ data[i];
875 /* ======================================================================== */
878 * http://burtleburtle.net/bob/hash/doobs.html
881 uint32_t oneAtATimeHash (const char * s, int len) {
885 for (hash = 0, i = 0; i < len; i++) {
887 hash += (hash << 10);
888 hash ^= (hash >> 6); /* Non-portable due to ANSI C */
891 hash ^= (hash >> 11); /* Non-portable due to ANSI C */
892 hash += (hash << 15);
893 return (uint32_t) hash;
896 /* ======================================================================== */
898 uint32_t oneAtATimeHashPH (const char * s, int len) {
899 int32_t hash0 = 0, hash1 = 0x23456789;
902 if (len & 1) hash1 ^= *s++;
904 for (i = 1; i < len; i+=2) {
907 hash0 += (hash0 << 10);
908 hash1 += (hash1 << 10);
909 hash0 ^= (hash0 >> 6); /* Non-portable due to ANSI C */
910 hash1 ^= (hash1 >> 6); /* Non-portable due to ANSI C */
915 hash0 += (hash0 << 3);
916 hash0 ^= (hash0 >> 11); /* Non-portable due to ANSI C */
917 hash0 += (hash0 << 15);
918 return (uint32_t) hash0;
921 /* ======================================================================== */
923 /* By Paul Hsieh (C) 2004, 2005. Covered under the Paul Hsieh derivative
925 http://www.azillionmonkeys.com/qed/weblicense.html for license details.
927 http://www.azillionmonkeys.com/qed/hash.html */
931 #if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \
932 || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__)
933 #define get16bits(d) (*((const uint16_t *) (d)))
937 #if !defined (get16bits)
938 #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
939 +(uint32_t)(((const uint8_t *)(d))[0]) )
942 uint32_t SuperFastHash (const char * data, int len)
946 if (len <= 0 || data == NULL) return 0;
948 unsigned rem = len & 3;
952 for (;len > 0; len--) {
953 hash += get16bits(data);
954 hash = (hash << 16) ^ ((get16bits(data+2) << 11) ^ hash);
956 data += 2*sizeof(uint16_t);
959 /* Handle end cases */
962 hash += get16bits(data);
964 hash ^= data[sizeof (uint16_t)] << 18;
969 hash += get16bits (data);
974 case 1 : hash += *data;
979 /* Force "avalanching" of final 127 bits */
990 /* ======================================================================== */
992 /* A hashing function that I believe was created by either Chris Torek or
995 uint32_t alphaNumHash (const char * s, int len) {
999 for (h = 0, i = 0; i < len; i++) {
1000 h = (h << 5) + (h * 5) + (unsigned char) s[i];
1005 uint32_t bernstein (const char * s, int len) {
1009 for (h = 0, i = 0; i < len; i++) {
1010 h = (h << 5) + h + (unsigned char) s[i];
1016 // found somewhere in the 2nd addition
1017 uint32_t stroustrup (const char * s, int len) {
1021 for (register int i=0; i < len; ++s, ++i)
1023 h = (h << 1) ^ (unsigned char) s[i];
1030 /* ======================================================================== */
1032 typedef uint32_t (* hashFn) (const char * s, int len);
1034 #define BUFF_SZ (128*2)
1035 #define NTESTS 5000000
1037 double test (hashFn hash) {
1038 static char buff[BUFF_SZ];
1042 for (buff[0]=0, i=1; i < BUFF_SZ; i++) buff[i] = (char) (i + buff[i-1]);
1045 for (i=0; i < NTESTS; i++) hash (buff, BUFF_SZ);
1047 return (c1 - c0)*(1.0 / (double)CLOCKS_PER_SEC);
1055 // { 0.0, "CRC32\t\t", GetCRC32 },
1056 // { 0.0, "oneAtATimeHash\t", oneAtATimeHash },
1057 // { 0.0, "alphaNumHash\t", alphaNumHash },
1058 { 0.0, "FNVHash\t\t", FNVHash },
1059 { 0.0, "bernstein\t", bernstein },
1060 { 0.0, "stroustrup\t", stroustrup },
1061 { 0.0, "hashLookup3\t", hashLookup3 },
1062 { 0.0, "hashLookup3Orig\t", hashLookup3Orig },
1063 { 0.0, "SuperFastHash\t", SuperFastHash },
1069 GenerateCRC32Table ();
1071 for (j=0; tests[j].name != NULL; j++) {
1072 for (i=0; i < 3; i++) {
1073 double res = test (tests[j].hash);
1074 if (tests[j].res == 0.0 || tests[j].res > res) tests[j].res = res;
1076 printf ("%s:%8.4fs\n", tests[j].name, tests[j].res);