/Users/brunogarcia/projects/bitcoin-core-dev/src/util/overflow.h
Line | Count | Source |
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 <concepts> |
10 | | #include <limits> |
11 | | #include <optional> |
12 | | #include <type_traits> |
13 | | |
14 | | template <class T> |
15 | | [[nodiscard]] bool AdditionOverflow(const T i, const T j) noexcept |
16 | 244k | { |
17 | 244k | static_assert(std::is_integral_v<T>, "Integral required."); |
18 | 244k | if constexpr (std::numeric_limits<T>::is_signed) { |
19 | 0 | return (i > 0 && j > std::numeric_limits<T>::max() - i) || |
20 | 0 | (i < 0 && j < std::numeric_limits<T>::min() - i); |
21 | 0 | } |
22 | 0 | return std::numeric_limits<T>::max() - i < j; |
23 | 244k | } Unexecuted instantiation: bool AdditionOverflow<long long>(long long, long long) Unexecuted instantiation: bool AdditionOverflow<unsigned long long>(unsigned long long, unsigned long long) Unexecuted instantiation: bool AdditionOverflow<int>(int, int) Unexecuted instantiation: bool AdditionOverflow<unsigned int>(unsigned int, unsigned int) Unexecuted instantiation: bool AdditionOverflow<short>(short, short) Unexecuted instantiation: bool AdditionOverflow<unsigned short>(unsigned short, unsigned short) Unexecuted instantiation: bool AdditionOverflow<char>(char, char) Unexecuted instantiation: bool AdditionOverflow<unsigned char>(unsigned char, unsigned char) Unexecuted instantiation: bool AdditionOverflow<signed char>(signed char, signed char) bool AdditionOverflow<unsigned long>(unsigned long, unsigned long) Line | Count | Source | 16 | 244k | { | 17 | 244k | static_assert(std::is_integral_v<T>, "Integral required."); | 18 | | if constexpr (std::numeric_limits<T>::is_signed) { | 19 | | return (i > 0 && j > std::numeric_limits<T>::max() - i) || | 20 | | (i < 0 && j < std::numeric_limits<T>::min() - i); | 21 | | } | 22 | 244k | return std::numeric_limits<T>::max() - i < j; | 23 | 244k | } |
|
24 | | |
25 | | template <class T> |
26 | | [[nodiscard]] std::optional<T> CheckedAdd(const T i, const T j) noexcept |
27 | 244k | { |
28 | 244k | if (AdditionOverflow(i, j)) { |
29 | 0 | return std::nullopt; |
30 | 0 | } |
31 | 244k | return i + j; |
32 | 244k | } Unexecuted instantiation: std::__1::optional<long long> CheckedAdd<long long>(long long, long long) Unexecuted instantiation: std::__1::optional<unsigned long long> CheckedAdd<unsigned long long>(unsigned long long, unsigned long long) Unexecuted instantiation: std::__1::optional<int> CheckedAdd<int>(int, int) Unexecuted instantiation: std::__1::optional<unsigned int> CheckedAdd<unsigned int>(unsigned int, unsigned int) Unexecuted instantiation: std::__1::optional<short> CheckedAdd<short>(short, short) Unexecuted instantiation: std::__1::optional<unsigned short> CheckedAdd<unsigned short>(unsigned short, unsigned short) Unexecuted instantiation: std::__1::optional<char> CheckedAdd<char>(char, char) Unexecuted instantiation: std::__1::optional<unsigned char> CheckedAdd<unsigned char>(unsigned char, unsigned char) Unexecuted instantiation: std::__1::optional<signed char> CheckedAdd<signed char>(signed char, signed char) std::__1::optional<unsigned long> CheckedAdd<unsigned long>(unsigned long, unsigned long) Line | Count | Source | 27 | 244k | { | 28 | 244k | if (AdditionOverflow(i, j)) { | 29 | 0 | return std::nullopt; | 30 | 0 | } | 31 | 244k | return i + j; | 32 | 244k | } |
|
33 | | |
34 | | template <class T> |
35 | | [[nodiscard]] T SaturatingAdd(const T i, const T j) noexcept |
36 | 4.56k | { |
37 | 4.56k | if constexpr (std::numeric_limits<T>::is_signed) { |
38 | 4.56k | if (i > 0 && j > std::numeric_limits<T>::max() - i2.84k ) { |
39 | 0 | return std::numeric_limits<T>::max(); |
40 | 0 | } |
41 | 4.56k | if (i < 0 && j < std::numeric_limits<T>::min() - i0 ) { |
42 | 0 | return std::numeric_limits<T>::min(); |
43 | 0 | } |
44 | 4.56k | } else { |
45 | 0 | if (std::numeric_limits<T>::max() - i < j) { |
46 | 0 | return std::numeric_limits<T>::max(); |
47 | 0 | } |
48 | 0 | } |
49 | 4.56k | return i + j; |
50 | 4.56k | } long long SaturatingAdd<long long>(long long, long long) Line | Count | Source | 36 | 4.56k | { | 37 | 4.56k | if constexpr (std::numeric_limits<T>::is_signed) { | 38 | 4.56k | if (i > 0 && j > std::numeric_limits<T>::max() - i2.84k ) { | 39 | 0 | return std::numeric_limits<T>::max(); | 40 | 0 | } | 41 | 4.56k | if (i < 0 && j < std::numeric_limits<T>::min() - i0 ) { | 42 | 0 | return std::numeric_limits<T>::min(); | 43 | 0 | } | 44 | | } else { | 45 | | if (std::numeric_limits<T>::max() - i < j) { | 46 | | return std::numeric_limits<T>::max(); | 47 | | } | 48 | | } | 49 | 4.56k | return i + j; | 50 | 4.56k | } |
Unexecuted instantiation: unsigned long long SaturatingAdd<unsigned long long>(unsigned long long, unsigned long long) Unexecuted instantiation: int SaturatingAdd<int>(int, int) Unexecuted instantiation: unsigned int SaturatingAdd<unsigned int>(unsigned int, unsigned int) Unexecuted instantiation: short SaturatingAdd<short>(short, short) Unexecuted instantiation: unsigned short SaturatingAdd<unsigned short>(unsigned short, unsigned short) Unexecuted instantiation: char SaturatingAdd<char>(char, char) Unexecuted instantiation: unsigned char SaturatingAdd<unsigned char>(unsigned char, unsigned char) Unexecuted instantiation: signed char SaturatingAdd<signed char>(signed char, signed char) |
51 | | |
52 | | /** |
53 | | * @brief Left bit shift with overflow checking. |
54 | | * @param input The input value to be left shifted. |
55 | | * @param shift The number of bits to left shift. |
56 | | * @return (input * 2^shift) or nullopt if it would not fit in the return type. |
57 | | */ |
58 | | template <std::integral T> |
59 | | constexpr std::optional<T> CheckedLeftShift(T input, unsigned shift) noexcept |
60 | 0 | { |
61 | 0 | if (shift == 0 || input == 0) return input; |
62 | | // Avoid undefined c++ behaviour if shift is >= number of bits in T. |
63 | 0 | if (shift >= sizeof(T) * CHAR_BIT) return std::nullopt; |
64 | | // If input << shift is too big to fit in T, return nullopt. |
65 | 0 | if (input > (std::numeric_limits<T>::max() >> shift)) return std::nullopt; |
66 | 0 | if (input < (std::numeric_limits<T>::min() >> shift)) return std::nullopt; |
67 | 0 | return input << shift; |
68 | 0 | } Unexecuted instantiation: std::__1::optional<unsigned long long> CheckedLeftShift<unsigned long long>(unsigned long long, unsigned int) Unexecuted instantiation: std::__1::optional<signed char> CheckedLeftShift<signed char>(signed char, unsigned int) Unexecuted instantiation: std::__1::optional<short> CheckedLeftShift<short>(short, unsigned int) Unexecuted instantiation: std::__1::optional<int> CheckedLeftShift<int>(int, unsigned int) Unexecuted instantiation: std::__1::optional<unsigned char> CheckedLeftShift<unsigned char>(unsigned char, unsigned int) Unexecuted instantiation: std::__1::optional<unsigned short> CheckedLeftShift<unsigned short>(unsigned short, unsigned int) Unexecuted instantiation: std::__1::optional<unsigned int> CheckedLeftShift<unsigned int>(unsigned int, unsigned int) |
69 | | |
70 | | /** |
71 | | * @brief Left bit shift with safe minimum and maximum values. |
72 | | * @param input The input value to be left shifted. |
73 | | * @param shift The number of bits to left shift. |
74 | | * @return (input * 2^shift) clamped to fit between the lowest and highest |
75 | | * representable values of the type T. |
76 | | */ |
77 | | template <std::integral T> |
78 | | constexpr T SaturatingLeftShift(T input, unsigned shift) noexcept |
79 | 0 | { |
80 | 0 | if (auto result{CheckedLeftShift(input, shift)}) return *result; |
81 | | // If input << shift is too big to fit in T, return biggest positive or negative |
82 | | // number that fits. |
83 | 0 | return input < 0 ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max(); |
84 | 0 | } Unexecuted instantiation: signed char SaturatingLeftShift<signed char>(signed char, unsigned int) Unexecuted instantiation: short SaturatingLeftShift<short>(short, unsigned int) Unexecuted instantiation: int SaturatingLeftShift<int>(int, unsigned int) Unexecuted instantiation: unsigned char SaturatingLeftShift<unsigned char>(unsigned char, unsigned int) Unexecuted instantiation: unsigned short SaturatingLeftShift<unsigned short>(unsigned short, unsigned int) Unexecuted instantiation: unsigned int SaturatingLeftShift<unsigned int>(unsigned int, unsigned int) Unexecuted instantiation: unsigned long long SaturatingLeftShift<unsigned long long>(unsigned long long, unsigned int) |
85 | | |
86 | | #endif // BITCOIN_UTIL_OVERFLOW_H |