| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #ifndef UTIL_HASH_H | ||
| 2 | #define UTIL_HASH_H | ||
| 3 | |||
| 4 | #include <limits.h> | ||
| 5 | #include <stddef.h> | ||
| 6 | #include <stdint.h> | ||
| 7 | #include "ascii.h" | ||
| 8 | #include "bit.h" | ||
| 9 | #include "macros.h" | ||
| 10 | |||
| 11 | 70778 | static inline size_t fnv_1a_init(void) | |
| 12 | { | ||
| 13 | 70778 | return (BITSIZE(size_t) >= 64) ? 14695981039346656037ULL : 2166136261U; | |
| 14 | } | ||
| 15 | |||
| 16 | 70778 | static inline size_t fnv_1a_prime(void) | |
| 17 | { | ||
| 18 | 70778 | return (BITSIZE(size_t) >= 64) ? 1099511628211ULL : 16777619U; | |
| 19 | } | ||
| 20 | |||
| 21 | // https://datatracker.ietf.org/doc/html/draft-eastlake-fnv-31#name-fnv-basics | ||
| 22 | 67972 | static inline size_t fnv_1a_hash(const char *str, size_t n) | |
| 23 | { | ||
| 24 | 67972 | const size_t prime = fnv_1a_prime(); | |
| 25 | 67972 | size_t hash = fnv_1a_init(); | |
| 26 |
2/2✓ Branch 0 (4→3) taken 449869 times.
✓ Branch 1 (4→5) taken 67972 times.
|
517841 | while (n--) { |
| 27 | 449869 | hash ^= (unsigned char)*str++; | |
| 28 | 449869 | hash *= prime; | |
| 29 | } | ||
| 30 | 67972 | return hash; | |
| 31 | } | ||
| 32 | |||
| 33 | 2806 | static inline size_t fnv_1a_hash_icase(const char *str, size_t n) | |
| 34 | { | ||
| 35 | 2806 | const size_t prime = fnv_1a_prime(); | |
| 36 | 2806 | size_t hash = fnv_1a_init(); | |
| 37 |
2/2✓ Branch 0 (4→3) taken 27284 times.
✓ Branch 1 (4→5) taken 2806 times.
|
30090 | while (n--) { |
| 38 | 27284 | hash ^= ascii_tolower(*str++); | |
| 39 | 27284 | hash *= prime; | |
| 40 | } | ||
| 41 | 2806 | return hash; | |
| 42 | } | ||
| 43 | |||
| 44 | #endif | ||
| 45 |