2
0

hashfuncs.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*************************************************************************/
  2. /* hashfuncs.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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 "math_defs.h"
  33. #include "math_funcs.h"
  34. #include "typedefs.h"
  35. /**
  36. * Hashing functions
  37. */
  38. /**
  39. * DJB2 Hash function
  40. * @param C String
  41. * @return 32-bits hashcode
  42. */
  43. static inline uint32_t hash_djb2(const char *p_cstr) {
  44. const unsigned char *chr = (const unsigned char *)p_cstr;
  45. uint32_t hash = 5381;
  46. uint32_t c;
  47. while ((c = *chr++))
  48. hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
  49. return hash;
  50. }
  51. static inline uint32_t hash_djb2_buffer(const uint8_t *p_buff, int p_len, uint32_t p_prev = 5381) {
  52. uint32_t hash = p_prev;
  53. for (int i = 0; i < p_len; i++)
  54. hash = ((hash << 5) + hash) + p_buff[i]; /* hash * 33 + c */
  55. return hash;
  56. }
  57. static inline uint32_t hash_djb2_one_32(uint32_t p_in, uint32_t p_prev = 5381) {
  58. return ((p_prev << 5) + p_prev) + p_in;
  59. }
  60. static inline uint32_t hash_one_uint64(const uint64_t p_int) {
  61. uint64_t v = p_int;
  62. v = (~v) + (v << 18); // v = (v << 18) - v - 1;
  63. v = v ^ (v >> 31);
  64. v = v * 21; // v = (v + (v << 2)) + (v << 4);
  65. v = v ^ (v >> 11);
  66. v = v + (v << 6);
  67. v = v ^ (v >> 22);
  68. return (int)v;
  69. }
  70. static inline uint32_t hash_djb2_one_float(double p_in, uint32_t p_prev = 5381) {
  71. union {
  72. double d;
  73. uint64_t i;
  74. } u;
  75. // Normalize +/- 0.0 and NaN values so they hash the same.
  76. if (p_in == 0.0f)
  77. u.d = 0.0;
  78. else if (Math::is_nan(p_in))
  79. u.d = NAN;
  80. else
  81. u.d = p_in;
  82. return ((p_prev << 5) + p_prev) + hash_one_uint64(u.i);
  83. }
  84. template <class T>
  85. static inline uint32_t make_uint32_t(T p_in) {
  86. union {
  87. T t;
  88. uint32_t _u32;
  89. } _u;
  90. _u._u32 = 0;
  91. _u.t = p_in;
  92. return _u._u32;
  93. }
  94. static inline uint64_t hash_djb2_one_64(uint64_t p_in, uint64_t p_prev = 5381) {
  95. return ((p_prev << 5) + p_prev) + p_in;
  96. }
  97. template <class T>
  98. static inline uint64_t make_uint64_t(T p_in) {
  99. union {
  100. T t;
  101. uint64_t _u64;
  102. } _u;
  103. _u._u64 = 0; // in case p_in is smaller
  104. _u.t = p_in;
  105. return _u._u64;
  106. }
  107. #endif