hashfuncs.h 19 KB

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