fnv.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #pragma once
  2. #include <util/system/defaults.h>
  3. #define FNV32INIT 2166136261U
  4. #define FNV32PRIME 16777619U
  5. #define FNV64INIT ULL(14695981039346656037)
  6. #define FNV64PRIME ULL(1099511628211)
  7. namespace NFnvPrivate {
  8. template <class It>
  9. constexpr ui32 FnvHash32(It b, It e, ui32 init) {
  10. while (b != e) {
  11. init = (init * FNV32PRIME) ^ (unsigned char)*b++;
  12. }
  13. return init;
  14. }
  15. template <class It>
  16. constexpr ui64 FnvHash64(It b, It e, ui64 init) {
  17. while (b != e) {
  18. init = (init * FNV64PRIME) ^ (unsigned char)*b++;
  19. }
  20. return init;
  21. }
  22. template <unsigned N>
  23. struct TFnvHelper;
  24. #define DEF_FNV(t) \
  25. template <> \
  26. struct TFnvHelper<t> { \
  27. static const ui##t Init = FNV##t##INIT; \
  28. template <class It> \
  29. static constexpr ui##t FnvHash(It b, It e, ui##t init) { \
  30. return FnvHash##t(b, e, init); \
  31. } \
  32. };
  33. DEF_FNV(32)
  34. DEF_FNV(64)
  35. #undef DEF_FNV
  36. }
  37. template <class T, class It>
  38. static constexpr T FnvHash(It b, It e, T init) {
  39. static_assert(sizeof(*b) == 1, "expect sizeof(*b) == 1");
  40. return (T)NFnvPrivate::TFnvHelper<8 * sizeof(T)>::FnvHash(b, e, init);
  41. }
  42. template <class T, class It>
  43. static constexpr T FnvHash(It b, It e) {
  44. return FnvHash<T>(b, e, (T)NFnvPrivate::TFnvHelper<8 * sizeof(T)>::Init);
  45. }
  46. template <class T>
  47. static constexpr T FnvHash(const void* buf, size_t len, T init) {
  48. return FnvHash<T>((const unsigned char*)buf, (const unsigned char*)buf + len, init);
  49. }
  50. template <class T>
  51. static constexpr T FnvHash(const void* buf, size_t len) {
  52. return FnvHash<T>((const unsigned char*)buf, (const unsigned char*)buf + len);
  53. }
  54. template <class T, class Buf>
  55. static constexpr T FnvHash(const Buf& buf) {
  56. return FnvHash<T>(buf.data(), buf.size() * sizeof(*buf.data()));
  57. }