From aa4c3c2d3271abc9a02ac3d4d5cbc6f0b0c375ac Mon Sep 17 00:00:00 2001 From: bumbread Date: Thu, 9 Jun 2022 09:05:50 +1100 Subject: [PATCH 1/2] LESS FILES --- code/math/abs.c | 16 ---------------- code/math/{fmod.c => basic.c} | 16 ++++++++++++++++ code/math/copysign.c | 0 code/math/cosh.c | 0 code/math/erf.c | 0 code/math/erfc.c | 0 code/math/exp.c | 0 code/math/{acos.c => exponential.c} | 0 code/math/fdim.c | 0 code/math/{acosh.c => float-classify.c} | 0 code/math/{asin.c => float-manupulation.c} | 0 code/math/floats.c | 0 code/math/floor.c | 0 code/math/fma.c | 0 code/math/fmax.c | 12 ------------ code/math/frexp.c | 0 code/math/{asinh.c => hyperbolic.c} | 0 code/math/hypot.c | 0 code/math/ilogb.c | 0 code/math/ldexp.c | 0 code/math/lgamma.c | 0 code/math/llrint.c | 0 code/math/llround.c | 0 code/math/log.c | 0 code/math/lrint.c | 0 code/math/mod.c | 0 code/math/nan.c | 0 code/math/nearbyint.c | 0 code/math/{atan.c => nearest-int.c} | 0 code/math/nextafter.c | 0 code/math/pow.c | 0 code/math/{atanh.c => power.c} | 0 code/math/{cbrt.c => probability-statistics.c} | 0 code/math/remainder.c | 0 code/math/remquo.c | 0 code/math/rint.c | 0 code/math/scalbln.c | 0 code/math/scalbnf.c | 0 code/math/sin.c | 0 code/math/sinh.c | 0 code/math/sqrt.c | 0 code/math/tan.c | 0 code/math/tanh.c | 0 code/math/tgamma.c | 0 code/math/{ceil.c => trigonometry.c} | 0 code/math/trunc.c | 0 46 files changed, 16 insertions(+), 28 deletions(-) delete mode 100644 code/math/abs.c rename code/math/{fmod.c => basic.c} (54%) delete mode 100644 code/math/copysign.c delete mode 100644 code/math/cosh.c delete mode 100644 code/math/erf.c delete mode 100644 code/math/erfc.c delete mode 100644 code/math/exp.c rename code/math/{acos.c => exponential.c} (100%) delete mode 100644 code/math/fdim.c rename code/math/{acosh.c => float-classify.c} (100%) rename code/math/{asin.c => float-manupulation.c} (100%) delete mode 100644 code/math/floats.c delete mode 100644 code/math/floor.c delete mode 100644 code/math/fma.c delete mode 100644 code/math/fmax.c delete mode 100644 code/math/frexp.c rename code/math/{asinh.c => hyperbolic.c} (100%) delete mode 100644 code/math/hypot.c delete mode 100644 code/math/ilogb.c delete mode 100644 code/math/ldexp.c delete mode 100644 code/math/lgamma.c delete mode 100644 code/math/llrint.c delete mode 100644 code/math/llround.c delete mode 100644 code/math/log.c delete mode 100644 code/math/lrint.c delete mode 100644 code/math/mod.c delete mode 100644 code/math/nan.c delete mode 100644 code/math/nearbyint.c rename code/math/{atan.c => nearest-int.c} (100%) delete mode 100644 code/math/nextafter.c delete mode 100644 code/math/pow.c rename code/math/{atanh.c => power.c} (100%) rename code/math/{cbrt.c => probability-statistics.c} (100%) delete mode 100644 code/math/remainder.c delete mode 100644 code/math/remquo.c delete mode 100644 code/math/rint.c delete mode 100644 code/math/scalbln.c delete mode 100644 code/math/scalbnf.c delete mode 100644 code/math/sin.c delete mode 100644 code/math/sinh.c delete mode 100644 code/math/sqrt.c delete mode 100644 code/math/tan.c delete mode 100644 code/math/tanh.c delete mode 100644 code/math/tgamma.c rename code/math/{ceil.c => trigonometry.c} (100%) delete mode 100644 code/math/trunc.c 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.c similarity index 54% rename from code/math/fmod.c rename to code/math/basic.c index 2029d25..a8baeac 100644 --- a/code/math/fmod.c +++ b/code/math/basic.c @@ -1,4 +1,20 @@ +#include + +double fabs(double x) { + if(x >= 0) { + return x; + } + return -x; +} + +float fabsf(float 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/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.c similarity index 100% rename from code/math/acos.c rename to code/math/exponential.c diff --git a/code/math/fdim.c b/code/math/fdim.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/acosh.c b/code/math/float-classify.c similarity index 100% rename from code/math/acosh.c rename to code/math/float-classify.c diff --git a/code/math/asin.c b/code/math/float-manupulation.c similarity index 100% rename from code/math/asin.c rename to code/math/float-manupulation.c diff --git a/code/math/floats.c b/code/math/floats.c deleted file mode 100644 index e69de29..0000000 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/asinh.c b/code/math/hyperbolic.c similarity index 100% rename from code/math/asinh.c rename to code/math/hyperbolic.c 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/atan.c b/code/math/nearest-int.c similarity index 100% rename from code/math/atan.c rename to code/math/nearest-int.c 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/atanh.c b/code/math/power.c similarity index 100% rename from code/math/atanh.c rename to code/math/power.c diff --git a/code/math/cbrt.c b/code/math/probability-statistics.c similarity index 100% rename from code/math/cbrt.c rename to code/math/probability-statistics.c 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/ceil.c b/code/math/trigonometry.c similarity index 100% rename from code/math/ceil.c rename to code/math/trigonometry.c diff --git a/code/math/trunc.c b/code/math/trunc.c deleted file mode 100644 index e69de29..0000000 From 1c6938e88026e435898b5ec939f83164d48331d4 Mon Sep 17 00:00:00 2001 From: bumbread Date: Thu, 9 Jun 2022 12:25:33 +1100 Subject: [PATCH 2/2] aljsndlkjasn --- code/math.c | 42 +++++++++++++++++ code/math/{basic.c => basic.h} | 11 +---- code/math/{exponential.c => exponential.h} | 0 code/math/floats.h | 45 +++++++++++++++++++ code/math/{float-classify.c => hyperbolic.h} | 0 .../{float-manupulation.c => nearest-int.h} | 0 code/math/{hyperbolic.c => power.h} | 0 code/math/probability-statistics.c | 0 ...nearest-int.c => probability-statistics.h} | 0 code/math/trigonometry.c | 0 code/math/{power.c => trigonometry.h} | 0 inc/math.h | 5 +-- 12 files changed, 89 insertions(+), 14 deletions(-) create mode 100644 code/math.c rename code/math/{basic.c => basic.h} (69%) rename code/math/{exponential.c => exponential.h} (100%) create mode 100644 code/math/floats.h rename code/math/{float-classify.c => hyperbolic.h} (100%) rename code/math/{float-manupulation.c => nearest-int.h} (100%) rename code/math/{hyperbolic.c => power.h} (100%) delete mode 100644 code/math/probability-statistics.c rename code/math/{nearest-int.c => probability-statistics.h} (100%) delete mode 100644 code/math/trigonometry.c rename code/math/{power.c => trigonometry.h} (100%) 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/basic.c b/code/math/basic.h similarity index 69% rename from code/math/basic.c rename to code/math/basic.h index a8baeac..73b7390 100644 --- a/code/math/basic.c +++ b/code/math/basic.h @@ -1,14 +1,5 @@ -#include - -double fabs(double x) { - if(x >= 0) { - return x; - } - return -x; -} - -float fabsf(float x) { +ftype fabs(ftype x) { if(x >= 0) { return x; } diff --git a/code/math/exponential.c b/code/math/exponential.h similarity index 100% rename from code/math/exponential.c rename to code/math/exponential.h 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/float-classify.c b/code/math/hyperbolic.h similarity index 100% rename from code/math/float-classify.c rename to code/math/hyperbolic.h diff --git a/code/math/float-manupulation.c b/code/math/nearest-int.h similarity index 100% rename from code/math/float-manupulation.c rename to code/math/nearest-int.h diff --git a/code/math/hyperbolic.c b/code/math/power.h similarity index 100% rename from code/math/hyperbolic.c rename to code/math/power.h diff --git a/code/math/probability-statistics.c b/code/math/probability-statistics.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/nearest-int.c b/code/math/probability-statistics.h similarity index 100% rename from code/math/nearest-int.c rename to code/math/probability-statistics.h diff --git a/code/math/trigonometry.c b/code/math/trigonometry.c deleted file mode 100644 index e69de29..0000000 diff --git a/code/math/power.c b/code/math/trigonometry.h similarity index 100% rename from code/math/power.c rename to code/math/trigonometry.h diff --git a/inc/math.h b/inc/math.h index 244aee2..81b97ef 100644 --- a/inc/math.h +++ b/inc/math.h @@ -27,10 +27,7 @@ typedef double double_t; #define FP_NORMAL 2 #define FP_SUBNORMAL 3 #define FP_ZERO 4 -#define fpclassify(x) ( \ - _is_float(x) ? _fpclassify_f(x) : - _is_double(x) ? _fpclassify_d(x) : - FP_NAN) +#define fpclassify(x) (_is_float(x) ? _fpclassifyf(x) : _fpclassify(x)) double acos(double x); float acosf(float x);