hash_test.cpp 6.4 KB

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