Truly independent web browser
AK: Compute math functions with LLVM libc
Shannon Booth6c8dd38d6aa1
diff --git a/AK/CMakeLists.txt b/AK/CMakeLists.txtindex 1bb85b6d95a..68d63f9e1f5 100644--- a/AK/CMakeLists.txt+++ b/AK/CMakeLists.txt@@ -16,6 +16,7 @@ set(SOURCESJsonObject.cppJsonParser.cppJsonValue.cpp+ MathFunctions.cppMemoryStream.cppNumberFormat.cppOptionParser.cpp@@ -60,6 +61,8 @@ ladybird_lib(AK ak)configure_file(Debug.h.in Debug.h @ONLY)+target_include_directories(AK SYSTEM PRIVATE "${LLVM_LIBC_INCLUDE_DIR}/llvm-libc")+if(cpptrace_FOUND AND LADYBIRD_ENABLE_CPPTRACE)target_link_libraries(AK PRIVATE cpptrace::cpptrace)diff --git a/AK/Math.h b/AK/Math.hindex ad15455cfbf..f612b24ad8b 100644--- a/AK/Math.h+++ b/AK/Math.h@@ -10,6 +10,7 @@#include <AK/Concepts.h>#include <AK/Endian.h>#include <AK/FloatingPoint.h>+#include <AK/MathFunctions.h>#include <AK/NumericLimits.h>#include <AK/StdLibExtraDetails.h>#include <AK/Types.h>@@ -41,8 +42,9 @@ constexpr T to_degrees(T radians)return radians * 180 / AK::Pi<T>;}-// 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.+// NB: These forward to the compiler builtins, which lower to single instructions where the target has them, and are+// constant-evaluated by the compiler where it supports that. Their results are exact, so they are the same on+// every platform.#define AK_MATH_BUILTIN(function, args...) \if constexpr (IsSame<T, long double>) \return __builtin_##function##l(args); \@@ -71,30 +73,35 @@ 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: These forward to LLVM libc, so that their results are the same on every platform.+#define AK_MATH_LLVM_LIBC(function, args...) \+ if constexpr (IsSame<T, float>) \+ return ladybird_math_##function##f(args); \+ else if constexpr (IsSame<T, double>) \+ return ladybird_math_##function(args); \+ else \+ static_assert(DependentFalse<T>, "LLVM libc has no " #function " for this floating point type");++#define AK_MATH_LLVM_LIBC_UNARY_FUNCTION(function) \+ template<FloatingPoint T> \+ T function(T x) \+ { \+ AK_MATH_LLVM_LIBC(function, x); \+ }++#define AK_MATH_LLVM_LIBC_BINARY_FUNCTION(function) \+ template<FloatingPoint T> \+ T function(T x, T y) \+ { \+ AK_MATH_LLVM_LIBC(function, x, y); \+ }++AK_ENUMERATE_UNARY_MATH_FUNCTIONS(AK_MATH_LLVM_LIBC_UNARY_FUNCTION)+AK_ENUMERATE_BINARY_MATH_FUNCTIONS(AK_MATH_LLVM_LIBC_BINARY_FUNCTION)// NB: Not every compiler constant-evaluates these builtins, so they get a fallback for constant evaluation.template<FloatingPoint T>@@ -135,10 +142,14 @@ constexpr T trunc(T num)}template<FloatingPoint T>-constexpr void sincos(T angle, T& sin_value, T& cos_value)+void sincos(T angle, T& sin_value, T& cos_value){- sin_value = sin(angle);- cos_value = cos(angle);+ if constexpr (IsSame<T, float>)+ ladybird_math_sincosf(angle, &sin_value, &cos_value);+ else if constexpr (IsSame<T, double>)+ ladybird_math_sincos(angle, &sin_value, &cos_value);+ else+ static_assert(DependentFalse<T>, "LLVM libc has no sincos for this floating point type");}// Rounds to the nearest integer, with ties going to even (in the default floating point environment). Values outside of@@ -179,6 +190,9 @@ constexpr I clamp_to(T value)return value;}+#undef AK_MATH_LLVM_LIBC_BINARY_FUNCTION+#undef AK_MATH_LLVM_LIBC_UNARY_FUNCTION+#undef AK_MATH_LLVM_LIBC#undef AK_MATH_BINARY_FUNCTION#undef AK_MATH_UNARY_FUNCTION#undef AK_MATH_BUILTINdiff --git a/AK/MathFunctions.cpp b/AK/MathFunctions.cppnew file mode 100644index 00000000000..e1aff36aaa2--- /dev/null+++ b/AK/MathFunctions.cpp@@ -0,0 +1,35 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#define LIBC_MATH (LIBC_MATH_NO_ERRNO | LIBC_MATH_NO_EXCEPT | LIBC_MATH_ASSUME_ROUND_NEAREST_ONLY)+#define LIBC_NAMESPACE __llvm_libc_ladybird++#include <AK/MathFunctions.h>+#include <shared/math.h>++#define AK_DEFINE_UNARY_MATH_FUNCTION(function) \+ double ladybird_math_##function(double x) { return LIBC_NAMESPACE::shared::function(x); } \+ float ladybird_math_##function##f(float x) { return LIBC_NAMESPACE::shared::function##f(x); }++#define AK_DEFINE_BINARY_MATH_FUNCTION(function) \+ double ladybird_math_##function(double x, double y) { return LIBC_NAMESPACE::shared::function(x, y); } \+ float ladybird_math_##function##f(float x, float y) { return LIBC_NAMESPACE::shared::function##f(x, y); }++extern "C" {++AK_ENUMERATE_UNARY_MATH_FUNCTIONS(AK_DEFINE_UNARY_MATH_FUNCTION)+AK_ENUMERATE_BINARY_MATH_FUNCTIONS(AK_DEFINE_BINARY_MATH_FUNCTION)++void ladybird_math_sincos(double x, double* sin_value, double* cos_value)+{+ LIBC_NAMESPACE::shared::sincos(x, sin_value, cos_value);+}++void ladybird_math_sincosf(float x, float* sin_value, float* cos_value)+{+ LIBC_NAMESPACE::shared::sincosf(x, sin_value, cos_value);+}+}diff --git a/AK/MathFunctions.h b/AK/MathFunctions.hnew file mode 100644index 00000000000..bb728ca6f7a--- /dev/null+++ b/AK/MathFunctions.h@@ -0,0 +1,53 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++// NB: The Rust ak crate compiles AK/MathFunctions.cpp without the rest of AK, so this includes nothing.++#define AK_ENUMERATE_UNARY_MATH_FUNCTIONS(X) \+ X(acos) \+ X(asin) \+ X(atan) \+ X(cbrt) \+ X(cos) \+ X(cosh) \+ X(exp) \+ X(exp2) \+ X(expm1) \+ X(log) \+ X(log10) \+ X(log1p) \+ X(log2) \+ X(sin) \+ X(sinh) \+ X(tan) \+ X(tanh)++#define AK_ENUMERATE_BINARY_MATH_FUNCTIONS(X) \+ X(atan2) \+ X(hypot) \+ X(pow)++#define AK_DECLARE_UNARY_MATH_FUNCTION(function) \+ double ladybird_math_##function(double x); \+ float ladybird_math_##function##f(float x);++#define AK_DECLARE_BINARY_MATH_FUNCTION(function) \+ double ladybird_math_##function(double x, double y); \+ float ladybird_math_##function##f(float x, float y);++extern "C" {++AK_ENUMERATE_UNARY_MATH_FUNCTIONS(AK_DECLARE_UNARY_MATH_FUNCTION)+AK_ENUMERATE_BINARY_MATH_FUNCTIONS(AK_DECLARE_BINARY_MATH_FUNCTION)++void ladybird_math_sincos(double x, double* sin_value, double* cos_value);+void ladybird_math_sincosf(float x, float* sin_value, float* cos_value);+}++#undef AK_DECLARE_BINARY_MATH_FUNCTION+#undef AK_DECLARE_UNARY_MATH_FUNCTION