Efficient mathematical utilities for embedded.
Main docs: Home
Functions | |
| template<typename A , typename B , typename T > | |
| constexpr bool | pw::CheckedAdd (A a, B b, T &result) |
| template<typename T , typename A , typename B > | |
| constexpr std::optional< T > | pw::CheckedAdd (A a, B b) |
| template<typename T , typename Inc > | |
| constexpr bool | pw::CheckedIncrement (T &base, Inc inc) |
| template<typename A , typename B , typename T > | |
| constexpr bool | pw::CheckedSub (A a, B b, T &result) |
| template<typename T , typename A , typename B > | |
| constexpr std::optional< T > | pw::CheckedSub (A a, B b) |
| template<typename T , typename Dec > | |
| constexpr bool | pw::CheckedDecrement (T &base, Dec dec) |
| template<typename A , typename B , typename T > | |
| constexpr bool | pw::CheckedMul (A a, B b, T &result) |
| template<typename T , typename A , typename B > | |
| constexpr std::optional< T > | pw::CheckedMul (A a, B b) |
| template<typename T > | |
| constexpr T | pw::IntegerDivisionRoundNearest (T dividend, T divisor) |
| template<typename T > | |
| constexpr T | pw::add_sat (T lhs, T rhs) noexcept |
| template<typename T > | |
| constexpr T | pw::mul_sat (T lhs, T rhs) noexcept |
|
constexprnoexcept |
Polyfill of C++26's std::add_sat. Returns the sum of two integers, giving the integer's maximum or minimum value if the sum would otherwise have overflowed.
For example, add_sat<uint8_t>(250, 6) returns 255 instead of the overflowed value (0).
|
constexpr |
Adds two numbers, checking for overflow.
| T | The type of the result, which is checked for overflow. |
| A | The type of the first addend, a. |
| B | The type of the second addend, b. |
| [in] | a | The first addend. |
| [in] | b | The second addend. |
a + b) if addition was successful, or nullopt if the addition would overflow.pw::CheckedAdd<uint32_t>(...).
|
constexpr |
Adds two numbers, checking for overflow.
| A | The type of the first addend, a. |
| B | The type of the second addend, b. |
| T | The type of the result, which is checked for overflow. |
| [in] | a | The first addend. |
| [in] | b | The second addend. |
| [out] | result | Reference to t |
a + b) if addition was successful, or false if the addition would overflow and result is unmodified.pw::CheckedAdd<uint32_t>(...).
|
constexpr |
Decrements a variable by some amount.
| T | The type of the variable to be decremented. |
| Dec | The type of the variable to subtract. |
| [in] | base | The variable to be decremented. |
| [in] | dec | The number to subtract from base. |
base was decremented (base -= dec); False if the subtraction would overflow and base is unmodified.
|
constexpr |
Increments a variable by some amount.
| T | The type of the variable to be incremented. |
| Inc | The type of the variable to add. |
| [in] | base | The variable to be incremented. |
| [in] | inc | The number to add to base. |
base was incremented (base += inc); False if the addition would overflow and base is unmodified.
|
constexpr |
Multiplies two numbers, checking for overflow.
| T | The type of the result, which is checked for overflow. |
| A | The type of the first factor, a. |
| B | The type of the second factor, b. |
| [in] | a | The first factor. |
| [in] | b | The second factor. |
a * b) if multiplication was successful, or nullopt if the multiplication would overflow.pw::CheckedMul<uint32_t>(...).
|
constexpr |
Multiplies two numbers, checking for overflow.
| A | The type of the first addend, a. |
| B | The type of the second addend, b. |
| T | The type of the result, which is checked for overflow. |
| [in] | a | The first addend. |
| [in] | b | The second addend. |
| [out] | result | Reference to the result. |
true if the multiplication was successful, false if it would overflow.pw::CheckedMul<uint32_t>(...).
|
constexpr |
Subtracts two numbers, checking for overflow.
| T | The type of the result, which is checked for overflow. |
| A | The type of the minuend, a. |
| B | The type of the subtrahend, b. |
| [in] | a | The minuend (the number from which b is subtracted). |
| [in] | b | The subtrahend (the number subtracted from a). |
a - b) if subtraction was successful, or nullopt if the subtraction would overflow.pw::CheckedSub<uint32_t>(...).
|
constexpr |
Subtracts two numbers, checking for overflow.
| A | The type of the first addend, a. |
| B | The type of the second addend, b. |
| T | The type of the result, which is checked for overflow. |
| [in] | a | The first addend. |
| [in] | b | The second addend. |
| [out] | result | Reference to the result. |
true if the subtraction was successful, false if it would overflow and result is unmodified.pw::CheckedSub<uint32_t>(...).
|
constexpr |
Performs integer division and rounds to the nearest integer. Gives the same result as std::round(static_cast<double>(dividend) / static_cast<double>(divisor)), but requires no floating point operations and is constexpr.
embed:rst:leading-asterisk * .. warning:: * * ``signed`` or ``unsigned`` ``int``, ``long``, or ``long long`` operands * overflow if: * * - the quotient is positive and ``dividend + divisor/2`` overflows, or * - the quotient is negative and ``dividend - divisor/2`` overflows. * * To avoid overflow, do not use this function with very large * operands, or cast ``int`` or ``long`` to a larger type first. Overflow * cannot occur with types smaller than ``int`` because C++ implicitly * converts them to ``int``. *
|
constexprnoexcept |
Polyfill of C++26's std::mul_sat. Returns the product of two integers, giving the integer's maximum or minimum value if the product would otherwise have overflowed.
For example, for 100 * 10 = 1000, mul_sat<uint8_t>(100, 10) returns 255 instead of the overflowed value (232).