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