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
Notes: sideshowbarker 2024-07-17 09:37:30 +09:00

View file

@ -81,6 +81,18 @@ constexpr size_t product_odd() { return value * product_odd<value - 2>(); }
return res; \ 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 { namespace Rounding {
template<FloatingPoint T> template<FloatingPoint T>
constexpr T ceil(T num) constexpr T ceil(T num)
@ -494,23 +506,6 @@ constexpr T cbrt(T x)
return r; 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 { namespace Trigonometry {
template<FloatingPoint T> template<FloatingPoint T>