Embedded Template Library 1.0
fnv_1.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2014 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_FNV_1_INCLUDED
32#define ETL_FNV_1_INCLUDED
33
34#include "platform.h"
35#include "static_assert.h"
36#include "type_traits.h"
37#include "ihash.h"
39
40#include <stdint.h>
41
42#if defined(ETL_COMPILER_KEIL)
43#pragma diag_suppress 1300
44#endif
45
48
49namespace etl
50{
51#if ETL_USING_64BIT_TYPES
52 //***************************************************************************
55 //***************************************************************************
57 {
58 typedef uint64_t value_type;
59
60 uint64_t initial() const
61 {
62 return OFFSET_BASIS;
63 }
64
65 uint64_t add(uint64_t hash, uint8_t value) const
66 {
67 hash *= PRIME;
68 hash ^= value;
69 return hash;
70 }
71
72 uint64_t final(uint64_t hash) const
73 {
74 return hash;
75 }
76
77 static ETL_CONSTANT uint64_t OFFSET_BASIS = 0xCBF29CE484222325ull;
78 static ETL_CONSTANT uint64_t PRIME = 0x00000100000001b3ull;
79 };
80
81 //***************************************************************************
84 //***************************************************************************
85 class fnv_1_64 : public etl::frame_check_sequence<fnv_1_policy_64>
86 {
87 public:
88
89 //*************************************************************************
91 //*************************************************************************
93 {
94 this->reset();
95 }
96
97 //*************************************************************************
101 //*************************************************************************
102 template<typename TIterator>
103 fnv_1_64(TIterator begin, const TIterator end)
104 {
105 this->reset();
106 this->add(begin, end);
107 }
108 };
109
110 //***************************************************************************
113 //***************************************************************************
115 {
116 typedef uint64_t value_type;
117
118 uint64_t initial() const
119 {
120 return OFFSET_BASIS;
121 }
122
123 uint64_t add(uint64_t hash, uint8_t value) const
124 {
125 hash ^= value;
126 hash *= PRIME;
127 return hash;
128 }
129
130 uint64_t final(uint64_t hash) const
131 {
132 return hash;
133 }
134
135 static ETL_CONSTANT uint64_t OFFSET_BASIS = 0xCBF29CE484222325ull;
136 static ETL_CONSTANT uint64_t PRIME = 0x00000100000001b3ull;
137 };
138
139 //***************************************************************************
142 //***************************************************************************
143 class fnv_1a_64 : public etl::frame_check_sequence<fnv_1a_policy_64>
144 {
145 public:
146
147 //*************************************************************************
149 //*************************************************************************
151 {
152 this->reset();
153 }
154
155 //*************************************************************************
159 //*************************************************************************
160 template<typename TIterator>
161 fnv_1a_64(TIterator begin, const TIterator end)
162 {
163 this->reset();
164 this->add(begin, end);
165 }
166 };
167#endif
168
169 //***************************************************************************
172 //***************************************************************************
174 {
175 typedef uint32_t value_type;
176
177 uint32_t initial() const
178 {
179 return OFFSET_BASIS;
180 }
181
182 uint32_t add(uint32_t hash, uint8_t value) const
183 {
184 hash *= PRIME;
185 hash ^= value;
186 return hash;
187 }
188
189 uint32_t final(uint32_t hash) const
190 {
191 return hash;
192 }
193
194 static ETL_CONSTANT uint32_t OFFSET_BASIS = 0x811C9DC5UL;
195 static ETL_CONSTANT uint32_t PRIME = 0x01000193UL;
196 };
197
198 //***************************************************************************
201 //***************************************************************************
202 class fnv_1_32 : public etl::frame_check_sequence<fnv_1_policy_32>
203 {
204 public:
205
206 //*************************************************************************
208 //*************************************************************************
210 {
211 this->reset();
212 }
213
214 //*************************************************************************
218 //*************************************************************************
219 template<typename TIterator>
220 fnv_1_32(TIterator begin, const TIterator end)
221 {
222 this->reset();
223 this->add(begin, end);
224 }
225 };
226
227 //***************************************************************************
230 //***************************************************************************
232 {
233 typedef uint32_t value_type;
234
235 uint32_t initial() const
236 {
237 return OFFSET_BASIS;
238 }
239
240 uint32_t add(uint32_t hash, uint8_t value) const
241 {
242 hash ^= value;
243 hash *= PRIME;
244 return hash;
245 }
246
247 uint32_t final(uint32_t hash) const
248 {
249 return hash;
250 }
251
252 static ETL_CONSTANT uint32_t OFFSET_BASIS = 0x811C9DC5UL;
253 static ETL_CONSTANT uint32_t PRIME = 0x01000193UL;
254 };
255
256 //***************************************************************************
259 //***************************************************************************
260 class fnv_1a_32 : public etl::frame_check_sequence<fnv_1a_policy_32>
261 {
262 public:
263
264 //*************************************************************************
266 //*************************************************************************
268 {
269 this->reset();
270 }
271
272 //*************************************************************************
276 //*************************************************************************
277 template<typename TIterator>
278 fnv_1a_32(TIterator begin, const TIterator end)
279 {
280 this->reset();
281 this->add(begin, end);
282 }
283 };
284}
285
286#endif
Definition: fnv_1.h:203
fnv_1_32(TIterator begin, const TIterator end)
Definition: fnv_1.h:220
fnv_1_32()
Default constructor.
Definition: fnv_1.h:209
Definition: fnv_1.h:86
fnv_1_64(TIterator begin, const TIterator end)
Definition: fnv_1.h:103
fnv_1_64()
Default constructor.
Definition: fnv_1.h:92
Definition: fnv_1.h:261
fnv_1a_32()
Default constructor.
Definition: fnv_1.h:267
fnv_1a_32(TIterator begin, const TIterator end)
Definition: fnv_1.h:278
Definition: fnv_1.h:144
fnv_1a_64()
Default constructor.
Definition: fnv_1.h:150
fnv_1a_64(TIterator begin, const TIterator end)
Definition: fnv_1.h:161
void reset()
Resets the FCS to the initial state.
Definition: frame_check_sequence.h:134
void add(TIterator begin, const TIterator end)
Definition: frame_check_sequence.h:145
Definition: frame_check_sequence.h:100
bitset_ext
Definition: absolute.h:38
ETL_CONSTEXPR TContainer::iterator begin(TContainer &container)
Definition: iterator.h:931
ETL_CONSTEXPR TContainer::iterator end(TContainer &container)
Definition: iterator.h:961
Definition: fnv_1.h:174
Definition: fnv_1.h:57
Definition: fnv_1.h:232
Definition: fnv_1.h:115