Thorin 1.9.0
The Higher ORder INtermediate representation
Loading...
Searching...
No Matches
hash.h
Go to the documentation of this file.
1#pragma once
2
3#include "thorin/util/types.h"
4
5namespace thorin {
6
7/// @name Aliases for some Base Types
8///@{
9using hash_t = uint32_t;
10///@}
11
12/// @name Murmur3 Hash
13///@{
14/// See [Wikipedia](https://en.wikipedia.org/wiki/MurmurHash).
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///@{
74/// See [Wikipedia](https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV-1_hash).
75
76/// [Magic numbers](http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-var) for FNV-1 hash.
77struct FNV1 {
78 static const hash_t offset = 2166136261_u32;
79 static const hash_t prime = 16777619_u32;
80};
81
82/// Returns a new hash by combining the hash @p seed with @p val.
83template<class T> hash_t hash_combine(hash_t seed, T v) {
84 static_assert(std::is_signed<T>::value || std::is_unsigned<T>::value, "please provide your own hash function");
85
86 hash_t val = v;
87 for (hash_t i = 0; i < sizeof(T); ++i) {
88 hash_t octet = val & 0xff_u32; // extract lower 8 bits
89 seed ^= octet;
90 seed *= FNV1::prime;
91 val >>= 8_u32;
92 }
93 return seed;
94}
95
96template<class T> hash_t hash_combine(hash_t seed, T* val) { return hash_combine(seed, uintptr_t(val)); }
97
98template<class T, class... Args> hash_t hash_combine(hash_t seed, T val, Args&&... args) {
99 return hash_combine(hash_combine(seed, val), std::forward<Args>(args)...);
100}
101
102template<class T> hash_t hash_begin(T val) { return hash_combine(FNV1::offset, val); }
103inline hash_t hash_begin() { return FNV1::offset; }
104///@}
105
106/// @name String Hashing
107///@{
108hash_t hash(const char*);
109hash_t hash(std::string_view);
110///@}
111
112} // namespace thorin
Definition cfg.h:11
hash_t murmur3_finalize(hash_t h, hash_t len)
Definition hash.h:51
hash_t hash_begin()
Definition hash.h:103
hash_t hash_combine(hash_t seed, T v)
Returns a new hash by combining the hash seed with val.
Definition hash.h:83
uint32_t hash_t
Definition hash.h:9
hash_t hash(const char *)
Definition hash.cpp:5
hash_t murmur_32_scramble(hash_t k)
Definition hash.h:15
hash_t murmur3(hash_t h, uint32_t key)
Definition hash.h:22
hash_t murmur3_rest(hash_t h, uint8_t key)
Definition hash.h:41
Magic numbers for FNV-1 hash.
Definition hash.h:77
static const hash_t offset
Definition hash.h:78
static const hash_t prime
Definition hash.h:79