overflow builtin cleanup
This commit is contained in:
parent
6f99b58099
commit
97c1b1d31d
@ -48,7 +48,7 @@ B tbl_c2(Md1D* d, B w, B x) { B f = d->f;
|
||||
if (isAtm(x)) x = m_atomUnit(x);
|
||||
ur wr = rnk(w); usz wia = a(w)->ia;
|
||||
ur xr = rnk(x); usz xia = a(x)->ia;
|
||||
ur rr = wr+xr; usz ria = uszMulT(wia, xia);
|
||||
ur rr = wr+xr; usz ria = uszMul(wia, xia);
|
||||
if (rr<xr) thrF("⌜: Result rank too large (%i≡=𝕨, %i≡=𝕩)", wr, xr);
|
||||
|
||||
SGetU(w)
|
||||
@ -96,9 +96,9 @@ B scan_c1(Md1D* d, B x) { B f = d->f;
|
||||
if (xr==1 && xe<=el_f64 && isFun(f) && v(f)->flags) {
|
||||
u8 rtid = v(f)->flags-1;
|
||||
if (rtid==0) { // +
|
||||
if (xe==el_i8 ) { i8* xp=i8any_ptr (x); i32* rp; B r=m_i32arrv(&rp, ia); i32 c=0; for (usz i=0; i<ia; i++) { if(__builtin_add_overflow(c,xp[i],&c))goto base; rp[i]=c; } dec(x); return r; }
|
||||
if (xe==el_i16) { i16* xp=i16any_ptr(x); i32* rp; B r=m_i32arrv(&rp, ia); i32 c=0; for (usz i=0; i<ia; i++) { if(__builtin_add_overflow(c,xp[i],&c))goto base; rp[i]=c; } dec(x); return r; }
|
||||
if (xe==el_i32) { i32* xp=i32any_ptr(x); i32* rp; B r=m_i32arrv(&rp, ia); i32 c=0; for (usz i=0; i<ia; i++) { if(__builtin_add_overflow(c,xp[i],&c))goto base; rp[i]=c; } dec(x); return r; }
|
||||
if (xe==el_i8 ) { i8* xp=i8any_ptr (x); i32* rp; B r=m_i32arrv(&rp, ia); i32 c=0; for (usz i=0; i<ia; i++) { if(addOn(c,xp[i]))goto base; rp[i]=c; } dec(x); return r; }
|
||||
if (xe==el_i16) { i16* xp=i16any_ptr(x); i32* rp; B r=m_i32arrv(&rp, ia); i32 c=0; for (usz i=0; i<ia; i++) { if(addOn(c,xp[i]))goto base; rp[i]=c; } dec(x); return r; }
|
||||
if (xe==el_i32) { i32* xp=i32any_ptr(x); i32* rp; B r=m_i32arrv(&rp, ia); i32 c=0; for (usz i=0; i<ia; i++) { if(addOn(c,xp[i]))goto base; rp[i]=c; } dec(x); return r; }
|
||||
}
|
||||
if (rtid==7) { // ⌈
|
||||
if (xe==el_i8 ) { i8* xp=i8any_ptr (x); i8* rp; B r=m_i8arrv (&rp, ia); i8 c=I8_MIN ; for (usz i=0; i<ia; i++) { if (xp[i]>c)c=xp[i]; rp[i]=c; } dec(x); return r; }
|
||||
@ -144,9 +144,9 @@ B scan_c2(Md1D* d, B w, B x) { B f = d->f;
|
||||
u8 rtid = v(f)->flags-1;
|
||||
i32 wv = o2iu(w);
|
||||
if (rtid==0) { // +
|
||||
if (xe==el_i8 ) { i8* xp=i8any_ptr (x); i32* rp; B r=m_i32arrv(&rp, ia); i32 c=wv; for (usz i=0; i<ia; i++) { if(__builtin_add_overflow(c,xp[i],&c))goto base; rp[i]=c; } dec(x); return r; }
|
||||
if (xe==el_i16) { i16* xp=i16any_ptr(x); i32* rp; B r=m_i32arrv(&rp, ia); i32 c=wv; for (usz i=0; i<ia; i++) { if(__builtin_add_overflow(c,xp[i],&c))goto base; rp[i]=c; } dec(x); return r; }
|
||||
if (xe==el_i32) { i32* xp=i32any_ptr(x); i32* rp; B r=m_i32arrv(&rp, ia); i32 c=wv; for (usz i=0; i<ia; i++) { if(__builtin_add_overflow(c,xp[i],&c))goto base; rp[i]=c; } dec(x); return r; }
|
||||
if (xe==el_i8 ) { i8* xp=i8any_ptr (x); i32* rp; B r=m_i32arrv(&rp, ia); i32 c=wv; for (usz i=0; i<ia; i++) { if(addOn(c,xp[i]))goto base; rp[i]=c; } dec(x); return r; }
|
||||
if (xe==el_i16) { i16* xp=i16any_ptr(x); i32* rp; B r=m_i32arrv(&rp, ia); i32 c=wv; for (usz i=0; i<ia; i++) { if(addOn(c,xp[i]))goto base; rp[i]=c; } dec(x); return r; }
|
||||
if (xe==el_i32) { i32* xp=i32any_ptr(x); i32* rp; B r=m_i32arrv(&rp, ia); i32 c=wv; for (usz i=0; i<ia; i++) { if(addOn(c,xp[i]))goto base; rp[i]=c; } dec(x); return r; }
|
||||
}
|
||||
if (rtid==7) { // ⌈
|
||||
if (xe==el_i8 && wv==(i8 )wv) { i8* xp=i8any_ptr (x); i8* rp; B r=m_i8arrv (&rp, ia); i8 c=wv; for (usz i=0; i<ia; i++) { if (xp[i]>c)c=xp[i]; rp[i]=c; } dec(x); return r; }
|
||||
@ -199,16 +199,16 @@ B fold_c1(Md1D* d, B x) { B f = d->f;
|
||||
if (isFun(f) && v(f)->flags && xe<=el_f64) {
|
||||
u8 rtid = v(f)->flags-1;
|
||||
if (rtid==0) { // +
|
||||
if (xe==el_i8 ) { i8* xp = i8any_ptr (x); i64 c=0; for (usz i=0; i<ia; i++) c+=xp[i]; dec(x); return m_f64(c); } // won't worry about 64TB array sum float inaccuracy for now
|
||||
if (xe==el_i16) { i16* xp = i16any_ptr(x); i32 c=0; for (usz i=0; i<ia; i++) if (__builtin_add_overflow(c,xp[i],&c))goto base; dec(x); return m_i32(c); }
|
||||
if (xe==el_i32) { i32* xp = i32any_ptr(x); i32 c=0; for (usz i=0; i<ia; i++) if (__builtin_add_overflow(c,xp[i],&c))goto base; dec(x); return m_i32(c); }
|
||||
if (xe==el_f64) { f64* xp = f64any_ptr(x); f64 c=0; for (usz i=0; i<ia; i++) c+= xp[i]; dec(x); return m_f64(c); }
|
||||
if (xe==el_i8 ) { i8* xp = i8any_ptr (x); i64 c=0; for (usz i=0; i<ia; i++) c+=xp[i]; dec(x); return m_f64(c); } // won't worry about 64TB array sum float inaccuracy for now
|
||||
if (xe==el_i16) { i16* xp = i16any_ptr(x); i32 c=0; for (usz i=0; i<ia; i++) if (addOn(c,xp[i]))goto base; dec(x); return m_i32(c); }
|
||||
if (xe==el_i32) { i32* xp = i32any_ptr(x); i32 c=0; for (usz i=0; i<ia; i++) if (addOn(c,xp[i]))goto base; dec(x); return m_i32(c); }
|
||||
if (xe==el_f64) { f64* xp = f64any_ptr(x); f64 c=0; for (usz i=0; i<ia; i++) c+= xp[i]; dec(x); return m_f64(c); }
|
||||
}
|
||||
if (rtid==2 | rtid==10) { // ×/∧
|
||||
if (xe==el_i8 ) { i8* xp = i8any_ptr (x); i32 c=1; for (usz i=0; i<ia; i++) if (__builtin_mul_overflow(c,xp[i],&c))goto base; dec(x); return m_f64(c); }
|
||||
if (xe==el_i16) { i16* xp = i16any_ptr(x); i32 c=1; for (usz i=0; i<ia; i++) if (__builtin_mul_overflow(c,xp[i],&c))goto base; dec(x); return m_i32(c); }
|
||||
if (xe==el_i32) { i32* xp = i32any_ptr(x); i32 c=1; for (usz i=0; i<ia; i++) if (__builtin_mul_overflow(c,xp[i],&c))goto base; dec(x); return m_i32(c); }
|
||||
if (xe==el_f64) { f64* xp = f64any_ptr(x); f64 c=1; for (usz i=0; i<ia; i++) c*= xp[i]; dec(x); return m_f64(c); }
|
||||
if (xe==el_i8 ) { i8* xp = i8any_ptr (x); i32 c=1; for (usz i=0; i<ia; i++) if (mulOn(c,xp[i]))goto base; dec(x); return m_f64(c); }
|
||||
if (xe==el_i16) { i16* xp = i16any_ptr(x); i32 c=1; for (usz i=0; i<ia; i++) if (mulOn(c,xp[i]))goto base; dec(x); return m_i32(c); }
|
||||
if (xe==el_i32) { i32* xp = i32any_ptr(x); i32 c=1; for (usz i=0; i<ia; i++) if (mulOn(c,xp[i]))goto base; dec(x); return m_i32(c); }
|
||||
if (xe==el_f64) { f64* xp = f64any_ptr(x); f64 c=1; for (usz i=0; i<ia; i++) c*= xp[i]; dec(x); return m_f64(c); }
|
||||
}
|
||||
if (rtid==6) { // ⌊
|
||||
if (xe==el_i8 ) { i8* xp = i8any_ptr (x); i8 c=I8_MAX ; for (usz i=0; i<ia; i++) if (xp[i]<c) c=xp[i]; dec(x); return m_i32(c); }
|
||||
@ -250,14 +250,14 @@ B fold_c2(Md1D* d, B w, B x) { B f = d->f;
|
||||
i32 wi = o2iu(w);
|
||||
u8 rtid = v(f)->flags-1;
|
||||
if (rtid==0) { // +
|
||||
if (xe==el_i8 ) { i8* xp = i8any_ptr (x); i64 c=wi; for (usz i=0; i<ia; i++) c+=xp[i]; dec(x); return m_f64(c); }
|
||||
if (xe==el_i16) { i16* xp = i16any_ptr(x); i32 c=wi; for (usz i=0; i<ia; i++) if (__builtin_add_overflow(c,xp[i],&c))goto base; dec(x); return m_i32(c); }
|
||||
if (xe==el_i32) { i32* xp = i32any_ptr(x); i32 c=wi; for (usz i=0; i<ia; i++) if (__builtin_add_overflow(c,xp[i],&c))goto base; dec(x); return m_i32(c); }
|
||||
if (xe==el_i8 ) { i8* xp = i8any_ptr (x); i64 c=wi; for (usz i=0; i<ia; i++) c+=xp[i]; dec(x); return m_f64(c); }
|
||||
if (xe==el_i16) { i16* xp = i16any_ptr(x); i32 c=wi; for (usz i=0; i<ia; i++) if (addOn(c,xp[i]))goto base; dec(x); return m_i32(c); }
|
||||
if (xe==el_i32) { i32* xp = i32any_ptr(x); i32 c=wi; for (usz i=0; i<ia; i++) if (addOn(c,xp[i]))goto base; dec(x); return m_i32(c); }
|
||||
}
|
||||
if (rtid==2 | rtid==10) { // ×/∧
|
||||
if (xe==el_i8 ) { i8* xp = i8any_ptr (x); i32 c=wi; for (usz i=0; i<ia; i++) if (__builtin_mul_overflow(c,xp[i],&c))goto base; dec(x); return m_i32(c); }
|
||||
if (xe==el_i16) { i16* xp = i16any_ptr(x); i32 c=wi; for (usz i=0; i<ia; i++) if (__builtin_mul_overflow(c,xp[i],&c))goto base; dec(x); return m_i32(c); }
|
||||
if (xe==el_i32) { i32* xp = i32any_ptr(x); i32 c=wi; for (usz i=0; i<ia; i++) if (__builtin_mul_overflow(c,xp[i],&c))goto base; dec(x); return m_i32(c); }
|
||||
if (xe==el_i8 ) { i8* xp = i8any_ptr (x); i32 c=wi; for (usz i=0; i<ia; i++) if (mulOn(c,xp[i]))goto base; dec(x); return m_i32(c); }
|
||||
if (xe==el_i16) { i16* xp = i16any_ptr(x); i32 c=wi; for (usz i=0; i<ia; i++) if (mulOn(c,xp[i]))goto base; dec(x); return m_i32(c); }
|
||||
if (xe==el_i32) { i32* xp = i32any_ptr(x); i32 c=wi; for (usz i=0; i<ia; i++) if (mulOn(c,xp[i]))goto base; dec(x); return m_i32(c); }
|
||||
}
|
||||
if (rtid==6) { // ⌊
|
||||
if (xe==el_i8 ) { i8* xp = i8any_ptr (x); i32 c=wi; for (usz i=0; i<ia; i++) if (xp[i]<c) c=xp[i]; dec(x); return m_i32(c); }
|
||||
|
||||
@ -92,7 +92,7 @@ B shape_c2(B t, B w, B x) {
|
||||
bool bad=false, good=false;
|
||||
for (i32 i = 0; i < nr; i++) {
|
||||
if (wi[i]<0) thrF("⥊: 𝕨 contained %i", wi[i]);
|
||||
bad|= uszMul(&nia, wi[i]);
|
||||
bad|= mulOn(nia, wi[i]);
|
||||
good|= wi[i]==0;
|
||||
}
|
||||
if (bad && !good) thrM("⥊: 𝕨 too large");
|
||||
@ -106,7 +106,7 @@ B shape_c2(B t, B w, B x) {
|
||||
if (isF64(c)) {
|
||||
usz v = o2s(c);
|
||||
if (sh) sh->a[i] = v;
|
||||
bad|= uszMul(&nia, v);
|
||||
bad|= mulOn(nia, v);
|
||||
good|= v==0;
|
||||
} else {
|
||||
if (isArr(c) || !isVal(c)) thrM("⥊: 𝕨 must consist of natural numbers or ∘ ⌊ ⌽ ↑");
|
||||
@ -136,7 +136,7 @@ B shape_c2(B t, B w, B x) {
|
||||
fill = true;
|
||||
} else UD;
|
||||
if (sh) sh->a[unkPos] = item;
|
||||
nia = uszMulT(nia, item);
|
||||
nia = uszMul(nia, item);
|
||||
if (fill) {
|
||||
if (!isArr(x)) x = m_atomUnit(x);
|
||||
Arr* a = take_impl(nia, x);
|
||||
|
||||
@ -96,11 +96,11 @@ B m_vec2(B a, B b);
|
||||
|
||||
// random stuff
|
||||
|
||||
static bool uszMul(usz* c, usz n) {
|
||||
return __builtin_mul_overflow(*c, n, c);
|
||||
}
|
||||
static usz uszMulT(usz a, usz b) {
|
||||
if (uszMul(&a, b)) thrM("Size too large");
|
||||
#define addOn(V,X) ({ AUTO v_ = &(V); __builtin_add_overflow(*v_, X, v_); })
|
||||
#define mulOn(V,X) ({ AUTO v_ = &(V); __builtin_mul_overflow(*v_, X, v_); })
|
||||
|
||||
static usz uszMul(usz a, usz b) {
|
||||
if (mulOn(a, b)) thrM("Size too large");
|
||||
return a;
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user