mirror of
https://github.com/bitcoin/bitcoin.git
synced 2024-11-20 10:38:42 +01:00
util: Add SaturatingAdd helper
This commit is contained in:
parent
1337b93f50
commit
faa7d8a3f7
@ -26,6 +26,12 @@ void TestAdditionOverflow(FuzzedDataProvider& fuzzed_data_provider)
|
||||
const T i = fuzzed_data_provider.ConsumeIntegral<T>();
|
||||
const T j = fuzzed_data_provider.ConsumeIntegral<T>();
|
||||
const bool is_addition_overflow_custom = AdditionOverflow(i, j);
|
||||
const auto maybe_add{CheckedAdd(i, j)};
|
||||
const auto sat_add{SaturatingAdd(i, j)};
|
||||
assert(is_addition_overflow_custom == !maybe_add.has_value());
|
||||
assert(is_addition_overflow_custom == AdditionOverflow(j, i));
|
||||
assert(maybe_add == CheckedAdd(j, i));
|
||||
assert(sat_add == SaturatingAdd(j, i));
|
||||
#if defined(HAVE_BUILTIN_ADD_OVERFLOW)
|
||||
T result_builtin;
|
||||
const bool is_addition_overflow_builtin = __builtin_add_overflow(i, j, &result_builtin);
|
||||
@ -33,11 +39,14 @@ void TestAdditionOverflow(FuzzedDataProvider& fuzzed_data_provider)
|
||||
if (!is_addition_overflow_custom) {
|
||||
assert(i + j == result_builtin);
|
||||
}
|
||||
#else
|
||||
if (!is_addition_overflow_custom) {
|
||||
(void)(i + j);
|
||||
}
|
||||
#endif
|
||||
if (is_addition_overflow_custom) {
|
||||
assert(sat_add == std::numeric_limits<T>::min() || sat_add == std::numeric_limits<T>::max());
|
||||
} else {
|
||||
const auto add{i + j};
|
||||
assert(add == maybe_add.value());
|
||||
assert(add == sat_add);
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
|
@ -1474,9 +1474,17 @@ static void TestAddMatrixOverflow()
|
||||
constexpr T MAXI{std::numeric_limits<T>::max()};
|
||||
BOOST_CHECK(!CheckedAdd(T{1}, MAXI));
|
||||
BOOST_CHECK(!CheckedAdd(MAXI, MAXI));
|
||||
BOOST_CHECK_EQUAL(MAXI, SaturatingAdd(T{1}, MAXI));
|
||||
BOOST_CHECK_EQUAL(MAXI, SaturatingAdd(MAXI, MAXI));
|
||||
|
||||
BOOST_CHECK_EQUAL(0, CheckedAdd(T{0}, T{0}).value());
|
||||
BOOST_CHECK_EQUAL(MAXI, CheckedAdd(T{0}, MAXI).value());
|
||||
BOOST_CHECK_EQUAL(MAXI, CheckedAdd(T{1}, MAXI - 1).value());
|
||||
BOOST_CHECK_EQUAL(MAXI - 1, CheckedAdd(T{1}, MAXI - 2).value());
|
||||
BOOST_CHECK_EQUAL(0, SaturatingAdd(T{0}, T{0}));
|
||||
BOOST_CHECK_EQUAL(MAXI, SaturatingAdd(T{0}, MAXI));
|
||||
BOOST_CHECK_EQUAL(MAXI, SaturatingAdd(T{1}, MAXI - 1));
|
||||
BOOST_CHECK_EQUAL(MAXI - 1, SaturatingAdd(T{1}, MAXI - 2));
|
||||
}
|
||||
|
||||
/* Check for overflow or underflow */
|
||||
@ -1488,9 +1496,17 @@ static void TestAddMatrix()
|
||||
constexpr T MAXI{std::numeric_limits<T>::max()};
|
||||
BOOST_CHECK(!CheckedAdd(T{-1}, MINI));
|
||||
BOOST_CHECK(!CheckedAdd(MINI, MINI));
|
||||
BOOST_CHECK_EQUAL(MINI, SaturatingAdd(T{-1}, MINI));
|
||||
BOOST_CHECK_EQUAL(MINI, SaturatingAdd(MINI, MINI));
|
||||
|
||||
BOOST_CHECK_EQUAL(MINI, CheckedAdd(T{0}, MINI).value());
|
||||
BOOST_CHECK_EQUAL(MINI, CheckedAdd(T{-1}, MINI + 1).value());
|
||||
BOOST_CHECK_EQUAL(-1, CheckedAdd(MINI, MAXI).value());
|
||||
BOOST_CHECK_EQUAL(MINI + 1, CheckedAdd(T{-1}, MINI + 2).value());
|
||||
BOOST_CHECK_EQUAL(MINI, SaturatingAdd(T{0}, MINI));
|
||||
BOOST_CHECK_EQUAL(MINI, SaturatingAdd(T{-1}, MINI + 1));
|
||||
BOOST_CHECK_EQUAL(MINI + 1, SaturatingAdd(T{-1}, MINI + 2));
|
||||
BOOST_CHECK_EQUAL(-1, SaturatingAdd(MINI, MAXI));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(util_overflow)
|
||||
|
@ -13,7 +13,7 @@ template <class T>
|
||||
[[nodiscard]] bool AdditionOverflow(const T i, const T j) noexcept
|
||||
{
|
||||
static_assert(std::is_integral<T>::value, "Integral required.");
|
||||
if (std::numeric_limits<T>::is_signed) {
|
||||
if constexpr (std::numeric_limits<T>::is_signed) {
|
||||
return (i > 0 && j > std::numeric_limits<T>::max() - i) ||
|
||||
(i < 0 && j < std::numeric_limits<T>::min() - i);
|
||||
}
|
||||
@ -29,4 +29,22 @@ template <class T>
|
||||
return i + j;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
[[nodiscard]] T SaturatingAdd(const T i, const T j) noexcept
|
||||
{
|
||||
if constexpr (std::numeric_limits<T>::is_signed) {
|
||||
if (i > 0 && j > std::numeric_limits<T>::max() - i) {
|
||||
return std::numeric_limits<T>::max();
|
||||
}
|
||||
if (i < 0 && j < std::numeric_limits<T>::min() - i) {
|
||||
return std::numeric_limits<T>::min();
|
||||
}
|
||||
} else {
|
||||
if (std::numeric_limits<T>::max() - i < j) {
|
||||
return std::numeric_limits<T>::max();
|
||||
}
|
||||
}
|
||||
return i + j;
|
||||
}
|
||||
|
||||
#endif // BITCOIN_UTIL_OVERFLOW_H
|
||||
|
Loading…
Reference in New Issue
Block a user