diff --git a/code/math.c b/code/math.c new file mode 100644 index 0000000..05de3f2 --- /dev/null +++ b/code/math.c @@ -0,0 +1,42 @@ + +#include + +#define ftype float +#define itype uint32_t +#define f_bits 32 +#define f_sig_bits 24 +#define f_exp_max 127 +#define suffix(name) #name ## "f" +#include "math/floats.h" +#include "math/basic.h" +#include "math/exponential.h" +#include "math/hyperbolic.h" +#include "math/nearest-int.h" +#include "math/power.h" +#include "math/probability-statistics.h" +#undef ftype +#undef itype +#undef f_bits +#undef f_sig_bits +#undef f_exp_max +#undef suffix + +#define ftype double +#define itype uint64_t +#define f_bits 64 +#define f_sig_bits 53 +#define f_exp_max 1023 +#define suffix(name) #name +#include "math/basic.h" +#include "math/exponential.h" +#include "math/floats.h" +#include "math/hyperbolic.h" +#include "math/nearest-int.h" +#include "math/power.h" +#include "math/probability-statistics.h" +#undef ftype +#undef itype +#undef f_bits +#undef f_sig_bits +#undef f_exp_max +#undef suffix diff --git a/code/math/abs.c b/code/math/abs.c deleted file mode 100644 index 246376b..0000000 --- a/code/math/abs.c +++ /dev/null @@ -1,16 +0,0 @@ - -#include - -double fabs(double x) { - if(x >= 0) { - return x; - } - return -x; -} - -float fabsf(float x) { - if(x >= 0) { - return x; - } - return -x; -} diff --git a/code/math/fmod.c b/code/math/basic.h similarity index 73% rename from code/math/fmod.c rename to code/math/basic.h index 2029d25..73b7390 100644 --- a/code/math/fmod.c +++ b/code/math/basic.h @@ -1,4 +1,11 @@ +ftype fabs(ftype x) { + if(x >= 0) { + return x; + } + return -x; +} + double fmod(double x, double y) { int n = 0; while(y < x*n) { diff --git a/code/math/cbrt.c b/code/math/cbrt.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/ceil.c b/code/math/ceil.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/copysign.c b/code/math/copysign.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/cosh.c b/code/math/cosh.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/erf.c b/code/math/erf.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/erfc.c b/code/math/erfc.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/exp.c b/code/math/exp.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/acos.c b/code/math/exponential.h similarity index 100% rename from code/math/acos.c rename to code/math/exponential.h diff --git a/code/math/fdim.c b/code/math/fdim.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/floats.c b/code/math/floats.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/floats.h b/code/math/floats.h new file mode 100644 index 0000000..79e2e72 --- /dev/null +++ b/code/math/floats.h @@ -0,0 +1,45 @@ + +#include + +#define f_man_bits (f_sig_bits - 1) +#define f_exp_bits (f_bits - f_man_bits - 1) + +#define b_sign_val(b) ((b) >> (f_exp_bits + f_man_bits)) +#define b_bexp_val(b) (((b) >> f_man_bits) & f_exp_mask) +#define b_mant_val(b) ((b) & f_man_mask) +#define b_mant_mask ((1 << f_man_bits) - 1) +#define b_bexp_mask ((1 << f_exp_bits) - 1) +#define b_mant_max ((1 << f_man_bits) - 1) +#define b_bexp_max ((1 << f_exp_bits) - 1) + +#define b_exp_bias ((1 << (f_exp_bits-1)) - 1) +#define b_exp_val(b) (b_bexp_val(b) - b_exp_bias) + +static inline itype suffix(getbits)(ftype f) { + union _f { + ftype f; + itype i; + } u; + u.f = f; + int bits = u.i; + return bits; +} + +int suffix(_fpclassify)(ftype f) { + itype bits = suffix(getbits)(f); + itype sign = b_sign_val(bits); + itype bexp = b_bexp_val(bits); + itype mant = b_mant_val(bits); + if(bexp == b_bexp_max) { + if(mant == 0) return FP_INFINITE; + else return FP_NAN; + } + else if(bexp == 0) { + if(mant == 0) return FP_ZERO; + else return FP_SUBNORMAL; + } + else { + return FP_NORMAL; + } +} + diff --git a/code/math/floor.c b/code/math/floor.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/fma.c b/code/math/fma.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/fmax.c b/code/math/fmax.c deleted file mode 100644 index ca5193a..0000000 --- a/code/math/fmax.c +++ /dev/null @@ -1,12 +0,0 @@ - -double fmax(double x, double y) { - if(x>y) return x; - return y; -} - -float fmaxf(float x, float y) { - if(x>y) return x; - return y; -} - - diff --git a/code/math/frexp.c b/code/math/frexp.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/acosh.c b/code/math/hyperbolic.h similarity index 100% rename from code/math/acosh.c rename to code/math/hyperbolic.h diff --git a/code/math/hypot.c b/code/math/hypot.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/ilogb.c b/code/math/ilogb.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/ldexp.c b/code/math/ldexp.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/lgamma.c b/code/math/lgamma.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/llrint.c b/code/math/llrint.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/llround.c b/code/math/llround.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/log.c b/code/math/log.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/lrint.c b/code/math/lrint.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/mod.c b/code/math/mod.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/nan.c b/code/math/nan.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/nearbyint.c b/code/math/nearbyint.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/asin.c b/code/math/nearest-int.h similarity index 100% rename from code/math/asin.c rename to code/math/nearest-int.h diff --git a/code/math/nextafter.c b/code/math/nextafter.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/pow.c b/code/math/pow.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/asinh.c b/code/math/power.h similarity index 100% rename from code/math/asinh.c rename to code/math/power.h diff --git a/code/math/atan.c b/code/math/probability-statistics.h similarity index 100% rename from code/math/atan.c rename to code/math/probability-statistics.h diff --git a/code/math/remainder.c b/code/math/remainder.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/remquo.c b/code/math/remquo.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/rint.c b/code/math/rint.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/scalbln.c b/code/math/scalbln.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/scalbnf.c b/code/math/scalbnf.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/sin.c b/code/math/sin.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/sinh.c b/code/math/sinh.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/sqrt.c b/code/math/sqrt.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/tan.c b/code/math/tan.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/tanh.c b/code/math/tanh.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/tgamma.c b/code/math/tgamma.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/atanh.c b/code/math/trigonometry.h similarity index 100% rename from code/math/atanh.c rename to code/math/trigonometry.h diff --git a/code/math/trunc.c b/code/math/trunc.c deleted file mode 100644 index e69de29..0000000 diff --git a/inc/math.h b/inc/math.h index 3326e0c..0a954f4 100644 --- a/inc/math.h +++ b/inc/math.h @@ -28,8 +28,7 @@ typedef double double_t; #define FP_SUBNORMAL 3 #define FP_ZERO 4 -#define fpclassify(x) 0 -#define isfinite(x) 0 +#define fpclassify(x) (_is_float(x) ? _fpclassifyf(x) : _fpclassify(x)) double acos(double x); float acosf(float x);