Pioneer
FNV1a.h
Go to the documentation of this file.
1 // This file was released as public domain | original author: ruby0x1
2 // https://gist.github.com/ruby0x1/81308642d0325fd386237cfa3b44785c
3 
4 #pragma once
5 #include <stddef.h>
6 #include <stdint.h>
7 
8 //fnv1a 32 and 64 bit hash functions
9 // key is the data to hash, len is the size of the data (or how much of it to hash against)
10 // code license: public domain or equivalent
11 // post: https://notes.underscorediscovery.com/constexpr-fnv1a/
12 
13 inline constexpr uint32_t hash_32_fnv1a(const char *const data, size_t len)
14 {
15 
16  uint32_t hash = 0x811c9dc5;
17  uint32_t prime = 0x1000193;
18 
19  for (uint32_t i = 0; i < len; ++i) {
20  uint8_t value = data[i];
21  hash = hash ^ value;
22  hash *= prime;
23  }
24 
25  return hash;
26 
27 } //hash_32_fnv1a
28 
29 inline constexpr uint64_t hash_64_fnv1a(const char *const data, size_t len)
30 {
31 
32  uint64_t hash = 0xcbf29ce484222325;
33  uint64_t prime = 0x100000001b3;
34 
35  for (uint64_t i = 0; i < len; ++i) {
36  uint8_t value = data[i];
37  hash = hash ^ value;
38  hash *= prime;
39  }
40 
41  return hash;
42 
43 } //hash_64_fnv1a
constexpr uint32_t hash_32_fnv1a(const char *const data, size_t len)
Definition: FNV1a.h:13
constexpr uint64_t hash_64_fnv1a(const char *const data, size_t len)
Definition: FNV1a.h:29