mirror of
https://github.com/SerenityOS/serenity
synced 2024-07-22 18:46:18 +00:00
AK: Use __has_builtin() in Checked.h
Instead of avoiding overflow-checking builtins with AK_COMPILER_CLANG, we can use the preprocessor's __has_builtin() mechanism to check if they are available.
This commit is contained in:
parent
c4bc889346
commit
f11e6beca8
12
AK/Checked.h
12
AK/Checked.h
|
@ -328,26 +328,26 @@ public:
|
||||||
template<typename U, typename V>
|
template<typename U, typename V>
|
||||||
[[nodiscard]] static constexpr bool addition_would_overflow(U u, V v)
|
[[nodiscard]] static constexpr bool addition_would_overflow(U u, V v)
|
||||||
{
|
{
|
||||||
#if defined(AK_COMPILER_CLANG)
|
#if __has_builtin(__builtin_add_overflow_p)
|
||||||
|
return __builtin_add_overflow_p(u, v, (T)0);
|
||||||
|
#else
|
||||||
Checked checked;
|
Checked checked;
|
||||||
checked = u;
|
checked = u;
|
||||||
checked += v;
|
checked += v;
|
||||||
return checked.has_overflow();
|
return checked.has_overflow();
|
||||||
#else
|
|
||||||
return __builtin_add_overflow_p(u, v, (T)0);
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename U, typename V>
|
template<typename U, typename V>
|
||||||
[[nodiscard]] static constexpr bool multiplication_would_overflow(U u, V v)
|
[[nodiscard]] static constexpr bool multiplication_would_overflow(U u, V v)
|
||||||
{
|
{
|
||||||
#if defined(AK_COMPILER_CLANG)
|
#if __has_builtin(__builtin_mul_overflow_p)
|
||||||
|
return __builtin_mul_overflow_p(u, v, (T)0);
|
||||||
|
#else
|
||||||
Checked checked;
|
Checked checked;
|
||||||
checked = u;
|
checked = u;
|
||||||
checked *= v;
|
checked *= v;
|
||||||
return checked.has_overflow();
|
return checked.has_overflow();
|
||||||
#else
|
|
||||||
return __builtin_mul_overflow_p(u, v, (T)0);
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue