hash.h 847 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #ifndef HASH_H
  2. #define HASH_H
  3. #include <stdint.h>
  4. #include <stdlib.h>
  5. typedef char byte_t;
  6. __attribute__((access (read_only, 1), nonnull, pure))
  7. uint64_t hash(const byte_t* data, size_t size);
  8. #if defined(CSTRING_IMP) || defined(IMPLEMENTATIONS)
  9. uint64_t hash(const byte_t* data, size_t size) {
  10. static uint8_t _primes_list[] = {
  11. 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59,
  12. 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127,
  13. 131, 137, 139, 149, 151, 157, 163, 167, 173
  14. };
  15. static uint8_t _primes_list_size = 10;
  16. uint64_t max = UINT64_MAX >> 1;
  17. uint64_t ret = 1;
  18. uint8_t p = 0;
  19. size_t i = 0;
  20. byte_t b = 0;
  21. for ( i = 0; i < size; ++i ) {
  22. b = data[i];
  23. p = _primes_list[(i + b) % _primes_list_size];
  24. ret = (ret * (b * p)) % max;
  25. }
  26. return ret;
  27. }
  28. #endif /* HASH_IMP || IMPLEMENTATIONS */
  29. #endif