diff --git a/src/core/c32arr.h b/src/core/c32arr.h index bf9aff94..d5bc7af6 100644 --- a/src/core/c32arr.h +++ b/src/core/c32arr.h @@ -9,16 +9,16 @@ typedef struct C32Slice { static B m_c32arrv(u32** p, usz ia) { - C32Arr* r = mm_allocN(fsizeof(C32Arr,a,u32,ia), t_c32arr); B rb = tag(r, ARR_TAG); + C32Arr* r = mm_allocN(fsizeof(C32Arr,a,u32,ia), t_c32arr); *p = r->a; - arr_shVec(rb, ia); - return rb; + arrP_shVec((Arr*)r, ia); + return tag(r, ARR_TAG); } static B m_c32arrc(u32** p, B x) { assert(isArr(x)); - C32Arr* r = mm_allocN(fsizeof(C32Arr,a,u32,a(x)->ia), t_c32arr); B rb = tag(r, ARR_TAG); + C32Arr* r = mm_allocN(fsizeof(C32Arr,a,u32,a(x)->ia), t_c32arr); *p = r->a; - arr_shCopy(rb, x); - return rb; + arrP_shCopy((Arr*)r, x); + return tag(r, ARR_TAG); } static B m_c32arrp(u32** p, usz ia) { // doesn't write shape/rank C32Arr* r = mm_allocN(fsizeof(C32Arr,a,u32,ia), t_c32arr); diff --git a/src/core/f64arr.h b/src/core/f64arr.h index db3e2a05..7148df53 100644 --- a/src/core/f64arr.h +++ b/src/core/f64arr.h @@ -9,16 +9,16 @@ typedef struct F64Slice { static B m_f64arrv(f64** p, usz ia) { - F64Arr* r = mm_allocN(fsizeof(F64Arr,a,f64,ia), t_f64arr); B rb = tag(r, ARR_TAG); + F64Arr* r = mm_allocN(fsizeof(F64Arr,a,f64,ia), t_f64arr); *p = r->a; - arr_shVec(rb, ia); - return rb; + arrP_shVec((Arr*)r, ia); + return tag(r, ARR_TAG); } static B m_f64arrc(f64** p, B x) { assert(isArr(x)); - F64Arr* r = mm_allocN(fsizeof(F64Arr,a,f64,a(x)->ia), t_f64arr); B rb = tag(r, ARR_TAG); + F64Arr* r = mm_allocN(fsizeof(F64Arr,a,f64,a(x)->ia), t_f64arr); *p = r->a; - arr_shCopy(rb, x); - return rb; + arrP_shCopy((Arr*)r, x); + return tag(r, ARR_TAG); } static B m_f64arrp(f64** p, usz ia) { // doesn't write shape/rank F64Arr* r = mm_allocN(fsizeof(F64Arr,a,f64,ia), t_f64arr); diff --git a/src/core/fillarr.c b/src/core/fillarr.c index 45d4b82d..f2fcd4d8 100644 --- a/src/core/fillarr.c +++ b/src/core/fillarr.c @@ -194,12 +194,12 @@ B withFill(B x, B fill) { // consumes both return r; } base:; - B r = m_arr(fsizeof(FillArr,a,B,ia), t_fillarr); - arr_shCopy(r, x); - c(FillArr,r)->fill = fill; - B* a = c(FillArr,r)->a; + FillArr* r = mm_allocN(fsizeof(FillArr,a,B,ia), t_fillarr); + arrP_shCopy((Arr*)r, x); + r->fill = fill; + B* a = r->a; BS2B xget = TI(x).get; for (usz i = 0; i < ia; i++) a[i] = xget(x,i); dec(x); - return r; + return tag(r, ARR_TAG); } \ No newline at end of file diff --git a/src/core/fillarr.h b/src/core/fillarr.h index 40b53ff5..f2d888a5 100644 --- a/src/core/fillarr.h +++ b/src/core/fillarr.h @@ -60,7 +60,7 @@ static B getFillE(B x) { // errors if there's no fill static B m_fillarrp(usz ia) { // doesn't set ia - return m_arr(fsizeof(FillArr,a,B,ia), t_fillarr); + return tag(mm_allocN(fsizeof(FillArr,a,B,ia), t_fillarr), ARR_TAG); } static void fillarr_setFill(B x, B fill) { // consumes fill c(FillArr, x)->fill = fill; @@ -84,11 +84,11 @@ static B m_unit(B x) { r.a[0] = x; return r.b; } - B r = m_arr(fsizeof(FillArr,a,B,1), t_fillarr); - arr_shAllocI(r, 1, 0); - c(FillArr,r)->fill = xf; - c(FillArr,r)->a[0] = x; - return r; + FillArr* r = mm_allocN(fsizeof(FillArr,a,B,1), t_fillarr); + arrP_shAllocI((Arr*)r, 1, 0); + r->fill = xf; + r->a[0] = x; + return tag(r, ARR_TAG); } static B m_atomUnit(B x) { diff --git a/src/core/harr.h b/src/core/harr.h index dfaad060..39b6e7b6 100644 --- a/src/core/harr.h +++ b/src/core/harr.h @@ -17,15 +17,19 @@ static inline HArr_p harr_parts(B b) { HArr* p = c(HArr,b); return (HArr_p){.b = b, .a = p->a, .c = p}; } +static inline HArr_p harrP_parts(HArr* p) { + return (HArr_p){.b = tag(p,ARR_TAG), .a = p->a, .c = p}; +} NOINLINE void harr_pfree(B x, usz am); // am - item after last written static HArr_p m_harrs(usz ia, usz* ctr) { // writes just ia - B r = m_arr(fsizeof(HArr,a,B,ia), t_harrPartial); - a(r)->ia = ia; - a(r)->sh = ctr; - gsAdd(r); - return harr_parts(r); + HArr* r = mm_allocN(fsizeof(HArr,a,B,ia), t_harrPartial); + r->ia = ia; + r->sh = ctr; + HArr_p rp = harrP_parts(r); + gsAdd(rp.b); + return rp; } static B harr_fv(HArr_p p) { VTY(p.b, t_harrPartial); assert(p.c->ia == *p.c->sh); @@ -61,19 +65,19 @@ static void harr_abandon(HArr_p p) { VTY(p.b, t_harrPartial); } static HArr_p m_harrUv(usz ia) { - B r = m_arr(fsizeof(HArr,a,B,ia), t_harr); - arr_shVec(r, ia); - return harr_parts(r); + HArr* r = mm_allocN(fsizeof(HArr,a,B,ia), t_harr); + arrP_shVec((Arr*)r, ia); + return harrP_parts(r); } static HArr_p m_harrUc(B x) { assert(isArr(x)); - B r = m_arr(fsizeof(HArr,a,B,a(x)->ia), t_harr); - arr_shCopy(r, x); - return harr_parts(r); + HArr* r = mm_allocN(fsizeof(HArr,a,B,a(x)->ia), t_harr); + arrP_shCopy((Arr*)r, x); + return harrP_parts(r); } static HArr_p m_harrUp(usz ia) { // doesn't write shape/rank - B r = m_arr(fsizeof(HArr,a,B,ia), t_harr); - a(r)->ia = ia; - return harr_parts(r); + HArr* r = mm_allocN(fsizeof(HArr,a,B,ia), t_harr); + r->ia = ia; + return harrP_parts(r); } static B m_hunit(B x) { diff --git a/src/core/i32arr.h b/src/core/i32arr.h index 459a3626..4a86a694 100644 --- a/src/core/i32arr.h +++ b/src/core/i32arr.h @@ -9,16 +9,16 @@ typedef struct I32Slice { static B m_i32arrv(i32** p, usz ia) { - I32Arr* r = mm_allocN(fsizeof(I32Arr,a,i32,ia), t_i32arr); B rb = tag(r, ARR_TAG); + I32Arr* r = mm_allocN(fsizeof(I32Arr,a,i32,ia), t_i32arr); *p = r->a; - arr_shVec(rb, ia); - return rb; + arrP_shVec((Arr*)r, ia); + return tag(r, ARR_TAG); } static B m_i32arrc(i32** p, B x) { assert(isArr(x)); - I32Arr* r = mm_allocN(fsizeof(I32Arr,a,i32,a(x)->ia), t_i32arr); B rb = tag(r, ARR_TAG); + I32Arr* r = mm_allocN(fsizeof(I32Arr,a,i32,a(x)->ia), t_i32arr); *p = r->a; - arr_shCopy(rb, x); - return rb; + arrP_shCopy((Arr*)r, x); + return tag(r, ARR_TAG); } static B m_i32arrp(i32** p, usz ia) { // doesn't write shape/rank I32Arr* r = mm_allocN(fsizeof(I32Arr,a,i32,ia), t_i32arr); diff --git a/src/core/stuff.h b/src/core/stuff.h index 68940cda..95125f02 100644 --- a/src/core/stuff.h +++ b/src/core/stuff.h @@ -34,15 +34,17 @@ static void decSh(Value* x) { if (prnk(x)>1) ptr_dec(shObjP(x)); } // some array stuff #define WRAP(X,IA,MSG) ({ i64 wV=(i64)(X); u64 iaW=(IA); if(RARE((u64)wV >= iaW)) { if(wV<0) wV+= iaW; if((u64)wV >= iaW) {MSG;} }; (usz)wV; }) + +static ShArr* m_shArr(ur r) { + assert(r>1); + return ((ShArr*)mm_allocN(fsizeof(ShArr, a, usz, r), t_shape)); +} + static void arr_shVec(B x, usz ia) { a(x)->ia = ia; srnk(x, 1); a(x)->sh = &a(x)->ia; } -static ShArr* m_shArr(ur r) { - assert(r>1); - return ((ShArr*)mm_allocN(fsizeof(ShArr, a, usz, r), t_shape)); -} static usz* arr_shAllocR(B x, ur r) { // allocates shape, sets rank srnk(x,r); if (r>1) return a(x)->sh = m_shArr(r)->a; @@ -70,6 +72,40 @@ static void arr_shCopy(B n, B o) { // copy shape,rank,ia from o to n a(n)->sh = a(o)->sh; } } + +static void arrP_shVec(Arr* x, usz ia) { + x->ia = ia; + sprnk(x, 1); + x->sh = &x->ia; +} +static usz* arrP_shAllocR(Arr* x, ur r) { // allocates shape, sets rank + sprnk(x,r); + if (r>1) return x->sh = m_shArr(r)->a; + x->sh = &x->ia; + return 0; +} +static usz* arrP_shAllocI(Arr* x, usz ia, ur r) { // allocates shape, sets ia,rank + x->ia = ia; + return arrP_shAllocR(x, r); +} +static void arrP_shSetI(Arr* x, usz ia, ur r, ShArr* sh) { + sprnk(x,r); + x->ia = ia; + if (r>1) { x->sh = sh->a; ptr_inc(sh); } + else { x->sh = &x->ia; } +} +static void arrP_shCopy(Arr* n, B o) { // copy shape,rank,ia from o to n + assert(isArr(o)); + n->ia = a(o)->ia; + ur r = sprnk(n,rnk(o)); + if (r<=1) { + n->sh = &n->ia; + } else { + ptr_inc(shObj(o)); + n->sh = a(o)->sh; + } +} + static usz arr_csz(B x) { ur xr = rnk(x); if (xr<=1) return 1; @@ -90,7 +126,6 @@ static bool eqShape(B w, B x) { assert(isArr(w)); assert(isArr(x)); } -static B m_arr(usz min, u8 type) { return mm_alloc(min, type, ftag(ARR_TAG)); } static B m_v1(B a ); // consumes all static B m_v2(B a, B b ); // consumes all static B m_v3(B a, B b, B c ); // consumes all