hash_test.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright 2010-2025 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx/blob/master/LICENSE
  4. */
  5. #include "test.h"
  6. #include <bx/hash.h>
  7. void makeCrcTable(uint32_t _poly)
  8. {
  9. for (uint32_t ii = 0; ii < 256; ++ii)
  10. {
  11. uint32_t crc = ii;
  12. for (uint32_t jj = 0; jj < 8; ++jj)
  13. {
  14. if (1 == (crc & 1) )
  15. {
  16. crc = (crc >> 1) ^ _poly;
  17. }
  18. else
  19. {
  20. crc >>= 1;
  21. }
  22. }
  23. printf("0x%08x,%c", crc, 7 == (ii & 7) ? '\n' : ' ');
  24. }
  25. }
  26. struct HashTest
  27. {
  28. uint32_t crc32[bx::HashCrc32::Count];
  29. uint32_t adler32;
  30. uint32_t murmur2a;
  31. uint32_t murmur3;
  32. const char* input;
  33. };
  34. const HashTest s_hashTest[] =
  35. {
  36. // Crc32 | Adler32 | Murmur2A | Murmur3 | Input
  37. // Ieee Castagnoli Koopman | | | |
  38. { { 0, 0, 0 }, 1, 0, 0, "" },
  39. { { 0xe8b7be43, 0xc1d04330, 0x0da2aa8a }, 0x00620062, 0x0803888b, 0x3c2569b2, "a" },
  40. { { 0x9e83486d, 0xe2a22936, 0x31ec935a }, 0x012600c4, 0x618515af, 0x9bbfd75f, "ab" },
  41. { { 0xc340daab, 0x49e1b6e3, 0x945a1e78 }, 0x06060205, 0x94e3dc4d, 0x1e661875, "abvgd" },
  42. { { 0x07642fe2, 0x45a04162, 0x3d4bf72d }, 0x020a00d6, 0xe602fc07, 0x7af40d31, "1389" },
  43. { { 0x26d75737, 0xb73d7b80, 0xd524eb40 }, 0x04530139, 0x58d37863, 0x0c090160, "555333" },
  44. };
  45. TEST_CASE("HashCrc32", "[hash]")
  46. {
  47. #if 0
  48. makeCrcTable(0xedb88320);
  49. printf("\n");
  50. makeCrcTable(0x82f63b78);
  51. printf("\n");
  52. makeCrcTable(0xeb31d82e);
  53. #endif // 0
  54. for (uint32_t ii = 0; ii < BX_COUNTOF(s_hashTest); ++ii)
  55. {
  56. const HashTest& test = s_hashTest[ii];
  57. for (uint32_t jj = 0; jj < bx::HashCrc32::Count; ++jj)
  58. {
  59. bx::HashCrc32 hash;
  60. hash.begin(bx::HashCrc32::Enum(jj) );
  61. hash.add(test.input, bx::strLen(test.input) );
  62. REQUIRE(test.crc32[jj] == hash.end() );
  63. }
  64. }
  65. }
  66. TEST_CASE("HashAdler32", "[hash]")
  67. {
  68. for (uint32_t ii = 0; ii < BX_COUNTOF(s_hashTest); ++ii)
  69. {
  70. const HashTest& test = s_hashTest[ii];
  71. bx::HashAdler32 hash;
  72. hash.begin();
  73. hash.add(test.input, bx::strLen(test.input) );
  74. REQUIRE(test.adler32 == hash.end() );
  75. }
  76. }
  77. namespace
  78. {
  79. /*-----------------------------------------------------------------------------
  80. // MurmurHash2A, by Austin Appleby
  81. //
  82. // This is a variant of MurmurHash2 modified to use the Merkle-Damgard
  83. // construction. Bulk speed should be identical to Murmur2, small-key speed
  84. // will be 10%-20% slower due to the added overhead at the end of the hash.
  85. //
  86. // This variant fixes a minor issue where null keys were more likely to
  87. // collide with each other than expected, and also makes the function
  88. // more amenable to incremental implementations.
  89. */
  90. uint32_t MurmurHash2A(const void * key, int len, uint32_t seed = 0)
  91. {
  92. const uint32_t m = 0x5bd1e995;
  93. const int r = 24;
  94. uint32_t l = len;
  95. const unsigned char * data = (const unsigned char *)key;
  96. uint32_t h = seed;
  97. #define mmix(h,k) { k *= m; k ^= k >> r; k *= m; h *= m; h ^= k; }
  98. while(len >= 4)
  99. {
  100. uint32_t k = *(uint32_t*)data;
  101. mmix(h,k);
  102. data += 4;
  103. len -= 4;
  104. }
  105. uint32_t t = 0;
  106. switch(len)
  107. {
  108. case 3: t ^= data[2] << 16; [[fallthrough]];
  109. case 2: t ^= data[1] << 8; [[fallthrough]];
  110. case 1: t ^= data[0];
  111. };
  112. mmix(h,t);
  113. mmix(h,l);
  114. #undef mmix
  115. h ^= h >> 13;
  116. h *= m;
  117. h ^= h >> 15;
  118. return h;
  119. }
  120. } // namespace
  121. TEST_CASE("HashMurmur2A", "[hash]")
  122. {
  123. uint32_t seed = 0;
  124. for (uint32_t ii = 0; ii < BX_COUNTOF(s_hashTest); ++ii)
  125. {
  126. const HashTest& test = s_hashTest[ii];
  127. bx::HashMurmur2A hash;
  128. hash.begin(seed);
  129. hash.add(test.input, bx::strLen(test.input) );
  130. REQUIRE(test.murmur2a == hash.end() );
  131. REQUIRE(test.murmur2a == MurmurHash2A(test.input, bx::strLen(test.input), seed) );
  132. }
  133. }
  134. TEST_CASE("HashMurmur2A-Separate-Add", "[hash]")
  135. {
  136. bx::HashMurmur2A hash;
  137. hash.begin();
  138. hash.add("0123456789");
  139. hash.add("abvgd012345");
  140. hash.add("1389");
  141. hash.add("555333");
  142. REQUIRE(MurmurHash2A("0123456789abvgd0123451389555333", 31) == hash.end() );
  143. }
  144. namespace
  145. {
  146. BX_FORCE_INLINE uint32_t fmix32 ( uint32_t h )
  147. {
  148. h ^= h >> 16;
  149. h *= 0x85ebca6b;
  150. h ^= h >> 13;
  151. h *= 0xc2b2ae35;
  152. h ^= h >> 16;
  153. return h;
  154. }
  155. inline uint32_t rotl32 ( uint32_t x, int8_t r )
  156. {
  157. return (x << r) | (x >> (32 - r));
  158. }
  159. uint32_t MurmurHash3_x86_32(const void * key, int len, uint32_t seed)
  160. {
  161. const uint8_t * data = (const uint8_t*)key;
  162. const int nblocks = len / 4;
  163. uint32_t h1 = seed;
  164. const uint32_t c1 = 0xcc9e2d51;
  165. const uint32_t c2 = 0x1b873593;
  166. //----------
  167. // body
  168. const uint32_t * blocks = (const uint32_t *)(data + nblocks*4);
  169. for(int i = -nblocks; i; i++)
  170. {
  171. uint32_t k1 = blocks[i];
  172. k1 *= c1;
  173. k1 = rotl32(k1,15);
  174. k1 *= c2;
  175. h1 ^= k1;
  176. h1 = rotl32(h1,13);
  177. h1 = h1*5+0xe6546b64;
  178. }
  179. //----------
  180. // tail
  181. const uint8_t * tail = (const uint8_t*)(data + nblocks*4);
  182. uint32_t k1 = 0;
  183. switch(len & 3)
  184. {
  185. case 3: k1 ^= tail[2] << 16; [[fallthrough]];
  186. case 2: k1 ^= tail[1] << 8; [[fallthrough]];
  187. case 1: k1 ^= tail[0];
  188. k1 *= c1; k1 = rotl32(k1,15); k1 *= c2; h1 ^= k1;
  189. };
  190. //----------
  191. // finalization
  192. h1 ^= len;
  193. h1 = fmix32(h1);
  194. return h1;
  195. }
  196. } // namespace
  197. TEST_CASE("HashMurmur3", "[hash]")
  198. {
  199. uint32_t seed = 0;
  200. for (uint32_t ii = 0; ii < BX_COUNTOF(s_hashTest); ++ii)
  201. {
  202. const HashTest& test = s_hashTest[ii];
  203. bx::HashMurmur3 hash;
  204. hash.begin(seed);
  205. hash.add(test.input, bx::strLen(test.input) );
  206. const uint32_t result = hash.end();
  207. const uint32_t sanity = MurmurHash3_x86_32(test.input, bx::strLen(test.input), seed);
  208. REQUIRE(test.murmur3 == result);
  209. REQUIRE(test.murmur3 == sanity);
  210. }
  211. }
  212. TEST_CASE("HashMurmur3-Separate-Add", "[hash]")
  213. {
  214. bx::HashMurmur3 hash;
  215. hash.begin();
  216. hash.add("0123456789");
  217. hash.add("abvgd012345");
  218. hash.add("1389");
  219. hash.add("555333");
  220. REQUIRE(MurmurHash3_x86_32("0123456789abvgd0123451389555333", 31, 0) == hash.end() );
  221. }