hashfuncs.h 16 KB

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