AK: Make Checked.h work with Clang

Apparently Clang does not have __builtin_foo_overflow_p()

Fixes #2044.
This commit is contained in:
Andreas Kling 2020-05-01 17:30:47 +02:00
parent b5039a047f
commit ea839861e5
Notes: sideshowbarker 2024-07-19 07:07:48 +09:00

View file

@ -237,13 +237,27 @@ public:
template<typename U, typename V> template<typename U, typename V>
static bool addition_would_overflow(U u, V v) static bool addition_would_overflow(U u, V v)
{ {
#ifdef __clang__
Checked checked;
checked = u;
checked += v;
return checked.has_overflow();
#else
return __builtin_add_overflow_p(u, v, (T)0); return __builtin_add_overflow_p(u, v, (T)0);
#endif
} }
template<typename U, typename V, typename X> template<typename U, typename V, typename X>
static bool multiplication_would_overflow(U u, V v) static bool multiplication_would_overflow(U u, V v)
{ {
#ifdef __clang__
Checked checked;
checked = u;
checked *= v;
return checked.has_overflow();
#else
return __builtin_mul_overflow_p(u, v, (T)0); return __builtin_mul_overflow_p(u, v, (T)0);
#endif
} }
template<typename U, typename V, typename X> template<typename U, typename V, typename X>