split up arith.c

This commit is contained in:
dzaima 2021-05-24 21:35:53 +03:00
parent 33b548fbda
commit 899175f48b
6 changed files with 124 additions and 116 deletions

View File

@ -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 $<

View File

@ -2,31 +2,6 @@
#include "../utils/each.h"
#include <math.h>
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;i<ria;i++)rp[i]=wv OP xp[i]; } \
else if (!xa) { i32 xv=o2iu(x); i32* wp=i32any_ptr(w); for(usz i=0;i<ria;i++)rp[i]=wp[i] OP xv; } \
else { i32* wp=i32any_ptr(w); i32* xp=i32any_ptr(x); for(usz i=0;i<ria;i++)rp[i]=wp[i] OP xp[i]; } \
if(wa) dec(w); if(xa) dec(x); return r; \
} \
if (we==el_c32 && xe==el_c32) { \
i32* rp; B r = m_i32arrc(&rp, isArr(x)? x : w); usz ria=a(r)->ia; \
if (!wa) { u32 wv=o2cu(w); u32* xp=c32any_ptr(x); for(usz i=0;i<ria;i++)rp[i]=wv OP xp[i]; } \
else if (!xa) { u32 xv=o2cu(x); u32* wp=c32any_ptr(w); for(usz i=0;i<ria;i++)rp[i]=wp[i] OP xv; } \
else { u32* wp=c32any_ptr(w); u32* xp=c32any_ptr(x); for(usz i=0;i<ria;i++)rp[i]=wp[i] OP xp[i]; } \
if(wa) dec(w); if(xa) dec(x); return r; \
} \
}
#define CMP(NAME,OP,FC,CF) \
B NAME##_c2(B t, B w, B x) { \
CMP_IMPL(OP, FC, CF); \
P2(NAME); \
return m_i32(compare(w, x) OP 0); \
}
CMP(le, <=, 1, 0)
CMP(ge, >=, 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);
}

37
src/builtins/arithm.c Normal file
View File

@ -0,0 +1,37 @@
#include "../core.h"
#include "../utils/each.h"
#include <math.h>
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);
}

64
src/builtins/cmp.c Normal file
View File

@ -0,0 +1,64 @@
#include "../core.h"
#include "../utils/each.h"
#include <math.h>
#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;i<ria;i++)rp[i]=wv OP xp[i]; } \
else if (!xa) { i32 xv=o2iu(x); i32* wp=i32any_ptr(w); for(usz i=0;i<ria;i++)rp[i]=wp[i] OP xv; } \
else { i32* wp=i32any_ptr(w); i32* xp=i32any_ptr(x); for(usz i=0;i<ria;i++)rp[i]=wp[i] OP xp[i]; } \
if(wa) dec(w); if(xa) dec(x); return r; \
} \
if (we==el_c32 && xe==el_c32) { \
i32* rp; B r = m_i32arrc(&rp, isArr(x)? x : w); usz ria=a(r)->ia; \
if (!wa) { u32 wv=o2cu(w); u32* xp=c32any_ptr(x); for(usz i=0;i<ria;i++)rp[i]=wv OP xp[i]; } \
else if (!xa) { u32 xv=o2cu(x); u32* wp=c32any_ptr(w); for(usz i=0;i<ria;i++)rp[i]=wp[i] OP xv; } \
else { u32* wp=c32any_ptr(w); u32* xp=c32any_ptr(x); for(usz i=0;i<ria;i++)rp[i]=wp[i] OP xp[i]; } \
if(wa) dec(w); if(xa) dec(x); return r; \
} \
}
#define CMP(NAME,OP,FC,CF) \
B NAME##_c2(B t, B w, B x) { \
CMP_IMPL(OP, FC, CF); \
P2(NAME); \
return m_i32(compare(w, x) OP 0); \
}
CMP(le, <=, 1, 0)
CMP(ge, >=, 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);
}

View File

@ -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"

View File

@ -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