-
Notifications
You must be signed in to change notification settings - Fork 2
/
fnv.h
48 lines (37 loc) · 897 Bytes
/
fnv.h
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
#pragma once
#include "stdint.h"
typedef struct fnv_hash {
uint64_t state = 14695981039346656037u;
fnv_hash(uint32_t seed) {
add(seed);
}
fnv_hash& add(uint8_t value) {
state = state ^ value;
state = state * 1099511628211u;
return *this;
}
fnv_hash& add(uint16_t value) {
return this->add((uint8_t)value).add((uint8_t)(value >> 8));
}
fnv_hash& add(uint32_t value) {
return this->add((uint16_t)value).add((uint16_t)(value >> 16));
}
fnv_hash& add(uint64_t value) {
return this->add((uint32_t)value).add((uint32_t)(value >> 32));
}
uint64_t hash64() {
return state;
}
uint32_t hash32() {
auto hash = hash64();
return hash ^ (hash >> 32);
}
uint16_t hash16() {
auto hash = hash32();
return hash ^ (hash >> 16);
}
uint8_t hash8() {
auto hash = hash16();
return hash ^ (hash >> 8);
}
} fnv_hash;