From 65750586b6c22c32736f2718281f7bc6f647ad42 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 21 Mar 2024 15:19:44 +0200 Subject: [PATCH] LibC: Use GCC builtins for math functions --- libc/CMakeLists.txt | 2 +- libc/math.S | 26 ------------------- libc/math.cpp | 63 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 27 deletions(-) delete mode 100644 libc/math.S create mode 100644 libc/math.cpp diff --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt index 84a6e38824..1eceb64027 100644 --- a/libc/CMakeLists.txt +++ b/libc/CMakeLists.txt @@ -28,7 +28,7 @@ set(LIBC_SOURCES termios.cpp time.cpp unistd.cpp - math.S + math.cpp icxxabi.cpp ../BAN/BAN/Assert.cpp diff --git a/libc/math.S b/libc/math.S deleted file mode 100644 index 2e6f8fdc3e..0000000000 --- a/libc/math.S +++ /dev/null @@ -1,26 +0,0 @@ - -.global floorl; -floorl: - fldt 8(%rsp) - - fnstenv -28(%rsp) /* store fpu environment */ - - /* We use here %edx although only the low 1 bits are defined. - But none of the operations should care and they are faster - than the 16 bit operations. */ - movl $0x400,%edx /* round towards -oo */ - orl -28(%rsp),%edx - andl $0xf7ff,%edx - movl %edx,-32(%rsp) - fldcw -32(%rsp) /* load modified control word */ - - frndint /* round */ - - /* Preserve "invalid" exceptions from sNaN input. */ - fnstsw - andl $0x1, %eax - orl %eax, -24(%rsp) - - fldenv -28(%rsp) /* restore original environment */ - - ret diff --git a/libc/math.cpp b/libc/math.cpp new file mode 100644 index 0000000000..53764dcf3f --- /dev/null +++ b/libc/math.cpp @@ -0,0 +1,63 @@ +#include + +#define BUILTINS1(func) \ + float func##f(float a) { return __builtin_##func##f(a); } \ + double func(double a) { return __builtin_##func(a); } \ + long double func##l(long double a) { return __builtin_##func##l(a); } + +#define BUILTINS2(func) \ + float func##f(float a, float b) { return __builtin_##func##f(a, b); } \ + double func(double a, double b) { return __builtin_##func(a, b); } \ + long double func##l(long double a, long double b) { return __builtin_##func##l(a, b); } \ + +__BEGIN_DECLS + +BUILTINS1(acos) +BUILTINS1(acosh) +BUILTINS1(asin) +BUILTINS1(asinh) +BUILTINS1(atan) +BUILTINS2(atan2) +BUILTINS1(atanh) +BUILTINS1(cbrt) +BUILTINS1(ceil) +BUILTINS2(copysign) +BUILTINS1(cos) +BUILTINS1(cosh) +BUILTINS1(erf) +BUILTINS1(erfc) +BUILTINS1(exp) +BUILTINS1(exp2) +BUILTINS1(expm1) +BUILTINS1(fabs) +BUILTINS2(fdim) +BUILTINS1(floor) +BUILTINS2(fmax) +BUILTINS2(fmin) +BUILTINS2(fmod) +BUILTINS2(hypot) +BUILTINS1(j0) +BUILTINS1(j1) +BUILTINS1(lgamma) +BUILTINS1(log) +BUILTINS1(log10) +BUILTINS1(log1p) +BUILTINS1(log2) +BUILTINS1(logb) +BUILTINS1(nearbyint) +BUILTINS2(nextafter) +BUILTINS2(pow) +BUILTINS2(remainder) +BUILTINS1(rint) +BUILTINS1(round) +BUILTINS1(sin) +BUILTINS1(sinh) +BUILTINS1(sqrt) +BUILTINS1(tan) +BUILTINS1(tanh) +BUILTINS1(tgamma) +BUILTINS1(trunc) +BUILTINS1(y0) +BUILTINS1(y1) + +__END_DECLS