fnv1a-hash.h 750 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #pragma once
  2. #include <stdint.h>
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #define FNV_1A_INIT 2166136261UL
  7. // FNV-1a hash, 32-bit
  8. uint32_t fnv1a_buffer_hash(const uint8_t* buffer, uint32_t length, uint32_t hash);
  9. #ifdef __cplusplus
  10. }
  11. #endif
  12. #ifdef __cplusplus
  13. // constexpr FNV-1a hash for strings, 32-bit
  14. inline constexpr uint32_t fnv1a_string_hash(const char* str) {
  15. uint32_t hash = FNV_1A_INIT;
  16. while(*str) {
  17. hash = (hash ^ *str) * 16777619ULL;
  18. str += 1;
  19. }
  20. return hash;
  21. }
  22. #else
  23. // FNV-1a hash for strings, 32-bit
  24. inline uint32_t fnv1a_string_hash(const char* str) {
  25. uint32_t hash = FNV_1A_INIT;
  26. while(*str) {
  27. hash = (hash ^ *str) * 16777619ULL;
  28. str += 1;
  29. }
  30. return hash;
  31. }
  32. #endif