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 | 69631 | static inline size_t fnv_1a_init(void) | |
12 | { | ||
13 | 69631 | return (BITSIZE(size_t) >= 64) ? 14695981039346656037ULL : 2166136261U; | |
14 | } | ||
15 | |||
16 | 69631 | static inline size_t fnv_1a_prime(void) | |
17 | { | ||
18 | 69631 | return (BITSIZE(size_t) >= 64) ? 1099511628211ULL : 16777619U; | |
19 | } | ||
20 | |||
21 | // https://datatracker.ietf.org/doc/html/draft-eastlake-fnv-31#name-fnv-basics | ||
22 | 66891 | static inline size_t fnv_1a_hash(const unsigned char *str, size_t n) | |
23 | { | ||
24 | 66891 | const size_t prime = fnv_1a_prime(); | |
25 | 66891 | size_t hash = fnv_1a_init(); | |
26 |
2/2✓ Branch 0 (4→3) taken 435878 times.
✓ Branch 1 (4→5) taken 66891 times.
|
502769 | while (n--) { |
27 | 435878 | hash ^= *str++; | |
28 | 435878 | hash *= prime; | |
29 | } | ||
30 | 66891 | return hash; | |
31 | } | ||
32 | |||
33 | 2740 | static inline size_t fnv_1a_hash_icase(const unsigned char *str, size_t n) | |
34 | { | ||
35 | 2740 | const size_t prime = fnv_1a_prime(); | |
36 | 2740 | size_t hash = fnv_1a_init(); | |
37 |
2/2✓ Branch 0 (4→3) taken 26392 times.
✓ Branch 1 (4→5) taken 2740 times.
|
29132 | while (n--) { |
38 | 26392 | hash ^= ascii_tolower(*str++); | |
39 | 26392 | hash *= prime; | |
40 | } | ||
41 | 2740 | return hash; | |
42 | } | ||
43 | |||
44 | #endif | ||
45 |