Stefan Scholz / ETL
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers fnv_1.h Source File

fnv_1.h

Go to the documentation of this file.
00001 ///\file
00002 
00003 /******************************************************************************
00004 The MIT License(MIT)
00005 
00006 Embedded Template Library.
00007 https://github.com/ETLCPP/etl
00008 http://www.etlcpp.com
00009 
00010 Copyright(c) 2014 jwellbelove
00011 
00012 Permission is hereby granted, free of charge, to any person obtaining a copy
00013 of this software and associated documentation files(the "Software"), to deal
00014 in the Software without restriction, including without limitation the rights
00015 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
00016 copies of the Software, and to permit persons to whom the Software is
00017 furnished to do so, subject to the following conditions :
00018 
00019 The above copyright notice and this permission notice shall be included in all
00020 copies or substantial portions of the Software.
00021 
00022 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00023 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00024 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
00025 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00026 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00027 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00028 SOFTWARE.
00029 ******************************************************************************/
00030 
00031 #ifndef __ETL_FNV_1__
00032 #define __ETL_FNV_1__
00033 
00034 #include <stdint.h>
00035 
00036 #include "platform.h "
00037 #include "static_assert.h"
00038 #include "type_traits.h "
00039 #include "ihash.h "
00040 #include "frame_check_sequence.h "
00041 
00042 
00043 #if defined(ETL_COMPILER_KEIL)
00044 #pragma diag_suppress 1300 
00045 #endif
00046 
00047 ///\defgroup fnv_1 FNV-1 & FNV-1a 32 & 64 bit hash calculations
00048 ///\ingroup maths
00049 
00050 namespace etl
00051 {
00052   //***************************************************************************
00053   /// fnv_1 policy.
00054   /// Calculates FNV1.
00055   //***************************************************************************
00056   struct fnv_1_policy_64
00057   {
00058     typedef uint64_t value_type;
00059 
00060     inline uint64_t initial() const
00061     {
00062       return OFFSET_BASIS;
00063     }
00064 
00065     inline uint64_t add(uint64_t hash, uint8_t value) const
00066     {
00067       hash *= PRIME;
00068       hash ^= value;
00069       return  hash;
00070     }
00071 
00072     inline uint64_t final(uint64_t hash) const
00073     {
00074       return hash;
00075     }
00076 
00077     static const uint64_t OFFSET_BASIS = 0xCBF29CE484222325;
00078     static const uint64_t PRIME        = 0x00000100000001b3;
00079   };
00080   
00081   //***************************************************************************
00082   /// Calculates the fnv_1_64 hash.
00083   ///\ingroup fnv_1_64
00084   //***************************************************************************
00085   class fnv_1_64 : public etl::frame_check_sequence<fnv_1_policy_64>
00086   {
00087   public:
00088 
00089     //*************************************************************************
00090     /// Default constructor.
00091     //*************************************************************************
00092     fnv_1_64()
00093     {
00094       this->reset();
00095     }
00096 
00097     //*************************************************************************
00098     /// Constructor from range.
00099     /// \param begin Start of the range.
00100     /// \param end   End of the range.
00101     //*************************************************************************
00102     template<typename TIterator>
00103     fnv_1_64(TIterator begin, const TIterator end)
00104     {
00105       this->reset();
00106       this->add(begin, end);
00107     }
00108   };
00109 
00110   //***************************************************************************
00111   /// fnv_1a policy.
00112   /// Calculates FNV1A.
00113   //***************************************************************************
00114   struct fnv_1a_policy_64
00115     {
00116     typedef uint64_t value_type;
00117 
00118     inline uint64_t initial() const
00119       {
00120       return OFFSET_BASIS;
00121     }
00122 
00123     inline uint64_t add(uint64_t hash, uint8_t value) const
00124     {
00125       hash ^= value;
00126       hash *= PRIME;
00127       return hash;
00128     }
00129 
00130     inline uint64_t final(uint64_t hash) const
00131     {
00132       return hash;
00133     }
00134 
00135     static const uint64_t OFFSET_BASIS = 0xCBF29CE484222325;
00136     static const uint64_t PRIME        = 0x00000100000001b3;
00137   };
00138 
00139   //***************************************************************************
00140   /// Calculates the fnv_1a_64 hash.
00141   ///\ingroup fnv_1a_64
00142   //***************************************************************************
00143   class fnv_1a_64 : public etl::frame_check_sequence<fnv_1a_policy_64>
00144   {
00145   public:
00146 
00147     //*************************************************************************
00148     /// Default constructor.
00149     //*************************************************************************
00150     fnv_1a_64()
00151     {
00152       this->reset();
00153     }
00154 
00155     //*************************************************************************
00156     /// Constructor from range.
00157     /// \param begin Start of the range.
00158     /// \param end   End of the range.
00159     //*************************************************************************
00160     template<typename TIterator>
00161     fnv_1a_64(TIterator begin, const TIterator end)
00162     {
00163       this->reset();
00164       this->add(begin, end);
00165     }
00166   };
00167 
00168   //***************************************************************************
00169   /// fnv_1 policy.
00170   /// Calculates FNV1.
00171   //***************************************************************************
00172   struct fnv_1_policy_32
00173     {
00174     typedef uint32_t value_type;
00175       
00176     inline uint32_t initial() const
00177       {
00178       return OFFSET_BASIS;
00179     }
00180 
00181     inline uint32_t add(uint32_t hash, uint8_t value) const
00182     {
00183       hash *= PRIME;
00184       hash ^= value;
00185       return hash;
00186     }
00187 
00188     inline uint32_t final(uint32_t hash) const
00189     {
00190       return hash;
00191     }
00192 
00193     static const uint32_t OFFSET_BASIS = 0x811C9DC5;
00194     static const uint32_t PRIME        = 0x01000193;
00195   };
00196 
00197   //***************************************************************************
00198   /// Calculates the fnv_1_32 hash.
00199   ///\ingroup fnv_1_32
00200   //***************************************************************************
00201   class fnv_1_32 : public etl::frame_check_sequence<fnv_1_policy_32>
00202   {
00203   public:
00204 
00205     //*************************************************************************
00206     /// Default constructor.
00207     //*************************************************************************
00208     fnv_1_32()
00209     {
00210       this->reset();
00211     }
00212 
00213     //*************************************************************************
00214     /// Constructor from range.
00215     /// \param begin Start of the range.
00216     /// \param end   End of the range.
00217     //*************************************************************************
00218     template<typename TIterator>
00219     fnv_1_32(TIterator begin, const TIterator end)
00220     {
00221       this->reset();
00222       this->add(begin, end);
00223     }
00224   };
00225 
00226   //***************************************************************************
00227   /// fnv_1a policy.
00228   /// Calculates FNV1A.
00229   //***************************************************************************
00230   struct fnv_1a_policy_32
00231     {
00232     typedef uint32_t value_type;
00233 
00234     inline uint32_t initial() const
00235       {
00236       return OFFSET_BASIS;
00237     }
00238 
00239     inline uint32_t add(uint32_t hash, uint8_t value) const
00240     {
00241       hash ^= value;
00242       hash *= PRIME;
00243       return hash;
00244     }
00245 
00246     inline uint32_t final(uint32_t hash) const
00247     {
00248       return hash;
00249     }
00250 
00251     static const uint32_t OFFSET_BASIS = 0x811C9DC5;
00252     static const uint32_t PRIME        = 0x01000193;
00253   };
00254 
00255   //***************************************************************************
00256   /// Calculates the fnv_1a_32 hash.
00257   ///\ingroup fnv_1a_32
00258   //***************************************************************************
00259   class fnv_1a_32 : public etl::frame_check_sequence<fnv_1a_policy_32>
00260   {
00261   public:
00262 
00263     //*************************************************************************
00264     /// Default constructor.
00265     //*************************************************************************
00266     fnv_1a_32()
00267     {
00268       this->reset();
00269     }
00270 
00271     //*************************************************************************
00272     /// Constructor from range.
00273     /// \param begin Start of the range.
00274     /// \param end   End of the range.
00275     //*************************************************************************
00276     template<typename TIterator>
00277     fnv_1a_32(TIterator begin, const TIterator end)
00278     {
00279       this->reset();
00280       this->add(begin, end);
00281     }
00282   };
00283 }
00284 
00285 #endif
00286