sfh.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #pragma once
  2. #include <util/system/defaults.h>
  3. #include <util/system/unaligned_mem.h>
  4. inline ui32 SuperFastHash(const void* d, size_t l) noexcept {
  5. ui32 hash = (ui32)l;
  6. ui32 tmp;
  7. if (!l || !d)
  8. return 0;
  9. TUnalignedMemoryIterator<ui16, 4> iter(d, l);
  10. while (!iter.AtEnd()) {
  11. hash += (ui32)iter.Next();
  12. tmp = ((ui32)iter.Next() << 11) ^ hash;
  13. hash = (hash << 16) ^ tmp;
  14. hash += hash >> 11;
  15. }
  16. switch (iter.Left()) {
  17. case 3:
  18. hash += (ui32)iter.Next();
  19. hash ^= hash << 16;
  20. hash ^= ((ui32)(i32) * (const i8*)iter.Last()) << 18;
  21. hash += hash >> 11;
  22. break;
  23. case 2:
  24. hash += (ui32)iter.Cur();
  25. hash ^= hash << 11;
  26. hash += hash >> 17;
  27. break;
  28. case 1:
  29. hash += *((const i8*)iter.Last());
  30. hash ^= hash << 10;
  31. hash += hash >> 1;
  32. }
  33. /* Force "avalanching" of final 127 bits */
  34. hash ^= hash << 3;
  35. hash += hash >> 5;
  36. hash ^= hash << 4;
  37. hash += hash >> 17;
  38. hash ^= hash << 25;
  39. hash += hash >> 6;
  40. return hash;
  41. }