/**************************************************************************/ /* hashfuncs.cpp */ /**************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /**************************************************************************/ /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ /* "Software"), to deal in the Software without restriction, including */ /* without limitation the rights to use, copy, modify, merge, publish, */ /* distribute, sublicense, and/or sell copies of the Software, and to */ /* permit persons to whom the Software is furnished to do so, subject to */ /* the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /**************************************************************************/ #include "hashfuncs.h" uint32_t hash_murmur3_one_float(float p_in, uint32_t p_seed) { union { float f; uint32_t i; } u; // Normalize +/- 0.0 and NaN values so they hash the same. if (p_in == 0.0f) { u.f = 0.0; } else if (Math::is_nan(p_in)) { u.f = Math::NaN; } else { u.f = p_in; } return hash_murmur3_one_32(u.i, p_seed); } uint32_t hash_murmur3_one_double(double p_in, uint32_t p_seed) { union { double d; uint64_t i; } u; // Normalize +/- 0.0 and NaN values so they hash the same. if (p_in == 0.0f) { u.d = 0.0; } else if (Math::is_nan(p_in)) { u.d = Math::NaN; } else { u.d = p_in; } return hash_murmur3_one_64(u.i, p_seed); } uint32_t hash_murmur3_buffer(const void *key, int length, const uint32_t seed) { // Although not required, this is a random prime number. const uint8_t *data = (const uint8_t *)key; const int nblocks = length / 4; uint32_t h1 = seed; const uint32_t c1 = 0xcc9e2d51; const uint32_t c2 = 0x1b873593; const uint32_t *blocks = (const uint32_t *)(data + nblocks * 4); for (int i = -nblocks; i; i++) { uint32_t k1 = blocks[i]; k1 *= c1; k1 = hash_rotl32(k1, 15); k1 *= c2; h1 ^= k1; h1 = hash_rotl32(h1, 13); h1 = h1 * 5 + 0xe6546b64; } const uint8_t *tail = (const uint8_t *)(data + nblocks * 4); uint32_t k1 = 0; switch (length & 3) { case 3: k1 ^= tail[2] << 16; [[fallthrough]]; case 2: k1 ^= tail[1] << 8; [[fallthrough]]; case 1: k1 ^= tail[0]; k1 *= c1; k1 = hash_rotl32(k1, 15); k1 *= c2; h1 ^= k1; }; // Finalize with additional bit mixing. h1 ^= length; return hash_fmix32(h1); } uint32_t hash_djb2_one_float(double p_in, uint32_t p_prev) { union { double d; uint64_t i; } u; // Normalize +/- 0.0 and NaN values so they hash the same. if (p_in == 0.0f) { u.d = 0.0; } else if (Math::is_nan(p_in)) { u.d = Math::NaN; } else { u.d = p_in; } return ((p_prev << 5) + p_prev) + hash_one_uint64(u.i); } uint64_t hash_djb2_one_float_64(double p_in, uint64_t p_prev) { union { double d; uint64_t i; } u; // Normalize +/- 0.0 and NaN values so they hash the same. if (p_in == 0.0f) { u.d = 0.0; } else if (Math::is_nan(p_in)) { u.d = Math::NaN; } else { u.d = p_in; } return ((p_prev << 5) + p_prev) + u.i; }