From 85f8a5e7bd5df6e4e5698c49a3e0fd06cddf0a7d Mon Sep 17 00:00:00 2001 From: dzaima Date: Mon, 6 Sep 2021 01:02:11 +0300 Subject: [PATCH] saner comparison code --- src/builtins/cmp.c | 100 +++++++++++++++++++++------------------------ src/utils/mut.h | 53 +++++++++--------------- 2 files changed, 66 insertions(+), 87 deletions(-) diff --git a/src/builtins/cmp.c b/src/builtins/cmp.c index abfc9f13..37f0f4c5 100644 --- a/src/builtins/cmp.c +++ b/src/builtins/cmp.c @@ -4,65 +4,57 @@ #define P2(N) { if(isArr(w)|isArr(x)) return arith_recd(N##_c2, w, x); } +#define AL(X) i8* rp; B r = m_i8arrc(&rp, X); + #define CMP_IMPL(CHR,OP,FC,CF) \ if (isF64(w)&isF64(x)) return m_i32(w.f OP x.f); \ if (isC32(w)&isC32(x)) return m_i32(w.u OP x.u); \ if (isF64(w)&isC32(x)) return m_i32(FC); \ if (isC32(w)&isF64(x)) return m_i32(CF); \ - bool wa = isArr(w); \ - bool xa = isArr(x); \ - if (wa|xa && (!wa|!xa || rnk(w)==rnk(x))) { \ - if (wa&xa && !eqShape(w, x)) thrF(CHR": Expected equal shape prefix (%H ≡ ≢𝕨, %H ≡ ≢𝕩)", w, x); \ - u8 we = wa? TI(w,elType) : selfElType(w); \ - u8 xe = xa? TI(x,elType) : selfElType(x); \ - if (we==el_i8 && xe==el_i8) { \ - i8* rp; B r = m_i8arrc(&rp, isArr(x)? x : w); usz ria=a(r)->ia; /* (i8)o2iu safe bc _e==el_i8 */ \ - if (!wa) { i8 wv=o2iu(w); i8* xp=i8any_ptr(x); for(usz i=0;iia; \ - if (!wa) { i16 wv=o2iu(w); i16* xp=i16any_ptr(x); for(usz i=0;iia; \ - if (!wa) { i32 wv=o2iu(w); i32* xp=i32any_ptr(x); for(usz i=0;iia; \ - if (!wa) { f64 wv=o2fu(w); f64* xp=f64any_ptr(x); for(usz i=0;iia; \ - if (!wa) { u32 wv=o2cu(w); u32* xp=c32any_ptr(x); for(usz i=0;iia; \ - f64* wp=f64any_ptr(w); i32* xp=i32any_ptr(x); for(usz i=0;iia; \ - i32* wp=i32any_ptr(w); f64* xp=f64any_ptr(x); for(usz i=0;iia; \ + switch(we) { default: UD; \ + case el_i8 : { i8* wp=i8any_ptr (w); i8* xp=i8any_ptr (x); for(usz i=0;iia; \ + switch(we) { default: UD; \ + case el_i8: { if (!q_i8 (x)) break; i8 xv=o2iu(x); i8* wp=i8any_ptr (w); for(usz i=0;iia; \ + switch(xe) { default: UD; \ + case el_i8: { if (!q_i8 (w)) break; i8 wv=o2iu(w); i8* xp=i8any_ptr (x); for(usz i=0;itype) { default: UD; - case el_MAX: - nty = isF64(x)? (q_i32(x)? el_i32 : el_f64) : (isC32(x)? el_c32 : el_B); - goto change; - - case el_i32: { - if (!q_i32(x)) { nty = isF64(x)? el_f64 : el_B; goto change; } - m->ai32[ms] = o2iu(x); - return; - } - case el_c32: { - if (!isC32(x)) { nty = el_B; goto change; } - m->ac32[ms] = o2cu(x); - return; - } - case el_f64: { - if (!isF64(x)) { nty = el_B; goto change; } - m->af64[ms] = o2fu(x); - return; - } + case el_MAX: goto change; + case el_i8: if (!q_i8 (x)) goto change; m->ai8 [ms] = o2iu(x); return; + case el_i16: if (!q_i16(x)) goto change; m->ai16[ms] = o2iu(x); return; + case el_i32: if (!q_i32(x)) goto change; m->ai32[ms] = o2iu(x); return; + case el_c8: if (!q_c8 (x)) goto change; m->ac8 [ms] = o2cu(x); return; + case el_c16: if (!q_c16(x)) goto change; m->ac16[ms] = o2cu(x); return; + case el_c32: if (!q_c32(x)) goto change; m->ac32[ms] = o2cu(x); return; + case el_f64: if (!q_f64(x)) goto change; m->af64[ms] = o2fu(x); return; case el_B: { m->aB[ms] = x; return; } } change: - mut_to(m, nty); + mut_to(m, el_or(m->type, selfElType(x))); goto again; } static void mut_setG(Mut* m, usz ms, B x) { // consumes; sets m[ms] to x, assumes the current type can store it switch(m->type) { default: UD; - case el_i32: { assert(q_i32(x)); - m->ai32[ms] = o2iu(x); - return; - } - case el_c32: { assert(isC32(x)); - m->ac32[ms] = o2cu(x); - return; - } - case el_f64: { assert(isF64(x)); - m->af64[ms] = o2fu(x); - return; - } + case el_i8 : { assert(q_i8 (x)); m->ai8 [ms] = o2iu(x); return; } + case el_i16: { assert(q_i16(x)); m->ai16[ms] = o2iu(x); return; } + case el_i32: { assert(q_i32(x)); m->ai32[ms] = o2iu(x); return; } + case el_c8 : { assert(q_c8 (x)); m->ac8 [ms] = o2cu(x); return; } + case el_c16: { assert(q_c16(x)); m->ac16[ms] = o2cu(x); return; } + case el_c32: { assert(q_c32(x)); m->ac32[ms] = o2cu(x); return; } + case el_f64: { assert(q_f64(x)); m->af64[ms] = o2fu(x); return; } case el_B: { m->aB[ms] = x; return; @@ -141,7 +124,11 @@ static void mut_rm(Mut* m, usz ms) { // clears the object at position ms } static B mut_getU(Mut* m, usz ms) { switch(m->type) { default: UD; + case el_i8: return m_i32(m->ai8 [ms]); + case el_i16: return m_i32(m->ai16[ms]); case el_i32: return m_i32(m->ai32[ms]); + case el_c8: return m_c32(m->ac8 [ms]); + case el_c16: return m_c32(m->ac16[ms]); case el_c32: return m_c32(m->ac32[ms]); case el_f64: return m_f64(m->af64[ms]); case el_B: return m->aB[ms];