-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbase_hash.h
More file actions
81 lines (69 loc) · 1.73 KB
/
Copy pathbase_hash.h
File metadata and controls
81 lines (69 loc) · 1.73 KB
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
#ifndef LJRE_BASE_HASH_H
#define LJRE_BASE_HASH_H
#include "base.h"
// NOTE(ljre): FNV-1a implementation.
static inline FORCE_INLINE uint64
HashFnv1a(String memory)
{
Trace();
uint64 result = 14695981039346656037u;
for (intz i = 0; i < memory.size; ++i)
{
uint64 value = (uint64)memory.data[i] & 0xff;
result ^= value;
result *= 1099511628211u;
}
return result;
}
static inline FORCE_INLINE uint64
HashString(String memory)
{
return HashFnv1a(memory);
}
// NOTE(ljre): Perfect hash of 32bit integer permutation.
// Name: lowbias32
// https://github.com/skeeto/hash-prospector
static inline FORCE_INLINE uint32
HashInt32(uint32 x)
{
x ^= x >> 16;
x *= 0x7feb352d;
x ^= x >> 15;
x *= 0x846ca68b;
x ^= x >> 16;
return x;
}
// NOTE(ljre): Perfect hash of 64bit integer permitation.
// Name: SplittableRandom / SplitMix64
// https://xoshiro.di.unimi.it/splitmix64.c
static inline FORCE_INLINE uint64
HashInt64(uint64 x)
{
x ^= x >> 30;
x *= 0xbf58476d1ce4e5b9;
x ^= x >> 27;
x *= 0x94d049bb133111eb;
x ^= x >> 31;
return x;
}
// NOTE(ljre): This is a implementation of "MSI hash table".
// https://nullprogram.com/blog/2022/08/08/
static inline FORCE_INLINE intz
HashMsi(uint32 log2_of_cap, uint64 hash, intz index)
{
uint32 exp = log2_of_cap;
uint32 mask = (1u << exp) - 1;
uint32 step = (uint32)(hash >> (64 - exp)) | 1;
return (index + step) & mask;
}
static inline FORCE_INLINE uint64
HashCombine64(uint64 x, uint64 y)
{
return x ^ (y * 0x9e3779b97f4a7c15 + (x << 6) + (x >> 2));
}
static inline FORCE_INLINE uint32
HashCombine32(uint32 x, uint32 y)
{
return x ^ (y * 0x9e3779b9 + (x << 6) + (x >> 2));
}
#endif //LJRE_BASE_HASH_H