ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
fnv1a.h
Go to the documentation of this file.
1#pragma once
2
23
24#include <stddef.h>
25#include <stdint.h>
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31// NOTE: This is a low-level hash utility. To avoid circular dependencies with
32// asciichat_errno.h and common.h, we don't log errors here. Invalid inputs
33// return 0.
34
35/* ============================================================================
36 * FNV-1a Constants
37 * ============================================================================
38 */
39
41#define FNV1A_32_OFFSET_BASIS 2166136261ULL
42
44#define FNV1A_32_PRIME 16777619ULL
45
47#define FNV1A_32_MASK 0xFFFFFFFFULL
48
49/* ============================================================================
50 * Macros
51 * ============================================================================
52 */
53
55#define FNV1A_INIT(hash) uint64_t hash = FNV1A_32_OFFSET_BASIS;
56
58#define FNV1A_32_HASH(hash, byte) \
59 do { \
60 (hash) = (((hash) ^ (uint64_t)(byte)) * FNV1A_32_PRIME) & FNV1A_32_MASK; \
61 } while (0)
62
63/* ============================================================================
64 * FNV-1a Hash Functions
65 * ============================================================================
66 */
67
79static inline uint32_t fnv1a_hash_bytes(const void *data, size_t len) {
80 // Return 0 for invalid input (no error logging to avoid circular
81 // dependencies)
82 if (!data || len == 0) {
83 return 0;
84 }
85
87 const unsigned char *bytes = (const unsigned char *)data;
88 const unsigned char *end = bytes + len;
89
90 while (bytes < end) {
91 uint64_t byte = (uint64_t)*bytes++;
92 hash = ((hash ^ byte) * FNV1A_32_PRIME) & FNV1A_32_MASK;
93 }
94
95 return (uint32_t)hash;
96}
97
108static inline uint32_t fnv1a_hash_string(const char *str) {
109 // Return 0 for invalid input (no error logging to avoid circular
110 // dependencies)
111 if (!str) {
112 return 0;
113 }
114
116 const unsigned char *p = (const unsigned char *)str;
117
118 while (*p) {
119 uint64_t byte = (uint64_t)*p++;
120 hash = ((hash ^ byte) * FNV1A_32_PRIME) & FNV1A_32_MASK;
121 }
122
123 return (uint32_t)hash;
124}
125
136static inline uint32_t fnv1a_hash_uint32(uint32_t value) {
138
139 // Hash each byte of the 32-bit value
140 FNV1A_32_HASH(hash, (value >> 0) & FNV1A_32_MASK);
141 FNV1A_32_HASH(hash, (value >> 8) & FNV1A_32_MASK);
142 FNV1A_32_HASH(hash, (value >> 16) & FNV1A_32_MASK);
143 FNV1A_32_HASH(hash, (value >> 24) & FNV1A_32_MASK);
144
145 return (uint32_t)hash;
146}
147
158static inline uint32_t fnv1a_hash_uint64(uint64_t value) {
160
161 // Hash each byte of the 64-bit value
162 for (int i = 0; i < 8; i++) {
163 uint64_t byte = (value >> (i * 8)) & FNV1A_32_MASK;
164 hash = ((hash ^ byte) * FNV1A_32_PRIME) & FNV1A_32_MASK;
165 }
166
167 return (uint32_t)hash;
168}
169
171
172#ifdef __cplusplus
173}
174#endif
unsigned int uint32_t
unsigned long long uint64_t
#define FNV1A_32_HASH(hash, byte)
FNV-1a 32-bit hash macro for a single byte.
Definition fnv1a.h:58
#define FNV1A_32_OFFSET_BASIS
FNV-1a 32-bit offset basis.
Definition fnv1a.h:41
#define FNV1A_32_MASK
FNV-1a 32-bit mask.
Definition fnv1a.h:47
#define FNV1A_32_PRIME
FNV-1a 32-bit prime.
Definition fnv1a.h:44