-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFxHash.hpp
More file actions
48 lines (37 loc) · 1.01 KB
/
FxHash.hpp
File metadata and controls
48 lines (37 loc) · 1.01 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
#pragma once
#include "FxTypes.hpp"
#define FX_HASH_FNV1A_SEED 0x811C9DC5
#define FX_HASH_FNV1A_PRIME 0x01000193
using FxHash = uint32;
/**
* Hashes a string at compile time using FNV-1a.
*
* Source to algorithm: http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param
*/
inline constexpr FxHash FxHashStr(const char *str)
{
uint32 hash = FX_HASH_FNV1A_SEED;
unsigned char ch;
while ((ch = static_cast<unsigned char>(*(str++)))) {
hash = (hash ^ ch) * FX_HASH_FNV1A_PRIME;
}
return hash;
}
/**
* Hashes a string at compile time using FNV-1a.
*
* Source to algorithm: http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param
*/
inline constexpr FxHash FxHashStr(const char *str, uint32 length)
{
uint32 hash = FX_HASH_FNV1A_SEED;
unsigned char ch;
for (uint32 i = 0; i < length; i++) {
ch = static_cast<unsigned char>(str[i]);
if (ch == 0) {
return hash;
}
hash = (hash ^ ch) * FX_HASH_FNV1A_PRIME;
}
return hash;
}