From d1e3660288b33421456bcf459a03d7619aa3a0c4 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Wed, 4 Oct 2023 23:10:52 +0200 Subject: compat/int-hash: unroll it --- compat/int-hash.cpp | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) (limited to 'compat/int-hash.cpp') diff --git a/compat/int-hash.cpp b/compat/int-hash.cpp index 72c38179..a15fae29 100644 --- a/compat/int-hash.cpp +++ b/compat/int-hash.cpp @@ -1,33 +1,50 @@ +#include "compat/defs.hpp" #include "int-hash.hpp" #include namespace floormat { -template -static CORRADE_ALWAYS_INLINE -T FNVHash(const char* str, size_t size) +namespace { + +CORRADE_ALWAYS_INLINE uint32_t FNVHash_32(uint32_t x) +{ + const auto *str = (const char*)&x, *end = str + 4; + uint32_t hash = 0x811c9dc5u; +fm_UNROLL_4 + for (; str != end; ++str) + { + hash *= 0x01000193u; + hash ^= (uint8_t)*str; + } + return hash; +} + +CORRADE_ALWAYS_INLINE uint64_t FNVHash_64(uint64_t x) { - const auto* end = str + size; - T hash = offset_basis; + const auto *str = (const char*)&x, *end = str + 8; + uint64_t hash = 0xcbf29ce484222325u; +fm_UNROLL_8 for (; str != end; ++str) { - hash *= prime; + hash *= 0x100000001b3u; hash ^= (uint8_t)*str; } return hash; } +} // namespace + size_t int_hash(uint32_t x) noexcept { if constexpr(sizeof(size_t) == 4) - return FNVHash((const char*)&x, 4); + return FNVHash_32(x); else - return int_hash(uint64_t(x)); + return FNVHash_64(x); } size_t int_hash(uint64_t x) noexcept { - return FNVHash((const char*)&x, 8); + return FNVHash_64(x); } } // namespace floormat -- cgit v1.2.3