41 #include <sys/param.h>
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
63 # define HASH_LITTLE_ENDIAN 0
64 # define HASH_BIG_ENDIAN 0
67 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
115 a -= c; a ^= rot(c, 4); c += b; \
116 b -= a; b ^= rot(a, 6); a += c; \
117 c -= b; c ^= rot(b, 8); b += a; \
118 a -= c; a ^= rot(c,16); c += b; \
119 b -= a; b ^= rot(a,19); a += c; \
120 c -= b; c ^= rot(b, 4); b += a; \
148 #define final(a,b,c) \
150 c ^= b; c -= rot(b,14); \
151 a ^= c; a -= rot(c,11); \
152 b ^= a; b -= rot(a,25); \
153 c ^= b; c -= rot(b,16); \
154 a ^= c; a -= rot(c,4); \
155 b ^= a; b -= rot(a,14); \
156 c ^= b; c -= rot(b,24); \
180 a = b = c = 0xdeadbeef + (((uint32_t)length)<<2) + initval;
225 a = b = c = 0xdeadbeef + ((uint32_t)(length<<2)) + *pc;
281 uint32_t
hashlittle(
const void *key,
size_t length, uint32_t initval)
284 union {
const void *ptr;
size_t i; } u;
287 a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
291 const uint32_t *k = (
const uint32_t *)key;
318 case 12: c+=k[2]; b+=k[1]; a+=k[0];
break;
319 case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0];
break;
320 case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0];
break;
321 case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0];
break;
322 case 8 : b+=k[1]; a+=k[0];
break;
323 case 7 : b+=k[1]&0xffffff; a+=k[0];
break;
324 case 6 : b+=k[1]&0xffff; a+=k[0];
break;
325 case 5 : b+=k[1]&0xff; a+=k[0];
break;
326 case 4 : a+=k[0];
break;
327 case 3 : a+=k[0]&0xffffff;
break;
328 case 2 : a+=k[0]&0xffff;
break;
329 case 1 : a+=k[0]&0xff;
break;
335 const uint8_t *k8 = (
const uint8_t *)k;
339 case 12: c+=k[2]; b+=k[1]; a+=k[0];
break;
340 case 11: c+=((uint32_t)k8[10])<<16;
341 case 10: c+=((uint32_t)k8[9])<<8;
343 case 8 : b+=k[1]; a+=k[0];
break;
344 case 7 : b+=((uint32_t)k8[6])<<16;
345 case 6 : b+=((uint32_t)k8[5])<<8;
347 case 4 : a+=k[0];
break;
348 case 3 : a+=((uint32_t)k8[2])<<16;
349 case 2 : a+=((uint32_t)k8[1])<<8;
350 case 1 : a+=k8[0];
break;
357 const uint16_t *k = (
const uint16_t *)key;
363 a += k[0] + (((uint32_t)k[1])<<16);
364 b += k[2] + (((uint32_t)k[3])<<16);
365 c += k[4] + (((uint32_t)k[5])<<16);
372 k8 = (
const uint8_t *)k;
375 case 12: c+=k[4]+(((uint32_t)k[5])<<16);
376 b+=k[2]+(((uint32_t)k[3])<<16);
377 a+=k[0]+(((uint32_t)k[1])<<16);
379 case 11: c+=((uint32_t)k8[10])<<16;
381 b+=k[2]+(((uint32_t)k[3])<<16);
382 a+=k[0]+(((uint32_t)k[1])<<16);
385 case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
386 a+=k[0]+(((uint32_t)k[1])<<16);
388 case 7 : b+=((uint32_t)k8[6])<<16;
390 a+=k[0]+(((uint32_t)k[1])<<16);
393 case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
395 case 3 : a+=((uint32_t)k8[2])<<16;
404 const uint8_t *k = (
const uint8_t *)key;
410 a += ((uint32_t)k[1])<<8;
411 a += ((uint32_t)k[2])<<16;
412 a += ((uint32_t)k[3])<<24;
414 b += ((uint32_t)k[5])<<8;
415 b += ((uint32_t)k[6])<<16;
416 b += ((uint32_t)k[7])<<24;
418 c += ((uint32_t)k[9])<<8;
419 c += ((uint32_t)k[10])<<16;
420 c += ((uint32_t)k[11])<<24;
429 case 12: c+=((uint32_t)k[11])<<24;
430 case 11: c+=((uint32_t)k[10])<<16;
431 case 10: c+=((uint32_t)k[9])<<8;
433 case 8 : b+=((uint32_t)k[7])<<24;
434 case 7 : b+=((uint32_t)k[6])<<16;
435 case 6 : b+=((uint32_t)k[5])<<8;
437 case 4 : a+=((uint32_t)k[3])<<24;
438 case 3 : a+=((uint32_t)k[2])<<16;
439 case 2 : a+=((uint32_t)k[1])<<8;
485 union {
const void *ptr;
size_t i; } u;
488 a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
492 const uint32_t *k = (
const uint32_t *)key;
512 const uint8_t *k8 = (
const uint8_t *)k;
516 case 12: c+=k[2]; b+=k[1]; a+=k[0];
break;
517 case 11: c+=((uint32_t)k8[10])<<16;
518 case 10: c+=((uint32_t)k8[9])<<8;
520 case 8 : b+=k[1]; a+=k[0];
break;
521 case 7 : b+=((uint32_t)k8[6])<<16;
522 case 6 : b+=((uint32_t)k8[5])<<8;
524 case 4 : a+=k[0];
break;
525 case 3 : a+=((uint32_t)k8[2])<<16;
526 case 2 : a+=((uint32_t)k8[1])<<8;
527 case 1 : a+=k8[0];
break;
531 const uint16_t *k = (
const uint16_t *)key;
537 a += k[0] + (((uint32_t)k[1])<<16);
538 b += k[2] + (((uint32_t)k[3])<<16);
539 c += k[4] + (((uint32_t)k[5])<<16);
546 k8 = (
const uint8_t *)k;
549 case 12: c+=k[4]+(((uint32_t)k[5])<<16);
550 b+=k[2]+(((uint32_t)k[3])<<16);
551 a+=k[0]+(((uint32_t)k[1])<<16);
553 case 11: c+=((uint32_t)k8[10])<<16;
555 b+=k[2]+(((uint32_t)k[3])<<16);
556 a+=k[0]+(((uint32_t)k[1])<<16);
559 case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
560 a+=k[0]+(((uint32_t)k[1])<<16);
562 case 7 : b+=((uint32_t)k8[6])<<16;
564 a+=k[0]+(((uint32_t)k[1])<<16);
567 case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
569 case 3 : a+=((uint32_t)k8[2])<<16;
578 const uint8_t *k = (
const uint8_t *)key;
584 a += ((uint32_t)k[1])<<8;
585 a += ((uint32_t)k[2])<<16;
586 a += ((uint32_t)k[3])<<24;
588 b += ((uint32_t)k[5])<<8;
589 b += ((uint32_t)k[6])<<16;
590 b += ((uint32_t)k[7])<<24;
592 c += ((uint32_t)k[9])<<8;
593 c += ((uint32_t)k[10])<<16;
594 c += ((uint32_t)k[11])<<24;
603 case 12: c+=((uint32_t)k[11])<<24;
604 case 11: c+=((uint32_t)k[10])<<16;
605 case 10: c+=((uint32_t)k[9])<<8;
607 case 8 : b+=((uint32_t)k[7])<<24;
608 case 7 : b+=((uint32_t)k[6])<<16;
609 case 6 : b+=((uint32_t)k[5])<<8;
611 case 4 : a+=((uint32_t)k[3])<<24;
612 case 3 : a+=((uint32_t)k[2])<<16;
613 case 2 : a+=((uint32_t)k[1])<<8;
642 union {
const void *ptr;
size_t i; } u;
645 a = b = c = 0xdeadbeef + ((uint32_t)length) + *pc;
650 const uint32_t *k = (
const uint32_t *)key;
677 case 12: c+=k[2]; b+=k[1]; a+=k[0];
break;
678 case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0];
break;
679 case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0];
break;
680 case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0];
break;
681 case 8 : b+=k[1]; a+=k[0];
break;
682 case 7 : b+=k[1]&0xffffff; a+=k[0];
break;
683 case 6 : b+=k[1]&0xffff; a+=k[0];
break;
684 case 5 : b+=k[1]&0xff; a+=k[0];
break;
685 case 4 : a+=k[0];
break;
686 case 3 : a+=k[0]&0xffffff;
break;
687 case 2 : a+=k[0]&0xffff;
break;
688 case 1 : a+=k[0]&0xff;
break;
689 case 0 : *pc=c; *pb=b;
return;
694 const uint8_t *k8 = (
const uint8_t *)k;
697 case 12: c+=k[2]; b+=k[1]; a+=k[0];
break;
698 case 11: c+=((uint32_t)k8[10])<<16;
699 case 10: c+=((uint32_t)k8[9])<<8;
701 case 8 : b+=k[1]; a+=k[0];
break;
702 case 7 : b+=((uint32_t)k8[6])<<16;
703 case 6 : b+=((uint32_t)k8[5])<<8;
705 case 4 : a+=k[0];
break;
706 case 3 : a+=((uint32_t)k8[2])<<16;
707 case 2 : a+=((uint32_t)k8[1])<<8;
708 case 1 : a+=k8[0];
break;
709 case 0 : *pc=c; *pb=b;
return;
715 const uint16_t *k = (
const uint16_t *)key;
721 a += k[0] + (((uint32_t)k[1])<<16);
722 b += k[2] + (((uint32_t)k[3])<<16);
723 c += k[4] + (((uint32_t)k[5])<<16);
730 k8 = (
const uint8_t *)k;
733 case 12: c+=k[4]+(((uint32_t)k[5])<<16);
734 b+=k[2]+(((uint32_t)k[3])<<16);
735 a+=k[0]+(((uint32_t)k[1])<<16);
737 case 11: c+=((uint32_t)k8[10])<<16;
739 b+=k[2]+(((uint32_t)k[3])<<16);
740 a+=k[0]+(((uint32_t)k[1])<<16);
743 case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
744 a+=k[0]+(((uint32_t)k[1])<<16);
746 case 7 : b+=((uint32_t)k8[6])<<16;
748 a+=k[0]+(((uint32_t)k[1])<<16);
751 case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
753 case 3 : a+=((uint32_t)k8[2])<<16;
758 case 0 : *pc=c; *pb=b;
return;
762 const uint8_t *k = (
const uint8_t *)key;
768 a += ((uint32_t)k[1])<<8;
769 a += ((uint32_t)k[2])<<16;
770 a += ((uint32_t)k[3])<<24;
772 b += ((uint32_t)k[5])<<8;
773 b += ((uint32_t)k[6])<<16;
774 b += ((uint32_t)k[7])<<24;
776 c += ((uint32_t)k[9])<<8;
777 c += ((uint32_t)k[10])<<16;
778 c += ((uint32_t)k[11])<<24;
787 case 12: c+=((uint32_t)k[11])<<24;
788 case 11: c+=((uint32_t)k[10])<<16;
789 case 10: c+=((uint32_t)k[9])<<8;
791 case 8 : b+=((uint32_t)k[7])<<24;
792 case 7 : b+=((uint32_t)k[6])<<16;
793 case 6 : b+=((uint32_t)k[5])<<8;
795 case 4 : a+=((uint32_t)k[3])<<24;
796 case 3 : a+=((uint32_t)k[2])<<16;
797 case 2 : a+=((uint32_t)k[1])<<8;
800 case 0 : *pc=c; *pb=b;
return;
816 uint32_t
hashbig(
const void *key,
size_t length, uint32_t initval)
819 union {
const void *ptr;
size_t i; } u;
822 a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
826 const uint32_t *k = (
const uint32_t *)key;
853 case 12: c+=k[2]; b+=k[1]; a+=k[0];
break;
854 case 11: c+=k[2]&0xffffff00; b+=k[1]; a+=k[0];
break;
855 case 10: c+=k[2]&0xffff0000; b+=k[1]; a+=k[0];
break;
856 case 9 : c+=k[2]&0xff000000; b+=k[1]; a+=k[0];
break;
857 case 8 : b+=k[1]; a+=k[0];
break;
858 case 7 : b+=k[1]&0xffffff00; a+=k[0];
break;
859 case 6 : b+=k[1]&0xffff0000; a+=k[0];
break;
860 case 5 : b+=k[1]&0xff000000; a+=k[0];
break;
861 case 4 : a+=k[0];
break;
862 case 3 : a+=k[0]&0xffffff00;
break;
863 case 2 : a+=k[0]&0xffff0000;
break;
864 case 1 : a+=k[0]&0xff000000;
break;
870 const uint8_t *k8 = (
const uint8_t *)k;
873 case 12: c+=k[2]; b+=k[1]; a+=k[0];
break;
874 case 11: c+=((uint32_t)k8[10])<<8;
875 case 10: c+=((uint32_t)k8[9])<<16;
876 case 9 : c+=((uint32_t)k8[8])<<24;
877 case 8 : b+=k[1]; a+=k[0];
break;
878 case 7 : b+=((uint32_t)k8[6])<<8;
879 case 6 : b+=((uint32_t)k8[5])<<16;
880 case 5 : b+=((uint32_t)k8[4])<<24;
881 case 4 : a+=k[0];
break;
882 case 3 : a+=((uint32_t)k8[2])<<8;
883 case 2 : a+=((uint32_t)k8[1])<<16;
884 case 1 : a+=((uint32_t)k8[0])<<24;
break;
891 const uint8_t *k = (
const uint8_t *)key;
896 a += ((uint32_t)k[0])<<24;
897 a += ((uint32_t)k[1])<<16;
898 a += ((uint32_t)k[2])<<8;
899 a += ((uint32_t)k[3]);
900 b += ((uint32_t)k[4])<<24;
901 b += ((uint32_t)k[5])<<16;
902 b += ((uint32_t)k[6])<<8;
903 b += ((uint32_t)k[7]);
904 c += ((uint32_t)k[8])<<24;
905 c += ((uint32_t)k[9])<<16;
906 c += ((uint32_t)k[10])<<8;
907 c += ((uint32_t)k[11]);
917 case 11: c+=((uint32_t)k[10])<<8;
918 case 10: c+=((uint32_t)k[9])<<16;
919 case 9 : c+=((uint32_t)k[8])<<24;
921 case 7 : b+=((uint32_t)k[6])<<8;
922 case 6 : b+=((uint32_t)k[5])<<16;
923 case 5 : b+=((uint32_t)k[4])<<24;
925 case 3 : a+=((uint32_t)k[2])<<8;
926 case 2 : a+=((uint32_t)k[1])<<16;
927 case 1 : a+=((uint32_t)k[0])<<24;
949 for (i=0; i<256; ++i) buf[i] =
'x';
955 if (z-a > 0) printf(
"time %d %.8x\n", z-a, h);
965 uint8_t qa[MAXLEN+1], qb[MAXLEN+2], *a = &qa[0], *b = &qb[1];
966 uint32_t c[HASHSTATE], d[HASHSTATE], i=0, j=0, k, l,
m=0, z;
967 uint32_t e[HASHSTATE],f[HASHSTATE],g[HASHSTATE],h[HASHSTATE];
968 uint32_t x[HASHSTATE],y[HASHSTATE];
971 printf(
"No more than %d trials should ever be needed \n",MAXPAIR/2);
972 for (hlen=0; hlen < MAXLEN; ++hlen)
975 for (i=0; i<hlen; ++i)
979 for (
m = 1;
m < 8; ++
m)
981 for (l = 0; l < HASHSTATE; ++l)
982 e[l] = f[l] = g[l] = h[l] = x[l] = y[l] = ~((uint32_t)0);
985 for (k = 0; k < MAXPAIR; k += 2) {
986 uint32_t finished = 1;
988 for (l = 0; l < hlen + 1; ++l) {
989 a[l] = b[l] = (uint8_t)0;
993 a[i] ^= (k >> (8 - j));
995 b[i] ^= ((k + 1) << j);
996 b[i] ^= ((k + 1) >> (8 - j));
999 for (l = 0; l < HASHSTATE; ++l) {
1000 e[l] &= (c[l] ^ d[l]);
1001 f[l] &= ~(c[l] ^ d[l]);
1006 if (e[l] | f[l] | g[l] | h[l] | x[l] | y[l])
1015 printf(
"Some bit didn't change: ");
1016 printf(
"%.8x %.8x %.8x %.8x %.8x %.8x ", e[0], f[0], g[0], h[0], x[0], y[0]);
1017 printf(
"i %d j %d m %d len %d\n", i, j,
m, hlen);
1027 printf(
"Mix success %2d bytes %2d initvals ",i,
m);
1028 printf(
"required %d trials\n", z/2);
1037 uint8_t buf[MAXLEN+20], *b;
1039 uint8_t q[] =
"This is the time for all good men to come to the aid of their country...";
1041 uint8_t qq[] =
"xThis is the time for all good men to come to the aid of their country...";
1043 uint8_t qqq[] =
"xxThis is the time for all good men to come to the aid of their country...";
1045 uint8_t qqqq[] =
"xxxThis is the time for all good men to come to the aid of their country...";
1049 printf(
"Endianness. These lines should all be the same (for values filled in):\n");
1050 printf(
"%.8x %.8x %.8x\n",
1051 hashword((
const uint32_t *)q, (
sizeof(q)-1)/4, 13),
1052 hashword((
const uint32_t *)q, (
sizeof(q)-5)/4, 13),
1053 hashword((
const uint32_t *)q, (
sizeof(q)-9)/4, 13));
1055 printf(
"%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
1063 printf(
"%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
1071 printf(
"%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
1079 printf(
"%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
1092 printf(
"hashlittle2 and hashlittle mismatch\n");
1099 printf(
"hashword2 and hashword mismatch %x %x\n",
1103 for (h=0, b=buf+1; h<8; ++h, ++b)
1105 for (i=0; i<MAXLEN; ++i)
1108 for (j=0; j<i; ++j) *(b+j)=0;
1116 if ((ref != x) || (ref != y))
1118 printf(
"alignment error: %.8x %.8x %.8x %d %d\n",ref,x,y,
1129 uint32_t h,i,state[HASHSTATE];
1133 for (i=0; i<HASHSTATE; ++i) state[i] = 1;
1134 printf(
"These should all be different\n");
1135 for (i=0, h=0; i<8; ++i)
1138 printf(
"%2ld 0-byte strings, hash is %.8x\n", i, h);
1146 printf(
"hash is %.8lx %.8lx\n", c, b);
1148 printf(
"hash is %.8lx %.8lx\n", c, b);
1149 b=0xdeadbeef, c=0xdeadbeef,
hashlittle2(
"", 0, &c, &b);
1150 printf(
"hash is %.8lx %.8lx\n", c, b);
1151 b=0, c=0,
hashlittle2(
"Four score and seven years ago", 30, &c, &b);
1152 printf(
"hash is %.8lx %.8lx\n", c, b);
1153 b=1, c=0,
hashlittle2(
"Four score and seven years ago", 30, &c, &b);
1154 printf(
"hash is %.8lx %.8lx\n", c, b);
1155 b=0, c=1,
hashlittle2(
"Four score and seven years ago", 30, &c, &b);
1156 printf(
"hash is %.8lx %.8lx\n", c, b);
1157 c =
hashlittle(
"Four score and seven years ago", 30, 0);
1158 printf(
"hash is %.8lx\n", c);
1159 c =
hashlittle(
"Four score and seven years ago", 30, 1);
1160 printf(
"hash is %.8lx\n", c);