| 31 | |
| 32 | template <class T> |
| 33 | [[nodiscard]] T SaturatingAdd(const T i, const T j) noexcept |
| 34 | { |
| 35 | if constexpr (std::numeric_limits<T>::is_signed) { |
| 36 | if (i > 0 && j > std::numeric_limits<T>::max() - i) { |
| 37 | return std::numeric_limits<T>::max(); |
| 38 | } |
| 39 | if (i < 0 && j < std::numeric_limits<T>::min() - i) { |
| 40 | return std::numeric_limits<T>::min(); |
| 41 | } |
| 42 | } else { |
| 43 | if (std::numeric_limits<T>::max() - i < j) { |
| 44 | return std::numeric_limits<T>::max(); |
| 45 | } |
| 46 | } |
| 47 | return i + j; |
| 48 | } |
| 49 | |
| 50 | #endif // BITCOIN_UTIL_OVERFLOW_H |
no outgoing calls