From 95b940b807eb213e2e86a32f8f4cc98fd6b13400 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Mon, 10 Apr 2023 09:04:07 +0200 Subject: src/world: cleanup & fix hash stuff --- compat/int-hash.cpp | 37 +++++++++++++++++++++++++++++++++++++ compat/int-hash.hpp | 27 ++++----------------------- 2 files changed, 41 insertions(+), 23 deletions(-) create mode 100644 compat/int-hash.cpp (limited to 'compat') diff --git a/compat/int-hash.cpp b/compat/int-hash.cpp new file mode 100644 index 00000000..ae2e6729 --- /dev/null +++ b/compat/int-hash.cpp @@ -0,0 +1,37 @@ +#include "int-hash.hpp" +#include + +namespace floormat { + +size_t int_hash32(uint32_t x) noexcept +{ + if constexpr(sizeof(size_t) == 4) + { + // by Chris Wellons + + x ^= x >> 15; + x *= 0x2c1b3c6dU; + x ^= x >> 12; + x *= 0x297a2d39U; + x ^= x >> 15; + + return x; + } + else + return int_hash64(x); +} + +size_t int_hash64(uint64_t x) noexcept +{ + // NASAM by Pelle Evensen + + x ^= std::rotr(x, 25) ^ std::rotr(x, 47); + x *= 0x9E6C63D0676A9A99UL; + x ^= x >> 23 ^ x >> 51; + x *= 0x9E6D62D06F6A9A9BUL; + x ^= x >> 23 ^ x >> 51; + + return x; +} + +} // namespace floormat diff --git a/compat/int-hash.hpp b/compat/int-hash.hpp index 52b4c170..e5cfc67a 100644 --- a/compat/int-hash.hpp +++ b/compat/int-hash.hpp @@ -1,30 +1,11 @@ #pragma once -#include namespace floormat { -constexpr inline size_t int_hash(size_t x) noexcept -{ - if constexpr(sizeof(size_t) == 4) - { - // by Chris Wellons - x ^= x >> 15; - x *= 0x2c1b3c6dU; - x ^= x >> 12; - x *= 0x297a2d39U; - x ^= x >> 15; - } - else if constexpr(sizeof(size_t) == 8) - { - // NASAM by Pelle Evensen - x ^= std::rotr(x, 25) ^ std::rotr(x, 47); - x *= 0x9E6C63D0676A9A99UL; - x ^= x >> 23 ^ x >> 51; - x *= 0x9E6D62D06F6A9A9BUL; - x ^= x >> 23 ^ x >> 51; - } +size_t int_hash32(uint32_t x) noexcept; +size_t int_hash64(uint64_t x) noexcept; - return x; -} +inline size_t int_hash(uint32_t x) noexcept { return int_hash32(x); } +inline size_t int_hash(uint64_t x) noexcept { return int_hash64(x); } } // namespace floormat -- cgit v1.2.3