1
0
mirror of https://github.com/SerenityOS/serenity synced 2024-07-05 20:54:44 +00:00

AK: Use builtins in fabs implementation and move it to the top

Both GCC and Clang inline this function to use bit-wise logic and/or
appropriate instructions even on -O0 and allow their use in a constexpr
context, see
https://godbolt.org/z/de1393vha
This commit is contained in:
Hediadyoin1 2023-05-16 15:07:17 +02:00 committed by Andreas Kling
parent 594369121a
commit 29e0494e56

View File

@ -81,6 +81,18 @@ constexpr size_t product_odd() { return value * product_odd<value - 2>(); }
return res; \
}
template<FloatingPoint T>
constexpr T fabs(T x)
{
// Both GCC and Clang inline fabs by default, so this is just a cmath like wrapper
if constexpr (IsSame<T, long double>)
return __builtin_fabsl(x);
if constexpr (IsSame<T, double>)
return __builtin_fabs(x);
if constexpr (IsSame<T, float>)
return __builtin_fabsf(x);
}
namespace Rounding {
template<FloatingPoint T>
constexpr T ceil(T num)
@ -494,23 +506,6 @@ constexpr T cbrt(T x)
return r;
}
template<FloatingPoint T>
constexpr T fabs(T x)
{
if (is_constant_evaluated())
return x < 0 ? -x : x;
#if ARCH(X86_64)
asm(
"fabs"
: "+t"(x));
return x;
#elif ARCH(AARCH64)
AARCH64_INSTRUCTION(fabs, x);
#else
return __builtin_fabs(x);
#endif
}
namespace Trigonometry {
template<FloatingPoint T>