41 #include <sys/param.h>
50 #if (defined(WORDS_BIGENDIAN))
51 # define HASH_LITTLE_ENDIAN 0
52 # define HASH_BIG_ENDIAN 1
53 #elif (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \
54 __BYTE_ORDER == __LITTLE_ENDIAN) || \
55 (defined(i386) || defined(__i386__) || defined(__i486__) || \
56 defined(__i586__) || defined(__i686__) || defined(vax) || defined(MIPSEL))
57 # define HASH_LITTLE_ENDIAN 1
58 # define HASH_BIG_ENDIAN 0
59 #elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
60 __BYTE_ORDER == __BIG_ENDIAN) || \
61 (defined(sparc) || defined(POWERPC) || defined(mc68000) || defined(sel))
62 # define HASH_LITTLE_ENDIAN 0
63 # define HASH_BIG_ENDIAN 1
65 # define HASH_LITTLE_ENDIAN 0
66 # define HASH_BIG_ENDIAN 0
69 #define hashsize(n) ((uint32_t)1<<(n))
70 #define hashmask(n) (hashsize(n)-1)
71 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
74 static uint32_t hashword(
const uint32_t *k,
size_t length, uint32_t initval);
75 static void hashword2 (
const uint32_t *k,
size_t length, uint32_t *pc, uint32_t *pb);
76 static uint32_t hashlittle(
const void *key,
size_t length, uint32_t initval);
77 static uint32_t hashbig(
const void *key,
size_t length, uint32_t initval);
80 void hashlittle2(
const void *key,
size_t length, uint32_t *pc, uint32_t *pb);
128 a -= c; a ^= rot(c, 4); c += b; \
129 b -= a; b ^= rot(a, 6); a += c; \
130 c -= b; c ^= rot(b, 8); b += a; \
131 a -= c; a ^= rot(c,16); c += b; \
132 b -= a; b ^= rot(a,19); a += c; \
133 c -= b; c ^= rot(b, 4); b += a; \
161 #define final(a,b,c) \
163 c ^= b; c -= rot(b,14); \
164 a ^= c; a -= rot(c,11); \
165 b ^= a; b -= rot(a,25); \
166 c ^= b; c -= rot(b,16); \
167 a ^= c; a -= rot(c,4); \
168 b ^= a; b -= rot(a,14); \
169 c ^= b; c -= rot(b,24); \
186 static uint32_t hashword(
194 a = b = c = 0xdeadbeef + (((uint32_t)length)<<2) + initval;
231 static void hashword2 (
240 a = b = c = 0xdeadbeef + ((uint32_t)(length<<2)) + *pc;
296 static uint32_t hashlittle(
const void *key,
size_t length, uint32_t initval)
299 union {
const void *ptr;
size_t i; } u;
302 a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
306 const uint32_t *k = (
const uint32_t *)key;
334 case 12: c+=k[2]; b+=k[1]; a+=k[0];
break;
335 case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0];
break;
336 case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0];
break;
337 case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0];
break;
338 case 8 : b+=k[1]; a+=k[0];
break;
339 case 7 : b+=k[1]&0xffffff; a+=k[0];
break;
340 case 6 : b+=k[1]&0xffff; a+=k[0];
break;
341 case 5 : b+=k[1]&0xff; a+=k[0];
break;
342 case 4 : a+=k[0];
break;
343 case 3 : a+=k[0]&0xffffff;
break;
344 case 2 : a+=k[0]&0xffff;
break;
345 case 1 : a+=k[0]&0xff;
break;
351 k8 = (
const uint8_t *)k;
354 case 12: c+=k[2]; b+=k[1]; a+=k[0];
break;
355 case 11: c+=((uint32_t)k8[10])<<16;
356 case 10: c+=((uint32_t)k8[9])<<8;
358 case 8 : b+=k[1]; a+=k[0];
break;
359 case 7 : b+=((uint32_t)k8[6])<<16;
360 case 6 : b+=((uint32_t)k8[5])<<8;
362 case 4 : a+=k[0];
break;
363 case 3 : a+=((uint32_t)k8[2])<<16;
364 case 2 : a+=((uint32_t)k8[1])<<8;
365 case 1 : a+=k8[0];
break;
372 const uint16_t *k = (
const uint16_t *)key;
378 a += k[0] + (((uint32_t)k[1])<<16);
379 b += k[2] + (((uint32_t)k[3])<<16);
380 c += k[4] + (((uint32_t)k[5])<<16);
387 k8 = (
const uint8_t *)k;
390 case 12: c+=k[4]+(((uint32_t)k[5])<<16);
391 b+=k[2]+(((uint32_t)k[3])<<16);
392 a+=k[0]+(((uint32_t)k[1])<<16);
394 case 11: c+=((uint32_t)k8[10])<<16;
396 b+=k[2]+(((uint32_t)k[3])<<16);
397 a+=k[0]+(((uint32_t)k[1])<<16);
400 case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
401 a+=k[0]+(((uint32_t)k[1])<<16);
403 case 7 : b+=((uint32_t)k8[6])<<16;
405 a+=k[0]+(((uint32_t)k[1])<<16);
408 case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
410 case 3 : a+=((uint32_t)k8[2])<<16;
419 const uint8_t *k = (
const uint8_t *)key;
425 a += ((uint32_t)k[1])<<8;
426 a += ((uint32_t)k[2])<<16;
427 a += ((uint32_t)k[3])<<24;
429 b += ((uint32_t)k[5])<<8;
430 b += ((uint32_t)k[6])<<16;
431 b += ((uint32_t)k[7])<<24;
433 c += ((uint32_t)k[9])<<8;
434 c += ((uint32_t)k[10])<<16;
435 c += ((uint32_t)k[11])<<24;
444 case 12: c+=((uint32_t)k[11])<<24;
445 case 11: c+=((uint32_t)k[10])<<16;
446 case 10: c+=((uint32_t)k[9])<<8;
448 case 8 : b+=((uint32_t)k[7])<<24;
449 case 7 : b+=((uint32_t)k[6])<<16;
450 case 6 : b+=((uint32_t)k[5])<<8;
452 case 4 : a+=((uint32_t)k[3])<<24;
453 case 3 : a+=((uint32_t)k[2])<<16;
454 case 2 : a+=((uint32_t)k[1])<<8;
483 union {
const void *ptr;
size_t i; } u;
486 a = b = c = 0xdeadbeef + ((uint32_t)length) + *pc;
491 const uint32_t *k = (
const uint32_t *)key;
519 case 12: c+=k[2]; b+=k[1]; a+=k[0];
break;
520 case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0];
break;
521 case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0];
break;
522 case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0];
break;
523 case 8 : b+=k[1]; a+=k[0];
break;
524 case 7 : b+=k[1]&0xffffff; a+=k[0];
break;
525 case 6 : b+=k[1]&0xffff; a+=k[0];
break;
526 case 5 : b+=k[1]&0xff; a+=k[0];
break;
527 case 4 : a+=k[0];
break;
528 case 3 : a+=k[0]&0xffffff;
break;
529 case 2 : a+=k[0]&0xffff;
break;
530 case 1 : a+=k[0]&0xff;
break;
531 case 0 : *pc=c; *pb=b;
return;
536 k8 = (
const uint8_t *)k;
539 case 12: c+=k[2]; b+=k[1]; a+=k[0];
break;
540 case 11: c+=((uint32_t)k8[10])<<16;
541 case 10: c+=((uint32_t)k8[9])<<8;
543 case 8 : b+=k[1]; a+=k[0];
break;
544 case 7 : b+=((uint32_t)k8[6])<<16;
545 case 6 : b+=((uint32_t)k8[5])<<8;
547 case 4 : a+=k[0];
break;
548 case 3 : a+=((uint32_t)k8[2])<<16;
549 case 2 : a+=((uint32_t)k8[1])<<8;
550 case 1 : a+=k8[0];
break;
551 case 0 : *pc=c; *pb=b;
return;
557 const uint16_t *k = (
const uint16_t *)key;
563 a += k[0] + (((uint32_t)k[1])<<16);
564 b += k[2] + (((uint32_t)k[3])<<16);
565 c += k[4] + (((uint32_t)k[5])<<16);
572 k8 = (
const uint8_t *)k;
575 case 12: c+=k[4]+(((uint32_t)k[5])<<16);
576 b+=k[2]+(((uint32_t)k[3])<<16);
577 a+=k[0]+(((uint32_t)k[1])<<16);
579 case 11: c+=((uint32_t)k8[10])<<16;
581 b+=k[2]+(((uint32_t)k[3])<<16);
582 a+=k[0]+(((uint32_t)k[1])<<16);
585 case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
586 a+=k[0]+(((uint32_t)k[1])<<16);
588 case 7 : b+=((uint32_t)k8[6])<<16;
590 a+=k[0]+(((uint32_t)k[1])<<16);
593 case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
595 case 3 : a+=((uint32_t)k8[2])<<16;
600 case 0 : *pc=c; *pb=b;
return;
604 const uint8_t *k = (
const uint8_t *)key;
610 a += ((uint32_t)k[1])<<8;
611 a += ((uint32_t)k[2])<<16;
612 a += ((uint32_t)k[3])<<24;
614 b += ((uint32_t)k[5])<<8;
615 b += ((uint32_t)k[6])<<16;
616 b += ((uint32_t)k[7])<<24;
618 c += ((uint32_t)k[9])<<8;
619 c += ((uint32_t)k[10])<<16;
620 c += ((uint32_t)k[11])<<24;
629 case 12: c+=((uint32_t)k[11])<<24;
630 case 11: c+=((uint32_t)k[10])<<16;
631 case 10: c+=((uint32_t)k[9])<<8;
633 case 8 : b+=((uint32_t)k[7])<<24;
634 case 7 : b+=((uint32_t)k[6])<<16;
635 case 6 : b+=((uint32_t)k[5])<<8;
637 case 4 : a+=((uint32_t)k[3])<<24;
638 case 3 : a+=((uint32_t)k[2])<<16;
639 case 2 : a+=((uint32_t)k[1])<<8;
642 case 0 : *pc=c; *pb=b;
return;
658 static uint32_t hashbig(
const void *key,
size_t length, uint32_t initval)
661 union {
const void *ptr;
size_t i; } u;
664 a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
668 const uint32_t *k = (
const uint32_t *)key;
696 case 12: c+=k[2]; b+=k[1]; a+=k[0];
break;
697 case 11: c+=k[2]&0xffffff00; b+=k[1]; a+=k[0];
break;
698 case 10: c+=k[2]&0xffff0000; b+=k[1]; a+=k[0];
break;
699 case 9 : c+=k[2]&0xff000000; b+=k[1]; a+=k[0];
break;
700 case 8 : b+=k[1]; a+=k[0];
break;
701 case 7 : b+=k[1]&0xffffff00; a+=k[0];
break;
702 case 6 : b+=k[1]&0xffff0000; a+=k[0];
break;
703 case 5 : b+=k[1]&0xff000000; a+=k[0];
break;
704 case 4 : a+=k[0];
break;
705 case 3 : a+=k[0]&0xffffff00;
break;
706 case 2 : a+=k[0]&0xffff0000;
break;
707 case 1 : a+=k[0]&0xff000000;
break;
713 k8 = (
const uint8_t *)k;
716 case 12: c+=k[2]; b+=k[1]; a+=k[0];
break;
717 case 11: c+=((uint32_t)k8[10])<<8;
718 case 10: c+=((uint32_t)k8[9])<<16;
719 case 9 : c+=((uint32_t)k8[8])<<24;
720 case 8 : b+=k[1]; a+=k[0];
break;
721 case 7 : b+=((uint32_t)k8[6])<<8;
722 case 6 : b+=((uint32_t)k8[5])<<16;
723 case 5 : b+=((uint32_t)k8[4])<<24;
724 case 4 : a+=k[0];
break;
725 case 3 : a+=((uint32_t)k8[2])<<8;
726 case 2 : a+=((uint32_t)k8[1])<<16;
727 case 1 : a+=((uint32_t)k8[0])<<24;
break;
734 const uint8_t *k = (
const uint8_t *)key;
739 a += ((uint32_t)k[0])<<24;
740 a += ((uint32_t)k[1])<<16;
741 a += ((uint32_t)k[2])<<8;
742 a += ((uint32_t)k[3]);
743 b += ((uint32_t)k[4])<<24;
744 b += ((uint32_t)k[5])<<16;
745 b += ((uint32_t)k[6])<<8;
746 b += ((uint32_t)k[7]);
747 c += ((uint32_t)k[8])<<24;
748 c += ((uint32_t)k[9])<<16;
749 c += ((uint32_t)k[10])<<8;
750 c += ((uint32_t)k[11]);
760 case 11: c+=((uint32_t)k[10])<<8;
761 case 10: c+=((uint32_t)k[9])<<16;
762 case 9 : c+=((uint32_t)k[8])<<24;
764 case 7 : b+=((uint32_t)k[6])<<8;
765 case 6 : b+=((uint32_t)k[5])<<16;
766 case 5 : b+=((uint32_t)k[4])<<24;
768 case 3 : a+=((uint32_t)k[2])<<8;
769 case 2 : a+=((uint32_t)k[1])<<16;
770 case 1 : a+=((uint32_t)k[0])<<24;
void hashlittle2(const void *key, size_t length, uint32_t *pc, uint32_t *pb)
#define HASH_LITTLE_ENDIAN