47 #ifdef WORDS_BIGENDIAN 48 # define HASH_LITTLE_ENDIAN 0 49 # define HASH_BIG_ENDIAN 1 51 # define HASH_LITTLE_ENDIAN 1 52 # define HASH_BIG_ENDIAN 0 55 #define hashsize(n) ((uint32_t)1<<(n)) 56 #define hashmask(n) (hashsize(n)-1) 57 #define rot(x,k) (((x)<<(k)) ^ ((x)>>(32-(k)))) 105 a -= c; a ^= rot(c, 4); c += b; \ 106 b -= a; b ^= rot(a, 6); a += c; \ 107 c -= b; c ^= rot(b, 8); b += a; \ 108 a -= c; a ^= rot(c,16); c += b; \ 109 b -= a; b ^= rot(a,19); a += c; \ 110 c -= b; c ^= rot(b, 4); b += a; \ 138 #define final(a,b,c) \ 140 c ^= b; c -= rot(b,14); \ 141 a ^= c; a -= rot(c,11); \ 142 b ^= a; b -= rot(a,25); \ 143 c ^= b; c -= rot(b,16); \ 144 a ^= c; a -= rot(c,4); \ 145 b ^= a; b -= rot(a,14); \ 146 c ^= b; c -= rot(b,24); \ 170 a = b = c = 0xdeadbeef + (((uint32_t)length)<<2) + initval;
225 uint32_t hashlittle(
const void *key,
size_t length, uint32_t initval)
228 union {
const void *ptr;
size_t i; } u;
231 a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
234 if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
235 const uint32_t *k =(uint32_t*) key;
265 case 12: c+=k[2]; b+=k[1]; a+=k[0];
break;
266 case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0];
break;
267 case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0];
break;
268 case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0];
break;
269 case 8 : b+=k[1]; a+=k[0];
break;
270 case 7 : b+=k[1]&0xffffff; a+=k[0];
break;
271 case 6 : b+=k[1]&0xffff; a+=k[0];
break;
272 case 5 : b+=k[1]&0xff; a+=k[0];
break;
273 case 4 : a+=k[0];
break;
274 case 3 : a+=k[0]&0xffffff;
break;
275 case 2 : a+=k[0]&0xffff;
break;
276 case 1 : a+=k[0]&0xff;
break;
282 k8 = (
const uint8_t *)k;
285 case 12: c+=k[2]; b+=k[1]; a+=k[0];
break;
286 case 11: c+=((uint32_t)k8[10])<<16;
287 case 10: c+=((uint32_t)k8[9])<<8;
289 case 8 : b+=k[1]; a+=k[0];
break;
290 case 7 : b+=((uint32_t)k8[6])<<16;
291 case 6 : b+=((uint32_t)k8[5])<<8;
293 case 4 : a+=k[0];
break;
294 case 3 : a+=((uint32_t)k8[2])<<16;
295 case 2 : a+=((uint32_t)k8[1])<<8;
296 case 1 : a+=k8[0];
break;
302 }
else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
303 const uint16_t *k = (uint16_t*)key;
309 a += k[0] + (((uint32_t)k[1])<<16);
310 b += k[2] + (((uint32_t)k[3])<<16);
311 c += k[4] + (((uint32_t)k[5])<<16);
318 k8 = (
const uint8_t *)k;
321 case 12: c+=k[4]+(((uint32_t)k[5])<<16);
322 b+=k[2]+(((uint32_t)k[3])<<16);
323 a+=k[0]+(((uint32_t)k[1])<<16);
325 case 11: c+=((uint32_t)k8[10])<<16;
327 b+=k[2]+(((uint32_t)k[3])<<16);
328 a+=k[0]+(((uint32_t)k[1])<<16);
331 case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
332 a+=k[0]+(((uint32_t)k[1])<<16);
334 case 7 : b+=((uint32_t)k8[6])<<16;
336 a+=k[0]+(((uint32_t)k[1])<<16);
339 case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
341 case 3 : a+=((uint32_t)k8[2])<<16;
350 const uint8_t *k =(uint8_t*) key;
356 a += ((uint32_t)k[1])<<8;
357 a += ((uint32_t)k[2])<<16;
358 a += ((uint32_t)k[3])<<24;
360 b += ((uint32_t)k[5])<<8;
361 b += ((uint32_t)k[6])<<16;
362 b += ((uint32_t)k[7])<<24;
364 c += ((uint32_t)k[9])<<8;
365 c += ((uint32_t)k[10])<<16;
366 c += ((uint32_t)k[11])<<24;
375 case 12: c+=((uint32_t)k[11])<<24;
376 case 11: c+=((uint32_t)k[10])<<16;
377 case 10: c+=((uint32_t)k[9])<<8;
379 case 8 : b+=((uint32_t)k[7])<<24;
380 case 7 : b+=((uint32_t)k[6])<<16;
381 case 6 : b+=((uint32_t)k[5])<<8;
383 case 4 : a+=((uint32_t)k[3])<<24;
384 case 3 : a+=((uint32_t)k[2])<<16;
385 case 2 : a+=((uint32_t)k[1])<<8;
414 union {
const void *ptr;
size_t i; } u;
417 a = b = c = 0xdeadbeef + ((uint32_t)length) + *pc;
421 if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
422 const uint32_t *k = (uint32_t*)key;
452 case 12: c+=k[2]; b+=k[1]; a+=k[0];
break;
453 case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0];
break;
454 case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0];
break;
455 case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0];
break;
456 case 8 : b+=k[1]; a+=k[0];
break;
457 case 7 : b+=k[1]&0xffffff; a+=k[0];
break;
458 case 6 : b+=k[1]&0xffff; a+=k[0];
break;
459 case 5 : b+=k[1]&0xff; a+=k[0];
break;
460 case 4 : a+=k[0];
break;
461 case 3 : a+=k[0]&0xffffff;
break;
462 case 2 : a+=k[0]&0xffff;
break;
463 case 1 : a+=k[0]&0xff;
break;
464 case 0 : *pc=c; *pb=b;
return;
469 k8 = (
const uint8_t *)k;
472 case 12: c+=k[2]; b+=k[1]; a+=k[0];
break;
473 case 11: c+=((uint32_t)k8[10])<<16;
474 case 10: c+=((uint32_t)k8[9])<<8;
476 case 8 : b+=k[1]; a+=k[0];
break;
477 case 7 : b+=((uint32_t)k8[6])<<16;
478 case 6 : b+=((uint32_t)k8[5])<<8;
480 case 4 : a+=k[0];
break;
481 case 3 : a+=((uint32_t)k8[2])<<16;
482 case 2 : a+=((uint32_t)k8[1])<<8;
483 case 1 : a+=k8[0];
break;
484 case 0 : *pc=c; *pb=b;
return;
489 }
else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
490 const uint16_t *k = (uint16_t*)key;
496 a += k[0] + (((uint32_t)k[1])<<16);
497 b += k[2] + (((uint32_t)k[3])<<16);
498 c += k[4] + (((uint32_t)k[5])<<16);
505 k8 = (
const uint8_t *)k;
508 case 12: c+=k[4]+(((uint32_t)k[5])<<16);
509 b+=k[2]+(((uint32_t)k[3])<<16);
510 a+=k[0]+(((uint32_t)k[1])<<16);
512 case 11: c+=((uint32_t)k8[10])<<16;
514 b+=k[2]+(((uint32_t)k[3])<<16);
515 a+=k[0]+(((uint32_t)k[1])<<16);
518 case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
519 a+=k[0]+(((uint32_t)k[1])<<16);
521 case 7 : b+=((uint32_t)k8[6])<<16;
523 a+=k[0]+(((uint32_t)k[1])<<16);
526 case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
528 case 3 : a+=((uint32_t)k8[2])<<16;
533 case 0 : *pc=c; *pb=b;
return;
537 const uint8_t *k = (uint8_t*)key;
543 a += ((uint32_t)k[1])<<8;
544 a += ((uint32_t)k[2])<<16;
545 a += ((uint32_t)k[3])<<24;
547 b += ((uint32_t)k[5])<<8;
548 b += ((uint32_t)k[6])<<16;
549 b += ((uint32_t)k[7])<<24;
551 c += ((uint32_t)k[9])<<8;
552 c += ((uint32_t)k[10])<<16;
553 c += ((uint32_t)k[11])<<24;
562 case 12: c+=((uint32_t)k[11])<<24;
563 case 11: c+=((uint32_t)k[10])<<16;
564 case 10: c+=((uint32_t)k[9])<<8;
566 case 8 : b+=((uint32_t)k[7])<<24;
567 case 7 : b+=((uint32_t)k[6])<<16;
568 case 6 : b+=((uint32_t)k[5])<<8;
570 case 4 : a+=((uint32_t)k[3])<<24;
571 case 3 : a+=((uint32_t)k[2])<<16;
572 case 2 : a+=((uint32_t)k[1])<<8;
575 case 0 : *pc=c; *pb=b;
return;
580 *pc=c; *pb=b;
return;
591 uint32_t hashbig(
const void *key,
size_t length, uint32_t initval)
594 union {
const void *ptr;
size_t i; } u;
597 a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
600 if (HASH_BIG_ENDIAN && ((u.i & 0x3) == 0)) {
601 const uint32_t *k = (uint32_t*)key;
631 case 12: c+=k[2]; b+=k[1]; a+=k[0];
break;
632 case 11: c+=k[2]<<8; b+=k[1]; a+=k[0];
break;
633 case 10: c+=k[2]<<16; b+=k[1]; a+=k[0];
break;
634 case 9 : c+=k[2]<<24; b+=k[1]; a+=k[0];
break;
635 case 8 : b+=k[1]; a+=k[0];
break;
636 case 7 : b+=k[1]<<8; a+=k[0];
break;
637 case 6 : b+=k[1]<<16; a+=k[0];
break;
638 case 5 : b+=k[1]<<24; a+=k[0];
break;
639 case 4 : a+=k[0];
break;
640 case 3 : a+=k[0]<<8;
break;
641 case 2 : a+=k[0]<<16;
break;
642 case 1 : a+=k[0]<<24;
break;
648 k8 = (
const uint8_t *)k;
651 case 12: c+=k[2]; b+=k[1]; a+=k[0];
break;
652 case 11: c+=((uint32_t)k8[10])<<8;
653 case 10: c+=((uint32_t)k8[9])<<16;
654 case 9 : c+=((uint32_t)k8[8])<<24;
655 case 8 : b+=k[1]; a+=k[0];
break;
656 case 7 : b+=((uint32_t)k8[6])<<8;
657 case 6 : b+=((uint32_t)k8[5])<<16;
658 case 5 : b+=((uint32_t)k8[4])<<24;
659 case 4 : a+=k[0];
break;
660 case 3 : a+=((uint32_t)k8[2])<<8;
661 case 2 : a+=((uint32_t)k8[1])<<16;
662 case 1 : a+=((uint32_t)k8[0])<<24;
break;
669 const uint8_t *k = (uint8_t*)key;
674 a += ((uint32_t)k[0])<<24;
675 a += ((uint32_t)k[1])<<16;
676 a += ((uint32_t)k[2])<<8;
677 a += ((uint32_t)k[3]);
678 b += ((uint32_t)k[4])<<24;
679 b += ((uint32_t)k[5])<<16;
680 b += ((uint32_t)k[6])<<8;
681 b += ((uint32_t)k[7]);
682 c += ((uint32_t)k[8])<<24;
683 c += ((uint32_t)k[9])<<16;
684 c += ((uint32_t)k[10])<<8;
685 c += ((uint32_t)k[11]);
695 case 11: c+=((uint32_t)k[10])<<8;
696 case 10: c+=((uint32_t)k[9])<<16;
697 case 9 : c+=((uint32_t)k[8])<<24;
699 case 7 : b+=((uint32_t)k[6])<<8;
700 case 6 : b+=((uint32_t)k[5])<<16;
701 case 5 : b+=((uint32_t)k[4])<<24;
703 case 3 : a+=((uint32_t)k[2])<<8;
704 case 2 : a+=((uint32_t)k[1])<<16;
705 case 1 : a+=((uint32_t)k[0])<<24;
727 for (i=0; i<256; ++i) buf[i] =
'x';
730 h = hashlittle(&buf[0],1,h);
733 if (z-a > 0) printf(
"time %d %.8x\n", z-a, h);
743 uint8_t qa[MAXLEN+1], qb[MAXLEN+2], *a = &qa[0], *b = &qb[1];
744 uint32_t c[HASHSTATE], d[HASHSTATE], i=0, j=0, k, l, m=0, z;
745 uint32_t e[HASHSTATE],f[HASHSTATE],g[HASHSTATE],h[HASHSTATE];
746 uint32_t x[HASHSTATE],y[HASHSTATE];
749 printf(
"No more than %d trials should ever be needed \n",MAXPAIR/2);
750 for (hlen=0; hlen < MAXLEN; ++hlen)
753 for (i=0; i<hlen; ++i)
759 for (l=0; l<HASHSTATE; ++l)
760 e[l]=f[l]=g[l]=h[l]=x[l]=y[l]=~((uint32_t)0);
763 for (k=0; k<MAXPAIR; k+=2)
767 for (l=0; l<hlen+1; ++l) {a[l] = b[l] = (uint8_t)0;}
771 c[0] = hashlittle(a, hlen, m);
773 b[i] ^= ((k+1)>>(8-j));
774 d[0] = hashlittle(b, hlen, m);
776 for (l=0; l<HASHSTATE; ++l)
779 f[l] &= ~(c[l]^d[l]);
784 if (e[l]|f[l]|g[l]|h[l]|x[l]|y[l]) finished=0;
791 printf(
"Some bit didn't change: ");
792 printf(
"%.8x %.8x %.8x %.8x %.8x %.8x ",
793 e[0],f[0],g[0],h[0],x[0],y[0]);
794 printf(
"i %d j %d m %d len %d\n", i, j, m, hlen);
796 if (z==MAXPAIR)
goto done;
803 printf(
"Mix success %2d bytes %2d initvals ",i,m);
804 printf(
"required %d trials\n", z/2);
813 uint8_t buf[MAXLEN+20], *b;
815 uint8_t q[] =
"This is the time for all good men to come to the aid of their country...";
817 uint8_t qq[] =
"xThis is the time for all good men to come to the aid of their country...";
819 uint8_t qqq[] =
"xxThis is the time for all good men to come to the aid of their country...";
821 uint8_t qqqq[] =
"xxxThis is the time for all good men to come to the aid of their country...";
825 printf(
"Endianness. These lines should all be the same (for values filled in):\n");
826 printf(
"%.8x %.8x %.8x\n",
827 hashword((
const uint32_t *)q, (
sizeof(q)-1)/4, 13),
828 hashword((
const uint32_t *)q, (
sizeof(q)-5)/4, 13),
829 hashword((
const uint32_t *)q, (
sizeof(q)-9)/4, 13));
831 printf(
"%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
832 hashlittle(p,
sizeof(q)-1, 13), hashlittle(p,
sizeof(q)-2, 13),
833 hashlittle(p,
sizeof(q)-3, 13), hashlittle(p,
sizeof(q)-4, 13),
834 hashlittle(p,
sizeof(q)-5, 13), hashlittle(p,
sizeof(q)-6, 13),
835 hashlittle(p,
sizeof(q)-7, 13), hashlittle(p,
sizeof(q)-8, 13),
836 hashlittle(p,
sizeof(q)-9, 13), hashlittle(p,
sizeof(q)-10, 13),
837 hashlittle(p,
sizeof(q)-11, 13), hashlittle(p,
sizeof(q)-12, 13));
839 printf(
"%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
840 hashlittle(p,
sizeof(q)-1, 13), hashlittle(p,
sizeof(q)-2, 13),
841 hashlittle(p,
sizeof(q)-3, 13), hashlittle(p,
sizeof(q)-4, 13),
842 hashlittle(p,
sizeof(q)-5, 13), hashlittle(p,
sizeof(q)-6, 13),
843 hashlittle(p,
sizeof(q)-7, 13), hashlittle(p,
sizeof(q)-8, 13),
844 hashlittle(p,
sizeof(q)-9, 13), hashlittle(p,
sizeof(q)-10, 13),
845 hashlittle(p,
sizeof(q)-11, 13), hashlittle(p,
sizeof(q)-12, 13));
847 printf(
"%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
848 hashlittle(p,
sizeof(q)-1, 13), hashlittle(p,
sizeof(q)-2, 13),
849 hashlittle(p,
sizeof(q)-3, 13), hashlittle(p,
sizeof(q)-4, 13),
850 hashlittle(p,
sizeof(q)-5, 13), hashlittle(p,
sizeof(q)-6, 13),
851 hashlittle(p,
sizeof(q)-7, 13), hashlittle(p,
sizeof(q)-8, 13),
852 hashlittle(p,
sizeof(q)-9, 13), hashlittle(p,
sizeof(q)-10, 13),
853 hashlittle(p,
sizeof(q)-11, 13), hashlittle(p,
sizeof(q)-12, 13));
855 printf(
"%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
856 hashlittle(p,
sizeof(q)-1, 13), hashlittle(p,
sizeof(q)-2, 13),
857 hashlittle(p,
sizeof(q)-3, 13), hashlittle(p,
sizeof(q)-4, 13),
858 hashlittle(p,
sizeof(q)-5, 13), hashlittle(p,
sizeof(q)-6, 13),
859 hashlittle(p,
sizeof(q)-7, 13), hashlittle(p,
sizeof(q)-8, 13),
860 hashlittle(p,
sizeof(q)-9, 13), hashlittle(p,
sizeof(q)-10, 13),
861 hashlittle(p,
sizeof(q)-11, 13), hashlittle(p,
sizeof(q)-12, 13));
863 for (h=0, b=buf+1; h<8; ++h, ++b)
865 for (i=0; i<MAXLEN; ++i)
868 for (j=0; j<i; ++j) *(b+j)=0;
871 ref = hashlittle(b, len, (uint32_t)1);
874 x = hashlittle(b, len, (uint32_t)1);
875 y = hashlittle(b, len, (uint32_t)1);
876 if ((ref != x) || (ref != y))
878 printf(
"alignment error: %.8x %.8x %.8x %d %d\n",ref,x,y,
889 uint32_t h,i,state[HASHSTATE];
893 for (i=0; i<HASHSTATE; ++i) state[i] = 1;
894 printf(
"These should all be different\n");
895 for (i=0, h=0; i<8; ++i)
897 h = hashlittle(buf, 0, h);
898 printf(
"%2ld 0-byte strings, hash is %.8x\n", i, h);
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.