mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +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:
parent
594369121a
commit
29e0494e56
Notes:
sideshowbarker
2024-07-17 09:37:30 +09:00
Author: https://github.com/Hendiadyoin1 Commit: https://github.com/SerenityOS/serenity/commit/29e0494e56 Pull-request: https://github.com/SerenityOS/serenity/pull/18998 Reviewed-by: https://github.com/kleinesfilmroellchen ✅ Reviewed-by: https://github.com/linusg ✅
1 changed files with 12 additions and 17 deletions
29
AK/Math.h
29
AK/Math.h
|
@ -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>
|
||||
|
|
Loading…
Reference in a new issue