hashfuncs.hpp 16 KB

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