Version
Wwise SDK 2021.1.10
AkFNVHash.h
Go to the documentation of this file.
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: v2021.1.10 Build: 7883
25  Copyright (c) 2006-2022 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  static inline typename HashParams::HashType ComputeLowerCase(const char* in_pData);
87 
88  private:
89  typename HashParams::HashType m_uHash;
90  };
91 
92  #if defined(_MSC_VER)
93  #pragma warning(push)
94  #pragma warning(disable:4127)
95  #endif
96 
97  template <class HashParams>
98  FNVHash<HashParams>::FNVHash( typename HashParams::HashType in_uBase )
99  : m_uHash( in_uBase )
100  {
101  }
102 
103  template <class HashParams>
104  typename HashParams::HashType FNVHash<HashParams>::Compute( const void* in_pData, typename HashParams::SizeType in_dataSize )
105  {
106  const unsigned char* pData = (const unsigned char*) in_pData;
107  const unsigned char* pEnd = pData + in_dataSize; /* beyond end of buffer */
108 
109  typename HashParams::HashType hval = m_uHash;
110 
111  // FNV-1 hash each octet in the buffer
112  while( pData < pEnd )
113  {
114  hval *= HashParams::Prime(); // multiply by the 32 bit FNV magic prime mod 2^32
115  hval ^= *pData++; // xor the bottom with the current octet
116  }
117 
118  m_uHash = hval;
119 
120  // XOR-Fold to the required number of bits
121  if( HashParams::Bits() >= sizeof(typename HashParams::HashType) * 8 )
122  return hval;
123 
124  typename HashParams::HashType mask = static_cast<typename HashParams::HashType>(((typename HashParams::HashType)1 << HashParams::Bits())-1);
125  return (typename HashParams::HashType)(hval >> HashParams::Bits()) ^ (hval & mask);
126  }
127 
128  template <class HashParams>
129  typename HashParams::HashType FNVHash<HashParams>::ComputeLowerCase(const char* in_pData)
130  {
131  typename HashParams::HashType hval = HashParams::s_offsetBasis;
132 
133  //Convert to lowercase and hash the string in one loop
134  while (*in_pData != 0)
135  {
136  hval *= HashParams::Prime(); // multiply by the 32 bit FNV magic prime mod 2^32
137  unsigned char c = (unsigned char)*in_pData++;
138  c = (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c;
139  hval ^= c; // xor the bottom with the current octet
140  }
141 
142  // XOR-Fold to the required number of bits
143  if (HashParams::Bits() >= sizeof(typename HashParams::HashType) * 8)
144  return hval;
145 
146  typename HashParams::HashType mask = static_cast<typename HashParams::HashType>(((typename HashParams::HashType)1 << HashParams::Bits()) - 1);
147  return (typename HashParams::HashType)(hval >> HashParams::Bits()) ^ (hval & mask);
148  }
149 
150  #if defined(_MSC_VER)
151  #pragma warning(pop)
152  #endif
153 
157 }
158 
159 #endif
unsigned int HashType
Definition: AkFNVHash.h:50
FNVHash< Hash32 > FNVHash32
Definition: AkFNVHash.h:154
unsigned int SizeType
Definition: AkFNVHash.h:51
Audiokinetic namespace.
static unsigned int Bits()
Definition: AkFNVHash.h:66
static unsigned int Bits()
Definition: AkFNVHash.h:59
static const HashType s_offsetBasis
Definition: AkFNVHash.h:68
static HashType Prime()
Definition: AkFNVHash.h:67
FNVHash(typename HashParams::HashType in_uBase=HashParams::s_offsetBasis)
Constructor.
Definition: AkFNVHash.h:98
static const HashType s_offsetBasis
Definition: AkFNVHash.h:54
unsigned long long SizeType
Definition: AkFNVHash.h:65
HashParams::HashType Compute(const T &in_pData)
Definition: AkFNVHash.h:84
unsigned long long HashType
Definition: AkFNVHash.h:64
static unsigned int Bits()
Definition: AkFNVHash.h:52
FNVHash< Hash64 > FNVHash64
Definition: AkFNVHash.h:156
FNVHash< Hash30 > FNVHash30
Definition: AkFNVHash.h:155
HashParams::HashType Compute(const void *in_pData, typename HashParams::SizeType in_dataSize)
Definition: AkFNVHash.h:104
static HashParams::HashType ComputeLowerCase(const char *in_pData)
Definition: AkFNVHash.h:129
HashParams::HashType Get() const
Definition: AkFNVHash.h:81
static HashType Prime()
Definition: AkFNVHash.h:53