Table of Contents

Wwise SDK 2019.2.1
AkFNVHash.h
1 /*******************************************************************************
2 The content of this file includes portions of the AUDIOKINETIC Wwise Technology
3 released in source code form as part of the SDK installer package.
4 
5 Commercial License Usage
6 
7 Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
8 may use this file in accordance with the end user license agreement provided
9 with the software or, alternatively, in accordance with the terms contained in a
10 written agreement between you and Audiokinetic Inc.
11 
12 Apache License Usage
13 
14 Alternatively, this file may be used under the Apache License, Version 2.0 (the
15 "Apache License"); you may not use this file except in compliance with the
16 Apache License. You may obtain a copy of the Apache License at
17 http://www.apache.org/licenses/LICENSE-2.0.
18 
19 Unless required by applicable law or agreed to in writing, software distributed
20 under the Apache License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
21 OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License for
22 the specific language governing permissions and limitations under the License.
23 
24  Version: <VERSION> Build: <BUILDNUMBER>
25  Copyright (c) <COPYRIGHTYEAR> Audiokinetic Inc.
26 *******************************************************************************/
27 
28 #ifndef _FNVHASH_H
29 #define _FNVHASH_H
30 
31 // http://www.isthe.com/chongo/tech/comp/fnv/
32 
33 //////////////////////////////////////////////////////////////////
34 //
35 // ***************************************************************
36 //
37 // IMPORTANT: The Migration Utility contains a C# version of this
38 // class, to assign Short IDs to objects created during migration.
39 // If you modify this class, be sure to update its C# counterpart,
40 // ShortIDGenerator, at the same time.
41 //
42 // ***************************************************************
43 //
44 //////////////////////////////////////////////////////////////////
45 
46 namespace AK
47 {
48  struct Hash32
49  {
50  typedef unsigned int HashType;
51  typedef unsigned int SizeType;
52  static inline unsigned int Bits() {return 32;}
53  static inline HashType Prime() {return 16777619;}
54  static const HashType s_offsetBasis = 2166136261U;
55  };
56 
57  struct Hash30 : public Hash32
58  {
59  static inline unsigned int Bits() {return 30;}
60  };
61 
62  struct Hash64
63  {
64  typedef unsigned long long HashType;
65  typedef unsigned long long SizeType;
66  static inline unsigned int Bits() {return 64;}
67  static inline HashType Prime() {return 1099511628211ULL;}
68  static const HashType s_offsetBasis = 14695981039346656037ULL;
69  };
70 
71  template <class HashParams>
72  class FNVHash
73  {
74  public:
75  inline FNVHash( typename HashParams::HashType in_uBase = HashParams::s_offsetBasis ); ///< Constructor
76 
77  /// Turn the provided data into a hash value.
78  /// When Wwise uses this hash with strings, it always provides lower case strings only.
79  /// Call this repeatedly on the same instance to build a hash incrementally.
80  inline typename HashParams::HashType Compute( const void* in_pData, typename HashParams::SizeType in_dataSize );
81  inline typename HashParams::HashType Get() const { return m_uHash; }
82 
83  template <typename T>
84  inline typename HashParams::HashType Compute(const T& in_pData) { return Compute(&in_pData, sizeof(T)); }
85 
86  private:
87  typename HashParams::HashType m_uHash;
88  };
89 
90  #if defined(_MSC_VER)
91  #pragma warning(push)
92  #pragma warning(disable:4127)
93  #endif
94 
95  template <class HashParams>
96  FNVHash<HashParams>::FNVHash( typename HashParams::HashType in_uBase )
97  : m_uHash( in_uBase )
98  {
99  }
100 
101  template <class HashParams>
102  typename HashParams::HashType FNVHash<HashParams>::Compute( const void* in_pData, typename HashParams::SizeType in_dataSize )
103  {
104  const unsigned char* pData = (const unsigned char*) in_pData;
105  const unsigned char* pEnd = pData + in_dataSize; /* beyond end of buffer */
106 
107  typename HashParams::HashType hval = m_uHash;
108 
109  // FNV-1 hash each octet in the buffer
110  while( pData < pEnd )
111  {
112  hval *= HashParams::Prime(); // multiply by the 32 bit FNV magic prime mod 2^32
113  hval ^= *pData++; // xor the bottom with the current octet
114  }
115 
116  m_uHash = hval;
117 
118  // XOR-Fold to the required number of bits
119  if( HashParams::Bits() >= sizeof(typename HashParams::HashType) * 8 )
120  return hval;
121 
122  typename HashParams::HashType mask = static_cast<typename HashParams::HashType>(((typename HashParams::HashType)1 << HashParams::Bits())-1);
123  return (typename HashParams::HashType)(hval >> HashParams::Bits()) ^ (hval & mask);
124  }
125 
126  #if defined(_MSC_VER)
127  #pragma warning(pop)
128  #endif
129 
133 }
134 
135 #endif
unsigned long long SizeType
Definition: AkFNVHash.h:65
static const HashType s_offsetBasis
Definition: AkFNVHash.h:68
FNVHash< Hash32 > FNVHash32
Definition: AkFNVHash.h:130
static HashType Prime()
Definition: AkFNVHash.h:67
static HashType Prime()
Definition: AkFNVHash.h:53
Audiokinetic namespace.
static const HashType s_offsetBasis
Definition: AkFNVHash.h:54
FNVHash< Hash64 > FNVHash64
Definition: AkFNVHash.h:132
FNVHash< Hash30 > FNVHash30
Definition: AkFNVHash.h:131
unsigned long long HashType
Definition: AkFNVHash.h:64
static unsigned int Bits()
Definition: AkFNVHash.h:66
static unsigned int Bits()
Definition: AkFNVHash.h:52
HashParams::HashType Compute(const T &in_pData)
Definition: AkFNVHash.h:84
unsigned int HashType
Definition: AkFNVHash.h:50
FNVHash(typename HashParams::HashType in_uBase=HashParams::s_offsetBasis)
Constructor.
Definition: AkFNVHash.h:96
HashParams::HashType Compute(const void *in_pData, typename HashParams::SizeType in_dataSize)
Definition: AkFNVHash.h:102
unsigned int SizeType
Definition: AkFNVHash.h:51
HashParams::HashType Get() const
Definition: AkFNVHash.h:81
static unsigned int Bits()
Definition: AkFNVHash.h:59