Version
menu_open
link
Wwise SDK 2021.1.14
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.14 Build: 6590
25  Copyright (c) 2006-2023 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 #include <AK/AkPlatforms.h>
47 
48 namespace AK
49 {
50  struct Hash32
51  {
52  typedef AkUInt32 HashType;
53  typedef AkUInt32 SizeType;
54  static inline unsigned int Bits() {return 32;}
55  static inline HashType Prime() {return 16777619;}
56  static const HashType s_offsetBasis = 2166136261U;
57  };
58 
59  struct Hash30 : public Hash32
60  {
61  static inline unsigned int Bits() {return 30;}
62  };
63 
64  struct Hash64
65  {
66  typedef AkUInt64 HashType;
67  typedef AkUInt64 SizeType;
68  static inline unsigned int Bits() {return 64;}
69  static inline HashType Prime() {return 1099511628211ULL;}
70  static const HashType s_offsetBasis = 14695981039346656037ULL;
71  };
72 
73  template <class HashParams>
74  class FNVHash
75  {
76  public:
77  inline FNVHash( typename HashParams::HashType in_uBase = HashParams::s_offsetBasis ); ///< Constructor
78 
79  /// Turn the provided data into a hash value.
80  /// When Wwise uses this hash with strings, it always provides lower case strings only.
81  /// Call this repeatedly on the same instance to build a hash incrementally.
82  inline typename HashParams::HashType Compute( const void* in_pData, typename HashParams::SizeType in_dataSize );
83  inline typename HashParams::HashType Get() const { return m_uHash; }
84 
85  template <typename T>
86  inline typename HashParams::HashType Compute(const T& in_pData) { return Compute(&in_pData, sizeof(T)); }
87 
88  static inline typename HashParams::HashType ComputeLowerCase(const char* in_pData);
89 
90  private:
91  typename HashParams::HashType m_uHash;
92  };
93 
94  #if defined(_MSC_VER)
95  #pragma warning(push)
96  #pragma warning(disable:4127)
97  #endif
98 
99  template <class HashParams>
100  FNVHash<HashParams>::FNVHash( typename HashParams::HashType in_uBase )
101  : m_uHash( in_uBase )
102  {
103  }
104 
105  template <class HashParams>
106  typename HashParams::HashType FNVHash<HashParams>::Compute( const void* in_pData, typename HashParams::SizeType in_dataSize )
107  {
108  const unsigned char* pData = (const unsigned char*) in_pData;
109  const unsigned char* pEnd = pData + in_dataSize; /* beyond end of buffer */
110 
111  typename HashParams::HashType hval = m_uHash;
112 
113  // FNV-1 hash each octet in the buffer
114  while( pData < pEnd )
115  {
116  hval *= HashParams::Prime(); // multiply by the 32 bit FNV magic prime mod 2^32
117  hval ^= *pData++; // xor the bottom with the current octet
118  }
119 
120  m_uHash = hval;
121 
122  // XOR-Fold to the required number of bits
123  if( HashParams::Bits() >= sizeof(typename HashParams::HashType) * 8 )
124  return hval;
125 
126  typename HashParams::HashType mask = static_cast<typename HashParams::HashType>(((typename HashParams::HashType)1 << HashParams::Bits())-1);
127  return (typename HashParams::HashType)(hval >> HashParams::Bits()) ^ (hval & mask);
128  }
129 
130  template <class HashParams>
131  typename HashParams::HashType FNVHash<HashParams>::ComputeLowerCase(const char* in_pData)
132  {
133  typename HashParams::HashType hval = HashParams::s_offsetBasis;
134 
135  //Convert to lowercase and hash the string in one loop
136  while (*in_pData != 0)
137  {
138  hval *= HashParams::Prime(); // multiply by the 32 bit FNV magic prime mod 2^32
139  unsigned char c = (unsigned char)*in_pData++;
140  c = (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c;
141  hval ^= c; // xor the bottom with the current octet
142  }
143 
144  // XOR-Fold to the required number of bits
145  if (HashParams::Bits() >= sizeof(typename HashParams::HashType) * 8)
146  return hval;
147 
148  typename HashParams::HashType mask = static_cast<typename HashParams::HashType>(((typename HashParams::HashType)1 << HashParams::Bits()) - 1);
149  return (typename HashParams::HashType)(hval >> HashParams::Bits()) ^ (hval & mask);
150  }
151 
152  #if defined(_MSC_VER)
153  #pragma warning(pop)
154  #endif
155 
159 }
160 
161 #endif
FNVHash< Hash32 > FNVHash32
Definition: AkFNVHash.h:156
Audiokinetic namespace.
static unsigned int Bits()
Definition: AkFNVHash.h:68
AkUInt32 HashType
Definition: AkFNVHash.h:52
static unsigned int Bits()
Definition: AkFNVHash.h:61
AkUInt32 SizeType
Definition: AkFNVHash.h:53
static const HashType s_offsetBasis
Definition: AkFNVHash.h:70
static HashType Prime()
Definition: AkFNVHash.h:69
FNVHash(typename HashParams::HashType in_uBase=HashParams::s_offsetBasis)
Constructor.
Definition: AkFNVHash.h:100
static const HashType s_offsetBasis
Definition: AkFNVHash.h:56
uint64_t AkUInt64
Unsigned 64-bit integer.
Definition: AkTypes.h:60
HashParams::HashType Compute(const T &in_pData)
Definition: AkFNVHash.h:86
static unsigned int Bits()
Definition: AkFNVHash.h:54
AkUInt64 SizeType
Definition: AkFNVHash.h:67
FNVHash< Hash64 > FNVHash64
Definition: AkFNVHash.h:158
FNVHash< Hash30 > FNVHash30
Definition: AkFNVHash.h:157
AkUInt64 HashType
Definition: AkFNVHash.h:66
uint32_t AkUInt32
Unsigned 32-bit integer.
Definition: AkTypes.h:59
HashParams::HashType Compute(const void *in_pData, typename HashParams::SizeType in_dataSize)
Definition: AkFNVHash.h:106
static HashParams::HashType ComputeLowerCase(const char *in_pData)
Definition: AkFNVHash.h:131
HashParams::HashType Get() const
Definition: AkFNVHash.h:83
static HashType Prime()
Definition: AkFNVHash.h:55

Was this page helpful?

Need Support?

Questions? Problems? Need more info? Contact us, and we can help!

Visit our Support page

Tell us about your project. We're here to help.

Register your project and we'll help you get started with no strings attached!

Get started with Wwise