|
| 1 | +// Copyright 2026 Matt Borland |
| 2 | +// Distributed under the Boost Software License, Version 1.0. |
| 3 | +// https://www.boost.org/LICENSE_1_0.txt |
| 4 | + |
| 5 | +#ifndef BOOST_INT128_HASH_HPP |
| 6 | +#define BOOST_INT128_HASH_HPP |
| 7 | + |
| 8 | +#include <boost/int128/int128.hpp> |
| 9 | + |
| 10 | +#ifndef BOOST_INT128_BUILD_MODULE |
| 11 | + |
| 12 | +#include <cstddef> |
| 13 | +#include <cstdint> |
| 14 | +#include <functional> |
| 15 | + |
| 16 | +#endif |
| 17 | + |
| 18 | +namespace boost { |
| 19 | +namespace int128 { |
| 20 | +namespace detail { |
| 21 | + |
| 22 | +// The cast is only useless for 64-bit platforms |
| 23 | +// Without we get an implicit conversion warning which is arguably worse |
| 24 | +#if defined(__GNUC__) && !defined(__clang__) |
| 25 | +# pragma GCC diagnostic push |
| 26 | +# pragma GCC diagnostic ignored "-Wuseless-cast" |
| 27 | +#endif |
| 28 | + |
| 29 | +// splitmix64 finalizer: mixes all 64 input bits into the result before any narrowing to size_t. |
| 30 | +// This is required for correctness on platforms where size_t is 32 bits |
| 31 | +inline std::size_t hash_finalize_64(std::uint64_t v) noexcept |
| 32 | +{ |
| 33 | + v ^= v >> 30; |
| 34 | + v *= UINT64_C(0xbf58476d1ce4e5b9); |
| 35 | + v ^= v >> 27; |
| 36 | + v *= UINT64_C(0x94d049bb133111eb); |
| 37 | + v ^= v >> 31; |
| 38 | + return static_cast<std::size_t>(v); |
| 39 | +} |
| 40 | + |
| 41 | +#if defined(__GNUC__) && !defined(__clang__) |
| 42 | +# pragma GCC diagnostic pop |
| 43 | +#endif |
| 44 | + |
| 45 | +} // namespace detail |
| 46 | +} // namespace int128 |
| 47 | +} // namespace boost |
| 48 | + |
| 49 | +namespace std { |
| 50 | + |
| 51 | +template <> |
| 52 | +struct hash<boost::int128::int128_t> |
| 53 | +{ |
| 54 | + auto operator()(const boost::int128::int128_t v) const noexcept -> std::size_t |
| 55 | + { |
| 56 | + const std::size_t low_hash {boost::int128::detail::hash_finalize_64(v.low)}; |
| 57 | + const std::size_t high_hash {boost::int128::detail::hash_finalize_64(static_cast<std::uint64_t>(v.high))}; |
| 58 | + |
| 59 | + // boost::hash_combine style mixing of the two finalized halves |
| 60 | + return low_hash ^ (high_hash + static_cast<std::size_t>(0x9e3779b9) + (low_hash << 6) + (low_hash >> 2)); |
| 61 | + } |
| 62 | +}; |
| 63 | + |
| 64 | +template <> |
| 65 | +struct hash<boost::int128::uint128_t> |
| 66 | +{ |
| 67 | + auto operator()(const boost::int128::uint128_t v) const noexcept -> std::size_t |
| 68 | + { |
| 69 | + const std::size_t low_hash {boost::int128::detail::hash_finalize_64(v.low)}; |
| 70 | + const std::size_t high_hash {boost::int128::detail::hash_finalize_64(v.high)}; |
| 71 | + |
| 72 | + // boost::hash_combine style mixing of the two finalized halves |
| 73 | + return low_hash ^ (high_hash + static_cast<std::size_t>(0x9e3779b9) + (low_hash << 6) + (low_hash >> 2)); |
| 74 | + } |
| 75 | +}; |
| 76 | + |
| 77 | +} // namespace std |
| 78 | + |
| 79 | +#endif // BOOST_INT128_HASH_HPP |
0 commit comments