Truly independent web browser
AK: Forward all math functions to the compiler builtins
Andreas Kling670d6429e9d3
diff --git a/AK/Math.h b/AK/Math.hindex 108b1785c68..ad15455cfbf 100644--- a/AK/Math.h+++ b/AK/Math.h@@ -29,29 +29,6 @@ constexpr T Sqrt2 = 1.414213562373095048801688724209698079L;template<FloatingPoint T>constexpr T Sqrt1_2 = 0.707106781186547524400844362104849039L;-template<FloatingPoint T>-constexpr T L2_10 = 3.321928094887362347870319429489390175864L;-template<FloatingPoint T>-constexpr T L2_E = 1.442695040888963407359924681001892137L;--namespace Details {--template<size_t>-constexpr size_t product_even();-template<>-constexpr size_t product_even<2>() { return 2; }-template<size_t value>-constexpr size_t product_even() { return value * product_even<value - 2>(); }--template<size_t>-constexpr size_t product_odd();-template<>-constexpr size_t product_odd<1>() { return 1; }-template<size_t value>-constexpr size_t product_odd() { return value * product_odd<value - 2>(); }--}-template<FloatingPoint T>constexpr T to_radians(T degrees){@@ -64,958 +41,121 @@ constexpr T to_degrees(T radians)return radians * 180 / AK::Pi<T>;}-#define CONSTEXPR_STATE(function, args...) \- if consteval { \- if constexpr (IsSame<T, long double>) \- return __builtin_##function##l(args); \- if constexpr (IsSame<T, double>) \- return __builtin_##function(args); \- if constexpr (IsSame<T, float>) \- return __builtin_##function##f(args); \- }--#define AARCH64_INSTRUCTION(instruction, arg) \+// NB: These all forward to the compiler builtins, which lower to the host's libm (or to single instructions where the+// target has them), and are constant-evaluated by the compiler where it supports that.+#define AK_MATH_BUILTIN(function, args...) \if constexpr (IsSame<T, long double>) \- TODO(); \- if constexpr (IsSame<T, double>) { \- double res; \- asm(#instruction " %d0, %d1" \- : "=w"(res) \- : "w"(arg)); \- return res; \- } \- if constexpr (IsSame<T, float>) { \- float res; \- asm(#instruction " %s0, %s1" \- : "=w"(res) \- : "w"(arg)); \- 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 {-+ return __builtin_##function##l(args); \+ else if constexpr (IsSame<T, double>) \+ return __builtin_##function(args); \+ else if constexpr (IsSame<T, float>) \+ return __builtin_##function##f(args); \+ else \+ static_assert(DependentFalse<T>, "There is no builtin for this floating point type");++#define AK_MATH_UNARY_FUNCTION(function) \+ template<FloatingPoint T> \+ constexpr T function(T x) \+ { \+ AK_MATH_BUILTIN(function, x); \+ }++#define AK_MATH_BINARY_FUNCTION(function) \+ template<FloatingPoint T> \+ constexpr T function(T x, T y) \+ { \+ AK_MATH_BUILTIN(function, x, y); \+ }++AK_MATH_UNARY_FUNCTION(fabs)+AK_MATH_UNARY_FUNCTION(rint)+AK_MATH_UNARY_FUNCTION(round)+AK_MATH_UNARY_FUNCTION(sqrt)+AK_MATH_UNARY_FUNCTION(cbrt)+AK_MATH_UNARY_FUNCTION(sin)+AK_MATH_UNARY_FUNCTION(cos)+AK_MATH_UNARY_FUNCTION(tan)+AK_MATH_UNARY_FUNCTION(asin)+AK_MATH_UNARY_FUNCTION(acos)+AK_MATH_UNARY_FUNCTION(atan)+AK_MATH_UNARY_FUNCTION(sinh)+AK_MATH_UNARY_FUNCTION(cosh)+AK_MATH_UNARY_FUNCTION(tanh)+AK_MATH_UNARY_FUNCTION(asinh)+AK_MATH_UNARY_FUNCTION(acosh)+AK_MATH_UNARY_FUNCTION(atanh)+AK_MATH_UNARY_FUNCTION(exp)+AK_MATH_UNARY_FUNCTION(exp2)+AK_MATH_UNARY_FUNCTION(log)+AK_MATH_UNARY_FUNCTION(log2)+AK_MATH_UNARY_FUNCTION(log10)++AK_MATH_BINARY_FUNCTION(fmod)+AK_MATH_BINARY_FUNCTION(remainder)+AK_MATH_BINARY_FUNCTION(hypot)+AK_MATH_BINARY_FUNCTION(atan2)+AK_MATH_BINARY_FUNCTION(pow)++// NB: Not every compiler constant-evaluates these builtins, so they get a fallback for constant evaluation.template<FloatingPoint T>constexpr T ceil(T num){- // FIXME: SSE4.1 rounds[sd] num, res, 0b110if consteval {- if (num < NumericLimits<i64>::min() || num > NumericLimits<i64>::max())+ if (!(num > static_cast<T>(NumericLimits<i64>::min()) && num < static_cast<T>(NumericLimits<i64>::max())))return num;return (static_cast<T>(static_cast<i64>(num)) == num)? static_cast<i64>(num): static_cast<i64>(num) + ((num > 0) ? 1 : 0);}-#if ARCH(AARCH64)- AARCH64_INSTRUCTION(frintp, num);-#else- if constexpr (IsSame<T, long double>)- return __builtin_ceill(num);- if constexpr (IsSame<T, double>)- return __builtin_ceil(num);- if constexpr (IsSame<T, float>)- return __builtin_ceilf(num);-#endif+ AK_MATH_BUILTIN(ceil, num);}template<FloatingPoint T>constexpr T floor(T num){- // FIXME: SSE4.1 rounds[sd] num, res, 0b101if consteval {- if (num < NumericLimits<i64>::min() || num > NumericLimits<i64>::max())+ if (!(num > static_cast<T>(NumericLimits<i64>::min()) && num < static_cast<T>(NumericLimits<i64>::max())))return num;return (static_cast<T>(static_cast<i64>(num)) == num)? static_cast<i64>(num): static_cast<i64>(num) - ((num > 0) ? 0 : 1);}-#if ARCH(AARCH64)- AARCH64_INSTRUCTION(frintm, num);-#else- if constexpr (IsSame<T, long double>)- return __builtin_floorl(num);- if constexpr (IsSame<T, double>)- return __builtin_floor(num);- if constexpr (IsSame<T, float>)- return __builtin_floorf(num);-#endif+ AK_MATH_BUILTIN(floor, num);}template<FloatingPoint T>constexpr T trunc(T num){-#if ARCH(AARCH64)if consteval {- if (num < NumericLimits<i64>::min() || num > NumericLimits<i64>::max())+ if (!(num > static_cast<T>(NumericLimits<i64>::min()) && num < static_cast<T>(NumericLimits<i64>::max())))return num;return static_cast<T>(static_cast<i64>(num));}- AARCH64_INSTRUCTION(frintz, num);-#endif- // FIXME: Use dedicated instruction in the non constexpr case- // SSE4.1: rounds[sd] %num, %res, 0b111- if (num < NumericLimits<i64>::min() || num > NumericLimits<i64>::max())- return num;- return static_cast<T>(static_cast<i64>(num));-}--template<FloatingPoint T>-constexpr T rint(T x)-{- CONSTEXPR_STATE(rint, x);- // Note: This does break tie to even- // But the behavior of frndint/rounds[ds]/frintx can be configured- // through the floating point control registers.- // FIXME: We should decide if we rename this to allow us to get away from- // the configurability "burden" rint has- // this would make us use `rounds[sd] %num, %res, 0b100`- // and `frintn` respectively,- // no such guaranteed round exists for x87 `frndint`-#if ARCH(X86_64)-# ifdef __SSE4_1__- if constexpr (IsSame<T, double>) {- T r;- asm(- "roundsd %1, %0"- : "=x"(r)- : "x"(x));- return r;- }- if constexpr (IsSame<T, float>) {- T r;- asm(- "roundss %1, %0"- : "=x"(r)- : "x"(x));- return r;- }-# else- asm(- "frndint"- : "+t"(x));- return x;-# endif-#elif ARCH(AARCH64)- AARCH64_INSTRUCTION(frintx, x);-#endif- TODO();-}--template<FloatingPoint T>-constexpr T round(T x)-{- CONSTEXPR_STATE(round, x);- // Note: This is break-tie-away-from-zero, so not the hw's understanding of- // "nearest", which would be towards even.- if (x == 0.)- return x;- if (x > 0.)- return floor(x + .5);- return ceil(x - .5);-}--template<Integral I, FloatingPoint P>-ALWAYS_INLINE I round_to(P value);--#if ARCH(X86_64)-template<Integral I>-ALWAYS_INLINE I round_to(long double value)-{- // Note: fistps outputs into a signed integer location (i16, i32, i64),- // so lets be nice and tell the compiler that.- Conditional<sizeof(I) >= sizeof(i16), MakeSigned<I>, i16> ret;- if constexpr (sizeof(I) == sizeof(i64)) {- asm("fistpll %0"- : "=m"(ret)- : "t"(value)- : "st");- } else if constexpr (sizeof(I) == sizeof(i32)) {- asm("fistpl %0"- : "=m"(ret)- : "t"(value)- : "st");- } else {- asm("fistps %0"- : "=m"(ret)- : "t"(value)- : "st");- }- return static_cast<I>(ret);-}--template<Integral I>-ALWAYS_INLINE I round_to(float value)-{- // FIXME: round_to<u64> might will cause issues, aka the indefinite value being set,- // if the value surpasses the i64 limit, even if the result could fit into an u64- // To solve this we would either need to detect that value or do a range check and- // then do a more specialized conversion, which might include a division (which is expensive)- if constexpr (sizeof(I) == sizeof(i64) || IsSame<I, u32>) {- i64 ret;- asm("cvtss2si %1, %0"- : "=r"(ret)- : "xm"(value));- return static_cast<I>(ret);- }- i32 ret;- asm("cvtss2si %1, %0"- : "=r"(ret)- : "xm"(value));- return static_cast<I>(ret);-}--template<Integral I>-ALWAYS_INLINE I round_to(double value)-{- // FIXME: round_to<u64> might will cause issues, aka the indefinite value being set,- // if the value surpasses the i64 limit, even if the result could fit into an u64- // To solve this we would either need to detect that value or do a range check and- // then do a more specialized conversion, which might include a division (which is expensive)- if constexpr (sizeof(I) == sizeof(i64) || IsSame<I, u32>) {- i64 ret;- asm("cvtsd2si %1, %0"- : "=r"(ret)- : "xm"(value));- return static_cast<I>(ret);- }- i32 ret;- asm("cvtsd2si %1, %0"- : "=r"(ret)- : "xm"(value));- return static_cast<I>(ret);-}--#elif ARCH(AARCH64)-template<Signed I>-ALWAYS_INLINE I round_to(float value)-{- if constexpr (sizeof(I) <= sizeof(u32)) {- i32 res;- asm("fcvtns %w0, %s1"- : "=r"(res)- : "w"(value));- return static_cast<I>(res);- }- i64 res;- asm("fcvtns %0, %s1"- : "=r"(res)- : "w"(value));- return static_cast<I>(res);-}--template<Signed I>-ALWAYS_INLINE I round_to(double value)-{- if constexpr (sizeof(I) <= sizeof(u32)) {- i32 res;- asm("fcvtns %w0, %d1"- : "=r"(res)- : "w"(value));- return static_cast<I>(res);- }- i64 res;- asm("fcvtns %0, %d1"- : "=r"(res)- : "w"(value));- return static_cast<I>(res);-}--template<Unsigned U>-ALWAYS_INLINE U round_to(float value)-{- if constexpr (sizeof(U) <= sizeof(u32)) {- u32 res;- asm("fcvtnu %w0, %s1"- : "=r"(res)- : "w"(value));- return static_cast<U>(res);- }- i64 res;- asm("fcvtnu %0, %s1"- : "=r"(res)- : "w"(value));- return static_cast<U>(res);-}--template<Unsigned U>-ALWAYS_INLINE U round_to(double value)-{- if constexpr (sizeof(U) <= sizeof(u32)) {- u32 res;- asm("fcvtns %w0, %d1"- : "=r"(res)- : "w"(value));- return static_cast<U>(res);- }- i64 res;- asm("fcvtns %0, %d1"- : "=r"(res)- : "w"(value));- return static_cast<U>(res);-}--#else-template<Integral I, FloatingPoint P>-ALWAYS_INLINE I round_to(P value)-{- if constexpr (IsSame<P, long double>)- return static_cast<I>(__builtin_llrintl(value));- if constexpr (IsSame<P, double>)- return static_cast<I>(__builtin_llrint(value));- if constexpr (IsSame<P, float>)- return static_cast<I>(__builtin_llrintf(value));-}-#endif--}--using Rounding::ceil;-using Rounding::floor;-using Rounding::rint;-using Rounding::round;-using Rounding::round_to;-using Rounding::trunc;--namespace Division {--template<FloatingPoint T>-constexpr T fmod(T x, T y)-{- CONSTEXPR_STATE(fmod, x, y);--#if ARCH(X86_64)- u16 fpu_status;- do {- asm(- "fprem\n"- "fnstsw %%ax\n"- : "+t"(x), "=a"(fpu_status)- : "u"(y));- } while (fpu_status & 0x400);- return x;- // FIXME: Add a generic implementation of this- // Neither- // ```- // return x - (y * trunc(x/y))- // ```- // nor- // ```- // double result = remainder(std::fabs(x), y = std::fabs(y));- // if (std::signbit(result))- // result += y;- // return std::copysign(result, x);- // ``` from (https://en.cppreference.com/w/cpp/numeric/math/fmod)- // provide enough precision for all cases- // other implementations seem to do this by hand with some fixed point steps in between- // For `remainder` the trivial solution of- // ```- // return x - (y * rint(x/y))- // ```- // might work--#else-# if defined(AK_OS_SERENITY)- // TODO: Add implementation for this function.- TODO();-# endif- if constexpr (IsSame<T, long double>)- return __builtin_fmodl(x, y);- if constexpr (IsSame<T, double>)- return __builtin_fmod(x, y);- if constexpr (IsSame<T, float>)- return __builtin_fmodf(x, y);-#endif-}--template<FloatingPoint T>-constexpr T remainder(T x, T y)-{- CONSTEXPR_STATE(remainder, x, y);--#if ARCH(X86_64)- u16 fpu_status;- do {- asm(- "fprem1\n"- "fnstsw %%ax\n"- : "+t"(x), "=a"(fpu_status)- : "u"(y));- } while (fpu_status & 0x400);- return x;-#else-# if defined(AK_OS_SERENITY)- // TODO: Add implementation for this function.- TODO();-# endif- if constexpr (IsSame<T, long double>)- return __builtin_remainderl(x, y);- if constexpr (IsSame<T, double>)- return __builtin_remainder(x, y);- if constexpr (IsSame<T, float>)- return __builtin_remainderf(x, y);-#endif-}--}--using Division::fmod;-using Division::remainder;--template<FloatingPoint T>-constexpr T sqrt(T x)-{- CONSTEXPR_STATE(sqrt, x);--#if ARCH(X86_64)- if constexpr (IsSame<T, float>) {- float res;- asm("sqrtss %1, %0"- : "=x"(res)- : "x"(x));- return res;- }- if constexpr (IsSame<T, double>) {- double res;- asm("sqrtsd %1, %0"- : "=x"(res)- : "x"(x));- return res;- }- T res;- asm("fsqrt"- : "=t"(res)- : "0"(x));- return res;-#elif ARCH(AARCH64)- AARCH64_INSTRUCTION(fsqrt, x);-#else- return __builtin_sqrt(x);-#endif-}--template<FloatingPoint T>-constexpr T cbrt(T x)-{- CONSTEXPR_STATE(cbrt, x);- if (__builtin_isinf(x) || x == 0)- return x;- if (x < 0)- return -cbrt(-x);-- T r = x;- T ex = 0;-- while (r < 0.125l) {- r *= 8;- ex--;- }- while (r > 1.0l) {- r *= 0.125l;- ex++;- }-- r = (-0.46946116l * r + 1.072302l) * r + 0.3812513l;-- while (ex < 0) {- r *= 0.5l;- ex++;- }- while (ex > 0) {- r *= 2.0l;- ex--;- }-- r = (2.0l / 3.0l) * r + (1.0l / 3.0l) * x / (r * r);- r = (2.0l / 3.0l) * r + (1.0l / 3.0l) * x / (r * r);- r = (2.0l / 3.0l) * r + (1.0l / 3.0l) * x / (r * r);- r = (2.0l / 3.0l) * r + (1.0l / 3.0l) * x / (r * r);-- return r;-}--namespace Trigonometry {--template<FloatingPoint T>-constexpr T hypot(T x, T y)-{- return sqrt(x * x + y * y);-}--template<FloatingPoint T>-constexpr T sin(T angle)-{- CONSTEXPR_STATE(sin, angle);--#if ARCH(X86_64)- T ret;- asm(- "fsin"- : "=t"(ret)- : "0"(angle));- return ret;-#else-# if defined(AK_OS_SERENITY)- // FIXME: This is a very naive implementation, and is only valid for small x.- // Probably a good idea to use a better algorithm in the future, such as a taylor approximation.- return angle;-# else- return __builtin_sin(angle);-# endif-#endif-}--template<FloatingPoint T>-constexpr T cos(T angle)-{- CONSTEXPR_STATE(cos, angle);--#if ARCH(X86_64)- T ret;- asm(- "fcos"- : "=t"(ret)- : "0"(angle));- return ret;-#else-# if defined(AK_OS_SERENITY)- // FIXME: This is a very naive implementation, and is only valid for small x.- // Probably a good idea to use a better algorithm in the future, such as a taylor approximation.- return 1 - ((angle * angle) / 2);-# else- return __builtin_cos(angle);-# endif-#endif-}--template<FloatingPoint T>-constexpr void sincos(T angle, T& sin_val, T& cos_val)-{- if consteval {- sin_val = sin(angle);- cos_val = cos(angle);- return;- }-#if ARCH(X86_64)- asm(- "fsincos"- : "=t"(cos_val), "=u"(sin_val)- : "0"(angle));-#else- sin_val = sin(angle);- cos_val = cos(angle);-#endif-}--template<FloatingPoint T>-constexpr T tan(T angle)-{- CONSTEXPR_STATE(tan, angle);--#if ARCH(X86_64)- T ret, one;- asm(- "fptan"- : "=t"(one), "=u"(ret)- : "0"(angle));-- return ret;-#else-# if defined(AK_OS_SERENITY)- // FIXME: This is a very naive implementation, and is only valid for small x.- // Probably a good idea to use a better algorithm in the future, such as a taylor approximation.- return angle;-# else- return __builtin_tan(angle);-# endif-#endif-}--template<FloatingPoint T>-constexpr T atan(T value)-{- CONSTEXPR_STATE(atan, value);--#if ARCH(X86_64)- T ret;- asm(- "fld1\n"- "fpatan\n"- : "=t"(ret)- : "0"(value));- return ret;-#else-# if defined(AK_OS_SERENITY)- // TODO: Add implementation for this function.- TODO();-# endif- return __builtin_atan(value);-#endif-}--template<FloatingPoint T>-constexpr T asin(T x)-{- CONSTEXPR_STATE(asin, x);- if (x > 1 || x < -1)- return NaN<T>;- if (x > (T)0.5 || x < (T)-0.5)- return 2 * atan<T>(x / (1 + sqrt<T>(1 - x * x)));- T squared = x * x;- T value = x;- T i = x * squared;- value += i * Details::product_odd<1>() / Details::product_even<2>() / 3;- i *= squared;- value += i * Details::product_odd<3>() / Details::product_even<4>() / 5;- i *= squared;- value += i * Details::product_odd<5>() / Details::product_even<6>() / 7;- i *= squared;- value += i * Details::product_odd<7>() / Details::product_even<8>() / 9;- i *= squared;- value += i * Details::product_odd<9>() / Details::product_even<10>() / 11;- i *= squared;- value += i * Details::product_odd<11>() / Details::product_even<12>() / 13;- i *= squared;- value += i * Details::product_odd<13>() / Details::product_even<14>() / 15;- i *= squared;- value += i * Details::product_odd<15>() / Details::product_even<16>() / 17;- return value;-}--template<FloatingPoint T>-constexpr T acos(T value)-{- CONSTEXPR_STATE(acos, value);-- // FIXME: I am naive- return static_cast<T>(0.5) * Pi<T> - asin<T>(value);-}--template<FloatingPoint T>-constexpr T atan2(T y, T x)-{- CONSTEXPR_STATE(atan2, y, x);--#if ARCH(X86_64)- T ret;- asm("fpatan"- : "=t"(ret)- : "0"(x), "u"(y)- : "st(1)");- return ret;-#else-# if defined(AK_OS_SERENITY)- // TODO: Add implementation for this function.- TODO();-# endif- return __builtin_atan2(y, x);-#endif-}--}--using Trigonometry::acos;-using Trigonometry::asin;-using Trigonometry::atan;-using Trigonometry::atan2;-using Trigonometry::cos;-using Trigonometry::hypot;-using Trigonometry::sin;-using Trigonometry::sincos;-using Trigonometry::tan;--namespace Exponentials {--template<FloatingPoint T>-constexpr T log2(T x)-{- CONSTEXPR_STATE(log2, x);--#if ARCH(X86_64)- if constexpr (IsSame<T, long double>) {- T ret;- asm(- "fld1\n"- "fxch %%st(1)\n"- "fyl2x\n"- : "=t"(ret)- : "0"(x));- return ret;- }-#endif- // References:- // Gist comparing some implementations- // * https://gist.github.com/Hendiadyoin1/f58346d66637deb9156ef360aa158bf9-- if (x == 0)- return -Infinity<T>;- if (x <= 0 || __builtin_isnan(x))- return NaN<T>;-- FloatExtractor<T> ext { .d = x };- T exponent = ext.exponent - FloatExtractor<T>::exponent_bias;-- // When the mantissa shows 0b00 (implicitly 1.0) we are on a power of 2- if (ext.mantissa == 0)- return exponent;-- // FIXME: Handle denormalized numbers separately-- // (1 <= mantissa < 2)- T m;- if constexpr (HostIsLittleEndian) {- m = ((FloatExtractor<T>) {- .mantissa = ext.mantissa,- .exponent = FloatExtractor<T>::exponent_bias,- .sign = ext.sign })- .d;- } else {- m = ((FloatExtractor<T>) {- .sign = ext.sign,- .exponent = FloatExtractor<T>::exponent_bias,- .mantissa = ext.mantissa })- .d;- }-- // This is a reconstruction of one of Sun's algorithms- // They use a transformation to lower the problem space,- // while keeping the precision, and a 14th degree polynomial,- // which is mirrored at sqrt(2)- // TODO: Sun has some more algorithms for this and other math functions,- // leveraging LUTs, investigate those, if they are better in performance and/or precision.- // These seem to be related to crLibM's implementations, for which papers and references- // are available.- // FIXME: Dynamically adjust the amount of precision between floats and doubles- // AKA floats might need less accuracy here, in comparison to doubles-- bool inverted = false;- // m > sqrt(2)- if (m > Sqrt2<T>) {- inverted = true;- m = 2 / m;- }- T s = (m - 1) / (m + 1);- // ln((1 + s) / (1 - s)) == ln(m)- T s2 = s * s;- // clang-format off- T high_approx = s2 * (static_cast<T>(0.6666666666666735130)- + s2 * (static_cast<T>(0.3999999999940941908)- + s2 * (static_cast<T>(0.2857142874366239149)- + s2 * (static_cast<T>(0.2222219843214978396)- + s2 * (static_cast<T>(0.1818357216161805012)- + s2 * (static_cast<T>(0.1531383769920937332)- + s2 * static_cast<T>(0.1479819860511658591)))))));- // clang-format on-- // ln(m) == 2 * s + s * high_approx- // log2(m) == log2(e) * ln(m)- T log2_mantissa = L2_E<T> * (2 * s + s * high_approx);- if (inverted)- log2_mantissa = 1 - log2_mantissa;- return exponent + log2_mantissa;-}--template<FloatingPoint T>-constexpr T log(T x)-{- CONSTEXPR_STATE(log, x);--#if ARCH(X86_64)- T ret;- asm(- "fldln2\n"- "fxch %%st(1)\n"- "fyl2x\n"- : "=t"(ret)- : "0"(x));- return ret;-#elif defined(AK_OS_SERENITY)- // FIXME: Adjust the polynomial and formula in log2 to fit this- return log2<T>(x) / L2_E<T>;-#else- return __builtin_log(x);-#endif-}--template<FloatingPoint T>-constexpr T log10(T x)-{- CONSTEXPR_STATE(log10, x);--#if ARCH(X86_64)- T ret;- asm(- "fldlg2\n"- "fxch %%st(1)\n"- "fyl2x\n"- : "=t"(ret)- : "0"(x));- return ret;-#elif defined(AK_OS_SERENITY)- // FIXME: Adjust the polynomial and formula in log2 to fit this- return log2<T>(x) / L2_10<T>;-#else- return __builtin_log10(x);-#endif+ AK_MATH_BUILTIN(trunc, num);}template<FloatingPoint T>-constexpr T exp(T exponent)+constexpr void sincos(T angle, T& sin_value, T& cos_value){- CONSTEXPR_STATE(exp, exponent);--#if ARCH(X86_64)- T res;- asm("fldl2e\n"- "fmulp\n"- "fld1\n"- "fld %%st(1)\n"- "fprem\n"- "f2xm1\n"- "faddp\n"- "fscale\n"- "fstp %%st(1)"- : "=t"(res)- : "0"(exponent));- return res;-#else-# if defined(AK_OS_SERENITY)- // TODO: Add implementation for this function.- TODO();-# endif- return __builtin_exp(exponent);-#endif+ sin_value = sin(angle);+ cos_value = cos(angle);}-template<FloatingPoint T>-constexpr T exp2(T exponent)+// Rounds to the nearest integer, with ties going to even (in the default floating point environment). Values outside of+// the range of I saturate to its limits, and NaN becomes zero.+template<Integral I, FloatingPoint T>+ALWAYS_INLINE I round_to(T value){- CONSTEXPR_STATE(exp2, exponent);--#if ARCH(X86_64)- T res;- asm("fld1\n"- "fld %%st(1)\n"- "fprem\n"- "f2xm1\n"- "faddp\n"- "fscale\n"- "fstp %%st(1)"- : "=t"(res)- : "0"(exponent));- return res;-#else-# if defined(AK_OS_SERENITY)- // TODO: Add implementation for this function.- TODO();-# endif- return __builtin_exp2(exponent);-#endif-}--}--using Exponentials::exp;-using Exponentials::exp2;-using Exponentials::log;-using Exponentials::log10;-using Exponentials::log2;--namespace Hyperbolic {--template<FloatingPoint T>-constexpr T sinh(T x)-{- T exponentiated = exp<T>(x);- if (x > 0)- return (exponentiated * exponentiated - 1) / 2 / exponentiated;- return (exponentiated - 1 / exponentiated) / 2;-}--template<FloatingPoint T>-constexpr T cosh(T x)-{- CONSTEXPR_STATE(cosh, x);-- T exponentiated = exp(-x);- if (x < 0)- return (1 + exponentiated * exponentiated) / 2 / exponentiated;- return (1 / exponentiated + exponentiated) / 2;-}--template<FloatingPoint T>-constexpr T tanh(T x)-{- if (x > 0) {- T exponentiated = exp<T>(2 * x);- return (exponentiated - 1) / (exponentiated + 1);- }- T plusX = exp<T>(x);- T minusX = 1 / plusX;- return (plusX - minusX) / (plusX + minusX);-}--template<FloatingPoint T>-constexpr T asinh(T x)-{- return log<T>(x + sqrt<T>(x * x + 1));-}--template<FloatingPoint T>-constexpr T acosh(T x)-{- return log<T>(x + sqrt<T>(x * x - 1));-}--template<FloatingPoint T>-constexpr T atanh(T x)-{- return log<T>((1 + x) / (1 - x)) / (T)2.0l;-}--}--using Hyperbolic::acosh;-using Hyperbolic::asinh;-using Hyperbolic::atanh;-using Hyperbolic::cosh;-using Hyperbolic::sinh;-using Hyperbolic::tanh;--template<FloatingPoint T>-constexpr T pow(T x, T y)-{- CONSTEXPR_STATE(pow, x, y);- // FIXME: I am naive- if (__builtin_isnan(y))- return y;- if (y == 0)- return 1;- if (x == 0)+ if (__builtin_isnan(value))return 0;- if (y == 1)- return x;- int y_as_int = (int)y;- if (y == (T)y_as_int) {- T result = x;- for (int i = 0; i < fabs<T>(y) - 1; ++i)- result *= x;- if (y < 0)- result = 1.0l / result;- return result;- }-- return exp2<T>(y * log2<T>(x));+ T rounded = rint(value);+ // NB: The maximum of I can round up when converted to T (to 2^63 for i64), so a value equal to it may already be+ // out of range.+ if (rounded >= static_cast<T>(NumericLimits<I>::max()))+ return NumericLimits<I>::max();+ if (rounded <= static_cast<T>(NumericLimits<I>::min()))+ return NumericLimits<I>::min();+ return static_cast<I>(rounded);}template<Integral I, typename T>@@ -1039,8 +179,9 @@ constexpr I clamp_to(T value)return value;}-#undef CONSTEXPR_STATE-#undef AARCH64_INSTRUCTION+#undef AK_MATH_BINARY_FUNCTION+#undef AK_MATH_UNARY_FUNCTION+#undef AK_MATH_BUILTIN}diff --git a/Libraries/LibJS/Rust/src/runtime/big_fraction.rs b/Libraries/LibJS/Rust/src/runtime/big_fraction.rsindex 548b420e520..605b9335c51 100644--- a/Libraries/LibJS/Rust/src/runtime/big_fraction.rs+++ b/Libraries/LibJS/Rust/src/runtime/big_fraction.rs@@ -35,9 +35,9 @@ impl Default for BigFraction {}}-/// AK::pow() of a double raised to an integral power, which multiplies the base by itself rather than calling the-/// C library, and inverts the product for negative powers.-fn ak_pow_integral(base: f64, power: i32) -> f64 {+/// A double raised to an integral power by multiplying the base by itself rather than calling the C library, with the+/// product inverted for negative powers. This keeps the digits that from_double() reads stable.+fn pow_by_repeated_multiplication(base: f64, power: i32) -> f64 {if power == 0 {return 1.0;}@@ -73,9 +73,9 @@ impl BigFraction {Self::new(value, SignedBigInteger::one())}- /// BigFraction(double), which reads the decimal digits of the double one at a time with AK::pow(), and so is- /// only exact for small values. Its exponent is an i8, which wraps for values of 10^127 and above, for which it- /// never finishes reading digits.+ /// BigFraction(double), which reads the decimal digits of the double one at a time with repeated multiplication,+ /// and so is only exact for small values. Its exponent is an i8, which wraps for values of 10^127 and above, for+ /// which it never finishes reading digits.pub fn from_double(mut value: f64) -> Self {let mut numerator = SignedBigInteger::zero();let mut denominator = SignedBigInteger::one();@@ -86,16 +86,16 @@ impl BigFraction {value = -value;}let mut current_power: i8 = 0;- while ak_pow_integral(10.0, i32::from(current_power)) <= value {+ while pow_by_repeated_multiplication(10.0, i32::from(current_power)) <= value {current_power = current_power.wrapping_add(1);}current_power = current_power.wrapping_sub(1);let mut decimal_places: u32 = 0;while value >= f64::EPSILON || current_power >= 0 {numerator *= 10;- let digit = ((value * ak_pow_integral(0.1, i32::from(current_power))) as u64 % 10) as i8;+ let digit = ((value * pow_by_repeated_multiplication(0.1, i32::from(current_power))) as u64 % 10) as i8;numerator += digit;- value -= f64::from(digit) * ak_pow_integral(10.0, i32::from(current_power));+ value -= f64::from(digit) * pow_by_repeated_multiplication(10.0, i32::from(current_power));if current_power < 0 {decimal_places += 1;denominator = SignedBigInteger::from(10).pow(decimal_places);diff --git a/Tests/AK/CMakeLists.txt b/Tests/AK/CMakeLists.txtindex 9ddf7d56c5e..84081805451 100644--- a/Tests/AK/CMakeLists.txt+++ b/Tests/AK/CMakeLists.txt@@ -46,6 +46,7 @@ set(AK_TEST_SOURCESTestIPv6Address.cppTestJSON.cppTestLEB128.cpp+ TestMath.cppTestMemory.cppTestMemoryStream.cppTestNeverDestroyed.cppdiff --git a/Tests/AK/TestMath.cpp b/Tests/AK/TestMath.cppnew file mode 100644index 00000000000..2e5acc0adec--- /dev/null+++ b/Tests/AK/TestMath.cpp@@ -0,0 +1,54 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <LibTest/TestCase.h>++#include <AK/Math.h>+#include <AK/NumericLimits.h>++TEST_CASE(round_to_rounds_ties_to_even)+{+ EXPECT_EQ(round_to<i32>(2.5), 2);+ EXPECT_EQ(round_to<i32>(3.5), 4);+ EXPECT_EQ(round_to<i32>(-2.5f), -2);+ EXPECT_EQ(round_to<u8>(254.6f), 255);+}++TEST_CASE(round_to_saturates)+{+ EXPECT_EQ(round_to<i32>(3e9), NumericLimits<i32>::max());+ EXPECT_EQ(round_to<i32>(-3e9f), NumericLimits<i32>::min());+ EXPECT_EQ(round_to<u8>(-1.0f), 0);+ EXPECT_EQ(round_to<u8>(300.0f), 255);+ EXPECT_EQ(round_to<i64>(1e19), NumericLimits<i64>::max());+ EXPECT_EQ(round_to<i64>(static_cast<double>(NumericLimits<i64>::max())), NumericLimits<i64>::max());+ EXPECT_EQ(round_to<u64>(1e19), 10'000'000'000'000'000'000ull);+ EXPECT_EQ(round_to<i32>(AK::Infinity<double>), NumericLimits<i32>::max());+ EXPECT_EQ(round_to<i32>(-AK::Infinity<float>), NumericLimits<i32>::min());+}++TEST_CASE(round_to_maps_nan_to_zero)+{+ EXPECT_EQ(round_to<i32>(AK::NaN<double>), 0);+ EXPECT_EQ(round_to<u64>(AK::NaN<float>), 0u);+}++TEST_CASE(constant_evaluated_rounding_out_of_i64_range)+{+ constexpr double two_to_the_63 = 9223372036854775808.0;+ static_assert(AK::floor(two_to_the_63) == two_to_the_63);+ static_assert(AK::ceil(-two_to_the_63) == -two_to_the_63);+ static_assert(AK::trunc(1e300) == 1e300);+ static_assert(AK::floor(-1.5) == -2.0);+ static_assert(AK::ceil(1.25f) == 2.0f);+}++TEST_CASE(pow_with_large_integral_exponent)+{+ EXPECT_EQ(AK::pow(1.0, 1e18), 1.0);+ EXPECT_EQ(AK::pow(2.0, 1e9), AK::Infinity<double>);+ EXPECT_EQ(AK::pow(2.0, -1e9), 0.0);+}diff --git a/Tests/LibWeb/Text/expected/wpt-import/webaudio/the-audio-api/the-audioparam-interface/audioparam-cancel-and-hold.txt b/Tests/LibWeb/Text/expected/wpt-import/webaudio/the-audio-api/the-audioparam-interface/audioparam-cancel-and-hold.txtindex 178fafb24cd..9101e44cc82 100644--- a/Tests/LibWeb/Text/expected/wpt-import/webaudio/the-audio-api/the-audioparam-interface/audioparam-cancel-and-hold.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/webaudio/the-audio-api/the-audioparam-interface/audioparam-cancel-and-hold.txt@@ -34,7 +34,7 @@ Pass Expected value for cancelling linearRampToValueAtTime: linearRampToValue(Pass < [linear] All assertions passed. (total 3 assertions)Pass > [exponential] Cancel exponentialRampAtTimePass exponentialRampToValue(0.001, 0.5) up to time 0.25 equals [0,0.0020833334419876337,0.004166666883975267,0.0062500000931322575,0.008333333767950535,0.010416666977107525,0.012500000186264515,0.014583333395421505,0.01666666753590107,0.01875000074505806,0.02083333395421505,0.02291666716337204,0.02500000037252903,0.02708333358168602,0.02916666679084301,0.03125...] with an element-wise tolerance of {"absoluteThreshold":5.9605e-8,"relativeThreshold":0}.-Pass Cancelling exponentialRampToValue(0.001, 0.5) at time 0.25 contains only the constant 0.033932220190763474.+Pass Cancelling exponentialRampToValue(0.001, 0.5) at time 0.25 contains only the constant 0.033932216465473175.Pass Expected value for cancelling exponentialRampToValue(0.001, 0.5) at time 0.25 is 0.033932216465473175 within an error of 0.0000018664.Pass < [exponential] All assertions passed. (total 3 assertions)Pass > [setTarget] Cancel setTargetAtTime