solidc
Robust collection of general-purpose cross-platform C libraries and data structures designed for rapid and safe development in C
Loading...
Searching...
No Matches
hash.c
1#include "../include/hash.h"
2
3#include <stddef.h>
4#include <string.h>
5
10uint32_t solidc_djb2_hash(const void* key) {
11 const unsigned char* str = (const unsigned char*)key;
12 uint32_t hash = 5381;
13 int c;
14
15 while ((c = *str++) != '\0') {
16 hash = ((hash << 5) + hash) + (uint32_t)c; // hash * 33 + c
17 }
18
19 return hash;
20}
21
26uint32_t solidc_djb2a_hash(const void* key) {
27 const unsigned char* str = (const unsigned char*)key;
28 uint32_t hash = 5381;
29 int c;
30
31 while ((c = *str++) != '\0') {
32 hash = ((hash << 5) + hash) ^ (unsigned)c; // hash * 33 ^ c
33 }
34
35 return hash;
36}
37
42uint32_t solidc_fnv1a_hash(const void* key) {
43 const unsigned char* str = (const unsigned char*)key;
44 uint32_t hash = 2166136261u; // FNV offset basis
45
46 while (*str) {
47 hash ^= (uint32_t)(*str++);
48 hash *= 16777619u; // FNV prime
49 }
50
51 return hash;
52}
53
58uint64_t solidc_fnv1a_hash64(const void* key) {
59 const unsigned char* str = (const unsigned char*)key;
60 uint64_t hash = 14695981039346656037ULL; // FNV offset basis
61
62 while (*str) {
63 hash ^= (uint64_t)(*str++);
64 hash *= 1099511628211ULL; // FNV prime
65 }
66
67 return hash;
68}
69
74uint32_t solidc_elf_hash(const void* key) {
75 const unsigned char* str = (const unsigned char*)key;
76 uint32_t hash = 0;
77 uint32_t x = 0;
78
79 while (*str) {
80 hash = (hash << 4) + (*str++);
81 x = hash & 0xF0000000UL;
82 if (x != 0) {
83 hash ^= (x >> 24);
84 }
85 hash &= ~x;
86 }
87
88 return hash;
89}
90
95uint32_t solidc_sdbm_hash(const void* key) {
96 const unsigned char* str = (const unsigned char*)key;
97 uint32_t hash = 0;
98 int c;
99
100 while ((c = *str++) != '\0') {
101 hash = (unsigned char)c + (hash << 6) + (hash << 16) - hash;
102 }
103 return hash;
104}
105
110uint32_t solidc_crc32_hash(const void* key, size_t len) {
111 const unsigned char* data = (const unsigned char*)key;
112 uint32_t crc = 0xFFFFFFFF;
113
114 while (len--) {
115 crc ^= *data++;
116 for (int k = 0; k < 8; k++) {
117 crc = (crc >> 1) ^ (0xEDB88320 & (-(crc & 1)));
118 }
119 }
120
121 return ~crc;
122}
123
128uint32_t solidc_murmur_hash(const char* key, uint32_t len, uint32_t seed) {
129 const uint8_t* data = (const uint8_t*)key;
130 const int nblocks = len / 4;
131
132 uint32_t h1 = seed;
133
134 const uint32_t c1 = 0xcc9e2d51;
135 const uint32_t c2 = 0x1b873593;
136
137 // Body: process 4-byte blocks
138 const uint32_t* blocks = (const uint32_t*)(data + nblocks * 4);
139
140 for (int i = -nblocks; i; i++) {
141 uint32_t k1 = blocks[i];
142
143 k1 *= c1;
144 k1 = (k1 << 15) | (k1 >> (32 - 15));
145 k1 *= c2;
146
147 h1 ^= k1;
148 h1 = (h1 << 13) | (h1 >> (32 - 13));
149 h1 = h1 * 5 + 0xe6546b64;
150 }
151
152 // Tail: process remaining bytes
153 const uint8_t* tail = (const uint8_t*)(data + nblocks * 4);
154 uint32_t k1 = 0;
155
156 switch (len & 3) {
157 case 3:
158 k1 ^= (uint32_t)(tail[2] << 16u);
159 // fallthrough
160 case 2:
161 k1 ^= (uint32_t)(tail[1] << 8u);
162 // fallthrough
163 case 1:
164 k1 ^= tail[0];
165 k1 *= c1;
166 k1 = (k1 << 15) | (k1 >> (32 - 15));
167 k1 *= c2;
168 h1 ^= k1;
169 }
170
171 // Finalization
172 h1 ^= len;
173
174 h1 ^= h1 >> 16;
175 h1 *= 0x85ebca6b;
176 h1 ^= h1 >> 13;
177 h1 *= 0xc2b2ae35;
178 h1 ^= h1 >> 16;
179
180 return h1;
181}
uint32_t solidc_crc32_hash(const void *key, size_t len)
CRC32 hash function for arbitrary binary data.
Definition hash.c:110
uint32_t solidc_fnv1a_hash(const void *key)
FNV-1a 32-bit hash function for null-terminated strings.
Definition hash.c:42
uint64_t solidc_fnv1a_hash64(const void *key)
FNV-1a 64-bit hash function for null-terminated strings.
Definition hash.c:58
uint32_t solidc_elf_hash(const void *key)
ELF hash function for null-terminated strings.
Definition hash.c:74
uint32_t solidc_sdbm_hash(const void *key)
SDBM hash function for null-terminated strings.
Definition hash.c:95
uint32_t solidc_djb2a_hash(const void *key)
DJB2A hash function (XOR variant) for null-terminated strings.
Definition hash.c:26
uint32_t solidc_murmur_hash(const char *key, uint32_t len, uint32_t seed)
MurmurHash3 32-bit hash function.
Definition hash.c:128
uint32_t solidc_djb2_hash(const void *key)
DJB2 hash function for null-terminated strings.
Definition hash.c:10