return Arr* from slices
This commit is contained in:
parent
daa8294252
commit
c52a042eb4
@ -236,7 +236,7 @@ B indexOf_c2(B t, B w, B x) {
|
||||
i32* rp; Arr* r = m_i32arrp(&rp, 1);
|
||||
arrP_shAllocR(r, 0);
|
||||
rp[0] = res;
|
||||
return tag(r, ARR_TAG);
|
||||
return taga(r);
|
||||
} else if (rnk(x)==1) {
|
||||
usz wia = a(w)->ia;
|
||||
usz xia = a(x)->ia;
|
||||
|
||||
@ -189,11 +189,11 @@ B variation_c2(B t, B w, B x) {
|
||||
if (xe==el_i32) { i32* xp=i32any_ptr(x); for (usz i = 0; i < xia; i++) rp[i] = m_f64(xp[i]); }
|
||||
else if (xe==el_f64) { f64* xp=f64any_ptr(x); for (usz i = 0; i < xia; i++) rp[i] = m_f64(xp[i]); }
|
||||
else for (usz i = 0; i < xia; i++) rp[i] = xget(x,i);
|
||||
} else thrF("•internal.Variation: Bad type \"%R\"", tag(wc,ARR_TAG));
|
||||
} else thrF("•internal.Variation: Bad type \"%R\"", taga(wc));
|
||||
if (slice) {
|
||||
B slice = TI(res).slice(res, 0);
|
||||
arr_shCopy(slice, res);
|
||||
res = slice;
|
||||
Arr* slice = TI(res).slice(res, 0);
|
||||
arrP_shCopy(slice, res);
|
||||
res = taga(slice);
|
||||
}
|
||||
if (u32_get(&wp, wpE, U"Inc")) {
|
||||
if (!variation_refs.u) {
|
||||
|
||||
@ -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 tag(toI32Arr(r), ARR_TAG); } // ≠ 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 taga(toI32Arr(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 tag(toI32Arr(r), ARR_TAG); }
|
||||
if (f.u==bi_feq.u || f.u==bi_fne.u) { dec(wf); dec(xf); return taga(toI32Arr(r)); }
|
||||
if (!noFill(wf) && !noFill(xf)) {
|
||||
if (CATCH) { dec(catchMessage); return r; }
|
||||
B rf = asFill(c2(f, wf, xf));
|
||||
@ -343,13 +343,13 @@ B cell_c1(B d, B x) { B f = c(Md1D,d)->f;
|
||||
memcpy(csh->a, a(x)->sh+1, sizeof(usz)*cr);
|
||||
}
|
||||
usz i = 0;
|
||||
BS2B slice = TI(x).slice;
|
||||
BS2A slice = TI(x).slice;
|
||||
HArr_p r = m_harrs(cam, &i);
|
||||
usz p = 0;
|
||||
for (; i < cam; i++) {
|
||||
B s = slice(inc(x), p);
|
||||
arr_shSetI(s, csz, cr, csh);
|
||||
r.a[i] = c1(f, s);
|
||||
Arr* s = slice(inc(x), p);
|
||||
arrP_shSetI(s, csz, cr, csh);
|
||||
r.a[i] = c1(f, taga(s));
|
||||
p+= csz;
|
||||
}
|
||||
if (cr>1) ptr_dec(csh);
|
||||
|
||||
@ -13,9 +13,9 @@ B shape_c1(B t, B x) {
|
||||
arr_shVec(x, ia);
|
||||
return x;
|
||||
}
|
||||
B r = TI(x).slice(x, 0);
|
||||
arr_shVec(r, ia);
|
||||
return r;
|
||||
Arr* r = TI(x).slice(x, 0);
|
||||
arrP_shVec(r, ia);
|
||||
return taga(r);
|
||||
}
|
||||
B shape_c2(B t, B w, B x) {
|
||||
if (isAtm(x)) { dec(x); dec(w); thrM("⥊: Reshaping non-array"); }
|
||||
@ -25,10 +25,10 @@ B shape_c2(B t, B w, B x) {
|
||||
if (wia>UR_MAX) thrM("⥊: Result rank too large");
|
||||
ur nr = (ur)wia;
|
||||
usz nia = a(x)->ia;
|
||||
B r;
|
||||
if (reusable(x)) { r = x; decSh(v(x)); }
|
||||
else r = TI(x).slice(x, 0);
|
||||
usz* sh = arr_shAllocI(r, nia, nr);
|
||||
B r; Arr* ra;
|
||||
if (reusable(x)) { r = x; decSh(v(x)); ra = (Arr*)v(r); }
|
||||
else { ra = TI(x).slice(x, 0); r = taga(ra); }
|
||||
usz* sh = arrP_shAllocI(ra, nia, nr);
|
||||
if (sh) for (u32 i = 0; i < nr; i++) sh[i] = o2s(wget(w,i));
|
||||
dec(w);
|
||||
return r;
|
||||
@ -62,16 +62,16 @@ B select_c1(B t, B x) {
|
||||
ur xr = rnk(x);
|
||||
if (xr==0) thrM("⊏: Argument cannot be rank 0");
|
||||
if (a(x)->sh[0]==0) thrF("⊏: Argument shape cannot start with 0 (%H ≡ ≢𝕩)", x);
|
||||
B r = TI(x).slice(inc(x),0);
|
||||
usz* sh = arr_shAllocR(r, xr-1);
|
||||
Arr* r = TI(x).slice(inc(x),0);
|
||||
usz* sh = arrP_shAllocR(r, xr-1);
|
||||
usz ia = 1;
|
||||
for (i32 i = 1; i < xr; i++) {
|
||||
if (sh) sh[i-1] = a(x)->sh[i];
|
||||
ia*= a(x)->sh[i];
|
||||
}
|
||||
a(r)->ia = ia;
|
||||
r->ia = ia;
|
||||
dec(x);
|
||||
return r;
|
||||
return taga(r);
|
||||
}
|
||||
B select_c2(B t, B w, B x) {
|
||||
if (isAtm(x)) thrM("⊏: 𝕩 cannot be an atom");
|
||||
@ -81,11 +81,11 @@ B select_c2(B t, B w, B x) {
|
||||
usz csz = arr_csz(x);
|
||||
usz cam = a(x)->sh[0];
|
||||
usz wi = WRAP(o2i64(w), cam, thrF("⊏: Indexing out-of-bounds (𝕨≡%R, %s≡≠𝕩)", w, cam));
|
||||
B r = TI(x).slice(inc(x), wi*csz);
|
||||
usz* sh = arr_shAllocI(r, csz, xr-1);
|
||||
Arr* r = TI(x).slice(inc(x), wi*csz);
|
||||
usz* sh = arrP_shAllocI(r, csz, xr-1);
|
||||
if (sh) memcpy(sh, a(x)->sh+1, (xr-1)*sizeof(usz));
|
||||
dec(x);
|
||||
return r;
|
||||
return taga(r);
|
||||
}
|
||||
B xf = getFillQ(x);
|
||||
BS2B xget = TI(x).get;
|
||||
@ -306,9 +306,9 @@ B slash_c2(B t, B w, B x) {
|
||||
i32 wv = o2i(w);
|
||||
if (wv<=0) {
|
||||
if (wv<0) thrM("/: 𝕨 cannot be negative");
|
||||
B r = TI(x).slice(x, 0);
|
||||
arr_shVec(r,0);
|
||||
return r;
|
||||
Arr* r = TI(x).slice(x, 0);
|
||||
arrP_shVec(r,0);
|
||||
return taga(r);
|
||||
}
|
||||
usz ri = 0;
|
||||
if (TI(x).elType==el_i32) {
|
||||
@ -336,9 +336,9 @@ B slash_c2(B t, B w, B x) {
|
||||
|
||||
B slicev(B x, usz s, usz ia) {
|
||||
usz xia = a(x)->ia; assert(s+ia <= xia);
|
||||
B r = TI(x).slice(x, s);
|
||||
arr_shVec(r, ia);
|
||||
return r;
|
||||
Arr* r = TI(x).slice(x, s);
|
||||
arrP_shVec(r, ia);
|
||||
return taga(r);
|
||||
}
|
||||
extern B rt_take, rt_drop;
|
||||
B take_c1(B t, B x) { return c1(rt_take, x); }
|
||||
@ -489,11 +489,11 @@ B couple_c1(B t, B x) {
|
||||
if (isArr(x)) {
|
||||
usz rr = rnk(x);
|
||||
usz ia = a(x)->ia;
|
||||
B r = TI(x).slice(inc(x),0);
|
||||
usz* sh = arr_shAllocI(r, ia, rr+1);
|
||||
Arr* r = TI(x).slice(inc(x),0);
|
||||
usz* sh = arrP_shAllocI(r, ia, rr+1);
|
||||
if (sh) { sh[0] = 1; memcpy(sh+1, a(x)->sh, rr*sizeof(usz)); }
|
||||
dec(x);
|
||||
return r;
|
||||
return taga(r);
|
||||
}
|
||||
if (q_i32(x)) { i32* rp; B r = m_i32arrv(&rp, 1); rp[0] = o2iu(x); return r; }
|
||||
if (isF64(x)) { f64* rp; B r = m_f64arrv(&rp, 1); rp[0] = o2fu(x); return r; }
|
||||
|
||||
@ -19,14 +19,14 @@ B m_str32(u32* s) {
|
||||
return r;
|
||||
}
|
||||
|
||||
static B m_c32slice(B p, u32* ptr) {
|
||||
static Arr* m_c32slice(B p, u32* ptr) {
|
||||
C32Slice* r = mm_alloc(sizeof(C32Slice), t_c32slice);
|
||||
r->p = p;
|
||||
r->a = ptr;
|
||||
return tag(r, ARR_TAG);
|
||||
return (Arr*)r;
|
||||
}
|
||||
static B c32arr_slice (B x, usz s) {return m_c32slice(x , c(C32Arr ,x)->a+s); }
|
||||
static B c32slice_slice(B x, usz s) { B r = m_c32slice(inc(c(Slice,x)->p), c(C32Slice,x)->a+s); dec(x); return r; }
|
||||
static Arr* c32arr_slice (B x, usz s) { return m_c32slice(x , c(C32Arr ,x)->a+s); }
|
||||
static Arr* c32slice_slice(B x, usz s) { Arr* r = m_c32slice(inc(c(Slice,x)->p), c(C32Slice,x)->a+s); dec(x); return r; }
|
||||
|
||||
static B c32arr_get (B x, usz n) { VTY(x,t_c32arr ); return m_c32(c(C32Arr ,x)->a[n]); }
|
||||
static B c32slice_get(B x, usz n) { VTY(x,t_c32slice); return m_c32(c(C32Slice,x)->a[n]); }
|
||||
|
||||
@ -12,13 +12,13 @@ static B m_c32arrv(u32** p, usz ia) {
|
||||
C32Arr* r = mm_alloc(fsizeof(C32Arr,a,u32,ia), t_c32arr);
|
||||
*p = r->a;
|
||||
arrP_shVec((Arr*)r, ia);
|
||||
return tag(r, ARR_TAG);
|
||||
return taga(r);
|
||||
}
|
||||
static B m_c32arrc(u32** p, B x) { assert(isArr(x));
|
||||
C32Arr* r = mm_alloc(fsizeof(C32Arr,a,u32,a(x)->ia), t_c32arr);
|
||||
*p = r->a;
|
||||
arrP_shCopy((Arr*)r, x);
|
||||
return tag(r, ARR_TAG);
|
||||
return taga(r);
|
||||
}
|
||||
static Arr* m_c32arrp(u32** p, usz ia) { // doesn't write shape/rank
|
||||
C32Arr* r = mm_alloc(fsizeof(C32Arr,a,u32,ia), t_c32arr);
|
||||
|
||||
@ -6,14 +6,14 @@ NOINLINE B m_caf64(usz sz, f64* a) {
|
||||
return r;
|
||||
}
|
||||
|
||||
static B m_f64slice(B p, f64* ptr) {
|
||||
static Arr* m_f64slice(B p, f64* ptr) {
|
||||
F64Slice* r = mm_alloc(sizeof(F64Slice), t_f64slice);
|
||||
r->p = p;
|
||||
r->a = ptr;
|
||||
return tag(r, ARR_TAG);
|
||||
return (Arr*)r;
|
||||
}
|
||||
static B f64arr_slice (B x, usz s) {return m_f64slice(x , c(F64Arr ,x)->a+s); }
|
||||
static B f64slice_slice(B x, usz s) { B r = m_f64slice(inc(c(Slice,x)->p), c(F64Slice,x)->a+s); dec(x); return r; }
|
||||
static Arr* f64arr_slice (B x, usz s) { return m_f64slice(x , c(F64Arr ,x)->a+s); }
|
||||
static Arr* f64slice_slice(B x, usz s) { Arr* r = m_f64slice(inc(c(Slice,x)->p), c(F64Slice,x)->a+s); dec(x); return r; }
|
||||
|
||||
static B f64arr_get (B x, usz n) { VTY(x,t_f64arr ); return m_f64(c(F64Arr ,x)->a[n]); }
|
||||
static B f64slice_get(B x, usz n) { VTY(x,t_f64slice); return m_f64(c(F64Slice,x)->a[n]); }
|
||||
|
||||
@ -12,13 +12,13 @@ static B m_f64arrv(f64** p, usz ia) {
|
||||
F64Arr* r = mm_alloc(fsizeof(F64Arr,a,f64,ia), t_f64arr);
|
||||
*p = r->a;
|
||||
arrP_shVec((Arr*)r, ia);
|
||||
return tag(r, ARR_TAG);
|
||||
return taga(r);
|
||||
}
|
||||
static B m_f64arrc(f64** p, B x) { assert(isArr(x));
|
||||
F64Arr* r = mm_alloc(fsizeof(F64Arr,a,f64,a(x)->ia), t_f64arr);
|
||||
*p = r->a;
|
||||
arrP_shCopy((Arr*)r, x);
|
||||
return tag(r, ARR_TAG);
|
||||
return taga(r);
|
||||
}
|
||||
static Arr* m_f64arrp(f64** p, usz ia) { // doesn't write shape/rank
|
||||
F64Arr* r = mm_alloc(fsizeof(F64Arr,a,f64,ia), t_f64arr);
|
||||
|
||||
@ -31,8 +31,15 @@ B asFill(B x) { // consumes
|
||||
return bi_noFill;
|
||||
}
|
||||
|
||||
static B fillarr_slice (B x, usz s) {return m_fillslice(x , c(FillArr ,x)->a+s); }
|
||||
static B fillslice_slice(B x, usz s) { B r = m_fillslice(inc(c(Slice,x)->p), c(FillSlice,x)->a+s); dec(x); return r; }
|
||||
static Arr* m_fillslice(B p, B* ptr) {
|
||||
FillSlice* r = mm_alloc(sizeof(FillSlice), t_fillslice);
|
||||
r->p = p;
|
||||
r->a = ptr;
|
||||
return (Arr*)r;
|
||||
}
|
||||
|
||||
static Arr* fillarr_slice (B x, usz s) { return m_fillslice(x , c(FillArr ,x)->a+s); }
|
||||
static Arr* fillslice_slice(B x, usz s) { Arr* r = m_fillslice(inc(c(Slice,x)->p), c(FillSlice,x)->a+s); dec(x); return r; }
|
||||
|
||||
static B fillarr_get (B x, usz n) { VTY(x,t_fillarr ); return inc(c(FillArr ,x)->a[n]); }
|
||||
static B fillslice_get (B x, usz n) { VTY(x,t_fillslice); return inc(c(FillSlice,x)->a[n]); }
|
||||
@ -201,5 +208,5 @@ B withFill(B x, B fill) { // consumes both
|
||||
BS2B xget = TI(x).get;
|
||||
for (usz i = 0; i < ia; i++) a[i] = xget(x,i);
|
||||
dec(x);
|
||||
return tag(r, ARR_TAG);
|
||||
return taga(r);
|
||||
}
|
||||
@ -60,19 +60,12 @@ static B getFillE(B x) { // errors if there's no fill
|
||||
|
||||
|
||||
static B m_fillarrp(usz ia) { // doesn't set ia
|
||||
return tag(mm_alloc(fsizeof(FillArr,a,B,ia), t_fillarr), ARR_TAG);
|
||||
return taga(mm_alloc(fsizeof(FillArr,a,B,ia), t_fillarr));
|
||||
}
|
||||
static void fillarr_setFill(B x, B fill) { // consumes fill
|
||||
c(FillArr, x)->fill = fill;
|
||||
}
|
||||
|
||||
static B m_fillslice(B p, B* ptr) {
|
||||
FillSlice* r = mm_alloc(sizeof(FillSlice), t_fillslice);
|
||||
r->p = p;
|
||||
r->a = ptr;
|
||||
return tag(r, ARR_TAG);
|
||||
}
|
||||
|
||||
static B* fillarr_ptr(B x) { VTY(x,t_fillarr); return c(FillArr,x)->a; }
|
||||
|
||||
|
||||
@ -88,7 +81,7 @@ static B m_unit(B x) {
|
||||
arrP_shAllocI((Arr*)r, 1, 0);
|
||||
r->fill = xf;
|
||||
r->a[0] = x;
|
||||
return tag(r, ARR_TAG);
|
||||
return taga(r);
|
||||
}
|
||||
|
||||
static B m_atomUnit(B x) {
|
||||
@ -97,13 +90,13 @@ static B m_atomUnit(B x) {
|
||||
if (q_i32(x)) { i32* rp; r = m_i32arrp(&rp, 1); rp[0] = o2iu(x); }
|
||||
else { f64* rp; r = m_f64arrp(&rp, 1); rp[0] = o2fu(x); }
|
||||
arrP_shAllocR(r, 0);
|
||||
return tag(r, ARR_TAG);
|
||||
return taga(r);
|
||||
}
|
||||
if (isC32(x)) {
|
||||
u32* rp; Arr* r = m_c32arrp(&rp, 1);
|
||||
rp[0] = o2cu(x);
|
||||
arrP_shAllocR(r,0);
|
||||
return tag(r, ARR_TAG);
|
||||
return taga(r);
|
||||
}
|
||||
return m_unit(x);
|
||||
}
|
||||
|
||||
@ -8,14 +8,14 @@ B toCells(B x) {
|
||||
usz cam = a(x)->sh[0];
|
||||
usz csz = arr_csz(x);
|
||||
usz i = 0;
|
||||
BS2B slice = TI(x).slice;
|
||||
BS2A slice = TI(x).slice;
|
||||
usz p = 0;
|
||||
HArr_p r = m_harrs(cam, &i);
|
||||
if (rnk(x)==2) {
|
||||
for (; i < cam; i++) {
|
||||
B s = slice(inc(x), p);
|
||||
arr_shVec(s, csz);
|
||||
r.a[i] = s;
|
||||
Arr* s = slice(inc(x), p);
|
||||
arrP_shVec(s, csz);
|
||||
r.a[i] = taga(s);
|
||||
p+= csz;
|
||||
}
|
||||
} else {
|
||||
@ -24,9 +24,9 @@ B toCells(B x) {
|
||||
usz* xsh = a(x)->sh;
|
||||
for (i32 i = 0; i < cr; i++) csh->a[i] = xsh[i+1];
|
||||
for (; i < cam; i++) {
|
||||
B s = slice(inc(x), p);
|
||||
arr_shSetI(s, csz, cr, csh);
|
||||
r.a[i] = s;
|
||||
Arr* s = slice(inc(x), p);
|
||||
arrP_shSetI(s, csz, cr, csh);
|
||||
r.a[i] = taga(s);
|
||||
p+= csz;
|
||||
}
|
||||
ptr_dec(csh);
|
||||
@ -50,11 +50,11 @@ B toKCells(B x, ur k) {
|
||||
usz i = 0;
|
||||
usz p = 0;
|
||||
HArr_p r = m_harrs(cam, &i);
|
||||
BS2B slice = TI(x).slice;
|
||||
BS2A slice = TI(x).slice;
|
||||
for (; i < cam; i++) {
|
||||
B s = slice(inc(x), p);
|
||||
arr_shSetI(s, csz, cr, csh);
|
||||
r.a[i] = s;
|
||||
Arr* s = slice(inc(x), p);
|
||||
arrP_shSetI(s, csz, cr, csh);
|
||||
r.a[i] = taga(s);
|
||||
p+= csz;
|
||||
}
|
||||
if (cr>1) ptr_dec(csh);
|
||||
@ -91,14 +91,14 @@ NOINLINE void harr_pfree(B x, usz am) { // am - item after last written
|
||||
|
||||
|
||||
|
||||
B m_hslice(B p, B* ptr) {
|
||||
static Arr* m_hslice(B p, B* ptr) {
|
||||
HSlice* r = mm_alloc(sizeof(HSlice), t_hslice);
|
||||
r->p = p;
|
||||
r->a = ptr;
|
||||
return tag(r, ARR_TAG);
|
||||
return (Arr*)r;
|
||||
}
|
||||
B harr_slice (B x, usz s) {return m_hslice(x , c(HArr ,x)->a+s); }
|
||||
B hslice_slice(B x, usz s) { B r = m_hslice(inc(c(Slice,x)->p), c(HSlice,x)->a+s); dec(x); return r; }
|
||||
Arr* harr_slice (B x, usz s) { return m_hslice(x , c(HArr ,x)->a+s); }
|
||||
Arr* hslice_slice(B x, usz s) { Arr* r = m_hslice(inc(c(Slice,x)->p), c(HSlice,x)->a+s); dec(x); return r; }
|
||||
|
||||
B harr_get (B x, usz n) { VTY(x,t_harr ); return inc(c(HArr ,x)->a[n]); }
|
||||
B hslice_get (B x, usz n) { VTY(x,t_hslice); return inc(c(HSlice,x)->a[n]); }
|
||||
|
||||
@ -18,7 +18,7 @@ static inline HArr_p harr_parts(B 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};
|
||||
return (HArr_p){.b = taga(p), .a = p->a, .c = p};
|
||||
}
|
||||
NOINLINE void harr_pfree(B x, usz am); // am - item after last written
|
||||
|
||||
|
||||
@ -6,14 +6,14 @@ NOINLINE B m_cai32(usz ia, i32* a) {
|
||||
return r;
|
||||
}
|
||||
|
||||
static B m_i32slice(B p, i32* ptr) {
|
||||
static Arr* m_i32slice(B p, i32* ptr) {
|
||||
I32Slice* r = mm_alloc(sizeof(I32Slice), t_i32slice);
|
||||
r->p = p;
|
||||
r->a = ptr;
|
||||
return tag(r, ARR_TAG);
|
||||
return (Arr*)r;
|
||||
}
|
||||
static B i32arr_slice (B x, usz s) {return m_i32slice(x , c(I32Arr ,x)->a+s); }
|
||||
static B i32slice_slice(B x, usz s) { B r = m_i32slice(inc(c(Slice,x)->p), c(I32Slice,x)->a+s); dec(x); return r; }
|
||||
static Arr* i32arr_slice (B x, usz s) { return m_i32slice(x , c(I32Arr ,x)->a+s); }
|
||||
static Arr* i32slice_slice(B x, usz s) { Arr* r = m_i32slice(inc(c(Slice,x)->p), c(I32Slice,x)->a+s); dec(x); return r; }
|
||||
|
||||
static B i32arr_get (B x, usz n) { VTY(x,t_i32arr ); return m_i32(c(I32Arr ,x)->a[n]); }
|
||||
static B i32slice_get(B x, usz n) { VTY(x,t_i32slice); return m_i32(c(I32Slice,x)->a[n]); }
|
||||
|
||||
@ -12,13 +12,13 @@ static B m_i32arrv(i32** p, usz ia) {
|
||||
I32Arr* r = mm_alloc(fsizeof(I32Arr,a,i32,ia), t_i32arr);
|
||||
*p = r->a;
|
||||
arrP_shVec((Arr*)r, ia);
|
||||
return tag(r, ARR_TAG);
|
||||
return taga(r);
|
||||
}
|
||||
static B m_i32arrc(i32** p, B x) { assert(isArr(x));
|
||||
I32Arr* r = mm_alloc(fsizeof(I32Arr,a,i32,a(x)->ia), t_i32arr);
|
||||
*p = r->a;
|
||||
arrP_shCopy((Arr*)r, x);
|
||||
return tag(r, ARR_TAG);
|
||||
return taga(r);
|
||||
}
|
||||
static Arr* m_i32arrp(i32** p, usz ia) { // doesn't write shape/rank
|
||||
I32Arr* r = mm_alloc(fsizeof(I32Arr,a,i32,ia), t_i32arr);
|
||||
|
||||
@ -390,7 +390,7 @@ B bqn_squeeze(B x) { // consumes
|
||||
if (xe==el_f64) {
|
||||
f64* xp = f64any_ptr(x);
|
||||
for (usz i = 0; i < ia; i++) if (xp[i] != (f64)(i32)xp[i]) return x;
|
||||
return tag(toI32Arr(x), ARR_TAG);
|
||||
return taga(toI32Arr(x));
|
||||
}
|
||||
assert(xe==el_B);
|
||||
BS2B xgetU = TI(x).getU;
|
||||
@ -401,16 +401,16 @@ B bqn_squeeze(B x) { // consumes
|
||||
if (!isNum(c)) return x;
|
||||
if (!q_i32(c)) {
|
||||
for (i++; i < ia; i++) if (!isNum(xgetU(x, i))) return x;
|
||||
return tag(toF64Arr(x), ARR_TAG);
|
||||
return taga(toF64Arr(x));
|
||||
}
|
||||
}
|
||||
return tag(toI32Arr(x), ARR_TAG);
|
||||
return taga(toI32Arr(x));
|
||||
} else if (isC32(x0)) {
|
||||
for (usz i = 1; i < ia; i++) {
|
||||
B c = xgetU(x, i);
|
||||
if (!isC32(c)) return x;
|
||||
}
|
||||
return tag(toC32Arr(x), ARR_TAG);
|
||||
return taga(toC32Arr(x));
|
||||
} else return x;
|
||||
}
|
||||
B bqn_merge(B x) { // consumes
|
||||
|
||||
8
src/h.h
8
src/h.h
@ -124,8 +124,6 @@ CTR_FOR(F)
|
||||
#undef F
|
||||
|
||||
#define fsizeof(T,F,E,N) (offsetof(T, F) + sizeof(E)*(N)) // type, flexible array member name, flexible array member type, item amount
|
||||
#define ftag(x) ((u64)(x) << 48)
|
||||
#define tag(v, t) b(((u64)(v)) | ftag(t))
|
||||
// .FF0 .111111111110000000000000000000000000000000000000000000000000000 infinity
|
||||
// .FF8 .111111111111000000000000000000000000000000000000000000000000000 qNaN
|
||||
// .FF. .111111111110nnn................................................ sNaN aka tagged aka not f64, if nnn≠0
|
||||
@ -142,6 +140,9 @@ static const u16 NSP_TAG = 0b1111111111110101; // FFF5 1111111111110101ppppppppp
|
||||
static const u16 OBJ_TAG = 0b1111111111110110; // FFF6 1111111111110110ppppppppppppppppppppppppppppppppppppppppppppp000 custom object (e.g. bigints)
|
||||
static const u16 ARR_TAG = 0b1111111111110111; // FFF7 1111111111110111ppppppppppppppppppppppppppppppppppppppppppppp000 array (everything else is an atom)
|
||||
static const u16 VAL_TAG = 0b1111111111110 ; // FFF. 1111111111110................................................... pointer to Value, needs refcounting
|
||||
#define ftag(X) ((u64)(X) << 48)
|
||||
#define tag(V, T) b(((u64)(V)) | ftag(T))
|
||||
#define taga(V) tag(V, ARR_TAG)
|
||||
|
||||
void cbqn_init();
|
||||
|
||||
@ -348,6 +349,7 @@ void slice_print(B x);
|
||||
|
||||
|
||||
typedef void (*B2v)(B);
|
||||
typedef Arr* (*BS2A)(B, usz);
|
||||
typedef B (* BS2B)(B, usz);
|
||||
typedef B (*BSS2B)(B, usz, usz);
|
||||
typedef B (* B2B)(B);
|
||||
@ -364,7 +366,7 @@ typedef struct TypeInfo {
|
||||
BS2B getU; // like get, but doesn't increment result (mostly equivalent to `B t=get(…); dec(t); t`)
|
||||
BB2B m1_d; // consume all args; (m, f)
|
||||
BBB2B m2_d; // consume all args; (m, f, g)
|
||||
BS2B slice; // consumes; create slice from given starting position; add ia, rank, shape yourself; may not actually be a Slice object; preserves fill
|
||||
BS2A slice; // consumes; create slice from given starting position; add ia, rank, shape yourself; may not actually be a Slice object; preserves fill
|
||||
B2B identity; // return identity element of this function; doesn't consume
|
||||
|
||||
BBB2B fn_uc1; // t,o, x→r; r≡O⌾( T ) x; consumes x
|
||||
|
||||
@ -180,7 +180,7 @@ static void* nvm_alloc(u64 sz) {
|
||||
// if (r==MAP_FAILED) thrM("JIT: Failed to allocate executable memory");
|
||||
// return r;
|
||||
TmpFile* src = mmX_allocN(fsizeof(TmpFile,a,u8,sz), t_i8arr);
|
||||
arr_shVec(tag(src,ARR_TAG), sz);
|
||||
arrP_shVec((Arr*)src, sz);
|
||||
return src->a;
|
||||
}
|
||||
void nvm_free(u8* ptr) {
|
||||
|
||||
@ -332,7 +332,7 @@ static B def_identity(B f) { return bi_N; }
|
||||
static B def_get(B x, usz n) { return inc(x); }
|
||||
static B def_m1_d(B m, B f ) { thrM("cannot derive this"); }
|
||||
static B def_m2_d(B m, B f, B g) { thrM("cannot derive this"); }
|
||||
static B def_slice(B x, usz s) { thrM("cannot slice non-array!"); }
|
||||
static Arr* def_slice(B x, usz s) { thrM("cannot slice non-array!"); }
|
||||
|
||||
#ifdef DONT_FREE
|
||||
static B empty_get(B x, usz n) {
|
||||
|
||||
@ -31,7 +31,7 @@ TmpFile* file_bytes(B path) { // consumes
|
||||
u64 len = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
TmpFile* src = mm_alloc(fsizeof(TmpFile,a,u8,len), t_i8arr);
|
||||
arr_shVec(tag(src,ARR_TAG), len);
|
||||
arrP_shVec((Arr*)src, len);
|
||||
if (fread((char*)src->a, 1, len, f)!=len) thrF("Error reading file \"%R\"", path);
|
||||
dec(path);
|
||||
fclose(f);
|
||||
@ -74,9 +74,9 @@ B path_dir(B path) { // consumes; returns directory part of file path with trail
|
||||
for (usz i = 0; i < pia; i++) if (!isC32(pgetU(path, i))) thrM("Paths must be character vectors");
|
||||
for (i64 i = (i64)pia-1; i >= 0; i--) {
|
||||
if (o2cu(pgetU(path, i))=='/') {
|
||||
B r = TI(path).slice(path, 0);
|
||||
arr_shVec(r, i+1);
|
||||
return r;
|
||||
Arr* r = TI(path).slice(path, 0);
|
||||
arrP_shVec(r, i+1);
|
||||
return taga(r);
|
||||
}
|
||||
}
|
||||
dec(path);
|
||||
|
||||
@ -33,35 +33,35 @@ static void mut_to(Mut* m, u8 n) {
|
||||
#ifdef DEBUG
|
||||
if (n==el_B && o==el_f64) { // hack to make toHArr calling f64arr_get not cry about possible sNaN floats
|
||||
usz ia = m->val->ia;
|
||||
f64* p = f64arr_ptr(tag(m->val,ARR_TAG));
|
||||
f64* p = f64arr_ptr(taga(m->val));
|
||||
for (usz i = 0; i < ia; i++) if (!isF64(b(p[i]))) p[i] = 1.2217638442043777e161; // 0x6161616161616161
|
||||
}
|
||||
#endif
|
||||
switch(n) { default: UD;
|
||||
case el_i32: { I32Arr* t= toI32Arr(tag(m->val, ARR_TAG)); m->val = (Arr*)t; m->ai32 = t->a; return; }
|
||||
case el_f64: { F64Arr* t= toF64Arr(tag(m->val, ARR_TAG)); m->val = (Arr*)t; m->af64 = t->a; return; }
|
||||
case el_c32: { C32Arr* t= toC32Arr(tag(m->val, ARR_TAG)); m->val = (Arr*)t; m->ac32 = t->a; return; }
|
||||
case el_B : { HArr* t= toHArr (tag(m->val, ARR_TAG)); m->val = (Arr*)t; m->aB = t->a; return; }
|
||||
case el_i32: { I32Arr* t= toI32Arr(taga(m->val)); m->val = (Arr*)t; m->ai32 = t->a; return; }
|
||||
case el_f64: { F64Arr* t= toF64Arr(taga(m->val)); m->val = (Arr*)t; m->af64 = t->a; return; }
|
||||
case el_c32: { C32Arr* t= toC32Arr(taga(m->val)); m->val = (Arr*)t; m->ac32 = t->a; return; }
|
||||
case el_B : { HArr* t= toHArr (taga(m->val)); m->val = (Arr*)t; m->aB = t->a; return; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static B mut_fv(Mut* m) { assert(m->type!=el_MAX);
|
||||
m->val->sh = &m->val->ia;
|
||||
B r = tag(m->val, ARR_TAG);
|
||||
B r = taga(m->val);
|
||||
srnk(r, 1);
|
||||
return r;
|
||||
}
|
||||
static B mut_fp(Mut* m) { assert(m->type!=el_MAX); // has ia set
|
||||
return tag(m->val, ARR_TAG);
|
||||
return taga(m->val);
|
||||
}
|
||||
static B mut_fc(Mut* m, B x) { assert(m->type!=el_MAX);
|
||||
B r = tag(m->val, ARR_TAG);
|
||||
B r = taga(m->val);
|
||||
arr_shCopy(r, x);
|
||||
return r;
|
||||
}
|
||||
static B mut_fcd(Mut* m, B x) { assert(m->type!=el_MAX);
|
||||
B r = tag(m->val, ARR_TAG);
|
||||
B r = taga(m->val);
|
||||
arr_shCopy(r, x);
|
||||
dec(x);
|
||||
return r;
|
||||
@ -80,7 +80,7 @@ static u8 el_or(u8 a, u8 b) {
|
||||
}
|
||||
|
||||
static void mut_pfree(Mut* m, usz n) { // free the first n elements
|
||||
if (m->type==el_B) harr_pfree(tag(m->val,ARR_TAG), n);
|
||||
if (m->type==el_B) harr_pfree(taga(m->val), n);
|
||||
else mm_free((Value*) m->val);
|
||||
}
|
||||
|
||||
|
||||
6
src/vm.c
6
src/vm.c
@ -262,7 +262,7 @@ NOINLINE Block* compile(B bcq, B objs, B blocks, B indices, B tokenInfo, B src,
|
||||
u32* bc = (u32*)bca->a;
|
||||
usz bcIA = bca->ia;
|
||||
Comp* comp = mm_alloc(sizeof(Comp), t_comp);
|
||||
comp->bc = tag(bca, ARR_TAG);
|
||||
comp->bc = taga(bca);
|
||||
comp->indices = indices;
|
||||
comp->src = src;
|
||||
comp->path = path;
|
||||
@ -842,8 +842,8 @@ NOINLINE B vm_fmtPoint(B src, B prepend, B path, usz cs, usz ce) { // consumes p
|
||||
i64 padStart = padEnd;
|
||||
while (padStart>0 && o2cu(srcGetU(s,padStart-1))!='\n') padStart--;
|
||||
|
||||
B slice = TI(src).slice(inc(src),srcS); arr_shVec(slice, srcE-srcS);
|
||||
AJOIN(slice);
|
||||
Arr* slice = TI(src).slice(inc(src),srcS); arrP_shVec(slice, srcE-srcS);
|
||||
AJOIN(taga(slice));
|
||||
cs-= srcS;
|
||||
ce-= srcS;
|
||||
ACHR('\n');
|
||||
|
||||
Loading…
Reference in New Issue
Block a user