From 31c2053c1cbcb430c940f3af50dde688c01059f7 Mon Sep 17 00:00:00 2001 From: "Justin C. Miller" Date: Sun, 13 Mar 2022 18:11:45 -0700 Subject: [PATCH] [libc] Implement some math.h functions with builtins This commit adds implementation of some of the basic math.h functions with compiler builtins. --- configs/user-debug.yaml | 1 - src/libraries/libc/math.h | 15 +++++++-- src/libraries/libc/math/builtins.cpp.cog | 42 ++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 src/libraries/libc/math/builtins.cpp.cog diff --git a/configs/user-debug.yaml b/configs/user-debug.yaml index 8eb123b..06ca8d9 100644 --- a/configs/user-debug.yaml +++ b/configs/user-debug.yaml @@ -6,7 +6,6 @@ variables: ccflags: [ "--target=x86_64-jsix-elf", - "-mno-sse", "-fno-omit-frame-pointer", "-fno-stack-protector", diff --git a/src/libraries/libc/math.h b/src/libraries/libc/math.h index 890459d..68678f2 100644 --- a/src/libraries/libc/math.h +++ b/src/libraries/libc/math.h @@ -12,8 +12,12 @@ // NOTE: libc math.h is only stubbed out currently! +#ifdef __cplusplus +extern "C" { +#endif + #define NAN 0 -#define INFINITY 0 +#define INFINITY __builtin_inff() #define FP_NAN (-1) #define FP_ZERO 0 @@ -25,7 +29,7 @@ typedef float float_t; typedef double double_t; -#define isinf(x) 0 +#define isinf(x) (__builtin_isinf_sign((x)) != 0) #define isnan(x) 0 #define isnormal(x) 0 @@ -38,7 +42,7 @@ typedef double double_t; FP_SUBNORMAL) -#define signbit(x) 0 +#define signbit(x) (__builtin_signbit((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); float fmaf(float x, float y, float z); long double fmal(long double x, long double y, long double z); + + +#ifdef __cplusplus +} // extern C +#endif diff --git a/src/libraries/libc/math/builtins.cpp.cog b/src/libraries/libc/math/builtins.cpp.cog new file mode 100644 index 0000000..79fef92 --- /dev/null +++ b/src/libraries/libc/math/builtins.cpp.cog @@ -0,0 +1,42 @@ +// vim: ft=cpp +#include + +/*[[[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); }