[libc] Implement some math.h functions with builtins

This commit adds implementation of some of the basic math.h functions
with compiler builtins.
This commit is contained in:
Justin C. Miller
2022-03-13 18:11:45 -07:00
parent d759aae318
commit 31c2053c1c
3 changed files with 54 additions and 4 deletions

View File

@@ -6,7 +6,6 @@ variables:
ccflags: [ ccflags: [
"--target=x86_64-jsix-elf", "--target=x86_64-jsix-elf",
"-mno-sse",
"-fno-omit-frame-pointer", "-fno-omit-frame-pointer",
"-fno-stack-protector", "-fno-stack-protector",

View File

@@ -12,8 +12,12 @@
// NOTE: libc math.h is only stubbed out currently! // NOTE: libc math.h is only stubbed out currently!
#ifdef __cplusplus
extern "C" {
#endif
#define NAN 0 #define NAN 0
#define INFINITY 0 #define INFINITY __builtin_inff()
#define FP_NAN (-1) #define FP_NAN (-1)
#define FP_ZERO 0 #define FP_ZERO 0
@@ -25,7 +29,7 @@
typedef float float_t; typedef float float_t;
typedef double double_t; typedef double double_t;
#define isinf(x) 0 #define isinf(x) (__builtin_isinf_sign((x)) != 0)
#define isnan(x) 0 #define isnan(x) 0
#define isnormal(x) 0 #define isnormal(x) 0
@@ -38,7 +42,7 @@ typedef double double_t;
FP_SUBNORMAL) FP_SUBNORMAL)
#define signbit(x) 0 #define signbit(x) (__builtin_signbit((x)))
double acos(double x); double acos(double x);
@@ -265,3 +269,8 @@ long double fminl(long double x, long double y);
double fma(double x, double y, double z); double fma(double x, double y, double z);
float fmaf(float x, float y, float z); float fmaf(float x, float y, float z);
long double fmal(long double x, long double y, long double z); long double fmal(long double x, long double y, long double z);
#ifdef __cplusplus
} // extern C
#endif

View File

@@ -0,0 +1,42 @@
// vim: ft=cpp
#include <math.h>
/*[[[cog code generation
builtins_single = [
"acos", "asin", "atan", "ceil", "cos", "cosh", "exp", "fabs",
"floor", "log10", "log", "sin", "sinh", "sqrt", "tan", "tanh",
]
builtins_double = ["atan2", "fmod", "pow"]
builtins_intp = [
"frexp", "ldexp", "modf",
]
for fname in builtins_single:
cog.outl(f"double {fname} (double x) {{ return __builtin_{fname}(x); }}")
cog.outl(f"float {fname}f (float x) {{ return __builtin_{fname}f(x); }}")
cog.outl(f"long double {fname}l (long double x) {{ return __builtin_{fname}l(x); }}")
cog.outl()
for fname in builtins_double:
cog.outl(f"double {fname} (double x, double y) {{ return __builtin_{fname}(x, y); }}")
cog.outl(f"float {fname}f (float x, float y) {{ return __builtin_{fname}f(x, y); }}")
cog.outl(f"long double {fname}l (long double x, long double y) {{ return __builtin_{fname}l(x, y); }}")
cog.outl()
]]]*/
/// [[[end]]]
double frexp(double value, int *exp) { return __builtin_frexp(value, exp); }
float frexpf(float value, int *exp) { return __builtin_frexpf(value, exp); }
long double frexpl(long double value, int *exp) { return __builtin_frexpl(value, exp); }
double ldexp(double x, int exp) { return __builtin_ldexp(x, exp); }
float ldexpf(float x, int exp) { return __builtin_ldexpf(x, exp); }
long double ldexpl(long double x, int exp) { return __builtin_ldexpl(x, exp); }
double modf(double value, double *iptr) { return __builtin_modf(value, iptr); }
float modff(float value, float *iptr) { return __builtin_modff(value, iptr); }
long double modfl(long double value, long double *iptr) { return __builtin_modfl(value, iptr); }