MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
hash.h
Go to the documentation of this file.
1#pragma once
2
3#include "mim/util/types.h"
4
5namespace mim {
6
7/// @name Aliases for some Base Types
8///@{
9using hash_t = uint32_t;
10///@}
11
12/// @name Murmur3 Hash
13/// See [Wikipedia](https://en.wikipedia.org/wiki/MurmurHash).
14///@{
16 k *= 0xcc9e2d51;
17 k = (k << 15) | (k >> 17);
18 k *= 0x1b873593;
19 return k;
20}
21
22inline hash_t murmur3(hash_t h, uint32_t key) {
23 h ^= murmur_32_scramble(key);
24 h = (h << 13) | (h >> 19);
25 h = h * 5 + 0xe6546b64;
26 return h;
27}
28
29inline hash_t murmur3(hash_t h, uint64_t key) {
30 hash_t k = hash_t(key);
31 h ^= murmur_32_scramble(k);
32 h = (h << 13) | (h >> 19);
33 h = h * 5 + 0xe6546b64;
34 k = hash_t(key >> 32);
35 h ^= murmur_32_scramble(k);
36 h = (h << 13) | (h >> 19);
37 h = h * 5 + 0xe6546b64;
38 return h;
39}
40
41inline hash_t murmur3_rest(hash_t h, uint8_t key) {
42 h ^= murmur_32_scramble(key);
43 return h;
44}
45
46inline hash_t murmur3_rest(hash_t h, uint16_t key) {
47 h ^= murmur_32_scramble(key);
48 return h;
49}
50
52 h ^= len;
53 h ^= h >> 16;
54 h *= 0x85ebca6b;
55 h ^= h >> 13;
56 h *= 0xc2b2ae35;
57 h ^= h >> 16;
58 return h;
59}
60
61/// Use for a single value to hash.
63 h ^= h >> 16;
64 h *= 0x85ebca6b;
65 h ^= h >> 13;
66 h *= 0xc2b2ae35;
67 h ^= h >> 16;
68 return h;
69}
70///@}
71
72/// @name FNV-1 Hash
73/// See [Wikipedia](https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV-1_hash).
74///@{
75/// [Magic numbers](http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-var) for FNV-1 hash.
76struct FNV1 {
77 static const hash_t offset = 2166136261_u32;
78 static const hash_t prime = 16777619_u32;
79};
80
81/// Returns a new hash by combining the hash @p seed with @p val.
82template<class T> hash_t hash_combine(hash_t seed, T v) {
83 static_assert(std::is_signed<T>::value || std::is_unsigned<T>::value, "please provide your own hash function");
84
85 hash_t val = v;
86 for (hash_t i = 0; i < sizeof(T); ++i) {
87 hash_t octet = val & 0xff_u32; // extract lower 8 bits
88 seed ^= octet;
89 seed *= FNV1::prime;
90 val >>= 8_u32;
91 }
92 return seed;
93}
94
95template<class T> hash_t hash_combine(hash_t seed, T* val) { return hash_combine(seed, uintptr_t(val)); }
96
97template<class T, class... Args> hash_t hash_combine(hash_t seed, T val, Args&&... args) {
98 return hash_combine(hash_combine(seed, val), std::forward<Args>(args)...);
99}
100
101template<class T> hash_t hash_begin(T val) { return hash_combine(FNV1::offset, val); }
102inline hash_t hash_begin() { return FNV1::offset; }
103///@}
104
105/// @name String Hashing
106///@{
107hash_t hash(const char*);
108hash_t hash(std::string_view);
109///@}
110
111} // namespace mim
Definition cfg.h:11
hash_t murmur_32_scramble(hash_t k)
Definition hash.h:15
hash_t hash_combine(hash_t seed, T v)
Returns a new hash by combining the hash seed with val.
Definition hash.h:82
hash_t murmur3(hash_t h, uint32_t key)
Definition hash.h:22
hash_t hash_begin()
Definition hash.h:102
uint32_t hash_t
Definition hash.h:9
hash_t hash(const char *)
Definition hash.cpp:5
hash_t murmur3_finalize(hash_t h, hash_t len)
Definition hash.h:51
hash_t murmur3_rest(hash_t h, uint8_t key)
Definition hash.h:41
static const hash_t offset
Definition hash.h:77
static const hash_t prime
Definition hash.h:78