hashfuncs.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /**************************************************************************/
  2. /* hashfuncs.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "core/math/aabb.h"
  32. #include "core/math/basis.h"
  33. #include "core/math/color.h"
  34. #include "core/math/math_defs.h"
  35. #include "core/math/math_funcs.h"
  36. #include "core/math/plane.h"
  37. #include "core/math/projection.h"
  38. #include "core/math/quaternion.h"
  39. #include "core/math/rect2.h"
  40. #include "core/math/rect2i.h"
  41. #include "core/math/transform_2d.h"
  42. #include "core/math/transform_3d.h"
  43. #include "core/math/vector2.h"
  44. #include "core/math/vector2i.h"
  45. #include "core/math/vector3.h"
  46. #include "core/math/vector3i.h"
  47. #include "core/math/vector4.h"
  48. #include "core/math/vector4i.h"
  49. #include "core/object/object_id.h"
  50. #include "core/string/node_path.h"
  51. #include "core/string/string_name.h"
  52. #include "core/string/ustring.h"
  53. #include "core/templates/rid.h"
  54. #include "core/typedefs.h"
  55. #ifdef _MSC_VER
  56. #include <intrin.h> // Needed for `__umulh` below.
  57. #endif
  58. /**
  59. * Hashing functions
  60. */
  61. /**
  62. * DJB2 Hash function
  63. * @param C String
  64. * @return 32-bits hashcode
  65. */
  66. static _FORCE_INLINE_ uint32_t hash_djb2(const char *p_cstr) {
  67. const unsigned char *chr = (const unsigned char *)p_cstr;
  68. uint32_t hash = 5381;
  69. uint32_t c = *chr++;
  70. while (c) {
  71. hash = ((hash << 5) + hash) ^ c; /* hash * 33 ^ c */
  72. c = *chr++;
  73. }
  74. return hash;
  75. }
  76. static _FORCE_INLINE_ uint32_t hash_djb2_buffer(const uint8_t *p_buff, int p_len, uint32_t p_prev = 5381) {
  77. uint32_t hash = p_prev;
  78. for (int i = 0; i < p_len; i++) {
  79. hash = ((hash << 5) + hash) ^ p_buff[i]; /* hash * 33 + c */
  80. }
  81. return hash;
  82. }
  83. static _FORCE_INLINE_ uint32_t hash_djb2_one_32(uint32_t p_in, uint32_t p_prev = 5381) {
  84. return ((p_prev << 5) + p_prev) ^ p_in;
  85. }
  86. /**
  87. * Thomas Wang's 64-bit to 32-bit Hash function:
  88. * https://web.archive.org/web/20071223173210/https:/www.concentric.net/~Ttwang/tech/inthash.htm
  89. *
  90. * @param p_int - 64-bit unsigned integer key to be hashed
  91. * @return unsigned 32-bit value representing hashcode
  92. */
  93. static _FORCE_INLINE_ uint32_t hash_one_uint64(const uint64_t p_int) {
  94. uint64_t v = p_int;
  95. v = (~v) + (v << 18); // v = (v << 18) - v - 1;
  96. v = v ^ (v >> 31);
  97. v = v * 21; // v = (v + (v << 2)) + (v << 4);
  98. v = v ^ (v >> 11);
  99. v = v + (v << 6);
  100. v = v ^ (v >> 22);
  101. return uint32_t(v);
  102. }
  103. static _FORCE_INLINE_ uint64_t hash64_murmur3_64(uint64_t key, uint64_t seed) {
  104. key ^= seed;
  105. key ^= key >> 33;
  106. key *= 0xff51afd7ed558ccd;
  107. key ^= key >> 33;
  108. key *= 0xc4ceb9fe1a85ec53;
  109. key ^= key >> 33;
  110. return key;
  111. }
  112. #define HASH_MURMUR3_SEED 0x7F07C65
  113. // Murmurhash3 32-bit version.
  114. // All MurmurHash versions are public domain software, and the author disclaims all copyright to their code.
  115. static _FORCE_INLINE_ uint32_t hash_murmur3_one_32(uint32_t p_in, uint32_t p_seed = HASH_MURMUR3_SEED) {
  116. p_in *= 0xcc9e2d51;
  117. p_in = (p_in << 15) | (p_in >> 17);
  118. p_in *= 0x1b873593;
  119. p_seed ^= p_in;
  120. p_seed = (p_seed << 13) | (p_seed >> 19);
  121. p_seed = p_seed * 5 + 0xe6546b64;
  122. return p_seed;
  123. }
  124. static _FORCE_INLINE_ uint32_t hash_murmur3_one_float(float p_in, uint32_t p_seed = HASH_MURMUR3_SEED) {
  125. union {
  126. float f;
  127. uint32_t i;
  128. } u;
  129. // Normalize +/- 0.0 and NaN values so they hash the same.
  130. if (p_in == 0.0f) {
  131. u.f = 0.0;
  132. } else if (Math::is_nan(p_in)) {
  133. u.f = NAN;
  134. } else {
  135. u.f = p_in;
  136. }
  137. return hash_murmur3_one_32(u.i, p_seed);
  138. }
  139. static _FORCE_INLINE_ uint32_t hash_murmur3_one_64(uint64_t p_in, uint32_t p_seed = HASH_MURMUR3_SEED) {
  140. p_seed = hash_murmur3_one_32(p_in & 0xFFFFFFFF, p_seed);
  141. return hash_murmur3_one_32(p_in >> 32, p_seed);
  142. }
  143. static _FORCE_INLINE_ uint32_t hash_murmur3_one_double(double p_in, uint32_t p_seed = HASH_MURMUR3_SEED) {
  144. union {
  145. double d;
  146. uint64_t i;
  147. } u;
  148. // Normalize +/- 0.0 and NaN values so they hash the same.
  149. if (p_in == 0.0f) {
  150. u.d = 0.0;
  151. } else if (Math::is_nan(p_in)) {
  152. u.d = NAN;
  153. } else {
  154. u.d = p_in;
  155. }
  156. return hash_murmur3_one_64(u.i, p_seed);
  157. }
  158. static _FORCE_INLINE_ uint32_t hash_murmur3_one_real(real_t p_in, uint32_t p_seed = HASH_MURMUR3_SEED) {
  159. #ifdef REAL_T_IS_DOUBLE
  160. return hash_murmur3_one_double(p_in, p_seed);
  161. #else
  162. return hash_murmur3_one_float(p_in, p_seed);
  163. #endif
  164. }
  165. static _FORCE_INLINE_ uint32_t hash_rotl32(uint32_t x, int8_t r) {
  166. return (x << r) | (x >> (32 - r));
  167. }
  168. static _FORCE_INLINE_ uint32_t hash_fmix32(uint32_t h) {
  169. h ^= h >> 16;
  170. h *= 0x85ebca6b;
  171. h ^= h >> 13;
  172. h *= 0xc2b2ae35;
  173. h ^= h >> 16;
  174. return h;
  175. }
  176. static _FORCE_INLINE_ uint32_t hash_murmur3_buffer(const void *key, int length, const uint32_t seed = HASH_MURMUR3_SEED) {
  177. // Although not required, this is a random prime number.
  178. const uint8_t *data = (const uint8_t *)key;
  179. const int nblocks = length / 4;
  180. uint32_t h1 = seed;
  181. const uint32_t c1 = 0xcc9e2d51;
  182. const uint32_t c2 = 0x1b873593;
  183. const uint32_t *blocks = (const uint32_t *)(data + nblocks * 4);
  184. for (int i = -nblocks; i; i++) {
  185. uint32_t k1 = blocks[i];
  186. k1 *= c1;
  187. k1 = hash_rotl32(k1, 15);
  188. k1 *= c2;
  189. h1 ^= k1;
  190. h1 = hash_rotl32(h1, 13);
  191. h1 = h1 * 5 + 0xe6546b64;
  192. }
  193. const uint8_t *tail = (const uint8_t *)(data + nblocks * 4);
  194. uint32_t k1 = 0;
  195. switch (length & 3) {
  196. case 3:
  197. k1 ^= tail[2] << 16;
  198. [[fallthrough]];
  199. case 2:
  200. k1 ^= tail[1] << 8;
  201. [[fallthrough]];
  202. case 1:
  203. k1 ^= tail[0];
  204. k1 *= c1;
  205. k1 = hash_rotl32(k1, 15);
  206. k1 *= c2;
  207. h1 ^= k1;
  208. };
  209. // Finalize with additional bit mixing.
  210. h1 ^= length;
  211. return hash_fmix32(h1);
  212. }
  213. static _FORCE_INLINE_ uint32_t hash_djb2_one_float(double p_in, uint32_t p_prev = 5381) {
  214. union {
  215. double d;
  216. uint64_t i;
  217. } u;
  218. // Normalize +/- 0.0 and NaN values so they hash the same.
  219. if (p_in == 0.0f) {
  220. u.d = 0.0;
  221. } else if (Math::is_nan(p_in)) {
  222. u.d = NAN;
  223. } else {
  224. u.d = p_in;
  225. }
  226. return ((p_prev << 5) + p_prev) + hash_one_uint64(u.i);
  227. }
  228. template <typename T>
  229. static _FORCE_INLINE_ uint32_t hash_make_uint32_t(T p_in) {
  230. union {
  231. T t;
  232. uint32_t _u32;
  233. } _u;
  234. _u._u32 = 0;
  235. _u.t = p_in;
  236. return _u._u32;
  237. }
  238. static _FORCE_INLINE_ uint64_t hash_djb2_one_float_64(double p_in, uint64_t p_prev = 5381) {
  239. union {
  240. double d;
  241. uint64_t i;
  242. } u;
  243. // Normalize +/- 0.0 and NaN values so they hash the same.
  244. if (p_in == 0.0f) {
  245. u.d = 0.0;
  246. } else if (Math::is_nan(p_in)) {
  247. u.d = NAN;
  248. } else {
  249. u.d = p_in;
  250. }
  251. return ((p_prev << 5) + p_prev) + u.i;
  252. }
  253. static _FORCE_INLINE_ uint64_t hash_djb2_one_64(uint64_t p_in, uint64_t p_prev = 5381) {
  254. return ((p_prev << 5) + p_prev) ^ p_in;
  255. }
  256. template <typename T>
  257. static _FORCE_INLINE_ uint64_t hash_make_uint64_t(T p_in) {
  258. union {
  259. T t;
  260. uint64_t _u64;
  261. } _u;
  262. _u._u64 = 0; // in case p_in is smaller
  263. _u.t = p_in;
  264. return _u._u64;
  265. }
  266. template <typename T>
  267. class Ref;
  268. struct HashMapHasherDefault {
  269. // Generic hash function for any type.
  270. template <typename T>
  271. static _FORCE_INLINE_ uint32_t hash(const T *p_pointer) { return hash_one_uint64((uint64_t)p_pointer); }
  272. template <typename T>
  273. static _FORCE_INLINE_ uint32_t hash(const Ref<T> &p_ref) { return hash_one_uint64((uint64_t)p_ref.operator->()); }
  274. static _FORCE_INLINE_ uint32_t hash(const String &p_string) { return p_string.hash(); }
  275. static _FORCE_INLINE_ uint32_t hash(const char *p_cstr) { return hash_djb2(p_cstr); }
  276. static _FORCE_INLINE_ uint32_t hash(const wchar_t p_wchar) { return hash_fmix32(uint32_t(p_wchar)); }
  277. static _FORCE_INLINE_ uint32_t hash(const char16_t p_uchar) { return hash_fmix32(uint32_t(p_uchar)); }
  278. static _FORCE_INLINE_ uint32_t hash(const char32_t p_uchar) { return hash_fmix32(uint32_t(p_uchar)); }
  279. static _FORCE_INLINE_ uint32_t hash(const RID &p_rid) { return hash_one_uint64(p_rid.get_id()); }
  280. static _FORCE_INLINE_ uint32_t hash(const CharString &p_char_string) { return hash_djb2(p_char_string.get_data()); }
  281. static _FORCE_INLINE_ uint32_t hash(const StringName &p_string_name) { return p_string_name.hash(); }
  282. static _FORCE_INLINE_ uint32_t hash(const NodePath &p_path) { return p_path.hash(); }
  283. static _FORCE_INLINE_ uint32_t hash(const ObjectID &p_id) { return hash_one_uint64(p_id); }
  284. static _FORCE_INLINE_ uint32_t hash(const uint64_t p_int) { return hash_one_uint64(p_int); }
  285. static _FORCE_INLINE_ uint32_t hash(const int64_t p_int) { return hash_one_uint64(uint64_t(p_int)); }
  286. static _FORCE_INLINE_ uint32_t hash(const float p_float) { return hash_murmur3_one_float(p_float); }
  287. static _FORCE_INLINE_ uint32_t hash(const double p_double) { return hash_murmur3_one_double(p_double); }
  288. static _FORCE_INLINE_ uint32_t hash(const uint32_t p_int) { return hash_fmix32(p_int); }
  289. static _FORCE_INLINE_ uint32_t hash(const int32_t p_int) { return hash_fmix32(uint32_t(p_int)); }
  290. static _FORCE_INLINE_ uint32_t hash(const uint16_t p_int) { return hash_fmix32(uint32_t(p_int)); }
  291. static _FORCE_INLINE_ uint32_t hash(const int16_t p_int) { return hash_fmix32(uint32_t(p_int)); }
  292. static _FORCE_INLINE_ uint32_t hash(const uint8_t p_int) { return hash_fmix32(uint32_t(p_int)); }
  293. static _FORCE_INLINE_ uint32_t hash(const int8_t p_int) { return hash_fmix32(uint32_t(p_int)); }
  294. static _FORCE_INLINE_ uint32_t hash(const Vector2i &p_vec) {
  295. uint32_t h = hash_murmur3_one_32(uint32_t(p_vec.x));
  296. h = hash_murmur3_one_32(uint32_t(p_vec.y), h);
  297. return hash_fmix32(h);
  298. }
  299. static _FORCE_INLINE_ uint32_t hash(const Vector3i &p_vec) {
  300. uint32_t h = hash_murmur3_one_32(uint32_t(p_vec.x));
  301. h = hash_murmur3_one_32(uint32_t(p_vec.y), h);
  302. h = hash_murmur3_one_32(uint32_t(p_vec.z), h);
  303. return hash_fmix32(h);
  304. }
  305. static _FORCE_INLINE_ uint32_t hash(const Vector4i &p_vec) {
  306. uint32_t h = hash_murmur3_one_32(uint32_t(p_vec.x));
  307. h = hash_murmur3_one_32(uint32_t(p_vec.y), h);
  308. h = hash_murmur3_one_32(uint32_t(p_vec.z), h);
  309. h = hash_murmur3_one_32(uint32_t(p_vec.w), h);
  310. return hash_fmix32(h);
  311. }
  312. static _FORCE_INLINE_ uint32_t hash(const Vector2 &p_vec) {
  313. uint32_t h = hash_murmur3_one_real(p_vec.x);
  314. h = hash_murmur3_one_real(p_vec.y, h);
  315. return hash_fmix32(h);
  316. }
  317. static _FORCE_INLINE_ uint32_t hash(const Vector3 &p_vec) {
  318. uint32_t h = hash_murmur3_one_real(p_vec.x);
  319. h = hash_murmur3_one_real(p_vec.y, h);
  320. h = hash_murmur3_one_real(p_vec.z, h);
  321. return hash_fmix32(h);
  322. }
  323. static _FORCE_INLINE_ uint32_t hash(const Vector4 &p_vec) {
  324. uint32_t h = hash_murmur3_one_real(p_vec.x);
  325. h = hash_murmur3_one_real(p_vec.y, h);
  326. h = hash_murmur3_one_real(p_vec.z, h);
  327. h = hash_murmur3_one_real(p_vec.w, h);
  328. return hash_fmix32(h);
  329. }
  330. static _FORCE_INLINE_ uint32_t hash(const Rect2i &p_rect) {
  331. uint32_t h = hash_murmur3_one_32(uint32_t(p_rect.position.x));
  332. h = hash_murmur3_one_32(uint32_t(p_rect.position.y), h);
  333. h = hash_murmur3_one_32(uint32_t(p_rect.size.x), h);
  334. h = hash_murmur3_one_32(uint32_t(p_rect.size.y), h);
  335. return hash_fmix32(h);
  336. }
  337. static _FORCE_INLINE_ uint32_t hash(const Rect2 &p_rect) {
  338. uint32_t h = hash_murmur3_one_real(p_rect.position.x);
  339. h = hash_murmur3_one_real(p_rect.position.y, h);
  340. h = hash_murmur3_one_real(p_rect.size.x, h);
  341. h = hash_murmur3_one_real(p_rect.size.y, h);
  342. return hash_fmix32(h);
  343. }
  344. static _FORCE_INLINE_ uint32_t hash(const AABB &p_aabb) {
  345. uint32_t h = hash_murmur3_one_real(p_aabb.position.x);
  346. h = hash_murmur3_one_real(p_aabb.position.y, h);
  347. h = hash_murmur3_one_real(p_aabb.position.z, h);
  348. h = hash_murmur3_one_real(p_aabb.size.x, h);
  349. h = hash_murmur3_one_real(p_aabb.size.y, h);
  350. h = hash_murmur3_one_real(p_aabb.size.z, h);
  351. return hash_fmix32(h);
  352. }
  353. };
  354. struct HashHasher {
  355. static _FORCE_INLINE_ uint32_t hash(const int32_t hash) { return hash; }
  356. static _FORCE_INLINE_ uint32_t hash(const uint32_t hash) { return hash; }
  357. static _FORCE_INLINE_ uint64_t hash(const int64_t hash) { return hash; }
  358. static _FORCE_INLINE_ uint64_t hash(const uint64_t hash) { return hash; }
  359. };
  360. // TODO: Fold this into HashMapHasherDefault once C++20 concepts are allowed
  361. template <typename T>
  362. struct HashableHasher {
  363. static _FORCE_INLINE_ uint32_t hash(const T &hashable) { return hashable.hash(); }
  364. };
  365. template <typename T>
  366. struct HashMapComparatorDefault {
  367. static bool compare(const T &p_lhs, const T &p_rhs) {
  368. return p_lhs == p_rhs;
  369. }
  370. };
  371. template <>
  372. struct HashMapComparatorDefault<float> {
  373. static bool compare(const float &p_lhs, const float &p_rhs) {
  374. return Math::is_same(p_lhs, p_rhs);
  375. }
  376. };
  377. template <>
  378. struct HashMapComparatorDefault<double> {
  379. static bool compare(const double &p_lhs, const double &p_rhs) {
  380. return Math::is_same(p_lhs, p_rhs);
  381. }
  382. };
  383. template <>
  384. struct HashMapComparatorDefault<Color> {
  385. static bool compare(const Color &p_lhs, const Color &p_rhs) {
  386. return p_lhs.is_same(p_rhs);
  387. }
  388. };
  389. template <>
  390. struct HashMapComparatorDefault<Vector2> {
  391. static bool compare(const Vector2 &p_lhs, const Vector2 &p_rhs) {
  392. return p_lhs.is_same(p_rhs);
  393. }
  394. };
  395. template <>
  396. struct HashMapComparatorDefault<Vector3> {
  397. static bool compare(const Vector3 &p_lhs, const Vector3 &p_rhs) {
  398. return p_lhs.is_same(p_rhs);
  399. }
  400. };
  401. template <>
  402. struct HashMapComparatorDefault<Vector4> {
  403. static bool compare(const Vector4 &p_lhs, const Vector4 &p_rhs) {
  404. return p_lhs.is_same(p_rhs);
  405. }
  406. };
  407. template <>
  408. struct HashMapComparatorDefault<Rect2> {
  409. static bool compare(const Rect2 &p_lhs, const Rect2 &p_rhs) {
  410. return p_lhs.is_same(p_rhs);
  411. }
  412. };
  413. template <>
  414. struct HashMapComparatorDefault<AABB> {
  415. static bool compare(const AABB &p_lhs, const AABB &p_rhs) {
  416. return p_lhs.is_same(p_rhs);
  417. }
  418. };
  419. template <>
  420. struct HashMapComparatorDefault<Plane> {
  421. static bool compare(const Plane &p_lhs, const Plane &p_rhs) {
  422. return p_lhs.is_same(p_rhs);
  423. }
  424. };
  425. template <>
  426. struct HashMapComparatorDefault<Transform2D> {
  427. static bool compare(const Transform2D &p_lhs, const Transform2D &p_rhs) {
  428. return p_lhs.is_same(p_rhs);
  429. }
  430. };
  431. template <>
  432. struct HashMapComparatorDefault<Basis> {
  433. static bool compare(const Basis &p_lhs, const Basis &p_rhs) {
  434. return p_lhs.is_same(p_rhs);
  435. }
  436. };
  437. template <>
  438. struct HashMapComparatorDefault<Transform3D> {
  439. static bool compare(const Transform3D &p_lhs, const Transform3D &p_rhs) {
  440. return p_lhs.is_same(p_rhs);
  441. }
  442. };
  443. template <>
  444. struct HashMapComparatorDefault<Projection> {
  445. static bool compare(const Projection &p_lhs, const Projection &p_rhs) {
  446. return p_lhs.is_same(p_rhs);
  447. }
  448. };
  449. template <>
  450. struct HashMapComparatorDefault<Quaternion> {
  451. static bool compare(const Quaternion &p_lhs, const Quaternion &p_rhs) {
  452. return p_lhs.is_same(p_rhs);
  453. }
  454. };
  455. constexpr uint32_t HASH_TABLE_SIZE_MAX = 29;
  456. inline constexpr uint32_t hash_table_size_primes[HASH_TABLE_SIZE_MAX] = {
  457. 5,
  458. 13,
  459. 23,
  460. 47,
  461. 97,
  462. 193,
  463. 389,
  464. 769,
  465. 1543,
  466. 3079,
  467. 6151,
  468. 12289,
  469. 24593,
  470. 49157,
  471. 98317,
  472. 196613,
  473. 393241,
  474. 786433,
  475. 1572869,
  476. 3145739,
  477. 6291469,
  478. 12582917,
  479. 25165843,
  480. 50331653,
  481. 100663319,
  482. 201326611,
  483. 402653189,
  484. 805306457,
  485. 1610612741,
  486. };
  487. // Computed with elem_i = UINT64_C (0 x FFFFFFFF FFFFFFFF ) / d_i + 1, where d_i is the i-th element of the above array.
  488. inline constexpr uint64_t hash_table_size_primes_inv[HASH_TABLE_SIZE_MAX] = {
  489. 3689348814741910324,
  490. 1418980313362273202,
  491. 802032351030850071,
  492. 392483916461905354,
  493. 190172619316593316,
  494. 95578984837873325,
  495. 47420935922132524,
  496. 23987963684927896,
  497. 11955116055547344,
  498. 5991147799191151,
  499. 2998982941588287,
  500. 1501077717772769,
  501. 750081082979285,
  502. 375261795343686,
  503. 187625172388393,
  504. 93822606204624,
  505. 46909513691883,
  506. 23456218233098,
  507. 11728086747027,
  508. 5864041509391,
  509. 2932024948977,
  510. 1466014921160,
  511. 733007198436,
  512. 366503839517,
  513. 183251896093,
  514. 91625960335,
  515. 45812983922,
  516. 22906489714,
  517. 11453246088
  518. };
  519. /**
  520. * Fastmod computes ( n mod d ) given the precomputed c much faster than n % d.
  521. * The implementation of fastmod is based on the following paper by Daniel Lemire et al.
  522. * Faster Remainder by Direct Computation: Applications to Compilers and Software Libraries
  523. * https://arxiv.org/abs/1902.01961
  524. */
  525. static _FORCE_INLINE_ uint32_t fastmod(const uint32_t n, const uint64_t c, const uint32_t d) {
  526. #if defined(_MSC_VER)
  527. // Returns the upper 64 bits of the product of two 64-bit unsigned integers.
  528. // This intrinsic function is required since MSVC does not support unsigned 128-bit integers.
  529. #if defined(_M_X64) || defined(_M_ARM64)
  530. return __umulh(c * n, d);
  531. #else
  532. // Fallback to the slower method for 32-bit platforms.
  533. return n % d;
  534. #endif // _M_X64 || _M_ARM64
  535. #else
  536. #ifdef __SIZEOF_INT128__
  537. // Prevent compiler warning, because we know what we are doing.
  538. uint64_t lowbits = c * n;
  539. __extension__ typedef unsigned __int128 uint128;
  540. return static_cast<uint64_t>(((uint128)lowbits * d) >> 64);
  541. #else
  542. // Fallback to the slower method if no 128-bit unsigned integer type is available.
  543. return n % d;
  544. #endif // __SIZEOF_INT128__
  545. #endif // _MSC_VER
  546. }