23#define PW_ADD_OVERFLOW(a, b, out) __builtin_add_overflow(a, b, out)
33#define PW_SUB_OVERFLOW(a, b, out) __builtin_sub_overflow(a, b, out)
41#define PW_MUL_OVERFLOW(a, b, out) __builtin_mul_overflow(a, b, out)
69template <
typename A,
typename B,
typename T>
70[[nodiscard]]
constexpr bool CheckedAdd(A a, B b, T& result) {
73 if (PW_ADD_OVERFLOW(a, b, &temp)) {
95template <
typename T,
typename A,
typename B>
96[[nodiscard]]
constexpr std::optional<T>
CheckedAdd(A a, B b) {
117template <
typename T,
typename Inc>
137template <
typename A,
typename B,
typename T>
138[[nodiscard]]
constexpr bool CheckedSub(A a, B b, T& result) {
141 if (PW_SUB_OVERFLOW(a, b, &temp)) {
163template <
typename T,
typename A,
typename B>
164[[nodiscard]]
constexpr std::optional<T>
CheckedSub(A a, B b) {
185template <
typename T,
typename Dec>
205template <
typename A,
typename B,
typename T>
206[[nodiscard]]
constexpr bool CheckedMul(A a, B b, T& result) {
209 if (PW_MUL_OVERFLOW(a, b, &temp)) {
231template <
typename T,
typename A,
typename B>
232[[nodiscard]]
constexpr std::optional<T>
CheckedMul(A a, B b) {
constexpr bool CheckedDecrement(T &base, Dec dec)
Definition: checked_arithmetic.h:186
constexpr bool CheckedIncrement(T &base, Inc inc)
Definition: checked_arithmetic.h:118
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
constexpr bool CheckedSub(A a, B b, T &result)
Definition: checked_arithmetic.h:138
The Pigweed namespace.
Definition: alignment.h:27