Derive-C
Loading...
Searching...
No Matches
fnv1a.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdint.h>
4
7
9#define DC_FNV1A_64_OFFSET 14695981039346656037ull
10#define DC_FNV1A_64_PRIME 1099511628211ull
11
13DC_PUBLIC static inline uint64_t dc_fnv1a_str_borrow(const char* s) {
14 const unsigned char* p = (const unsigned char*)(s);
15 uint64_t h = DC_FNV1A_64_OFFSET;
16
17 for (unsigned char c = *p; c != 0; c = *++p) {
18 h ^= (uint64_t)c;
20 }
21 return h;
22}
23
24DC_PUBLIC static inline uint64_t dc_fnv1a_str(char* const* s) { return dc_fnv1a_str_borrow(*s); }
25
26DC_PUBLIC static inline uint64_t dc_fnv1a_str_const(const char* const* s) {
27 return dc_fnv1a_str_borrow(*s);
28}
29
30DC_PUBLIC static inline uint64_t dc_fnv1a_u64(uint64_t const* v) {
31 uint64_t h = DC_FNV1A_64_OFFSET;
32 uint64_t x = *v;
33
34 for (int i = 0; i < 8; ++i) {
35 h ^= (uint64_t)(x & 0xffU);
37 x >>= 8;
38 }
39 return h;
40}
41
42DC_PUBLIC static inline uint64_t dc_fnv1a_u32(uint32_t const* v) {
43 uint64_t h = DC_FNV1A_64_OFFSET;
44 uint32_t x = *v;
45
46 for (int i = 0; i < 4; ++i) {
47 h ^= (uint64_t)(x & 0xffU);
49 x >>= 8;
50 }
51 return h;
52}
53
55#define FNV1A_INTEGER(type, ...) \
56 DC_PUBLIC static size_t type##_hash_fnv1a(type const* key) { \
57 DC_STATIC_ASSERT(sizeof(type) <= sizeof(uint64_t), \
58 "fnv integer hashing only supports up to size_t integers"); \
59 if (sizeof(type) <= sizeof(uint32_t)) { \
60 uint32_t value = (uint32_t)(*key); \
61 return dc_fnv1a_u32(&value); \
62 } \
63 uint64_t value = (uint64_t)(*key); \
64 return dc_fnv1a_u64(&value); \
65 }
66
68
69#undef FNV1A_INTEGER
#define DC_FNV1A_64_OFFSET
FNV-1a 64-bit constants.
Definition fnv1a.h:9
static DC_PUBLIC uint64_t dc_fnv1a_u32(uint32_t const *v)
Definition fnv1a.h:42
#define DC_FNV1A_64_PRIME
Definition fnv1a.h:10
static DC_PUBLIC uint64_t dc_fnv1a_str(char *const *s)
Definition fnv1a.h:24
static DC_PUBLIC uint64_t dc_fnv1a_str_borrow(const char *s)
Hashes a null terminated string.
Definition fnv1a.h:13
#define FNV1A_INTEGER(type,...)
Applying the fnv1a hash for the size of the integer.
Definition fnv1a.h:55
static DC_PUBLIC uint64_t dc_fnv1a_str_const(const char *const *s)
Definition fnv1a.h:26
static DC_PUBLIC uint64_t dc_fnv1a_u64(uint64_t const *v)
Definition fnv1a.h:30
#define DC_PUBLIC
Definition namespace.h:25
#define DC_INT_REFLECT(F,...)
Definition reflect.h:11