From c077f0f904cce505e15543b855b4330ca547b53d Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Thu, 14 Sep 2023 09:02:23 +0200 Subject: replace hash with fnv1 --- compat/int-hash.cpp | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) (limited to 'compat') diff --git a/compat/int-hash.cpp b/compat/int-hash.cpp index 00f2e474..72c38179 100644 --- a/compat/int-hash.cpp +++ b/compat/int-hash.cpp @@ -3,35 +3,31 @@ namespace floormat { -size_t int_hash(uint32_t x) noexcept +template +static CORRADE_ALWAYS_INLINE +T FNVHash(const char* str, size_t size) { - if constexpr(sizeof(size_t) == 4) + const auto* end = str + size; + T hash = offset_basis; + for (; str != end; ++str) { - // by Chris Wellons - - x ^= x >> 15; - x *= 0x2c1b3c6dU; - x ^= x >> 12; - x *= 0x297a2d39U; - x ^= x >> 15; - - return x; + hash *= prime; + hash ^= (uint8_t)*str; } + return hash; +} + +size_t int_hash(uint32_t x) noexcept +{ + if constexpr(sizeof(size_t) == 4) + return FNVHash((const char*)&x, 4); else return int_hash(uint64_t(x)); } size_t int_hash(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; + return FNVHash((const char*)&x, 8); } } // namespace floormat -- cgit v1.2.3