hashfuncs.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**************************************************************************/
  2. /* hashfuncs.cpp */
  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. #include "hashfuncs.h"
  31. uint32_t hash_murmur3_one_float(float p_in, uint32_t p_seed) {
  32. union {
  33. float f;
  34. uint32_t i;
  35. } u;
  36. // Normalize +/- 0.0 and NaN values so they hash the same.
  37. if (p_in == 0.0f) {
  38. u.f = 0.0;
  39. } else if (Math::is_nan(p_in)) {
  40. u.f = Math::NaN;
  41. } else {
  42. u.f = p_in;
  43. }
  44. return hash_murmur3_one_32(u.i, p_seed);
  45. }
  46. uint32_t hash_murmur3_one_double(double p_in, uint32_t p_seed) {
  47. union {
  48. double d;
  49. uint64_t i;
  50. } u;
  51. // Normalize +/- 0.0 and NaN values so they hash the same.
  52. if (p_in == 0.0f) {
  53. u.d = 0.0;
  54. } else if (Math::is_nan(p_in)) {
  55. u.d = Math::NaN;
  56. } else {
  57. u.d = p_in;
  58. }
  59. return hash_murmur3_one_64(u.i, p_seed);
  60. }
  61. uint32_t hash_murmur3_buffer(const void *key, int length, const uint32_t seed) {
  62. // Although not required, this is a random prime number.
  63. const uint8_t *data = (const uint8_t *)key;
  64. const int nblocks = length / 4;
  65. uint32_t h1 = seed;
  66. const uint32_t c1 = 0xcc9e2d51;
  67. const uint32_t c2 = 0x1b873593;
  68. const uint32_t *blocks = (const uint32_t *)(data + nblocks * 4);
  69. for (int i = -nblocks; i; i++) {
  70. uint32_t k1 = blocks[i];
  71. k1 *= c1;
  72. k1 = hash_rotl32(k1, 15);
  73. k1 *= c2;
  74. h1 ^= k1;
  75. h1 = hash_rotl32(h1, 13);
  76. h1 = h1 * 5 + 0xe6546b64;
  77. }
  78. const uint8_t *tail = (const uint8_t *)(data + nblocks * 4);
  79. uint32_t k1 = 0;
  80. switch (length & 3) {
  81. case 3:
  82. k1 ^= tail[2] << 16;
  83. [[fallthrough]];
  84. case 2:
  85. k1 ^= tail[1] << 8;
  86. [[fallthrough]];
  87. case 1:
  88. k1 ^= tail[0];
  89. k1 *= c1;
  90. k1 = hash_rotl32(k1, 15);
  91. k1 *= c2;
  92. h1 ^= k1;
  93. };
  94. // Finalize with additional bit mixing.
  95. h1 ^= length;
  96. return hash_fmix32(h1);
  97. }
  98. uint32_t hash_djb2_one_float(double p_in, uint32_t p_prev) {
  99. union {
  100. double d;
  101. uint64_t i;
  102. } u;
  103. // Normalize +/- 0.0 and NaN values so they hash the same.
  104. if (p_in == 0.0f) {
  105. u.d = 0.0;
  106. } else if (Math::is_nan(p_in)) {
  107. u.d = Math::NaN;
  108. } else {
  109. u.d = p_in;
  110. }
  111. return ((p_prev << 5) + p_prev) + hash_one_uint64(u.i);
  112. }
  113. uint64_t hash_djb2_one_float_64(double p_in, uint64_t p_prev) {
  114. union {
  115. double d;
  116. uint64_t i;
  117. } u;
  118. // Normalize +/- 0.0 and NaN values so they hash the same.
  119. if (p_in == 0.0f) {
  120. u.d = 0.0;
  121. } else if (Math::is_nan(p_in)) {
  122. u.d = Math::NaN;
  123. } else {
  124. u.d = p_in;
  125. }
  126. return ((p_prev << 5) + p_prev) + u.i;
  127. }