Version
menu_open
link
Wwise SDK 2019.1.11
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  static inline unsigned int Bits() {return 32;}
52  static inline HashType Prime() {return 16777619;}
53  static const HashType s_offsetBasis = 2166136261U;
54  };
55 
56  struct Hash30 : public Hash32
57  {
58  static inline unsigned int Bits() {return 30;}
59  };
60 
61  struct Hash64
62  {
63  typedef unsigned long long HashType;
64  static inline unsigned int Bits() {return 64;}
65  static inline HashType Prime() {return 1099511628211ULL;}
66  static const HashType s_offsetBasis = 14695981039346656037ULL;
67  };
68 
69  template <class HashParams>
70  class FNVHash
71  {
72  public:
73  inline FNVHash( typename HashParams::HashType in_uBase = HashParams::s_offsetBasis ); ///< Constructor
74 
75  /// Turn the provided data into a hash value.
76  /// When Wwise uses this hash with strings, it always provides lower case strings only.
77  /// Call this repeatedly on the same instance to build a hash incrementally.
78  inline typename HashParams::HashType Compute( const void* in_pData, unsigned int in_dataSize );
79  inline typename HashParams::HashType Get() const { return m_uHash; }
80 
81  template <typename T>
82  inline typename HashParams::HashType Compute(const T& in_pData) { return Compute(&in_pData, sizeof(T)); }
83 
84  private:
85  typename HashParams::HashType m_uHash;
86  };
87 
88  #if defined(_MSC_VER)
89  #pragma warning(push)
90  #pragma warning(disable:4127)
91  #endif
92 
93 
94  template <class HashParams>
95  FNVHash<HashParams>::FNVHash( typename HashParams::HashType in_uBase )
96  : m_uHash( in_uBase )
97  {
98  }
99 
100 
101  template <class HashParams>
102  typename HashParams::HashType FNVHash<HashParams>::Compute( const void* in_pData, unsigned int 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 
136 #endif
unsigned int HashType
Definition: AkFNVHash.h:50
FNVHash< Hash32 > FNVHash32
Definition: AkFNVHash.h:130
Audiokinetic namespace.
HashParams::HashType Compute(const void *in_pData, unsigned int in_dataSize)
Definition: AkFNVHash.h:102
static unsigned int Bits()
Definition: AkFNVHash.h:64
static unsigned int Bits()
Definition: AkFNVHash.h:58
static const HashType s_offsetBasis
Definition: AkFNVHash.h:66
static HashType Prime()
Definition: AkFNVHash.h:65
FNVHash(typename HashParams::HashType in_uBase=HashParams::s_offsetBasis)
Constructor.
Definition: AkFNVHash.h:95
static const HashType s_offsetBasis
Definition: AkFNVHash.h:53
HashParams::HashType Compute(const T &in_pData)
Definition: AkFNVHash.h:82
unsigned long long HashType
Definition: AkFNVHash.h:63
static unsigned int Bits()
Definition: AkFNVHash.h:51
FNVHash< Hash64 > FNVHash64
Definition: AkFNVHash.h:132
FNVHash< Hash30 > FNVHash30
Definition: AkFNVHash.h:131
HashParams::HashType Get() const
Definition: AkFNVHash.h:79
static HashType Prime()
Definition: AkFNVHash.h:52

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