Hash.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "Assert.h"
  25. #include "Types.h"
  26. namespace crown
  27. {
  28. /// String hashing.
  29. namespace hash
  30. {
  31. // Constants
  32. const uint32_t FNV1A_OFFSET_BASIS_32 = 2166136261u;
  33. const uint32_t FNV1A_PRIME_32 = 16777619u;
  34. const uint64_t FNV1A_OFFSET_BASIS_64 = 14695981039346656037ull;
  35. const uint64_t FNV1A_PRIME_64 = 1099511628211ull;
  36. // Functions
  37. uint32_t murmur2_32(const void* key, size_t len, uint32_t seed);
  38. uint64_t murmur2_64(const void* key, size_t len, unsigned int seed);
  39. uint32_t fnv1a_32(const void* key, size_t len);
  40. uint64_t fnv1a_64(const void* key, size_t len);
  41. //-----------------------------------------------------------------------------
  42. /// MurmurHash2, by Austin Appleby
  43. ///
  44. /// @note
  45. /// This code makes a few assumptions about how your machine behaves
  46. ///
  47. /// 1. We can read a 4-byte value from any address without crashing
  48. /// 2. sizeof(int) == 4
  49. ///
  50. /// And it has a few limitations -
  51. ///
  52. /// 1. It will not work incrementally.
  53. /// 2. It will not produce the same results on little-endian and big-endian
  54. /// machines.
  55. inline uint32_t murmur2_32(const void* key, size_t len, uint32_t seed)
  56. {
  57. CE_ASSERT_NOT_NULL(key);
  58. // 'm' and 'r' are mixing constants generated offline.
  59. // They're not really 'magic', they just happen to work well.
  60. const unsigned int m = 0x5bd1e995;
  61. const int r = 24;
  62. // Initialize the hash to a 'random' value
  63. unsigned int h = seed ^ len;
  64. // Mix 4 bytes at a time into the hash
  65. const unsigned char * data = (const unsigned char *)key;
  66. while(len >= 4)
  67. {
  68. unsigned int k = *(unsigned int *)data;
  69. k *= m;
  70. k ^= k >> r;
  71. k *= m;
  72. h *= m;
  73. h ^= k;
  74. data += 4;
  75. len -= 4;
  76. }
  77. // Handle the last few bytes of the input array
  78. switch(len)
  79. {
  80. case 3: h ^= data[2] << 16;
  81. case 2: h ^= data[1] << 8;
  82. case 1: h ^= data[0];
  83. h *= m;
  84. };
  85. // Do a few final mixes of the hash to ensure the last few
  86. // bytes are well-incorporated.
  87. h ^= h >> 13;
  88. h *= m;
  89. h ^= h >> 15;
  90. return h;
  91. }
  92. //-----------------------------------------------------------------------------
  93. inline uint64_t murmur2_64(const void* key, size_t len, unsigned int seed)
  94. {
  95. CE_ASSERT_NOT_NULL(key);
  96. const unsigned int m = 0x5bd1e995;
  97. const int r = 24;
  98. unsigned int h1 = seed ^ len;
  99. unsigned int h2 = 0;
  100. const unsigned int * data = (const unsigned int *)key;
  101. while(len >= 8)
  102. {
  103. unsigned int k1 = *data++;
  104. k1 *= m; k1 ^= k1 >> r; k1 *= m;
  105. h1 *= m; h1 ^= k1;
  106. len -= 4;
  107. unsigned int k2 = *data++;
  108. k2 *= m; k2 ^= k2 >> r; k2 *= m;
  109. h2 *= m; h2 ^= k2;
  110. len -= 4;
  111. }
  112. if(len >= 4)
  113. {
  114. unsigned int k1 = *data++;
  115. k1 *= m; k1 ^= k1 >> r; k1 *= m;
  116. h1 *= m; h1 ^= k1;
  117. len -= 4;
  118. }
  119. switch(len)
  120. {
  121. case 3: h2 ^= ((unsigned char*)data)[2] << 16;
  122. case 2: h2 ^= ((unsigned char*)data)[1] << 8;
  123. case 1: h2 ^= ((unsigned char*)data)[0];
  124. h2 *= m;
  125. };
  126. h1 ^= h2 >> 18; h1 *= m;
  127. h2 ^= h1 >> 22; h2 *= m;
  128. h1 ^= h2 >> 17; h1 *= m;
  129. h2 ^= h1 >> 19; h2 *= m;
  130. uint64_t h = h1;
  131. h = (h << 32) | h2;
  132. return h;
  133. }
  134. //-----------------------------------------------------------------------------
  135. /// FNV-1a hash, 32 bit
  136. inline uint32_t fnv1a_32(const void* key, size_t len)
  137. {
  138. CE_ASSERT(key != NULL, "Key must be != NULL");
  139. // FNV-1a
  140. uint32_t hash = FNV1A_OFFSET_BASIS_32;
  141. for (size_t i = 0; i < len; i++)
  142. {
  143. unsigned char* k = (unsigned char*)key;
  144. hash ^= k[i];
  145. hash *= FNV1A_PRIME_32;
  146. }
  147. return hash;
  148. }
  149. //-----------------------------------------------------------------------------
  150. /// FNV-1a hash, 64 bit
  151. inline uint64_t fnv1a_64(const void* key, size_t len)
  152. {
  153. CE_ASSERT(key != NULL, "Key must be != NULL");
  154. // FNV-1a
  155. uint64_t hash = FNV1A_OFFSET_BASIS_64;
  156. for (size_t i = 0; i < len; i++)
  157. {
  158. unsigned char* k = (unsigned char*)key;
  159. hash ^= k[i];
  160. hash *= FNV1A_PRIME_64;
  161. }
  162. return hash;
  163. }
  164. } // namespace hash
  165. } // namespace crown