AkFNVHash.h 5.6 KB

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