hashfuncs.h 19 KB

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