Skip to content

Commit 5832979

Browse files
committed
Add std::hash overload
1 parent 558de21 commit 5832979

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

include/boost/decimal/hash.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@ struct hash<boost::decimal::decimal128>
6565
# pragma GCC diagnostic pop
6666
#endif
6767

68+
BOOST_DECIMAL_EXPORT template <>
69+
struct hash<boost::decimal::decimal32_fast>
70+
{
71+
// Need to convert into decimal32 then apply our memcpy
72+
auto operator()(const boost::decimal::decimal32_fast& v) const noexcept -> std::size_t
73+
{
74+
boost::decimal::decimal32 v_32 {v};
75+
std::uint32_t bits;
76+
std::memcpy(&bits, &v_32, sizeof(std::uint32_t));
77+
78+
return std::hash<std::uint32_t>{}(bits);
79+
}
80+
};
81+
6882
}
6983

7084
#endif //BOOST_DECIMAL_HASH_HPP

test/test_decimal32_fast_basis.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,15 +394,13 @@ void spot_check_addition(T a, T b, T res)
394394
}
395395
}
396396

397-
/*
398397
void test_hash()
399398
{
400399
decimal32_fast one {1, 0};
401400
decimal32_fast zero {0, 0};
402401

403402
BOOST_TEST_NE(std::hash<decimal32_fast>{}(one), std::hash<decimal32_fast>{}(zero));
404403
}
405-
*/
406404

407405
void test_shrink_significand()
408406
{
@@ -439,7 +437,7 @@ int main()
439437
test_multiplicatiom();
440438
// test_div_mod();
441439

442-
// test_hash();
440+
test_hash();
443441

444442
spot_check_addition(-1054191000, -920209700, -1974400700);
445443
spot_check_addition(353582500, -32044770, 321537730);

0 commit comments

Comments
 (0)