lookup3.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // lookup3 by Bob Jekins, code is public domain.
  2. #include "Platform.h"
  3. #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
  4. #define mix(a,b,c) \
  5. { \
  6. a -= c; a ^= rot(c, 4); c += b; \
  7. b -= a; b ^= rot(a, 6); a += c; \
  8. c -= b; c ^= rot(b, 8); b += a; \
  9. a -= c; a ^= rot(c,16); c += b; \
  10. b -= a; b ^= rot(a,19); a += c; \
  11. c -= b; c ^= rot(b, 4); b += a; \
  12. }
  13. #define final(a,b,c) \
  14. { \
  15. c ^= b; c -= rot(b,14); \
  16. a ^= c; a -= rot(c,11); \
  17. b ^= a; b -= rot(a,25); \
  18. c ^= b; c -= rot(b,16); \
  19. a ^= c; a -= rot(c,4); \
  20. b ^= a; b -= rot(a,14); \
  21. c ^= b; c -= rot(b,24); \
  22. }
  23. uint32_t lookup3 ( const void * key, int length, uint32_t initval )
  24. {
  25. uint32_t a,b,c; /* internal state */
  26. a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
  27. const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
  28. /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
  29. while (length > 12)
  30. {
  31. a += k[0];
  32. b += k[1];
  33. c += k[2];
  34. mix(a,b,c);
  35. length -= 12;
  36. k += 3;
  37. }
  38. switch(length)
  39. {
  40. case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
  41. case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
  42. case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
  43. case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
  44. case 8 : b+=k[1]; a+=k[0]; break;
  45. case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
  46. case 6 : b+=k[1]&0xffff; a+=k[0]; break;
  47. case 5 : b+=k[1]&0xff; a+=k[0]; break;
  48. case 4 : a+=k[0]; break;
  49. case 3 : a+=k[0]&0xffffff; break;
  50. case 2 : a+=k[0]&0xffff; break;
  51. case 1 : a+=k[0]&0xff; break;
  52. case 0 : { return c; } /* zero length strings require no mixing */
  53. }
  54. final(a,b,c);
  55. return c;
  56. }
  57. void lookup3_test ( const void * key, int len, uint32_t seed, void * out )
  58. {
  59. *(uint32_t*)out = lookup3(key,len,seed);
  60. }