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 "macros.h" | ||
9 | |||
10 | 69048 | static inline size_t fnv_1a_init(void) | |
11 | { | ||
12 | 69048 | static_assert(8 * CHAR_BIT == 64); | |
13 | 69048 | return (sizeof(size_t) >= 8) ? 14695981039346656037ULL : 2166136261U; | |
14 | } | ||
15 | |||
16 | 69048 | static inline size_t fnv_1a_prime(void) | |
17 | { | ||
18 | 69048 | return (sizeof(size_t) >= 8) ? 1099511628211ULL : 16777619U; | |
19 | } | ||
20 | |||
21 | 66308 | static inline size_t fnv_1a_hash(const unsigned char *str, size_t n) | |
22 | { | ||
23 | 66308 | const size_t prime = fnv_1a_prime(); | |
24 | 66308 | size_t hash = fnv_1a_init(); | |
25 |
2/2✓ Branch 0 (4→3) taken 428054 times.
✓ Branch 1 (4→5) taken 66308 times.
|
494362 | while (n--) { |
26 | 428054 | hash ^= *str++; | |
27 | 428054 | hash *= prime; | |
28 | } | ||
29 | 66308 | return hash; | |
30 | } | ||
31 | |||
32 | 2740 | static inline size_t fnv_1a_hash_icase(const unsigned char *str, size_t n) | |
33 | { | ||
34 | 2740 | const size_t prime = fnv_1a_prime(); | |
35 | 2740 | size_t hash = fnv_1a_init(); | |
36 |
2/2✓ Branch 0 (4→3) taken 26392 times.
✓ Branch 1 (4→5) taken 2740 times.
|
29132 | while (n--) { |
37 | 26392 | hash ^= ascii_tolower(*str++); | |
38 | 26392 | hash *= prime; | |
39 | } | ||
40 | 2740 | return hash; | |
41 | } | ||
42 | |||
43 | // NOTE: returns 0 if x is greater than the largest size_t power of 2 | ||
44 | 425 | static inline size_t next_pow2(size_t x) | |
45 | { | ||
46 |
2/2✓ Branch 0 (2→3) taken 424 times.
✓ Branch 1 (2→7) taken 1 times.
|
425 | if (unlikely(x == 0)) { |
47 | return 1; | ||
48 | } | ||
49 | 424 | x--; | |
50 | UNROLL_LOOP(8) | ||
51 |
2/2✓ Branch 0 (5→4) taken 2544 times.
✓ Branch 1 (5→6) taken 424 times.
|
2968 | for (size_t i = 1, n = sizeof(size_t) * CHAR_BIT; i < n; i <<= 1) { |
52 | 2544 | x |= x >> i; | |
53 | } | ||
54 | 424 | return x + 1; | |
55 | } | ||
56 | |||
57 | #endif | ||
58 |