/**************************************************************************/1/* hashfuncs.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "hashfuncs.h"3132uint32_t hash_murmur3_one_float(float p_in, uint32_t p_seed) {33union {34float f;35uint32_t i;36} u;3738// Normalize +/- 0.0 and NaN values so they hash the same.39if (p_in == 0.0f) {40u.f = 0.0;41} else if (Math::is_nan(p_in)) {42u.f = Math::NaN;43} else {44u.f = p_in;45}4647return hash_murmur3_one_32(u.i, p_seed);48}4950uint32_t hash_murmur3_one_double(double p_in, uint32_t p_seed) {51union {52double d;53uint64_t i;54} u;5556// Normalize +/- 0.0 and NaN values so they hash the same.57if (p_in == 0.0f) {58u.d = 0.0;59} else if (Math::is_nan(p_in)) {60u.d = Math::NaN;61} else {62u.d = p_in;63}6465return hash_murmur3_one_64(u.i, p_seed);66}6768uint32_t hash_murmur3_buffer(const void *key, int length, const uint32_t seed) {69// Although not required, this is a random prime number.70const uint8_t *data = (const uint8_t *)key;71const int nblocks = length / 4;7273uint32_t h1 = seed;7475const uint32_t c1 = 0xcc9e2d51;76const uint32_t c2 = 0x1b873593;7778const uint32_t *blocks = (const uint32_t *)(data + nblocks * 4);7980for (int i = -nblocks; i; i++) {81uint32_t k1 = blocks[i];8283k1 *= c1;84k1 = hash_rotl32(k1, 15);85k1 *= c2;8687h1 ^= k1;88h1 = hash_rotl32(h1, 13);89h1 = h1 * 5 + 0xe6546b64;90}9192const uint8_t *tail = (const uint8_t *)(data + nblocks * 4);9394uint32_t k1 = 0;9596switch (length & 3) {97case 3:98k1 ^= tail[2] << 16;99[[fallthrough]];100case 2:101k1 ^= tail[1] << 8;102[[fallthrough]];103case 1:104k1 ^= tail[0];105k1 *= c1;106k1 = hash_rotl32(k1, 15);107k1 *= c2;108h1 ^= k1;109};110111// Finalize with additional bit mixing.112h1 ^= length;113return hash_fmix32(h1);114}115116uint32_t hash_djb2_one_float(double p_in, uint32_t p_prev) {117union {118double d;119uint64_t i;120} u;121122// Normalize +/- 0.0 and NaN values so they hash the same.123if (p_in == 0.0f) {124u.d = 0.0;125} else if (Math::is_nan(p_in)) {126u.d = Math::NaN;127} else {128u.d = p_in;129}130131return ((p_prev << 5) + p_prev) + hash_one_uint64(u.i);132}133134uint64_t hash_djb2_one_float_64(double p_in, uint64_t p_prev) {135union {136double d;137uint64_t i;138} u;139140// Normalize +/- 0.0 and NaN values so they hash the same.141if (p_in == 0.0f) {142u.d = 0.0;143} else if (Math::is_nan(p_in)) {144u.d = Math::NaN;145} else {146u.d = p_in;147}148149return ((p_prev << 5) + p_prev) + u.i;150}151152153