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