16#if __has_include(<version>)
20#ifdef __cpp_lib_saturation_arithmetic
38#include "pw_numeric/checked_arithmetic.h"
53constexpr T
add_sat(T lhs, T rhs)
noexcept {
54 static_assert(std::is_integral_v<T>);
58 if constexpr (std::is_unsigned_v<T>) {
59 return std::numeric_limits<T>::max();
62 return std::numeric_limits<T>::min();
64 return std::numeric_limits<T>::max();
75constexpr T
mul_sat(T lhs, T rhs)
noexcept {
76 static_assert(std::is_integral_v<T>);
77 if (T product = 0;
CheckedMul(lhs, rhs, product)) {
80 if constexpr (std::is_unsigned_v<T>) {
81 return std::numeric_limits<T>::max();
83 if ((lhs < 0) == (rhs < 0)) {
84 return std::numeric_limits<T>::max();
86 return std::numeric_limits<T>::min();
constexpr T mul_sat(T lhs, T rhs) noexcept
Definition: saturating_arithmetic.h:75
constexpr T add_sat(T lhs, T rhs) noexcept
Definition: saturating_arithmetic.h:53
constexpr bool CheckedAdd(A a, B b, T &result)
Definition: checked_arithmetic.h:70
constexpr bool CheckedMul(A a, B b, T &result)
Definition: checked_arithmetic.h:206
The Pigweed namespace.
Definition: alignment.h:27