in-place c¨⌾(is⊸⊏)fillarr

+ use toEltypeArr for generic 𝔽⌾(is⊸⊏)𝕩; not "faster", but just nicer to work with
This commit is contained in:
dzaima 2025-05-23 23:47:12 +03:00
parent 124d094591
commit dd82d3fb54
3 changed files with 29 additions and 25 deletions

View File

@ -138,7 +138,7 @@ static NOINLINE DirectArr toFillArr(B x, B fill) {
return (DirectArr){taga(r), rp};
}
static void* reusableArr_ptr(Arr* t, u8 el) {
return el==el_B? (void*)harrv_ptr(t) : tyarrv_ptr((TyArr*)t);
return el==el_B? (void*)(PTY(t)==t_fillarr? fillarrv_ptr(t) : harrv_ptr(t)) : tyarrv_ptr((TyArr*)t);
}
static DirectArr toEltypeArr(B x, u8 re) { // consumes x; returns an array with eltype==re, with same shape/elements/fill as x, and its data pointer
assert(isArr(x));
@ -513,7 +513,7 @@ B select_replace(u32 chr, B w, B x, B rep, usz wia, usz cam, usz csz) { // consu
u8 we = TI(w,elType); assert(elNum(we) || wia==0);
u8 xe = TI(x,elType);
u8 re = el_or(xe, TI(rep,elType));
Arr* ra;
Arr* ra; B rb;
// w = taga(cpyF64Arr(w)); we = el_f64; // test the float path
if (we==el_f64) {
f64* wp = f64any_ptr(w);
@ -546,23 +546,23 @@ B select_replace(u32 chr, B w, B x, B rep, usz wia, usz cam, usz csz) { // consu
}
assert(elInt(we) || wia==0);
DirectArr r = toEltypeArr(x, re); rb = r.obj;
w = toI32Any(w);
i32* wp = i32any_ptr(w);
SPARSE_INIT(wp[i])
bool reuse = reusable(x) && re==reuseElType[TY(x)];
SLOWIF(!reuse && cam>100 && wia<cam/50) SLOW2("⌾(𝕨⊸⊏)𝕩 or ⌾(𝕨⊸⊑)𝕩 because not reusable", w, x);
switch (re) { default: UD;
case el_i8: rep = toI8Any(rep); ra = reuse? a(REUSE(x)) : cpyI8Arr(x); goto do_u8;
case el_c8: rep = toC8Any(rep); ra = reuse? a(REUSE(x)) : cpyC8Arr(x); goto do_u8;
case el_i16: rep = toI16Any(rep); ra = reuse? a(REUSE(x)) : cpyI16Arr(x); goto do_u16;
case el_c16: rep = toC16Any(rep); ra = reuse? a(REUSE(x)) : cpyC16Arr(x); goto do_u16;
case el_i32: rep = toI32Any(rep); ra = reuse? a(REUSE(x)) : cpyI32Arr(x); goto do_u32;
case el_c32: rep = toC32Any(rep); ra = reuse? a(REUSE(x)) : cpyC32Arr(x); goto do_u32;
case el_f64: rep = toF64Any(rep); ra = reuse? a(REUSE(x)) : cpyF64Arr(x); goto do_f64;
case el_bit: { ra = reuse? a(REUSE(x)) : cpyBitArr(x);
TyArr* na = toBitArr(rep); rep = taga(na);
u64* np = bitarrv_ptr(na);
u64* rp = bitarrv_ptr((TyArr*)ra);
case el_i8: rep = toI8Any(rep); goto do_u8;
case el_c8: rep = toC8Any(rep); goto do_u8;
case el_i16: rep = toI16Any(rep); goto do_u16;
case el_c16: rep = toC16Any(rep); goto do_u16;
case el_i32: rep = toI32Any(rep); goto do_u32;
case el_c32: rep = toC32Any(rep); goto do_u32;
case el_f64: rep = toF64Any(rep); goto do_f64;
case el_bit: {
assert(TI(rep,elType)==el_bit);
u64* np = bitarr_ptr(rep);
u64* rp = r.data;
if (csz==1) {
for (usz i = 0; i < wia; i++) {
READ_W(cw, i);
@ -577,11 +577,10 @@ B select_replace(u32 chr, B w, B x, B rep, usz wia, usz cam, usz csz) { // consu
COPY_TO(rp, el_bit, cw*csz, rep, i*csz, csz);
}
}
goto dec_ret_ra;
goto dec_ret_rb;
}
case el_B: {
ra = reuse? a(REUSE(x)) : cpyHArr(x);
B* rp = harrv_ptr(ra);
B* rp = r.data;
if (csz==1) {
SGet(rep)
for (usz i = 0; i < wia; i++) {
@ -600,13 +599,13 @@ B select_replace(u32 chr, B w, B x, B rep, usz wia, usz cam, usz csz) { // consu
COPY_TO(rp, el_B, cw*csz, rep, i*csz, csz);
}
}
goto dec_ret_ra;
goto dec_ret_rb;
}
}
#define IMPL(T, COMPATIBLE) do { \
if (csz!=1) goto do_tycell; \
T* rp = tyarrv_ptr((TyArr*)ra); \
T* rp = r.data; \
T* np = tyany_ptr(rep); \
for (usz i = 0; i < wia; i++) { \
READ_W(cw, i); \
@ -614,7 +613,7 @@ B select_replace(u32 chr, B w, B x, B rep, usz wia, usz cam, usz csz) { // consu
EQ1(!COMPATIBLE(cn, rp[cw])); \
rp[cw] = cn; \
} \
goto dec_ret_ra; \
goto dec_ret_rb; \
} while(0)
#define INT_EQ(A,B) ((A)==(B))
@ -627,7 +626,7 @@ B select_replace(u32 chr, B w, B x, B rep, usz wia, usz cam, usz csz) { // consu
do_tycell:;
u8 cwidth = csz * elWidth(re);
u8* rp = (u8*) tyarrv_ptr((TyArr*)ra);
u8* rp = r.data;
u8* np = tyany_ptr(rep);
MatchFnObj eq = MATCHR_GET(re,re);
for (usz i = 0; i < wia; i++) {
@ -635,14 +634,16 @@ B select_replace(u32 chr, B w, B x, B rep, usz wia, usz cam, usz csz) { // consu
EQ1(!MATCH_CALL(eq, rp + cw*cwidth, np + i*cwidth, csz));
COPY_TO(rp, re, cw*csz, rep, i*csz, csz);
}
goto dec_ret_ra;
goto dec_ret_rb;
dec_ret_ra:;
rb = taga(ra);
dec_ret_rb:;
decG(w); decG(rep);
FREE_CHECK;
return taga(ra);
return rb;
#undef SPARSE_INIT
#undef EQ

View File

@ -46,7 +46,7 @@ static void* harr_tyarr_ptr(Arr* t, u8 el) {
INIT_GLOBAL u8 reuseElType[t_COUNT];
void mut_init_copy(Mut* m, B x, u8 el) {
assert(m->fns == &mutFns[el_MAX]);
if (reusable(x) && reuseElType[TY(x)]==el) {
if (reusable(x) && reuseElType[TY(x)]==el && TY(x)!=t_fillarr) { // reuseElType is currently primarily used for toEltypeArr; currently just leaving this special-cased until it's decided what to do with this
m->fns = &mutFns[el];
Arr* a = m->val = a(REUSE(x));
m->a = harr_tyarr_ptr(a, el);
@ -834,6 +834,7 @@ void mutF_init(void) {
mutFns[el_c32].elType = el_c32; mutFns[el_c32].valType = t_c32arr; reuseElType[t_c32arr] = el_c32;
mutFns[el_f64].elType = el_f64; mutFns[el_f64].valType = t_f64arr; reuseElType[t_f64arr] = el_f64;
mutFns[el_B ].elType = el_B ; mutFns[el_B ].valType = t_harr; reuseElType[t_harr] = el_B;
/* and fillarr also to complete being able to reuse any target */ reuseElType[t_fillarr]= el_B;
mutFns[el_MAX].elType = el_MAX; mutFns[el_MAX].valType = t_COUNT;
for (u8 i = 0; i < el_MAX; i++) copyFns[i] = mutFns[i].m_copyG;
for (u8 i = 0; i < el_MAX; i++) fillFns[i] = mutFns[i].m_fillG;

View File

@ -33,6 +33,8 @@
%USE fastone a¨5+1e6 n{𝕊: a 1+(133¯1)}_fastone 4 ! (¨5678910000001000001100000210000031000004+0n0n00000n) (5a)¯5a %!DEBUG
%USE fastone a5+1e6 n{𝕊: a 1+(133¯1)}_fastone 4 ! ( 5678910000001000001100000210000031000004+0n0n00000n) (5a)¯5a %!DEBUG
%USE fastone a5+1e6@+10000 n{𝕊: a 1+(133¯1)}_fastone 4 ! (@+567891000010001100021000310004 +0n0n00000n) (5a)¯5a %!DEBUG
%USE fastone a5+1e6@+10000 n{𝕊: a'?'¨(133¯1)}_fastone 4 ! (@+567891000010001100021000310004{𝕩?63; 𝕨}¨0101000001) (5a)¯5a %!DEBUG
%USE fastone a5+1e6@+10000 n{𝕊: a 99¨(133¯1)}_fastone 4 ! ( 567891000010001100021000310004{𝕩?99;@+𝕨}¨0101000001) (5a)¯5a %!DEBUG
# in-place ⌾(l⊸⊏) highrank
%USE fastone a22¨5+1e6 n{𝕊: a 1+(133¯1)}_fastone 4 ! a (n×1,3,¯1+a˜a) + 22¨5+1e6 %!DEBUG