Bitcoin ABC 0.33.10
P2P Digital Currency
overflow.h
Go to the documentation of this file.
1// Copyright (c) 2021-2022 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_UTIL_OVERFLOW_H
6#define BITCOIN_UTIL_OVERFLOW_H
7
8#include <climits>
9#include <limits>
10#include <optional>
11#include <type_traits>
12
13template <std::integral T>
14[[nodiscard]] bool AdditionOverflow(const T i, const T j) noexcept {
15 if constexpr (std::numeric_limits<T>::is_signed) {
16 return (i > 0 && j > std::numeric_limits<T>::max() - i) ||
17 (i < 0 && j < std::numeric_limits<T>::min() - i);
18 }
19 return std::numeric_limits<T>::max() - i < j;
20}
21
22template <class T>
23[[nodiscard]] std::optional<T> CheckedAdd(const T i, const T j) noexcept {
24 if (AdditionOverflow(i, j)) {
25 return std::nullopt;
26 }
27 return i + j;
28}
29
30template <std::integral T>
31[[nodiscard]] T SaturatingAdd(const T i, const T j) noexcept {
32 if constexpr (std::numeric_limits<T>::is_signed) {
33 if (i > 0 && j > std::numeric_limits<T>::max() - i) {
34 return std::numeric_limits<T>::max();
35 }
36 if (i < 0 && j < std::numeric_limits<T>::min() - i) {
37 return std::numeric_limits<T>::min();
38 }
39 } else {
40 if (std::numeric_limits<T>::max() - i < j) {
41 return std::numeric_limits<T>::max();
42 }
43 }
44 return i + j;
45}
46
47template <std::unsigned_integral T, std::unsigned_integral U>
48[[nodiscard]] constexpr bool TrySub(T &i, const U j) noexcept {
49 if (i < T{j}) {
50 return false;
51 }
52 i -= T{j};
53 return true;
54}
55
62template <std::integral T>
63constexpr std::optional<T> CheckedLeftShift(T input, unsigned shift) noexcept {
64 if (shift == 0 || input == 0) {
65 return input;
66 }
67 // Avoid undefined c++ behaviour if shift is >= number of bits in T.
68 if (shift >= sizeof(T) * CHAR_BIT) {
69 return std::nullopt;
70 }
71 // If input << shift is too big to fit in T, return nullopt.
72 if (input > (std::numeric_limits<T>::max() >> shift)) {
73 return std::nullopt;
74 }
75 if (input < (std::numeric_limits<T>::min() >> shift)) {
76 return std::nullopt;
77 }
78 return input << shift;
79}
80
88template <std::integral T>
89constexpr T SaturatingLeftShift(T input, unsigned shift) noexcept {
90 if (auto result{CheckedLeftShift(input, shift)}) {
91 return *result;
92 }
93 // If input << shift is too big to fit in T, return biggest positive or
94 // negative number that fits.
95 return input < 0 ? std::numeric_limits<T>::min()
96 : std::numeric_limits<T>::max();
97}
98
99#endif // BITCOIN_UTIL_OVERFLOW_H
constexpr bool TrySub(T &i, const U j) noexcept
Definition: overflow.h:48
std::optional< T > CheckedAdd(const T i, const T j) noexcept
Definition: overflow.h:23
constexpr T SaturatingLeftShift(T input, unsigned shift) noexcept
Left bit shift with safe minimum and maximum values.
Definition: overflow.h:89
bool AdditionOverflow(const T i, const T j) noexcept
Definition: overflow.h:14
T SaturatingAdd(const T i, const T j) noexcept
Definition: overflow.h:31
constexpr std::optional< T > CheckedLeftShift(T input, unsigned shift) noexcept
Left bit shift with overflow checking.
Definition: overflow.h:63