From c26e4ed3d5c0fa532356365bddd275fba04ae2c3 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sat, 8 Jun 2024 08:23:17 +0200 Subject: compat: rename hash's source file only --- compat/hash.cpp | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++++ compat/hash.hpp | 28 ++++++++++ compat/int-hash.cpp | 146 ---------------------------------------------------- compat/int-hash.hpp | 28 ---------- 4 files changed, 174 insertions(+), 174 deletions(-) create mode 100644 compat/hash.cpp create mode 100644 compat/hash.hpp delete mode 100644 compat/int-hash.cpp delete mode 100644 compat/int-hash.hpp (limited to 'compat') diff --git a/compat/hash.cpp b/compat/hash.cpp new file mode 100644 index 00000000..3cb3cc35 --- /dev/null +++ b/compat/hash.cpp @@ -0,0 +1,146 @@ +#include "compat/defs.hpp" +#include "hash.hpp" +#include +#include +#include + +namespace floormat { + +namespace { + +using namespace floormat::Hash; + +[[maybe_unused]] +CORRADE_ALWAYS_INLINE size_t fnvhash_uint_32(uint32_t x) +{ + constexpr auto params = fnvhash_params{}; + constexpr auto a = params.a, b = params.b; + auto hash = a; + const auto* str = (const char*)&x; + + hash *= b; hash ^= (uint8_t)*str++; // 0 + hash *= b; hash ^= (uint8_t)*str++; // 1 + hash *= b; hash ^= (uint8_t)*str++; // 2 + hash *= b; hash ^= (uint8_t)*str++; // 3 + + return hash; +} + +CORRADE_ALWAYS_INLINE size_t fnvhash_uint_64(uint64_t x) +{ + constexpr auto params = fnvhash_params)*8>{}; + constexpr auto a = params.a, b = params.b; + auto hash = a; + const auto* str = (const char*)&x; + + hash *= b; hash ^= (uint8_t)*str++; // 0 + hash *= b; hash ^= (uint8_t)*str++; // 1 + hash *= b; hash ^= (uint8_t)*str++; // 2 + hash *= b; hash ^= (uint8_t)*str++; // 3 + hash *= b; hash ^= (uint8_t)*str++; // 4 + hash *= b; hash ^= (uint8_t)*str++; // 5 + hash *= b; hash ^= (uint8_t)*str++; // 6 + hash *= b; hash ^= (uint8_t)*str++; // 7 + + return hash; +} + +} // namespace + +uint32_t hash_32(const void* buf, size_t size) noexcept +{ + constexpr auto params = fnvhash_params<32>{}; + constexpr auto a = params.a, b = params.b; + auto hash = a; + const auto *str = (const char*)buf, *const end = str + size; + +fm_UNROLL_4 + for (; str != end; ++str) + { + hash *= b; + hash ^= (uint8_t)*str; + } + return hash; +} + +uint64_t hash_64(const void* buf, size_t size) noexcept +{ + constexpr auto params = fnvhash_params{}; + constexpr auto a = params.a, b = params.b; + auto hash = a; + const auto *str = (const char*)buf, *const end = str + size; + +fm_UNROLL_8 + for (; str != end; ++str) + { + hash *= b; + hash ^= (uint8_t)*str; + } + return hash; +} + +size_t hash_buf(const void* buf, size_t size) noexcept +{ +#if 1 + if constexpr(sizeof nullptr > 4) + return hash_64(buf, size); + else +#endif + return hash_32(buf, size); +} + +size_t hash_int(uint32_t x) noexcept +{ +#if 1 + if constexpr(sizeof nullptr > 4) + return fnvhash_uint_64(x); + else +#endif + return fnvhash_uint_32(x); +} + +size_t hash_int(uint64_t x) noexcept +{ + return fnvhash_uint_64(x); +} + +size_t hash_string_view::operator()(StringView s) const noexcept { return Hash::fnvhash_buf(s.data(), s.size()); } + +} // namespace floormat + +namespace floormat::Hash { + +size_t fnvhash_buf(const void* __restrict buf, size_t size, size_t seed) noexcept +{ + constexpr size_t b{fnvhash_params::b}; + size_t full_rounds = size / 8, rest = size % 8; + size_t hash = seed; + const char* str = (const char*)buf; + + while (full_rounds--) + { + hash *= b; hash ^= (uint8_t)*str++; // 0 + hash *= b; hash ^= (uint8_t)*str++; // 1 + hash *= b; hash ^= (uint8_t)*str++; // 2 + hash *= b; hash ^= (uint8_t)*str++; // 3 + hash *= b; hash ^= (uint8_t)*str++; // 4 + hash *= b; hash ^= (uint8_t)*str++; // 5 + hash *= b; hash ^= (uint8_t)*str++; // 6 + hash *= b; hash ^= (uint8_t)*str++; // 7 + } + switch (rest) + { + case 7: hash *= b; hash ^= (uint8_t)*str++; [[fallthrough]]; + case 6: hash *= b; hash ^= (uint8_t)*str++; [[fallthrough]]; + case 5: hash *= b; hash ^= (uint8_t)*str++; [[fallthrough]]; + case 4: hash *= b; hash ^= (uint8_t)*str++; [[fallthrough]]; + case 3: hash *= b; hash ^= (uint8_t)*str++; [[fallthrough]]; + case 2: hash *= b; hash ^= (uint8_t)*str++; [[fallthrough]]; + case 1: hash *= b; hash ^= (uint8_t)*str++; [[fallthrough]]; + case 0: break; + default: std::unreachable(); + } + return hash; +} + +} // namespace floormat::Hash diff --git a/compat/hash.hpp b/compat/hash.hpp new file mode 100644 index 00000000..c512d249 --- /dev/null +++ b/compat/hash.hpp @@ -0,0 +1,28 @@ +#pragma once + +// todo rename to hash-fnv.hpp + +namespace floormat::Hash { + +template struct fnvhash_params; +template<> struct fnvhash_params<32> { static constexpr uint32_t a = 0x811c9dc5u, b = 0x01000193u; }; +template<> struct fnvhash_params<64> { static constexpr uint64_t a = 0xcbf29ce484222325u, b = 0x100000001b3u; }; + +constexpr inline size_t fnvhash_seed = fnvhash_params<>::a; + +size_t fnvhash_buf(const void* __restrict buf, size_t size, size_t seed = fnvhash_seed) noexcept; + +} // namespace floormat::Hash + +namespace floormat { // todo + +uint64_t hash_64(const void* buf, size_t size) noexcept; +uint32_t hash_32(const void* buf, size_t size) noexcept; +size_t hash_buf(const void* buf, size_t size) noexcept; + +size_t hash_int(uint32_t x) noexcept; +size_t hash_int(uint64_t x) noexcept; + +struct hash_string_view { size_t operator()(StringView str) const noexcept; }; + +} // namespace floormat diff --git a/compat/int-hash.cpp b/compat/int-hash.cpp deleted file mode 100644 index 6415f0e6..00000000 --- a/compat/int-hash.cpp +++ /dev/null @@ -1,146 +0,0 @@ -#include "compat/defs.hpp" -#include "int-hash.hpp" -#include -#include -#include - -namespace floormat { - -namespace { - -using namespace floormat::Hash; - -[[maybe_unused]] -CORRADE_ALWAYS_INLINE size_t fnvhash_uint_32(uint32_t x) -{ - constexpr auto params = fnvhash_params{}; - constexpr auto a = params.a, b = params.b; - auto hash = a; - const auto* str = (const char*)&x; - - hash *= b; hash ^= (uint8_t)*str++; // 0 - hash *= b; hash ^= (uint8_t)*str++; // 1 - hash *= b; hash ^= (uint8_t)*str++; // 2 - hash *= b; hash ^= (uint8_t)*str++; // 3 - - return hash; -} - -CORRADE_ALWAYS_INLINE size_t fnvhash_uint_64(uint64_t x) -{ - constexpr auto params = fnvhash_params)*8>{}; - constexpr auto a = params.a, b = params.b; - auto hash = a; - const auto* str = (const char*)&x; - - hash *= b; hash ^= (uint8_t)*str++; // 0 - hash *= b; hash ^= (uint8_t)*str++; // 1 - hash *= b; hash ^= (uint8_t)*str++; // 2 - hash *= b; hash ^= (uint8_t)*str++; // 3 - hash *= b; hash ^= (uint8_t)*str++; // 4 - hash *= b; hash ^= (uint8_t)*str++; // 5 - hash *= b; hash ^= (uint8_t)*str++; // 6 - hash *= b; hash ^= (uint8_t)*str++; // 7 - - return hash; -} - -} // namespace - -uint32_t hash_32(const void* buf, size_t size) noexcept -{ - constexpr auto params = fnvhash_params<32>{}; - constexpr auto a = params.a, b = params.b; - auto hash = a; - const auto *str = (const char*)buf, *const end = str + size; - -fm_UNROLL_4 - for (; str != end; ++str) - { - hash *= b; - hash ^= (uint8_t)*str; - } - return hash; -} - -uint64_t hash_64(const void* buf, size_t size) noexcept -{ - constexpr auto params = fnvhash_params{}; - constexpr auto a = params.a, b = params.b; - auto hash = a; - const auto *str = (const char*)buf, *const end = str + size; - -fm_UNROLL_8 - for (; str != end; ++str) - { - hash *= b; - hash ^= (uint8_t)*str; - } - return hash; -} - -size_t hash_buf(const void* buf, size_t size) noexcept -{ -#if 1 - if constexpr(sizeof nullptr > 4) - return hash_64(buf, size); - else -#endif - return hash_32(buf, size); -} - -size_t hash_int(uint32_t x) noexcept -{ -#if 1 - if constexpr(sizeof nullptr > 4) - return fnvhash_uint_64(x); - else -#endif - return fnvhash_uint_32(x); -} - -size_t hash_int(uint64_t x) noexcept -{ - return fnvhash_uint_64(x); -} - -size_t hash_string_view::operator()(StringView s) const noexcept { return Hash::fnvhash_buf(s.data(), s.size()); } - -} // namespace floormat - -namespace floormat::Hash { - -size_t fnvhash_buf(const void* __restrict buf, size_t size, size_t seed) noexcept -{ - constexpr size_t b{fnvhash_params::b}; - size_t full_rounds = size / 8, rest = size % 8; - size_t hash = seed; - const char* str = (const char*)buf; - - while (full_rounds--) - { - hash *= b; hash ^= (uint8_t)*str++; // 0 - hash *= b; hash ^= (uint8_t)*str++; // 1 - hash *= b; hash ^= (uint8_t)*str++; // 2 - hash *= b; hash ^= (uint8_t)*str++; // 3 - hash *= b; hash ^= (uint8_t)*str++; // 4 - hash *= b; hash ^= (uint8_t)*str++; // 5 - hash *= b; hash ^= (uint8_t)*str++; // 6 - hash *= b; hash ^= (uint8_t)*str++; // 7 - } - switch (rest) - { - case 7: hash *= b; hash ^= (uint8_t)*str++; [[fallthrough]]; - case 6: hash *= b; hash ^= (uint8_t)*str++; [[fallthrough]]; - case 5: hash *= b; hash ^= (uint8_t)*str++; [[fallthrough]]; - case 4: hash *= b; hash ^= (uint8_t)*str++; [[fallthrough]]; - case 3: hash *= b; hash ^= (uint8_t)*str++; [[fallthrough]]; - case 2: hash *= b; hash ^= (uint8_t)*str++; [[fallthrough]]; - case 1: hash *= b; hash ^= (uint8_t)*str++; [[fallthrough]]; - case 0: break; - default: std::unreachable(); - } - return hash; -} - -} // namespace floormat::Hash diff --git a/compat/int-hash.hpp b/compat/int-hash.hpp deleted file mode 100644 index c512d249..00000000 --- a/compat/int-hash.hpp +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once - -// todo rename to hash-fnv.hpp - -namespace floormat::Hash { - -template struct fnvhash_params; -template<> struct fnvhash_params<32> { static constexpr uint32_t a = 0x811c9dc5u, b = 0x01000193u; }; -template<> struct fnvhash_params<64> { static constexpr uint64_t a = 0xcbf29ce484222325u, b = 0x100000001b3u; }; - -constexpr inline size_t fnvhash_seed = fnvhash_params<>::a; - -size_t fnvhash_buf(const void* __restrict buf, size_t size, size_t seed = fnvhash_seed) noexcept; - -} // namespace floormat::Hash - -namespace floormat { // todo - -uint64_t hash_64(const void* buf, size_t size) noexcept; -uint32_t hash_32(const void* buf, size_t size) noexcept; -size_t hash_buf(const void* buf, size_t size) noexcept; - -size_t hash_int(uint32_t x) noexcept; -size_t hash_int(uint64_t x) noexcept; - -struct hash_string_view { size_t operator()(StringView str) const noexcept; }; - -} // namespace floormat -- cgit v1.2.3