LibC: Use GCC builtins for math functions

This commit is contained in:
Bananymous 2024-03-21 15:19:44 +02:00
parent 62f6128ba1
commit 65750586b6
3 changed files with 64 additions and 27 deletions

View File

@ -28,7 +28,7 @@ set(LIBC_SOURCES
termios.cpp
time.cpp
unistd.cpp
math.S
math.cpp
icxxabi.cpp
../BAN/BAN/Assert.cpp

View File

@ -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

63
libc/math.cpp Normal file
View File

@ -0,0 +1,63 @@
#include <math.h>
#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