shorten mut_init

This commit is contained in:
dzaima 2021-07-19 19:25:09 +03:00
parent ca264d1332
commit 2e2b479d37
4 changed files with 34 additions and 16 deletions

View File

@ -8,20 +8,21 @@ typedef struct C32Slice {
} C32Slice;
#define C32A_SZ(IA) fsizeof(F64Arr,a,f64,IA)
static B m_c32arrv(u32** p, usz ia) {
C32Arr* r = mm_alloc(fsizeof(C32Arr,a,u32,ia), t_c32arr);
C32Arr* r = mm_alloc(C32A_SZ(ia), t_c32arr);
*p = r->a;
arr_shVec((Arr*)r, ia);
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);
C32Arr* r = mm_alloc(C32A_SZ(a(x)->ia), t_c32arr);
*p = r->a;
arr_shCopy((Arr*)r, x);
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);
C32Arr* r = mm_alloc(C32A_SZ(ia), t_c32arr);
*p = r->a;
r->ia = ia;
return (Arr*)r;

View File

@ -7,21 +7,21 @@ typedef struct F64Slice {
f64* a;
} F64Slice;
#define F64A_SZ(IA) fsizeof(F64Arr,a,f64,IA)
static B m_f64arrv(f64** p, usz ia) {
F64Arr* r = mm_alloc(fsizeof(F64Arr,a,f64,ia), t_f64arr);
F64Arr* r = mm_alloc(F64A_SZ(ia), t_f64arr);
*p = r->a;
arr_shVec((Arr*)r, ia);
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);
F64Arr* r = mm_alloc(F64A_SZ(a(x)->ia), t_f64arr);
*p = r->a;
arr_shCopy((Arr*)r, x);
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);
F64Arr* r = mm_alloc(F64A_SZ(ia), t_f64arr);
*p = r->a;
r->ia = ia;
return (Arr*)r;

View File

@ -8,20 +8,21 @@ typedef struct I32Slice {
} I32Slice;
#define I32A_SZ(IA) fsizeof(F64Arr,a,f64,IA)
static B m_i32arrv(i32** p, usz ia) {
I32Arr* r = mm_alloc(fsizeof(I32Arr,a,i32,ia), t_i32arr);
I32Arr* r = mm_alloc(I32A_SZ(ia), t_i32arr);
*p = r->a;
arr_shVec((Arr*)r, ia);
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);
I32Arr* r = mm_alloc(I32A_SZ(a(x)->ia), t_i32arr);
*p = r->a;
arr_shCopy((Arr*)r, x);
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);
I32Arr* r = mm_alloc(I32A_SZ(ia), t_i32arr);
*p = r->a;
r->ia = ia;
return (Arr*)r;

View File

@ -15,11 +15,27 @@ typedef struct Mut {
static void mut_init(Mut* m, u8 n) {
m->type = n;
usz ia = m->ia;
u8 ty;
usz sz;
// hack around inlining of the allocator too many times
switch(n) { default: UD;
case el_i32: m->val = m_i32arrp(&m->ai32, m->ia); return;
case el_f64: m->val = m_f64arrp(&m->af64, m->ia); return;
case el_c32: m->val = m_c32arrp(&m->ac32, m->ia); return;
case el_B :; HArr_p t = m_harrUp( m->ia); m->val = (Arr*)t.c; m->aB = t.c->a; return;
case el_i32: ty = t_i32arr; sz = I32A_SZ(ia); break;
case el_f64: ty = t_f64arr; sz = F64A_SZ(ia); break;
case el_c32: ty = t_c32arr; sz = C32A_SZ(ia); break;
case el_B:;
HArr_p t = m_harrUp(ia);
m->val = (Arr*)t.c;
m->aB = t.c->a;
return;
}
Arr* a = mm_alloc(sz, ty);
a->ia = ia;
m->val = a;
switch(n) { default: UD; // gcc generates horrible code for this (which should just be two instructions), but that's what gcc does
case el_i32: m->ai32 = ((I32Arr*)a)->a; break;
case el_f64: m->af64 = ((F64Arr*)a)->a; break;
case el_c32: m->ac32 = ((C32Arr*)a)->a; break;
}
}
void mut_to(Mut* m, u8 n);
@ -60,7 +76,7 @@ static u8 el_or(u8 a, u8 b) {
void mut_pfree(Mut* m, usz n);
static void mut_set(Mut* m, usz ms, B x) { // consumes x; sets m[ms] to x
again:
again:;
u8 nty;
switch(m->type) { default: UD;
case el_MAX:
@ -125,7 +141,7 @@ static B mut_getU(Mut* m, usz ms) {
// doesn't consume; fills m[ms…ms+l] with x
static void mut_fill(Mut* m, usz ms, B x, usz l) {
again:
again:;
u8 nty;
switch(m->type) { default: UD;
case el_MAX: