more typed array conversion

This commit is contained in:
dzaima 2021-09-11 22:45:37 +03:00
parent 0df5538a2a
commit e98660bfa8
8 changed files with 107 additions and 101 deletions

View File

@ -8,7 +8,7 @@
static B homFil1(B f, B r, B xf) {
assert(EACH_FILLS);
if (isPureFn(f)) {
if (f.u==bi_eq.u || f.u==bi_ne.u || f.u==bi_feq.u) { dec(xf); return taga(toI32Arr(r)); } // ≠ may return ≥2⋆31, but whatever, this thing is stupid anyway
if (f.u==bi_eq.u || f.u==bi_ne.u || f.u==bi_feq.u) { dec(xf); return toI32Any(r); } // ≠ may return ≥2⋆31, but whatever, this thing is stupid anyway
if (f.u==bi_fne.u) { dec(xf); return withFill(r, m_harrUv(0).b); }
if (!noFill(xf)) {
if (CATCH) { dec(catchMessage); return r; }
@ -23,7 +23,7 @@ static B homFil1(B f, B r, B xf) {
static B homFil2(B f, B r, B wf, B xf) {
assert(EACH_FILLS);
if (isPureFn(f)) {
if (f.u==bi_feq.u || f.u==bi_fne.u) { dec(wf); dec(xf); return taga(toI32Arr(r)); }
if (f.u==bi_feq.u || f.u==bi_fne.u) { dec(wf); dec(xf); return toI32Any(r); }
if (!noFill(wf) && !noFill(xf)) {
if (CATCH) { dec(catchMessage); return r; }
B rf = asFill(c2(f, wf, xf));

View File

@ -2,34 +2,18 @@ B m_str8(usz sz, char* s);
B m_str8l(char* s);
B m_str32(u32* s);
static C8Arr* toC8Arr(B x) {
if (v(x)->type==t_c8arr) return c(C8Arr,x);
u8* rp; B r = m_c8arrc(&rp, x);
usz ia = a(r)->ia;
SGetU(x)
for (usz i = 0; i < ia; i++) rp[i] = o2cu(GetU(x,i));
dec(x);
return c(C8Arr,r);
}
static C16Arr* toC16Arr(B x) {
if (v(x)->type==t_c16arr) return c(C16Arr,x);
u16* rp; B r = m_c16arrc(&rp, x);
usz ia = a(r)->ia;
SGetU(x)
for (usz i = 0; i < ia; i++) rp[i] = o2cu(GetU(x,i));
dec(x);
return c(C16Arr,r);
}
static C32Arr* toC32Arr(B x) {
if (v(x)->type==t_c32arr) return c(C32Arr,x);
u32* rp; B r = m_c32arrc(&rp, x);
usz ia = a(r)->ia;
SGetU(x)
for (usz i = 0; i < ia; i++) rp[i] = o2cu(GetU(x,i));
dec(x);
return c(C32Arr,r);
}
C8Arr* cpyC8Arr (B x); // consumes
C16Arr* cpyC16Arr(B x); // consumes
C32Arr* cpyC32Arr(B x); // consumes
// all consume x
static C8Arr* toC8Arr (B x) { return v(x)->type==t_c8arr ? c(C8Arr, x) : cpyC8Arr (x); }
static C16Arr* toC16Arr(B x) { return v(x)->type==t_c16arr? c(C16Arr,x) : cpyC16Arr(x); }
static C32Arr* toC32Arr(B x) { return v(x)->type==t_c32arr? c(C32Arr,x) : cpyC32Arr(x); }
static B toC8Any (B x) { u8 t=v(x)->type; return t==t_c8arr || t==t_c8slice ? x : taga(cpyC8Arr (x)); }
static B toC16Any(B x) { u8 t=v(x)->type; return t==t_c16arr || t==t_c16slice? x : taga(cpyC16Arr(x)); }
static B toC32Any(B x) { u8 t=v(x)->type; return t==t_c32arr || t==t_c32slice? x : taga(cpyC32Arr(x)); }
static bool eqStr(B w, u32* x) {
if (isAtm(w) || rnk(w)!=1) return false;

View File

@ -61,13 +61,22 @@ B toKCells(B x, ur k) {
return r.b;
}
HArr* toHArr(B x) {
if (v(x)->type==t_harr) return c(HArr,x);
HArr* cpyHArr(B x) {
usz ia = a(x)->ia;
HArr_p r = m_harrUc(x);
usz ia = r.c->ia;
SGet(x)
for (usz i = 0; i < ia; i++) r.a[i] = Get(x,i);
u8 xe = TI(x,elType);
if (xe==el_i8 ) { i8* xp = i8any_ptr (x); for(usz i=0; i<ia; i++) r.a[i]=m_f64(xp[i]); }
else if (xe==el_i16) { i16* xp = i16any_ptr(x); for(usz i=0; i<ia; i++) r.a[i]=m_f64(xp[i]); }
else if (xe==el_i32) { i32* xp = i32any_ptr(x); for(usz i=0; i<ia; i++) r.a[i]=m_f64(xp[i]); }
else if (xe==el_f64) { f64* xp = f64any_ptr(x); for(usz i=0; i<ia; i++) r.a[i]=m_f64(xp[i]); }
else if (xe==el_c8 ) { u8* xp = c8any_ptr (x); for(usz i=0; i<ia; i++) r.a[i]=m_c32(xp[i]); }
else if (xe==el_c16) { u16* xp = c16any_ptr(x); for(usz i=0; i<ia; i++) r.a[i]=m_c32(xp[i]); }
else if (xe==el_c32) { u32* xp = c32any_ptr(x); for(usz i=0; i<ia; i++) r.a[i]=m_c32(xp[i]); }
else {
B* xp = arr_bptr(x);
if (xp!=NULL) { for (usz i=0; i<ia; i++) r.a[i] = inc(xp[i]); }
else { SGet(x) for (usz i=0; i<ia; i++) r.a[i] = Get(x, i); }
}
dec(x);
return r.c;
}

View File

@ -85,11 +85,11 @@ static B m_hunit(B x) {
return r.b;
}
static B* harr_ptr(B x) { VTY(x,t_harr); return c(HArr,x)->a; }
static B* hany_ptr(B x) { return v(x)->type==t_hslice? c(HSlice,x)->a : harr_ptr(x); }
HArr* toHArr(B x);
HArr* cpyHArr(B x);
static HArr* toHArr(B x) { return v(x)->type==t_harr? c(HArr,x) : cpyHArr(x); }
B m_caB(usz ia, B* a);
// consumes all

View File

@ -1,57 +1,19 @@
B m_cai32(usz ia, i32* a);
B m_caf64(usz sz, f64* a);
static I8Arr* toI8Arr(B x) { // assumes it's possible
if (v(x)->type==t_i8arr) return c(I8Arr,x);
i8* rp; B r = m_i8arrc(&rp, x);
usz ia = a(r)->ia;
if (TI(x,elType)==el_i32) {
i32* fp = i32any_ptr(x);
for (usz i = 0; i < ia; i++) rp[i] = (i8)fp[i];
} else {
SGetU(x)
for (usz i = 0; i < ia; i++) rp[i] = o2iu(GetU(x,i));
}
dec(x);
return c(I8Arr,r);
}
static I16Arr* toI16Arr(B x) { // assumes it's possible
if (v(x)->type==t_i16arr) return c(I16Arr,x);
i16* rp; B r = m_i16arrc(&rp, x);
usz ia = a(r)->ia;
if (TI(x,elType)==el_f64) {
f64* fp = f64any_ptr(x);
for (usz i = 0; i < ia; i++) rp[i] = (i16)fp[i];
} else {
SGetU(x)
for (usz i = 0; i < ia; i++) rp[i] = o2iu(GetU(x,i));
}
dec(x);
return c(I16Arr,r);
}
I8Arr* cpyI8Arr (B x); // consumes
I16Arr* cpyI16Arr(B x); // consumes
I32Arr* cpyI32Arr(B x); // consumes
F64Arr* cpyF64Arr(B x); // consumes
static I32Arr* toI32Arr(B x) { // assumes it's possible
if (v(x)->type==t_i32arr) return c(I32Arr,x);
i32* rp; B r = m_i32arrc(&rp, x);
usz ia = a(r)->ia;
if (TI(x,elType)==el_f64) {
f64* fp = f64any_ptr(x);
for (usz i = 0; i < ia; i++) rp[i] = (i32)fp[i];
} else {
SGetU(x)
for (usz i = 0; i < ia; i++) rp[i] = o2iu(GetU(x,i));
}
dec(x);
return c(I32Arr,r);
}
// all consume x
static I8Arr* toI8Arr (B x) { return v(x)->type==t_i8arr ? c(I8Arr, x) : cpyI8Arr (x); }
static I16Arr* toI16Arr(B x) { return v(x)->type==t_i16arr? c(I16Arr,x) : cpyI16Arr(x); }
static I32Arr* toI32Arr(B x) { return v(x)->type==t_i32arr? c(I32Arr,x) : cpyI32Arr(x); }
static F64Arr* toF64Arr(B x) { return v(x)->type==t_f64arr? c(F64Arr,x) : cpyF64Arr(x); }
static F64Arr* toF64Arr(B x) {
if (v(x)->type==t_f64arr) return c(F64Arr,x);
f64* rp; B r = m_f64arrc(&rp, x);
usz ia = a(r)->ia;
SGetU(x)
for (usz i = 0; i < ia; i++) rp[i] = o2f(GetU(x,i));
dec(x);
return c(F64Arr,r);
}
static B toI8Any (B x) { u8 t=v(x)->type; return t==t_i8arr || t==t_i8slice ? x : taga(cpyI8Arr (x)); }
static B toI16Any(B x) { u8 t=v(x)->type; return t==t_i16arr || t==t_i16slice? x : taga(cpyI16Arr(x)); }
static B toI32Any(B x) { u8 t=v(x)->type; return t==t_i32arr || t==t_i32slice? x : taga(cpyI32Arr(x)); }
static B toF64Any(B x) { u8 t=v(x)->type; return t==t_f64arr || t==t_f64slice? x : taga(cpyF64Arr(x)); }

View File

@ -200,7 +200,7 @@ NOINLINE B do_fmt(B s, char* p, va_list a) {
AFMT("%f", o2f(b));
} else { assert(isArr(b) && rnk(b)==1);
if (TI(b,elType)==el_c32) AJOIN(inc(b));
else AJOIN(taga(toC32Arr(inc(b))));
else AJOIN(chr_squeeze(inc(b)));
}
break;
}
@ -654,11 +654,14 @@ NOINLINE void printAllocStats() {
}
#ifdef DEBUG
#ifdef OBJ_COUNTER
#define PRINT_ID(X) printf("Object ID: "N64u"\n", (X)->uid)
#else
#define PRINT_ID(X)
#endif
NOINLINE Value* VALIDATEP(Value* x) {
if (x->refc<=0 || (x->refc>>28) == 'a' || x->type==t_empty) {
#ifdef OBJ_COUNTER
printf("Object ID: "N64u"\n", x->uid);
#endif
PRINT_ID(x);
printf("bad refcount for type %d: %d\nattempting to print: ", x->type, x->refc); fflush(stdout);
print(tag(x,OBJ_TAG)); putchar('\n'); fflush(stdout);
err("");
@ -675,6 +678,7 @@ NOINLINE void printAllocStats() {
VALIDATEP(v(x));
if(isArr(x)!=TI(x,isArr) && v(x)->type!=t_freed && v(x)->type!=t_harrPartial) {
printf("bad array tag/type: type=%d, obj=%p\n", v(x)->type, (void*)x.u);
PRINT_ID(v(x));
print(x);
err("\nk");
}

View File

@ -35,6 +35,53 @@ NOINLINE B m_str32(u32* s) {
return r;
}
#define MAKE_ICPY(T,E) T##Arr* cpy##T##Arr(B x) { \
usz ia = a(x)->ia; \
E* rp; Arr* r = m_##E##arrp(&rp, ia); \
arr_shCopy(r, x); \
u8 xe = TI(x,elType); \
if (xe==el_i8 ) { i8* xp = i8any_ptr (x); for(usz i=0; i<ia; i++) rp[i]=xp[i]; } \
else if (xe==el_i16) { i16* xp = i16any_ptr(x); for(usz i=0; i<ia; i++) rp[i]=xp[i]; } \
else if (xe==el_i32) { i32* xp = i32any_ptr(x); for(usz i=0; i<ia; i++) rp[i]=xp[i]; } \
else if (xe==el_f64) { f64* xp = f64any_ptr(x); for(usz i=0; i<ia; i++) rp[i]=xp[i]; } \
else { \
B* xp = arr_bptr(x); \
if (xp!=NULL) { for (usz i=0; i<ia; i++) rp[i]=o2fu(xp[i] ); } \
else { SGetU(x) for (usz i=0; i<ia; i++) rp[i]=o2fu(GetU(x,i)); } \
} \
dec(x); \
return (T##Arr*)r; \
}
MAKE_ICPY(I8, i8)
MAKE_ICPY(I16, i16)
MAKE_ICPY(I32, i32)
MAKE_ICPY(F64, f64)
#undef MAKE_ICPY
#define MAKE_CCPY(T,E) \
T##Arr* cpy##T##Arr(B x) { \
usz ia = a(x)->ia; \
T##Atom* rp; Arr* r = m_##E##arrp(&rp, ia); \
arr_shCopy(r, x); \
u8 xe = TI(x,elType); \
if (xe==el_c8 ) { u8* xp = c8any_ptr (x); for(usz i=0; i<ia; i++) rp[i]=xp[i]; } \
else if (xe==el_c16) { u16* xp = c16any_ptr(x); for(usz i=0; i<ia; i++) rp[i]=xp[i]; } \
else if (xe==el_c32) { u32* xp = c32any_ptr(x); for(usz i=0; i<ia; i++) rp[i]=xp[i]; } \
else { \
B* xp = arr_bptr(x); \
if (xp!=NULL) { for (usz i=0; i<ia; i++) rp[i]=o2cu(xp[i] ); } \
else { SGetU(x) for (usz i=0; i<ia; i++) rp[i]=o2cu(GetU(x,i)); } \
} \
dec(x); \
return (T##Arr*)r; \
}
MAKE_CCPY(C8, c8)
MAKE_CCPY(C16, c16)
MAKE_CCPY(C32, c32)
#undef MAKE_CCPY
void tyarr_init() {
i8arr_init(); i16arr_init(); i32arr_init();
c8arr_init(); c16arr_init(); c32arr_init(); f64arr_init();

View File

@ -21,14 +21,14 @@ NOINLINE void mut_to(Mut* m, u8 n) {
}
#endif
switch(n) { default: UD;
case el_i8: { I8Arr* t=toI8Arr (taga(m->val)); m->val=(Arr*)t; m->ai8 =t->a; return; }
case el_i16: { I16Arr* t=toI16Arr(taga(m->val)); m->val=(Arr*)t; m->ai16=t->a; return; }
case el_i32: { I32Arr* t=toI32Arr(taga(m->val)); m->val=(Arr*)t; m->ai32=t->a; return; }
case el_c8: { C8Arr* t=toC8Arr (taga(m->val)); m->val=(Arr*)t; m->ac8 =t->a; return; }
case el_c16: { C16Arr* t=toC16Arr(taga(m->val)); m->val=(Arr*)t; m->ac16=t->a; return; }
case el_c32: { C32Arr* t=toC32Arr(taga(m->val)); m->val=(Arr*)t; m->ac32=t->a; return; }
case el_f64: { F64Arr* t=toF64Arr(taga(m->val)); m->val=(Arr*)t; m->af64=t->a; return; }
case el_B : { HArr* t=toHArr (taga(m->val)); m->val=(Arr*)t; m->aB =t->a; return; }
case el_i8: { I8Arr* t=cpyI8Arr (taga(m->val)); m->val=(Arr*)t; m->ai8 =t->a; return; }
case el_i16: { I16Arr* t=cpyI16Arr(taga(m->val)); m->val=(Arr*)t; m->ai16=t->a; return; }
case el_i32: { I32Arr* t=cpyI32Arr(taga(m->val)); m->val=(Arr*)t; m->ai32=t->a; return; }
case el_c8: { C8Arr* t=cpyC8Arr (taga(m->val)); m->val=(Arr*)t; m->ac8 =t->a; return; }
case el_c16: { C16Arr* t=cpyC16Arr(taga(m->val)); m->val=(Arr*)t; m->ac16=t->a; return; }
case el_c32: { C32Arr* t=cpyC32Arr(taga(m->val)); m->val=(Arr*)t; m->ac32=t->a; return; }
case el_f64: { F64Arr* t=cpyF64Arr(taga(m->val)); m->val=(Arr*)t; m->af64=t->a; return; }
case el_B : { HArr* t=cpyHArr (taga(m->val)); m->val=(Arr*)t; m->aB =t->a; return; }
}
}
}