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