Embedded Template Library 1.0
Loading...
Searching...
No Matches
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"
36#include "ihash.h"
37#include "type_traits.h"
38
39#include <stdint.h>
40
41#if defined(ETL_COMPILER_KEIL)
42 #pragma diag_suppress 1300
43#endif
44
47
48namespace etl
49{
50#if ETL_USING_64BIT_TYPES
51 //***************************************************************************
54 //***************************************************************************
56 {
57 typedef uint64_t value_type;
58
59 uint64_t initial() const
60 {
61 return OFFSET_BASIS;
62 }
63
64 uint64_t add(uint64_t hash, uint8_t value) const
65 {
66 hash *= PRIME;
67 hash ^= value;
68 return hash;
69 }
70
71 uint64_t final(uint64_t hash) const
72 {
73 return hash;
74 }
75
76 static ETL_CONSTANT uint64_t OFFSET_BASIS = 0xCBF29CE484222325ull;
77 static ETL_CONSTANT uint64_t PRIME = 0x00000100000001b3ull;
78 };
79
80 //***************************************************************************
83 //***************************************************************************
84 class fnv_1_64 : public etl::frame_check_sequence<fnv_1_policy_64>
85 {
86 public:
87
88 //*************************************************************************
90 //*************************************************************************
92 {
93 this->reset();
94 }
95
96 //*************************************************************************
100 //*************************************************************************
101 template <typename TIterator>
102 fnv_1_64(TIterator begin, const TIterator end)
103 {
104 this->reset();
105 this->add(begin, end);
106 }
107 };
108
109 //***************************************************************************
112 //***************************************************************************
114 {
115 typedef uint64_t value_type;
116
117 uint64_t initial() const
118 {
119 return OFFSET_BASIS;
120 }
121
122 uint64_t add(uint64_t hash, uint8_t value) const
123 {
124 hash ^= value;
125 hash *= PRIME;
126 return hash;
127 }
128
129 uint64_t final(uint64_t hash) const
130 {
131 return hash;
132 }
133
134 static ETL_CONSTANT uint64_t OFFSET_BASIS = 0xCBF29CE484222325ull;
135 static ETL_CONSTANT uint64_t PRIME = 0x00000100000001b3ull;
136 };
137
138 //***************************************************************************
141 //***************************************************************************
142 class fnv_1a_64 : public etl::frame_check_sequence<fnv_1a_policy_64>
143 {
144 public:
145
146 //*************************************************************************
148 //*************************************************************************
150 {
151 this->reset();
152 }
153
154 //*************************************************************************
158 //*************************************************************************
159 template <typename TIterator>
160 fnv_1a_64(TIterator begin, const TIterator end)
161 {
162 this->reset();
163 this->add(begin, end);
164 }
165 };
166#endif
167
168 //***************************************************************************
171 //***************************************************************************
173 {
174 typedef uint32_t value_type;
175
176 uint32_t initial() const
177 {
178 return OFFSET_BASIS;
179 }
180
181 uint32_t add(uint32_t hash, uint8_t value) const
182 {
183 hash *= PRIME;
184 hash ^= value;
185 return hash;
186 }
187
188 uint32_t final(uint32_t hash) const
189 {
190 return hash;
191 }
192
193 static ETL_CONSTANT uint32_t OFFSET_BASIS = 0x811C9DC5UL;
194 static ETL_CONSTANT uint32_t PRIME = 0x01000193UL;
195 };
196
197 //***************************************************************************
200 //***************************************************************************
201 class fnv_1_32 : public etl::frame_check_sequence<fnv_1_policy_32>
202 {
203 public:
204
205 //*************************************************************************
207 //*************************************************************************
209 {
210 this->reset();
211 }
212
213 //*************************************************************************
217 //*************************************************************************
218 template <typename TIterator>
219 fnv_1_32(TIterator begin, const TIterator end)
220 {
221 this->reset();
222 this->add(begin, end);
223 }
224 };
225
226 //***************************************************************************
229 //***************************************************************************
231 {
232 typedef uint32_t value_type;
233
234 uint32_t initial() const
235 {
236 return OFFSET_BASIS;
237 }
238
239 uint32_t add(uint32_t hash, uint8_t value) const
240 {
241 hash ^= value;
242 hash *= PRIME;
243 return hash;
244 }
245
246 uint32_t final(uint32_t hash) const
247 {
248 return hash;
249 }
250
251 static ETL_CONSTANT uint32_t OFFSET_BASIS = 0x811C9DC5UL;
252 static ETL_CONSTANT uint32_t PRIME = 0x01000193UL;
253 };
254
255 //***************************************************************************
258 //***************************************************************************
259 class fnv_1a_32 : public etl::frame_check_sequence<fnv_1a_policy_32>
260 {
261 public:
262
263 //*************************************************************************
265 //*************************************************************************
267 {
268 this->reset();
269 }
270
271 //*************************************************************************
275 //*************************************************************************
276 template <typename TIterator>
277 fnv_1a_32(TIterator begin, const TIterator end)
278 {
279 this->reset();
280 this->add(begin, end);
281 }
282 };
283} // namespace etl
284
285#endif
fnv_1_32(TIterator begin, const TIterator end)
Definition fnv_1.h:219
fnv_1_32()
Default constructor.
Definition fnv_1.h:208
fnv_1_64(TIterator begin, const TIterator end)
Definition fnv_1.h:102
fnv_1_64()
Default constructor.
Definition fnv_1.h:91
fnv_1a_32()
Default constructor.
Definition fnv_1.h:266
fnv_1a_32(TIterator begin, const TIterator end)
Definition fnv_1.h:277
fnv_1a_64()
Default constructor.
Definition fnv_1.h:149
fnv_1a_64(TIterator begin, const TIterator end)
Definition fnv_1.h:160
ETL_CONSTEXPR14 void add(TIterator begin, const TIterator end)
Definition frame_check_sequence.h:145
ETL_CONSTEXPR14 void reset()
Definition frame_check_sequence.h:134
Definition frame_check_sequence.h:98
Definition absolute.h:40
TContainer::iterator end(TContainer &container)
Definition iterator.h:1166
TContainer::iterator begin(TContainer &container)
Definition iterator.h:1136
Definition fnv_1.h:173
Definition fnv_1.h:56
Definition fnv_1.h:231
Definition fnv_1.h:114