From 0b678bc2741c2da531c1ba7270f2f317cf44fd91 Mon Sep 17 00:00:00 2001 From: dzaima Date: Sun, 8 Jun 2025 21:40:39 +0300 Subject: [PATCH] move some fill/eltype array creation helpers into core includes --- src/builtins/md1.c | 4 ++-- src/builtins/select.c | 4 ++-- src/builtins/sfns.c | 4 ++-- src/core/fillarr.c | 33 ++++++++++++++++----------------- src/core/fillarr.h | 10 ++++++++++ src/core/stuff.h | 9 ++++++++- src/utils/mut.c | 18 +++++------------- src/utils/mut.h | 4 ---- 8 files changed, 45 insertions(+), 41 deletions(-) diff --git a/src/builtins/md1.c b/src/builtins/md1.c index 324ede74..079a5dc3 100644 --- a/src/builtins/md1.c +++ b/src/builtins/md1.c @@ -100,8 +100,8 @@ B tbl_c2(Md1D* d, B w, B x) { B f = d->f; B expW, expX; if (0) { arith_empty:; - expW = taga(emptyArr(w, 1)); - expX = taga(emptyArr(x, 1)); + expW = taga(emptyVec(w)); + expX = taga(emptyVec(x)); } else { assert(wia>1); // implies ria > xia, a requirement of reshape_cycle expW = replicate_by(xia, wia, w); diff --git a/src/builtins/select.c b/src/builtins/select.c index cd41a664..b97c8435 100644 --- a/src/builtins/select.c +++ b/src/builtins/select.c @@ -959,12 +959,12 @@ B select_rows_B(B x, ux csz, ux cam, B inds) { // consumes inds,x; ⥊ inds⊸ assert(csz*cam == IA(x)); if (csz==0) goto generic; if (cam<=1) { - if (cam==0) return taga(emptyArr(x, 1)); + if (cam==0) return taga(emptyVec(x)); return C2(select, inds, taga(arr_shVec(TI(x,slice)(x, 0, IA(x))))); } ux in = IA(inds); - if (in == 0) return taga(emptyArr(x, 1)); + if (in == 0) return taga(emptyVec(x)); if (in == 1) { B w = IGetU(inds,0); if (!isF64(w)) goto generic; B r = select_cells_single(WRAP_SELECT_ONE(o2i64(w), csz, "%R", w), x, cam, csz, 1); diff --git a/src/builtins/sfns.c b/src/builtins/sfns.c index a0dee4be..2dc43f3d 100644 --- a/src/builtins/sfns.c +++ b/src/builtins/sfns.c @@ -101,7 +101,7 @@ static Arr* take_head(usz ria, B x) { // consumes; returns ria↑x with unset sh try_copy:; // if (used > 64) goto base; - UntaggedArr r = m_arrp_fill(x, ria); + UntaggedArr r = m_arrp_copyFill(x, ria); COPY_TO(r.data, TI(x,elType), 0, x, 0, ria); NOGC_E; decG(x); @@ -358,7 +358,7 @@ Arr* reshape_cycle(usz nia, usz xia, B x) { // used directly by tbl_c2 if (bi == 1) { memset(rp, rp[0], bf); bi=bf; } } else { if (TI(x,elType) == el_B) { - UntaggedArr r = m_barrp_fill(x, nia); + UntaggedArr r = m_barrp_copyFill(x, nia); i64 div = nia/xia; i64 mod = nia%xia; for (i64 i = 0; i < div; i++) COPY_TO(r.data, el_B, i*xia, x, 0, xia); diff --git a/src/core/fillarr.c b/src/core/fillarr.c index 5770e5e4..273aab07 100644 --- a/src/core/fillarr.c +++ b/src/core/fillarr.c @@ -202,27 +202,27 @@ FORCE_INLINE B m_oneItemArr(B x, ur rr) { NOINLINE B m_unit(B x) { return m_oneItemArr(x, 0); } NOINLINE B m_vec1(B x) { return m_oneItemArr(x, 1); } - -NOINLINE Arr* emptyArr(B x, ur xr) { +Arr* emptyVec(B x) { assert(isArr(x)); u8 xe = TI(x,elType); - if (xr==1) { - if (elNum(xe)) goto numVec; - if (elChr(xe)) goto chrVec; - assert(xe == el_B); - } + if (elNum(xe)) num: return a(emptyIVec()); + if (elChr(xe)) chr: return a(emptyCVec()); + assert(xe == el_B); + B xf = getFillR(x); + if (numFill(xf)) goto num; + if (chrFill(xf)) goto chr; + if ( noFill(xf)) return a(emptyHVec()); + return arr_shVec(m_fillarrpEmpty(xf)); +} + +NOINLINE Arr* emptyArr(B x, ur xr) { + assert(isArr(x) && xr>=1); + if (xr==1) return emptyVec(x); B xf = getFillR(x); - if (xr==1) { - if (numFill(xf)) numVec: return a(emptyIVec()); - if ( noFill(xf)) return a(emptyHVec()); - if (chrFill(xf)) chrVec: return a(emptyCVec()); - } Arr* r; if (numFill(xf)) { u64* rp; r = m_bitarrp(&rp, 0); } - else if ( noFill(xf)) { r = (Arr*) m_harrUp(0).c; } else if (chrFill(xf)) { u8* rp; r = m_c8arrp(&rp, 0); } - else { r = m_fillarrpEmpty(xf); } - if (xr<=1) arr_rnk01(r, xr); + else { r = m_barrp_withFill(0, xf).obj; } return r; } @@ -230,8 +230,7 @@ NOINLINE Arr* emptyWithFill(B fill) { u8 type; if (numFill(fill)) { type = t_bitarr; goto tyarr; } if (chrFill(fill)) { type = t_c8arr; goto tyarr; } - if ( noFill(fill)) return (Arr*) m_harrUp(0).c; - return m_fillarrpEmpty(fill); + return m_barrp_withFill(0, fill).obj; tyarr:; Arr* r; diff --git a/src/core/fillarr.h b/src/core/fillarr.h index 9d39e607..d209ec97 100644 --- a/src/core/fillarr.h +++ b/src/core/fillarr.h @@ -105,6 +105,16 @@ static Arr* m_fillarr0p(usz ia) { // zero-initialized fillarr, with both fill & return r; } +static UntaggedArr m_barrp_withFill(ux ia, B fill) { // doesn't consume + CHECK_IA(ia, sizeof(B)); + bool has = !noFill(fill); + Arr* r = m_arr(has? fsizeof(FillArr,a,B,ia) : fsizeof(HArr,a,B,ia), has? t_fillarr : t_harr, ia); + if (has) fillarr_setFill(r, fill); + if (ia) NOGC_S; + return (UntaggedArr){r, has? fillarrv_ptr(r) : harrv_ptr(r)}; +} + + B m_funit(B x); // consumes B m_unit(B x); // consumes diff --git a/src/core/stuff.h b/src/core/stuff.h index 2cd21b6e..13ce7d82 100644 --- a/src/core/stuff.h +++ b/src/core/stuff.h @@ -213,7 +213,14 @@ void bitwiden(void* rp, ux rcsz, void* xp, ux xcsz, ux cam); Arr* customizeShape(B x); // consumes; returns new array with unset shape Arr* cpyWithShape(B x); // consumes; returns new array with the same shape as x (SH(x) will be dangling, PSH(result) must be used to access it) -Arr* emptyArr(B x, ur xr); // doesn't consume; returns an empty array with the same fill as x; if xr>1, shape must be set +Arr* emptyArr(B x, ur xr); // doesn't consume; returns an empty array with the same fill as x; if xr>1, shape must be set; else, x may have refc>1 +Arr* emptyVec(B x); // doesn't consume; emptyArr(x, 1) + +typedef struct { Arr* obj; void* data; } UntaggedArr; +UntaggedArr m_arrp_copyFill(B x, ux ia); // doesn't consume; create new array with the fill and eltype of x +UntaggedArr m_barrp_copyFill(B x, ux ia); // doesn't consume; create new fillarr or harr with the fill of x +static UntaggedArr m_barrp_withFill(ux ia, B fill); // doesn't consume; create new fillarr or harr with the specified fill + NOINLINE Arr* emptyWithFill(B fill); // consumes; returns new array with unset shape and the specified fill B emptyNumsWithShape(B x); // consumes; empty bitarr with shape ≢x B emptyChrsWithShape(B x); // consumes; empty c8arr with shape ≢x diff --git a/src/utils/mut.c b/src/utils/mut.c index ff440b17..7cfc38e9 100644 --- a/src/utils/mut.c +++ b/src/utils/mut.c @@ -832,21 +832,13 @@ DirectArr toEltypeArr(B x, u8 re) { // consumes -UntaggedArr m_barrp_fill(B x, ux ia) { // doesn't consume - B fill = getFillR(x); - if (noFill(fill)) { - HArr_p r = m_harrUp(ia); - return (UntaggedArr) {(Arr*)r.c, r.a}; - } else { - Arr* r = m_fillarrp(ia); - fillarr_setFill(r, fill); - return (UntaggedArr){r, fillarrv_ptr(r)}; - } +UntaggedArr m_barrp_copyFill(B x, ux ia) { // doesn't consume + return m_barrp_withFill(ia, getFillR(x)); } - -UntaggedArr m_arrp_fill(B x, ux ia) { // doesn't consume + +UntaggedArr m_arrp_copyFill(B x, ux ia) { // doesn't consume u8 xe = TI(x,elType); - if (xe==el_B) return m_barrp_fill(x, ia); + if (xe==el_B) return m_barrp_copyFill(x, ia); Arr* r; void* rp = m_tyarrlbp(&r, elwBitLog(xe), ia, el2t(xe)); return (UntaggedArr) {r, rp}; diff --git a/src/utils/mut.h b/src/utils/mut.h index 0bc5663a..affaec01 100644 --- a/src/utils/mut.h +++ b/src/utils/mut.h @@ -297,10 +297,6 @@ DirectArr toEltypeArr(B x, u8 re); // consumes // Otherwise, functionality is the same as if a regular new array was made (i.e. uninitialized elements, may start NOGC) DirectArr potentiallyReuse(B x); // doesn't consume -typedef struct { Arr* obj; void* data; } UntaggedArr; -UntaggedArr m_barrp_fill(B x, ux ia); // doesn't consume; create new fillarr or harr with the fill of x -UntaggedArr m_arrp_fill(B x, ux ia); // doesn't consume; create new array with the fill and eltype of x - typedef struct { B res; void* rp;