Bitcoin Core 32.1.0
P2P Digital Currency
Loading...
Searching...
No Matches
overflow.h File Reference
#include <util/check.h>
#include <climits>
#include <concepts>
#include <limits>
#include <optional>
#include <type_traits>
Include dependency graph for overflow.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

template<std::integral T>
bool AdditionOverflow (const T i, const T j) noexcept
template<class T>
std::optional< T > CheckedAdd (const T i, const T j) noexcept
template<std::unsigned_integral T, std::unsigned_integral U>
constexpr bool TrySub (T &i, const U j) noexcept
template<std::integral T>
T SaturatingAdd (const T i, const T j) noexcept
template<std::unsigned_integral Dividend, std::unsigned_integral Divisor>
constexpr auto CeilDiv (const Dividend dividend, const Divisor divisor)
 Integer ceiling division (for unsigned values).
template<std::integral T>
constexpr std::optional< T > CheckedLeftShift (T input, unsigned shift) noexcept
 Left bit shift with overflow checking.
template<std::integral T>
constexpr T SaturatingLeftShift (T input, unsigned shift) noexcept
 Left bit shift with safe minimum and maximum values.

Function Documentation

◆ AdditionOverflow()

template<std::integral T>
bool AdditionOverflow ( const T i,
const T j )
nodiscardnoexcept

Definition at line 17 of file overflow.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CeilDiv()

template<std::unsigned_integral Dividend, std::unsigned_integral Divisor>
auto CeilDiv ( const Dividend dividend,
const Divisor divisor )
nodiscardconstexpr

Integer ceiling division (for unsigned values).

Computes the smallest integer q such that q * divisor >= dividend. Both dividend and divisor must be unsigned, and divisor must be non-zero.

The implementation avoids overflow that can occur with (dividend + divisor - 1) / divisor.

Definition at line 70 of file overflow.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CheckedAdd()

template<class T>
std::optional< T > CheckedAdd ( const T i,
const T j )
nodiscardnoexcept

Definition at line 27 of file overflow.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CheckedLeftShift()

template<std::integral T>
std::optional< T > CheckedLeftShift ( T input,
unsigned shift )
constexprnoexcept

Left bit shift with overflow checking.

Parameters
inputThe input value to be left shifted.
shiftThe number of bits to left shift.
Returns
(input * 2^shift) or nullopt if it would not fit in the return type.

Definition at line 83 of file overflow.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SaturatingAdd()

template<std::integral T>
T SaturatingAdd ( const T i,
const T j )
nodiscardnoexcept

Definition at line 44 of file overflow.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SaturatingLeftShift()

template<std::integral T>
T SaturatingLeftShift ( T input,
unsigned shift )
constexprnoexcept

Left bit shift with safe minimum and maximum values.

Parameters
inputThe input value to be left shifted.
shiftThe number of bits to left shift.
Returns
(input * 2^shift) clamped to fit between the lowest and highest representable values of the type T.

Definition at line 102 of file overflow.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ TrySub()

template<std::unsigned_integral T, std::unsigned_integral U>
bool TrySub ( T & i,
const U j )
nodiscardconstexprnoexcept

Definition at line 36 of file overflow.h.

Here is the call graph for this function:
Here is the caller graph for this function: