From 899175f48b4c7b1f2474978a660ff235a38bbe4f Mon Sep 17 00:00:00 2001 From: dzaima Date: Mon, 24 May 2021 21:35:53 +0300 Subject: [PATCH] split up arith.c --- obj/subMakefile | 2 +- src/builtins/{arith.c => arithd.c} | 114 ----------------------------- src/builtins/arithm.c | 37 ++++++++++ src/builtins/cmp.c | 64 ++++++++++++++++ src/opt/single.c | 4 +- src/utils/each.h | 19 +++++ 6 files changed, 124 insertions(+), 116 deletions(-) rename src/builtins/{arith.c => arithd.c} (65%) create mode 100644 src/builtins/arithm.c create mode 100644 src/builtins/cmp.c diff --git a/obj/subMakefile b/obj/subMakefile index 8e3a8e7e..6e5ea457 100644 --- a/obj/subMakefile +++ b/obj/subMakefile @@ -38,7 +38,7 @@ utils: utf.o hash.o file.o @echo $< | cut -c 11- @$(CMD) $@.d -c $< -builtins: arith.o sfns.o sort.o md1.o md2.o fns.o sysfn.o +builtins: arithm.o arithd.o cmp.o sfns.o sort.o md1.o md2.o fns.o sysfn.o %.o: ../../src/builtins/%.c @echo $< | cut -c 11- @$(CMD) $@.d -c $< diff --git a/src/builtins/arith.c b/src/builtins/arithd.c similarity index 65% rename from src/builtins/arith.c rename to src/builtins/arithd.c index b72a2140..9a5a6b73 100644 --- a/src/builtins/arith.c +++ b/src/builtins/arithd.c @@ -2,31 +2,6 @@ #include "../utils/each.h" #include -static inline B arith_recm(BB2B f, B x) { - B fx = getFillQ(x); - B r = eachm_fn(f, bi_N, x); - return withFill(r, fx); -} -#ifdef CATCH_ERRORS -static inline B arith_recd(BBB2B f, B w, B x) { - B fx = getFillQ(x); - if (noFill(fx)) return eachd_fn(f, bi_N, w, x); - B fw = getFillQ(w); - B r = eachd_fn(f, bi_N, w, x); - if (noFill(fw)) return r; - if (CATCH) { dec(catchMessage); return r; } - B fr = f(bi_N, fw, fx); - popCatch(); - return withFill(r, asFill(fr)); -} -#else -static inline B arith_recd(BBB2B f, B w, B x) { - return eachd_fn(f, bi_N, w, x); -} -#endif - - -#define P1(N) { if( isArr(x)) return arith_recm(N##_c1, x); } #define P2(N) { if(isArr(w)|isArr(x)) return arith_recd(N##_c2, w, x); } #if TYPED_ARITH static B f64_maybe_i32(B x) { @@ -192,93 +167,4 @@ GC2f(log , log(x.f)/log(w.f), {}) #undef GC2i #undef GC2f -B decp_c1(B t, B x); -#define CMP_IMPL(OP,FC,CF) \ - if (isF64(w)&isF64(x)) return m_i32(w.f OP x.f); \ - if (isC32(w)&isC32(x)) return m_f64(w.u OP x.u); \ - if (isF64(w)&isC32(x)) return m_f64(FC); \ - if (isC32(w)&isF64(x)) return m_f64(CF); \ - bool wa = isArr(w); \ - bool xa = isArr(x); \ - if (wa|xa) { \ - u8 we = wa? TI(w).elType : selfElType(w); \ - u8 xe = xa? TI(x).elType : selfElType(x); \ - if (we==el_i32 && xe==el_i32) { \ - i32* rp; B r = m_i32arrc(&rp, isArr(x)? x : w); usz ria=a(r)->ia; \ - if (!wa) { i32 wv=o2iu(w); i32* xp=i32any_ptr(x); for(usz i=0;iia; \ - if (!wa) { u32 wv=o2cu(w); u32* xp=c32any_ptr(x); for(usz i=0;i=, 0, 1) -CMP(lt, < , 1, 0) -CMP(gt, > , 0, 1) -#undef CMP - -B eq_c2(B t, B w, B x) { - CMP_IMPL(==, 0, 0); - P2(eq); - B r = m_i32(atomEqual(w, x)); - dec(w); dec(x); - return r; -} -B ne_c2(B t, B w, B x) { - CMP_IMPL(!=, 1, 1); - P2(ne); - B r = m_i32(!atomEqual(w, x)); - dec(w); dec(x); - return r; -} - -extern B rt_merge; -B gt_c1(B t, B x) { - if (isAtm(x)) return x; - return bqn_merge(x); -} - -B add_c1(B t, B x) { return x; } -B sub_c1(B t, B x) { if (isF64(x)) return m_f64( -x.f ); P1( sub); thrM("-: Negating non-number"); } -B not_c1(B t, B x) { if (isF64(x)) return m_f64( 1-x.f ); P1( not); thrM("¬: Argument was not a number"); } -B mul_c1(B t, B x) { if (isF64(x)) return m_f64(x.f==0?0:x.f>0?1:-1); P1(mul); thrM("×: Getting sign of non-number"); } -B div_c1(B t, B x) { if (isF64(x)) return m_f64( 1/x.f ); P1( div); thrM("÷: Getting reciprocal of non-number"); } -B pow_c1(B t, B x) { if (isF64(x)) return m_f64( exp(x.f)); P1( pow); thrM("⋆: Getting exp of non-number"); } -B floor_c1(B t, B x) { if (isF64(x)) return m_f64(floor(x.f)); P1(floor); thrM("⌊: Argument was not a number"); } -B ceil_c1(B t, B x) { if (isF64(x)) return m_f64( ceil(x.f)); P1( ceil); thrM("⌈: Argument was not a number"); } -B stile_c1(B t, B x) { if (isF64(x)) return m_f64( fabs(x.f)); P1(stile); thrM("|: Argument was not a number"); } -B log_c1(B t, B x) { if (isF64(x)) return m_f64( log(x.f)); P1( log); thrM("⋆⁼: Getting log of non-number"); } - -B lt_c1(B t, B x) { return m_unit(x); } -B eq_c1(B t, B x) { B r = m_i32(isArr(x)? rnk(x) : 0); decR(x); return r; } -B ne_c1(B t, B x) { B r = m_f64(isArr(x)&&rnk(x)? *a(x)->sh : 1); decR(x); return r; } - -extern B rt_sortDsc; -B or_c1(B t, B x) { return c1(rt_sortDsc, x); } -B and_c1(B t, B x); // defined in sort.c - -#undef P1 #undef P2 - - - -void arith_init() { - c(BFn,bi_add)->ident = c(BFn,bi_sub)->ident = c(BFn,bi_or )->ident = c(BFn,bi_ne)->ident = c(BFn,bi_gt)->ident = m_i32(0); - c(BFn,bi_mul)->ident = c(BFn,bi_div)->ident = c(BFn,bi_and)->ident = c(BFn,bi_eq)->ident = c(BFn,bi_ge)->ident = c(BFn,bi_pow)->ident = c(BFn,bi_not)->ident = m_i32(1); - c(BFn,bi_floor)->ident = m_f64(1.0/0.0); - c(BFn,bi_ceil )->ident = m_f64(-1.0/0.0); -} diff --git a/src/builtins/arithm.c b/src/builtins/arithm.c new file mode 100644 index 00000000..5cca2bc2 --- /dev/null +++ b/src/builtins/arithm.c @@ -0,0 +1,37 @@ +#include "../core.h" +#include "../utils/each.h" +#include + +static inline B arith_recm(BB2B f, B x) { + B fx = getFillQ(x); + B r = eachm_fn(f, bi_N, x); + return withFill(r, fx); +} + +#define P1(N) { if(isArr(x)) return arith_recm(N##_c1, x); } +B add_c1(B t, B x) { return x; } +B sub_c1(B t, B x) { if (isF64(x)) return m_f64( -x.f ); P1( sub); thrM("-: Negating non-number"); } +B not_c1(B t, B x) { if (isF64(x)) return m_f64( 1-x.f ); P1( not); thrM("¬: Argument was not a number"); } +B mul_c1(B t, B x) { if (isF64(x)) return m_f64(x.f==0?0:x.f>0?1:-1); P1(mul); thrM("×: Getting sign of non-number"); } +B div_c1(B t, B x) { if (isF64(x)) return m_f64( 1/x.f ); P1( div); thrM("÷: Getting reciprocal of non-number"); } +B pow_c1(B t, B x) { if (isF64(x)) return m_f64( exp(x.f)); P1( pow); thrM("⋆: Getting exp of non-number"); } +B floor_c1(B t, B x) { if (isF64(x)) return m_f64(floor(x.f)); P1(floor); thrM("⌊: Argument was not a number"); } +B ceil_c1(B t, B x) { if (isF64(x)) return m_f64( ceil(x.f)); P1( ceil); thrM("⌈: Argument was not a number"); } +B stile_c1(B t, B x) { if (isF64(x)) return m_f64( fabs(x.f)); P1(stile); thrM("|: Argument was not a number"); } +B log_c1(B t, B x) { if (isF64(x)) return m_f64( log(x.f)); P1( log); thrM("⋆⁼: Getting log of non-number"); } +#undef P1 + +B lt_c1(B t, B x) { return m_unit(x); } +B eq_c1(B t, B x) { B r = m_i32(isArr(x)? rnk(x) : 0); decR(x); return r; } +B ne_c1(B t, B x) { B r = m_f64(isArr(x)&&rnk(x)? *a(x)->sh : 1); decR(x); return r; } + +extern B rt_sortDsc; +B or_c1(B t, B x) { return c1(rt_sortDsc, x); } +B and_c1(B t, B x); // defined in sort.c + +void arith_init() { + c(BFn,bi_add)->ident = c(BFn,bi_sub)->ident = c(BFn,bi_or )->ident = c(BFn,bi_ne)->ident = c(BFn,bi_gt)->ident = m_i32(0); + c(BFn,bi_mul)->ident = c(BFn,bi_div)->ident = c(BFn,bi_and)->ident = c(BFn,bi_eq)->ident = c(BFn,bi_ge)->ident = c(BFn,bi_pow)->ident = c(BFn,bi_not)->ident = m_i32(1); + c(BFn,bi_floor)->ident = m_f64(1.0/0.0); + c(BFn,bi_ceil )->ident = m_f64(-1.0/0.0); +} diff --git a/src/builtins/cmp.c b/src/builtins/cmp.c new file mode 100644 index 00000000..304f2f11 --- /dev/null +++ b/src/builtins/cmp.c @@ -0,0 +1,64 @@ +#include "../core.h" +#include "../utils/each.h" +#include + +#define P2(N) { if(isArr(w)|isArr(x)) return arith_recd(N##_c2, w, x); } + +#define CMP_IMPL(OP,FC,CF) \ + if (isF64(w)&isF64(x)) return m_i32(w.f OP x.f); \ + if (isC32(w)&isC32(x)) return m_f64(w.u OP x.u); \ + if (isF64(w)&isC32(x)) return m_f64(FC); \ + if (isC32(w)&isF64(x)) return m_f64(CF); \ + bool wa = isArr(w); \ + bool xa = isArr(x); \ + if (wa|xa) { \ + u8 we = wa? TI(w).elType : selfElType(w); \ + u8 xe = xa? TI(x).elType : selfElType(x); \ + if (we==el_i32 && xe==el_i32) { \ + i32* rp; B r = m_i32arrc(&rp, isArr(x)? x : w); usz ria=a(r)->ia; \ + if (!wa) { i32 wv=o2iu(w); i32* xp=i32any_ptr(x); for(usz i=0;iia; \ + if (!wa) { u32 wv=o2cu(w); u32* xp=c32any_ptr(x); for(usz i=0;i=, 0, 1) +CMP(lt, < , 1, 0) +CMP(gt, > , 0, 1) +#undef CMP + +B eq_c2(B t, B w, B x) { + CMP_IMPL(==, 0, 0); + P2(eq); + B r = m_i32(atomEqual(w, x)); + dec(w); dec(x); + return r; +} +B ne_c2(B t, B w, B x) { + CMP_IMPL(!=, 1, 1); + P2(ne); + B r = m_i32(!atomEqual(w, x)); + dec(w); dec(x); + return r; +} + +extern B rt_merge; +B gt_c1(B t, B x) { + if (isAtm(x)) return x; + return bqn_merge(x); +} diff --git a/src/opt/single.c b/src/opt/single.c index 02bc1719..06776a21 100644 --- a/src/opt/single.c +++ b/src/opt/single.c @@ -15,7 +15,9 @@ #include "../builtins/sfns.c" #include "../builtins/sysfn.c" #include "../builtins/sort.c" -#include "../builtins/arith.c" +#include "../builtins/arithm.c" +#include "../builtins/arithd.c" +#include "../builtins/cmp.c" #include "../builtins/md1.c" #include "../builtins/md2.c" #include "../vm.c" diff --git a/src/utils/each.h b/src/utils/each.h index 2182d8c4..0a41f4ea 100644 --- a/src/utils/each.h +++ b/src/utils/each.h @@ -148,3 +148,22 @@ static B eachd(B f, B w, B x) { // complete w F¨ x without fills if (isAtm(w) & isAtm(x)) return m_hunit(c2(f, w, x)); return eachd_fn(c2fn(f), f, w, x); } + + +#ifdef CATCH_ERRORS +static inline B arith_recd(BBB2B f, B w, B x) { + B fx = getFillQ(x); + if (noFill(fx)) return eachd_fn(f, bi_N, w, x); + B fw = getFillQ(w); + B r = eachd_fn(f, bi_N, w, x); + if (noFill(fw)) return r; + if (CATCH) { dec(catchMessage); return r; } + B fr = f(bi_N, fw, fx); + popCatch(); + return withFill(r, asFill(fr)); +} +#else +static inline B arith_recd(BBB2B f, B w, B x) { + return eachd_fn(f, bi_N, w, x); +} +#endif \ No newline at end of file