Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions include/boost/decimal/hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <boost/decimal/decimal_fast64_t.hpp>
#include <boost/decimal/decimal_fast128_t.hpp>
#include <boost/decimal/detail/config.hpp>
#include <boost/decimal/detail/cmath/normalize.hpp>

#ifndef BOOST_DECIMAL_BUILD_MODULE
#include <functional>
Expand All @@ -26,8 +27,10 @@ struct hash<boost::decimal::decimal32_t>
// Since the underlying type is a std::uint32_t, we will rely on its hash function from the STL
auto operator()(const boost::decimal::decimal32_t& v) const noexcept -> std::size_t
{
const auto normalized_v {boost::decimal::normalize(v)};

std::uint32_t bits;
std::memcpy(&bits, &v, sizeof(std::uint32_t));
std::memcpy(&bits, &normalized_v, sizeof(std::uint32_t));

return std::hash<std::uint32_t>{}(bits);
}
Expand All @@ -39,8 +42,10 @@ struct hash<boost::decimal::decimal64_t>
// Since the underlying type is a std::uint64_t, we will rely on its hash function from the STL
auto operator()(const boost::decimal::decimal64_t& v) const noexcept -> std::size_t
{
const auto normalized_v {boost::decimal::normalize(v)};

std::uint64_t bits;
std::memcpy(&bits, &v, sizeof(std::uint64_t));
std::memcpy(&bits, &normalized_v, sizeof(std::uint64_t));

return std::hash<std::uint64_t>{}(bits);
}
Expand All @@ -57,8 +62,10 @@ struct hash<boost::decimal::decimal128_t>
// Take the xor of the two words and hash that
auto operator()(const boost::decimal::decimal128_t& v) const noexcept -> std::size_t
{
const auto normalized_v {boost::decimal::normalize(v)};

boost::int128::uint128_t bits;
std::memcpy(&bits, &v, sizeof(boost::int128::uint128_t));
std::memcpy(&bits, &normalized_v, sizeof(boost::int128::uint128_t));

return std::hash<std::uint64_t>{}(bits.high ^ bits.low);
}
Expand Down
33 changes: 33 additions & 0 deletions test/test_hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <boost/decimal/iostream.hpp>
#include <boost/core/lightweight_test.hpp>
#include <functional>
#include <array>

template <typename T>
void test_hash()
Expand All @@ -26,6 +27,31 @@ void test_hash()
}
}

// See: https://github.com/cppalliance/decimal/issues/1120
template <typename T>
void test_hash_cohorts()
{
const std::array<T, 7> values {
T {3, 7},
T {30, 6},
T {300, 5},
T {3000, 4},
T {30000, 3},
T {300000, 2},
T {3000000, 1}
};

std::hash<T> hasher;

for (const auto val1 : values)
{
for (const auto val2 : values)
{
BOOST_TEST_EQ(hasher(val1), hasher(val2));
}
}
}

int main()
{
test_hash<boost::decimal::decimal32_t>();
Expand All @@ -35,5 +61,12 @@ int main()
test_hash<boost::decimal::decimal_fast64_t>();
test_hash<boost::decimal::decimal_fast128_t>();

test_hash_cohorts<boost::decimal::decimal32_t>();
test_hash_cohorts<boost::decimal::decimal64_t>();
test_hash_cohorts<boost::decimal::decimal128_t>();
test_hash_cohorts<boost::decimal::decimal_fast32_t>();
test_hash_cohorts<boost::decimal::decimal_fast64_t>();
test_hash_cohorts<boost::decimal::decimal_fast128_t>();

return boost::report_errors();
}