hashfuncs.hpp 20 KB

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