Derive-C
Loading...
Searching...
No Matches
fnv1a.h File Reference
#include <stdint.h>
#include <derive-c/core/prelude.h>
#include <derive-c/core/std/reflect.h>

Go to the source code of this file.

Macros

#define DC_FNV1A_64_OFFSET   14695981039346656037ull
 FNV-1a 64-bit constants.
#define DC_FNV1A_64_PRIME   1099511628211ull
#define FNV1A_INTEGER(type, ...)
 Applying the fnv1a hash for the size of the integer.

Functions

static DC_PUBLIC uint64_t dc_fnv1a_str_borrow (const char *s)
 Hashes a null terminated string.
static DC_PUBLIC uint64_t dc_fnv1a_str (char *const *s)
static DC_PUBLIC uint64_t dc_fnv1a_str_const (const char *const *s)
static DC_PUBLIC uint64_t dc_fnv1a_u64 (uint64_t const *v)
static DC_PUBLIC uint64_t dc_fnv1a_u32 (uint32_t const *v)

Macro Definition Documentation

◆ DC_FNV1A_64_OFFSET

#define DC_FNV1A_64_OFFSET   14695981039346656037ull

FNV-1a 64-bit constants.

Definition at line 9 of file fnv1a.h.

◆ DC_FNV1A_64_PRIME

#define DC_FNV1A_64_PRIME   1099511628211ull

Definition at line 10 of file fnv1a.h.

◆ FNV1A_INTEGER

#define FNV1A_INTEGER ( type,
... )
Value:
DC_PUBLIC static size_t type##_hash_fnv1a(type const* key) { \
DC_STATIC_ASSERT(sizeof(type) <= sizeof(uint64_t), \
"fnv integer hashing only supports up to size_t integers"); \
if (sizeof(type) <= sizeof(uint32_t)) { \
uint32_t value = (uint32_t)(*key); \
return dc_fnv1a_u32(&value); \
} \
uint64_t value = (uint64_t)(*key); \
return dc_fnv1a_u64(&value); \
}
static DC_PUBLIC uint64_t dc_fnv1a_u32(uint32_t const *v)
Definition fnv1a.h:42
static DC_PUBLIC uint64_t dc_fnv1a_u64(uint64_t const *v)
Definition fnv1a.h:30
#define DC_PUBLIC
Definition namespace.h:25

Applying the fnv1a hash for the size of the integer.

Definition at line 55 of file fnv1a.h.

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 }

Function Documentation

◆ dc_fnv1a_str()

DC_PUBLIC uint64_t dc_fnv1a_str ( char *const * s)
inlinestatic

Definition at line 24 of file fnv1a.h.

24{ return dc_fnv1a_str_borrow(*s); }
static DC_PUBLIC uint64_t dc_fnv1a_str_borrow(const char *s)
Hashes a null terminated string.
Definition fnv1a.h:13

◆ dc_fnv1a_str_borrow()

DC_PUBLIC uint64_t dc_fnv1a_str_borrow ( const char * s)
inlinestatic

Hashes a null terminated string.

Definition at line 13 of file fnv1a.h.

13 {
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}
#define DC_FNV1A_64_OFFSET
FNV-1a 64-bit constants.
Definition fnv1a.h:9
#define DC_FNV1A_64_PRIME
Definition fnv1a.h:10

◆ dc_fnv1a_str_const()

DC_PUBLIC uint64_t dc_fnv1a_str_const ( const char *const * s)
inlinestatic
Examples
container/map.c, and container/set.c.

Definition at line 26 of file fnv1a.h.

26 {
27 return dc_fnv1a_str_borrow(*s);
28}

◆ dc_fnv1a_u32()

DC_PUBLIC uint64_t dc_fnv1a_u32 ( uint32_t const * v)
inlinestatic

Definition at line 42 of file fnv1a.h.

42 {
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}

◆ dc_fnv1a_u64()

DC_PUBLIC uint64_t dc_fnv1a_u64 ( uint64_t const * v)
inlinestatic

Definition at line 30 of file fnv1a.h.

30 {
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}