| 74 | |
| 75 | template <typename Int> |
| 76 | [[nodiscard]] bool AddWithOverflowGeneric(Int u, Int v, Int* out) { |
| 77 | #if USE_CHECKED_ARITHMETIC_BUILTINS |
| 78 | return __builtin_add_overflow(u, v, out); |
| 79 | #else |
| 80 | if constexpr (sizeof(Int) < 4) { |
| 81 | using UpscaledInt = upscaled_int32_t<Int>; |
| 82 | auto r = static_cast<UpscaledInt>(u) + static_cast<UpscaledInt>(v); |
| 83 | *out = static_cast<Int>(r); |
| 84 | return r != *out; |
| 85 | } else { |
| 86 | return SafeIntAddWithOverflow(u, v, out); |
| 87 | } |
| 88 | #endif |
| 89 | } |
| 90 | |
| 91 | template <typename Int> |
| 92 | [[nodiscard]] bool SubtractWithOverflowGeneric(Int u, Int v, Int* out) { |