diff --git a/luna/include/luna/SafeArithmetic.h b/luna/include/luna/SafeArithmetic.h index b0336d6d..31d59856 100644 --- a/luna/include/luna/SafeArithmetic.h +++ b/luna/include/luna/SafeArithmetic.h @@ -18,21 +18,27 @@ template constexpr bool mul_will_overflow(T a, T b) template Result safe_add(T a, T b) { - if (add_will_overflow(a, b)) return err(EOVERFLOW); + T result; - return a + b; + if (__builtin_add_overflow(a, b, &result)) return err(EOVERFLOW); + + return result; } template Result safe_sub(T a, T b) { - if (sub_will_overflow(a, b)) return err(EOVERFLOW); + T result; - return a - b; + if (__builtin_sub_overflow(a, b, &result)) return err(EOVERFLOW); + + return result; } template Result safe_mul(T a, T b) { - if (mul_will_overflow(a, b)) return err(EOVERFLOW); + T result; - return a * b; + if (__builtin_mul_overflow(a, b, &result)) return err(EOVERFLOW); + + return result; } \ No newline at end of file