48 #include <sys/param.h>
57 #if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \
58 __BYTE_ORDER == __LITTLE_ENDIAN) || \
59 (defined(i386) || defined(__i386__) || defined(__i486__) || \
60 defined(__i586__) || defined(__i686__) || defined(vax) || defined(MIPSEL))
61 # define HASH_LITTLE_ENDIAN 1
62 # define HASH_BIG_ENDIAN 0
63 #elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
64 __BYTE_ORDER == __BIG_ENDIAN) || \
65 (defined(sparc) || defined(POWERPC) || defined(mc68000) || defined(sel))
66 # define HASH_LITTLE_ENDIAN 0
67 # define HASH_BIG_ENDIAN 1
69 # define HASH_LITTLE_ENDIAN 0
70 # define HASH_BIG_ENDIAN 0
73 #define hashsize(n) ((uint32_t)1<<(n))
74 #define hashmask(n) (hashsize(n)-1)
75 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
124 a -= c; a ^= rot(c, 4); c += b; \
125 b -= a; b ^= rot(a, 6); a += c; \
126 c -= b; c ^= rot(b, 8); b += a; \
127 a -= c; a ^= rot(c,16); c += b; \
128 b -= a; b ^= rot(a,19); a += c; \
129 c -= b; c ^= rot(b, 4); b += a; \
158 #define final(a,b,c) \
160 c ^= b; c -= rot(b,14); \
161 a ^= c; a -= rot(c,11); \
162 b ^= a; b -= rot(a,25); \
163 c ^= b; c -= rot(b,16); \
164 a ^= c; a -= rot(c,4); \
165 b ^= a; b -= rot(a,14); \
166 c ^= b; c -= rot(b,24); \
189 a = b = c = 0xdeadbeef + (((uint32_t)length)<<2) + initval;
237 a = b = c = 0xdeadbeef + ((uint32_t)(length<<2)) + *pc;
298 uint32_t
hashlittle(
const void *key,
size_t length, uint32_t initval) {
306 a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
310 const uint32_t *k = (
const uint32_t *)key;
315 while (length > 12) {
391 k8 = (
const uint8_t *)k;
399 c+=((uint32_t)k8[10])<<16;
401 c+=((uint32_t)k8[9])<<8;
409 b+=((uint32_t)k8[6])<<16;
411 b+=((uint32_t)k8[5])<<8;
418 a+=((uint32_t)k8[2])<<16;
420 a+=((uint32_t)k8[1])<<8;
432 const uint16_t *k = (
const uint16_t *)key;
436 while (length > 12) {
437 a += k[0] + (((uint32_t)k[1])<<16);
438 b += k[2] + (((uint32_t)k[3])<<16);
439 c += k[4] + (((uint32_t)k[5])<<16);
446 k8 = (
const uint8_t *)k;
449 c+=k[4]+(((uint32_t)k[5])<<16);
450 b+=k[2]+(((uint32_t)k[3])<<16);
451 a+=k[0]+(((uint32_t)k[1])<<16);
454 c+=((uint32_t)k8[10])<<16;
458 b+=k[2]+(((uint32_t)k[3])<<16);
459 a+=k[0]+(((uint32_t)k[1])<<16);
465 b+=k[2]+(((uint32_t)k[3])<<16);
466 a+=k[0]+(((uint32_t)k[1])<<16);
469 b+=((uint32_t)k8[6])<<16;
473 a+=k[0]+(((uint32_t)k[1])<<16);
479 a+=k[0]+(((uint32_t)k[1])<<16);
482 a+=((uint32_t)k8[2])<<16;
495 const uint8_t *k = (
const uint8_t *)key;
498 while (length > 12) {
500 a += ((uint32_t)k[1])<<8;
501 a += ((uint32_t)k[2])<<16;
502 a += ((uint32_t)k[3])<<24;
504 b += ((uint32_t)k[5])<<8;
505 b += ((uint32_t)k[6])<<16;
506 b += ((uint32_t)k[7])<<24;
508 c += ((uint32_t)k[9])<<8;
509 c += ((uint32_t)k[10])<<16;
510 c += ((uint32_t)k[11])<<24;
519 c+=((uint32_t)k[11])<<24;
522 c+=((uint32_t)k[10])<<16;
525 c+=((uint32_t)k[9])<<8;
531 b+=((uint32_t)k[7])<<24;
534 b+=((uint32_t)k[6])<<16;
537 b+=((uint32_t)k[5])<<8;
543 a+=((uint32_t)k[3])<<24;
546 a+=((uint32_t)k[2])<<16;
549 a+=((uint32_t)k[1])<<8;
586 a = b = c = 0xdeadbeef + ((uint32_t)length) + *pc;
591 const uint32_t *k = (
const uint32_t *)key;
597 while (length > 12) {
675 k8 = (
const uint8_t *)k;
683 c+=((uint32_t)k8[10])<<16;
685 c+=((uint32_t)k8[9])<<8;
693 b+=((uint32_t)k8[6])<<16;
695 b+=((uint32_t)k8[5])<<8;
702 a+=((uint32_t)k8[2])<<16;
704 a+=((uint32_t)k8[1])<<8;
718 const uint16_t *k = (
const uint16_t *)key;
722 while (length > 12) {
723 a += k[0] + (((uint32_t)k[1])<<16);
724 b += k[2] + (((uint32_t)k[3])<<16);
725 c += k[4] + (((uint32_t)k[5])<<16);
732 k8 = (
const uint8_t *)k;
735 c+=k[4]+(((uint32_t)k[5])<<16);
736 b+=k[2]+(((uint32_t)k[3])<<16);
737 a+=k[0]+(((uint32_t)k[1])<<16);
740 c+=((uint32_t)k8[10])<<16;
744 b+=k[2]+(((uint32_t)k[3])<<16);
745 a+=k[0]+(((uint32_t)k[1])<<16);
751 b+=k[2]+(((uint32_t)k[3])<<16);
752 a+=k[0]+(((uint32_t)k[1])<<16);
755 b+=((uint32_t)k8[6])<<16;
759 a+=k[0]+(((uint32_t)k[1])<<16);
765 a+=k[0]+(((uint32_t)k[1])<<16);
768 a+=((uint32_t)k8[2])<<16;
783 const uint8_t *k = (
const uint8_t *)key;
786 while (length > 12) {
788 a += ((uint32_t)k[1])<<8;
789 a += ((uint32_t)k[2])<<16;
790 a += ((uint32_t)k[3])<<24;
792 b += ((uint32_t)k[5])<<8;
793 b += ((uint32_t)k[6])<<16;
794 b += ((uint32_t)k[7])<<24;
796 c += ((uint32_t)k[9])<<8;
797 c += ((uint32_t)k[10])<<16;
798 c += ((uint32_t)k[11])<<24;
807 c+=((uint32_t)k[11])<<24;
810 c+=((uint32_t)k[10])<<16;
813 c+=((uint32_t)k[9])<<8;
819 b+=((uint32_t)k[7])<<24;
822 b+=((uint32_t)k[6])<<16;
825 b+=((uint32_t)k[5])<<8;
831 a+=((uint32_t)k[3])<<24;
834 a+=((uint32_t)k[2])<<16;
837 a+=((uint32_t)k[1])<<8;
862 uint32_t
hashbig(
const void *key,
size_t length, uint32_t initval) {
870 a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
874 const uint32_t *k = (
const uint32_t *)key;
879 while (length > 12) {
955 k8 = (
const uint8_t *)k;
963 c+=((uint32_t)k8[10])<<8;
965 c+=((uint32_t)k8[9])<<16;
967 c+=((uint32_t)k8[8])<<24;
973 b+=((uint32_t)k8[6])<<8;
975 b+=((uint32_t)k8[5])<<16;
977 b+=((uint32_t)k8[4])<<24;
982 a+=((uint32_t)k8[2])<<8;
984 a+=((uint32_t)k8[1])<<16;
986 a+=((uint32_t)k8[0])<<24;
995 const uint8_t *k = (
const uint8_t *)key;
998 while (length > 12) {
999 a += ((uint32_t)k[0])<<24;
1000 a += ((uint32_t)k[1])<<16;
1001 a += ((uint32_t)k[2])<<8;
1002 a += ((uint32_t)k[3]);
1003 b += ((uint32_t)k[4])<<24;
1004 b += ((uint32_t)k[5])<<16;
1005 b += ((uint32_t)k[6])<<8;
1006 b += ((uint32_t)k[7]);
1007 c += ((uint32_t)k[8])<<24;
1008 c += ((uint32_t)k[9])<<16;
1009 c += ((uint32_t)k[10])<<8;
1010 c += ((uint32_t)k[11]);
1022 c+=((uint32_t)k[10])<<8;
1025 c+=((uint32_t)k[9])<<16;
1028 c+=((uint32_t)k[8])<<24;
1034 b+=((uint32_t)k[6])<<8;
1037 b+=((uint32_t)k[5])<<16;
1040 b+=((uint32_t)k[4])<<24;
1046 a+=((uint32_t)k[2])<<8;
1049 a+=((uint32_t)k[1])<<16;
1052 a+=((uint32_t)k[0])<<24;
1068 static void driver1() {
1075 for (i=0; i<256; ++i) {
1078 for (i=0; i<1; ++i) {
1083 printf(
"time %d %.8x\n", z-a, h);
1092 static void driver2() {
1093 uint8_t qa[MAXLEN+1], qb[MAXLEN+2], *a = &qa[0], *b = &qb[1];
1094 uint32_t c[HASHSTATE], d[HASHSTATE], i=0, j=0, k, l, m=0, z;
1095 uint32_t e[HASHSTATE],f[HASHSTATE],g[HASHSTATE],h[HASHSTATE];
1096 uint32_t x[HASHSTATE],y[HASHSTATE];
1099 printf(
"No more than %d trials should ever be needed \n",MAXPAIR/2);
1100 for (hlen=0; hlen < MAXLEN; ++hlen) {
1102 for (i=0; i<hlen; ++i) {
1103 for (j=0; j<8; ++j) {
1104 for (m=1; m<8; ++m) {
1105 for (l=0; l<HASHSTATE; ++l) {
1106 e[l]=f[l]=g[l]=h[l]=x[l]=y[l]=~((uint32_t)0);
1110 for (k=0; k<MAXPAIR; k+=2) {
1111 uint32_t finished=1;
1113 for (l=0; l<hlen+1; ++l) {
1114 a[l] = b[l] = (uint8_t)0;
1121 b[i] ^= ((k+1)>>(8-j));
1124 for (l=0; l<HASHSTATE; ++l) {
1125 e[l] &= (c[l]^d[l]);
1126 f[l] &= ~(c[l]^d[l]);
1131 if (e[l]|f[l]|g[l]|h[l]|x[l]|y[l]) {
1143 printf(
"Some bit didn't change: ");
1144 printf(
"%.8x %.8x %.8x %.8x %.8x %.8x ",
1145 e[0],f[0],g[0],h[0],x[0],y[0]);
1146 printf(
"i %d j %d m %d len %d\n", i, j, m, hlen);
1156 printf(
"Mix success %2d bytes %2d initvals ",i,m);
1157 printf(
"required %d trials\n", z/2);
1164 static void driver3() {
1165 uint8_t buf[MAXLEN+20], *b;
1167 uint8_t q[] =
"This is the time for all good men to come to the aid of their country...";
1169 uint8_t qq[] =
"xThis is the time for all good men to come to the aid of their country...";
1171 uint8_t qqq[] =
"xxThis is the time for all good men to come to the aid of their country...";
1173 uint8_t qqqq[] =
"xxxThis is the time for all good men to come to the aid of their country...";
1177 printf(
"Endianness. These lines should all be the same (for values filled in):\n");
1178 printf(
"%.8x %.8x %.8x\n",
1179 hashword((
const uint32_t *)q, (
sizeof(q)-1)/4, 13),
1180 hashword((
const uint32_t *)q, (
sizeof(q)-5)/4, 13),
1181 hashword((
const uint32_t *)q, (
sizeof(q)-9)/4, 13));
1183 printf(
"%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
1191 printf(
"%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
1199 printf(
"%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
1207 printf(
"%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
1221 printf(
"hashlittle2 and hashlittle mismatch\n");
1229 printf(
"hashword2 and hashword mismatch %x %x\n",
1233 for (h=0, b=buf+1; h<8; ++h, ++b) {
1234 for (i=0; i<MAXLEN; ++i) {
1236 for (j=0; j<i; ++j) {
1246 if ((ref != x) || (ref != y)) {
1247 printf(
"alignment error: %.8x %.8x %.8x %d %d\n",ref,x,y,
1255 static void driver4() {
1257 uint32_t h,i,state[HASHSTATE];
1261 for (i=0; i<HASHSTATE; ++i) {
1264 printf(
"These should all be different\n");
1265 for (i=0, h=0; i<8; ++i) {
1267 printf(
"%2ld 0-byte strings, hash is %.8x\n", i, h);
1271 static void driver5() {
1274 printf(
"hash is %.8lx %.8lx\n", c, b);
1276 printf(
"hash is %.8lx %.8lx\n", c, b);
1277 b=0xdeadbeef, c=0xdeadbeef,
hashlittle2(
"", 0, &c, &b);
1278 printf(
"hash is %.8lx %.8lx\n", c, b);
1279 b=0, c=0,
hashlittle2(
"Four score and seven years ago", 30, &c, &b);
1280 printf(
"hash is %.8lx %.8lx\n", c, b);
1281 b=1, c=0,
hashlittle2(
"Four score and seven years ago", 30, &c, &b);
1282 printf(
"hash is %.8lx %.8lx\n", c, b);
1283 b=0, c=1,
hashlittle2(
"Four score and seven years ago", 30, &c, &b);
1284 printf(
"hash is %.8lx %.8lx\n", c, b);
1285 c =
hashlittle(
"Four score and seven years ago", 30, 0);
1286 printf(
"hash is %.8lx\n", c);
1287 c =
hashlittle(
"Four score and seven years ago", 30, 1);
1288 printf(
"hash is %.8lx\n", c);
#define HASH_LITTLE_ENDIAN
uint32_t hashlittle(const void *key, size_t length, uint32_t initval)
hash a variable-length key into a 32-bit value (Little Endian)
void hashword2(const uint32_t *k, size_t length, uint32_t *pc, uint32_t *pb)
same as hashword(), but take two seeds and return two 32-bit values
uint32_t hashbig(const void *key, size_t length, uint32_t initval)
This is the same as hashword() on big-endian machines.
void hashlittle2(const void *key, size_t length, uint32_t *pc, uint32_t *pb)
return 2 32-bit hash values.
uint32_t hashword(const uint32_t *k, size_t length, uint32_t initval)
hash a variable-length key into a 32-bit value (Big Endian)
#define mix(a, b, c)
mix 3 32-bit values reversibly