3D Graphics Engine for ESP32
 
Loading...
Searching...
No Matches
fnv.hpp
Go to the documentation of this file.
1/*
2 * FNV
3 * Copyright © 2017+ Ángel Rodríguez Ballesteros
4 *
5 * Distributed under the Boost Software License, version 1.0
6 * See documents/LICENSE.TXT or www.boost.org/LICENSE_1_0.txt
7 *
8 * angel.rodriguez@esne.edu
9 *
10 * C1712171830
11 */
12
13#ifndef BASICS_FNV_HEADER
14#define BASICS_FNV_HEADER
15
16 #include <string>
17 #include <cstdint>
18
19 namespace basics
20 {
21
22 namespace internal
23 {
24
25 constexpr uint32_t fnv_basis_32 = 0x811c9dc5u;
26 constexpr uint32_t fnv_prime_32 = 0x01000193u;
27 constexpr uint64_t fnv_basis_64 = 0xcbf29ce484222325u;
28 constexpr uint64_t fnv_prime_64 = 0x00000100000001b3u;
29
30 template< size_t LENGTH >
31 constexpr uint32_t static_fnv32 (const char * chars)
32 {
33 return (static_fnv32< LENGTH - 1 > (chars) ^ chars[LENGTH - 2]) * fnv_prime_32;
34 }
35
36 template< >
37 constexpr uint32_t static_fnv32< 1 > (const char * )
38 {
39 return fnv_basis_32;
40 }
41
42 template< size_t LENGTH >
43 constexpr uint64_t static_fnv64 (const char * chars)
44 {
45 return (static_fnv64< LENGTH - 1 > (chars) ^ chars[LENGTH - 2]) * fnv_prime_64;
46 }
47
48 template< >
49 constexpr uint64_t static_fnv64< 1 > (const char * )
50 {
51 return fnv_basis_64;
52 }
53
54 }
55
56 // -----------------------------------------------------------------------------------------
57
65 template< size_t LENGTH >
66 constexpr uint32_t static_fnv32 (const char (& chars)[LENGTH])
67 {
69 }
70
71 // -----------------------------------------------------------------------------------------
72
73 template< size_t LENGTH >
74 constexpr uint64_t static_fnv64 (const char (& chars)[LENGTH])
75 {
77 }
78
79 // -----------------------------------------------------------------------------------------
80
81 #if BASICS_INT_SIZE == 4
82
83 template< size_t LENGTH >
84 constexpr unsigned static_fnv (const char (& chars)[LENGTH])
85 {
87 }
88
89 #else
90
91 template< size_t LENGTH >
92 constexpr unsigned static_fnv (const char (& chars)[LENGTH])
93 {
94 return static_cast < unsigned > (internal::static_fnv64< LENGTH > (chars));
95 }
96
97 #endif
98
99 // -----------------------------------------------------------------------------------------
100
101 template< size_t LENGTH >
102 uint32_t fnv32 (const char (& chars)[LENGTH])
103 {
104 uint32_t hash = internal::fnv_basis_32;
105
106 for (size_t index = 0; index < LENGTH; ++index)
107 {
108 hash ^= chars[index]; // Use array indexing instead
110 }
111
112 return hash;
113 }
114
115 inline uint32_t fnv32 (const std::string & s)
116 {
117 uint32_t hash = internal::fnv_basis_32;
118
119 for (auto c : s)
120 {
121 hash ^= c;
123 }
124
125 return hash;
126 }
127
128 }
129
130 constexpr unsigned operator "" _fnv (const char * c)
131 {
132 return c ? 1 : operator "" _fnv ("2");
133 }
134
135 // ---------------------------------------------------------------------------------------------
136
137 #define FNV(X) basics::static_fnv (#X)
138 #define FNV32(X) basics::static_fnv32 (#X)
139 #define FNV64(X) basics::static_fnv64 (#X)
140
141#endif
Definition fnv.hpp:23
constexpr uint32_t static_fnv32(const char *chars)
Definition fnv.hpp:31
constexpr uint64_t static_fnv64(const char *chars)
Definition fnv.hpp:43
constexpr uint32_t fnv_prime_32
Definition fnv.hpp:26
constexpr uint32_t static_fnv32< 1 >(const char *)
Definition fnv.hpp:37
constexpr uint64_t static_fnv64< 1 >(const char *)
Definition fnv.hpp:49
constexpr uint64_t fnv_prime_64
Definition fnv.hpp:28
constexpr uint64_t fnv_basis_64
Definition fnv.hpp:27
constexpr uint32_t fnv_basis_32
Definition fnv.hpp:25
Definition fnv.hpp:20
constexpr unsigned static_fnv(const char(&chars)[LENGTH])
Definition fnv.hpp:92
constexpr uint32_t static_fnv32(const char(&chars)[LENGTH])
Definition fnv.hpp:66
uint32_t fnv32(const char(&chars)[LENGTH])
Definition fnv.hpp:102
constexpr uint64_t static_fnv64(const char(&chars)[LENGTH])
Definition fnv.hpp:74