1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
// hash.h --- hashing functions -*- C -*-
//
// Copyright (c) 2026 Martin Michalec <martin@michalec.dev>
//
// SPDX-License-Identifier: MIT
//
// This file is part of COSMOS Codebase.
//
// Commentary:
//
// Code:
#ifndef INCLUDED_COSMOS_HASH_H
#define INCLUDED_COSMOS_HASH_H
#include "types.h"
#ifndef HASH_NO_FEAT_CRC_32
#define HASH_CRC_32_INIT 0xffffffff
u32 hash_crc_32_update (u32 crc, const void *buf, usz len);
static inline u32
hash_crc_32_final (u32 crc)
{
return crc ^ 0xffffffff;
}
static inline u32
hash_crc_32 (const void *buf, usz len)
{
return hash_crc_32_final (hash_crc_32_update (HASH_CRC_32_INIT, buf, len));
}
#endif // HASH_NO_FEAT_CRC_32
#ifndef HASH_NO_FEAT_FNV1A_32
#define HASH_FNV1A_32_INIT 0x811c9dc5
#define HASH_FNV1A_32_PRIME 0x01000193
static inline u32
hash_fnv1a_32_update (u32 hash, const void *buf, usz sz)
{
const u8 *ptr = buf;
const u8 *end = ptr + sz;
for (; ptr < end; ptr++) {
hash ^= *ptr;
hash *= HASH_FNV1A_32_PRIME;
}
return hash;
}
static inline u32
hash_fnv1a_32 (const void *buf, usz sz)
{
return hash_fnv1a_32_update (HASH_FNV1A_32_INIT, buf, sz);
}
#endif // HASH_NO_FEAT_FNV1A_32
#ifndef HASH_NO_FEAT_FNV1A_64
#define HASH_FNV1A_64_INIT 0xcbf29ce484222325
#define HASH_FNV1A_64_PRIME 0x00000100000001b3
static inline u64
hash_fnv1a_64_update (u64 h, const void *buf, usz sz)
{
const u8 *p = (const u8 *) buf;
const u8 *end = p + sz;
while (p < end) {
h ^= *p++;
h *= HASH_FNV1A_64_PRIME;
}
return h;
}
static inline u64
hash_fnv1a_64 (const void *buf, usz sz)
{
return hash_fnv1a_64_update (HASH_FNV1A_64_INIT, buf, sz);
}
#endif // HASH_NO_FEAT_FNV1A_64
#endif // INCLUDED_COSMOS_HASH_H
// hash.h ends here
|