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

Cette page a-t-elle été utile ?

Besoin d'aide ?

Des questions ? Des problèmes ? Besoin de plus d'informations ? Contactez-nous, nous pouvons vous aider !

Visitez notre page d'Aide

Décrivez-nous de votre projet. Nous sommes là pour vous aider.

Enregistrez votre projet et nous vous aiderons à démarrer sans aucune obligation !

Partir du bon pied avec Wwise