mirror of
https://github.com/dashpay/dash.git
synced 2024-12-28 05:23:01 +01:00
32 lines
908 B
C
32 lines
908 B
C
|
// Copyright (c) 2021 The Bitcoin Core developers
|
||
|
// Distributed under the MIT software license, see the accompanying
|
||
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||
|
|
||
|
#ifndef BITCOIN_UTIL_OVERFLOW_H
|
||
|
#define BITCOIN_UTIL_OVERFLOW_H
|
||
|
|
||
|
#include <limits>
|
||
|
#include <type_traits>
|
||
|
|
||
|
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) {
|
||
|
return (i > 0 && j > std::numeric_limits<T>::max() - i) ||
|
||
|
(i < 0 && j < std::numeric_limits<T>::min() - i);
|
||
|
}
|
||
|
return std::numeric_limits<T>::max() - i < j;
|
||
|
}
|
||
|
|
||
|
template <class T>
|
||
|
[[nodiscard]] std::optional<T> CheckedAdd(const T i, const T j) noexcept
|
||
|
{
|
||
|
if (AdditionOverflow(i, j)) {
|
||
|
return std::nullopt;
|
||
|
}
|
||
|
return i + j;
|
||
|
}
|
||
|
|
||
|
#endif // BITCOIN_UTIL_OVERFLOW_H
|