From a191222967d6601d0a2a1fa10c2b93682b561e3e Mon Sep 17 00:00:00 2001 From: dzaima Date: Sun, 5 Sep 2021 02:29:06 +0300 Subject: [PATCH] tyarrTemplate.h --- src/core.h | 22 +++++++++++++++++++++ src/core/c32arr.h | 32 ------------------------------ src/core/f64arr.h | 32 ------------------------------ src/core/i32arr.h | 32 ------------------------------ src/core/tyarrTemplate.h | 42 ++++++++++++++++++++++++++++++++++++++++ src/h.h | 3 +++ src/utils/mut.h | 12 ++++++------ 7 files changed, 73 insertions(+), 102 deletions(-) create mode 100644 src/core/tyarrTemplate.h diff --git a/src/core.h b/src/core.h index 2b1a5f80..a6f24dcc 100644 --- a/src/core.h +++ b/src/core.h @@ -15,9 +15,31 @@ #include "core/gstack.h" #include "core/harr.h" + +#define I8Atom i8 +#define C8Atom u8 +#define I16Atom i16 +#define C16Atom u16 +#define I32Atom i32 +#define C32Atom u32 +#define F64Atom f64 +#define TYARR_SZ(T,IA) fsizeof(T##Arr, a, T##Atom, IA) +#define TYARR_SZ2(T,IA) TYARR_SZ(T,IA) + +#define TU I32 +#define TP(W,X) W##i32##X +#include "core/tyarrTemplate.h" +#define TU C32 +#define TP(W,X) W##c32##X +#include "core/tyarrTemplate.h" +#define TU F64 +#define TP(W,X) W##f64##X +#include "core/tyarrTemplate.h" + #include "core/f64arr.h" #include "core/i32arr.h" #include "core/c32arr.h" + #include "core/fillarr.h" #include "core/derv.h" diff --git a/src/core/c32arr.h b/src/core/c32arr.h index 10db8740..f8d61fda 100644 --- a/src/core/c32arr.h +++ b/src/core/c32arr.h @@ -1,35 +1,3 @@ -typedef struct C32Arr { - struct Arr; - u32 a[]; -} C32Arr; -typedef struct C32Slice { - struct Slice; - u32* a; -} C32Slice; - - -#define C32A_SZ(IA) fsizeof(C32Arr,a,u32,IA) -static B m_c32arrv(u32** p, usz ia) { - C32Arr* r = m_arr(C32A_SZ(ia), t_c32arr, ia); - arr_shVec((Arr*)r); - *p = r->a; - return taga(r); -} -static B m_c32arrc(u32** p, B x) { assert(isArr(x)); - C32Arr* r = m_arr(C32A_SZ(a(x)->ia), t_c32arr, a(x)->ia); - *p = r->a; - arr_shCopy((Arr*)r, x); - return taga(r); -} -static Arr* m_c32arrp(u32** p, usz ia) { - C32Arr* r = m_arr(C32A_SZ(ia), t_c32arr, ia); - *p = r->a; - return (Arr*)r; -} - -static u32* c32arr_ptr(B x) { VTY(x, t_c32arr); return c(C32Arr,x)->a; } -static u32* c32any_ptr(B x) { assert(isArr(x)); u8 t=v(x)->type; if(t==t_c32arr) return c(C32Arr,x)->a; assert(t==t_c32slice); return c(C32Slice,x)->a; } - B m_str8(usz sz, char* s); B m_str8l(char* s); B m_str32(u32* s); diff --git a/src/core/f64arr.h b/src/core/f64arr.h index 285758dd..43f7730e 100644 --- a/src/core/f64arr.h +++ b/src/core/f64arr.h @@ -1,35 +1,3 @@ -typedef struct F64Arr { - struct Arr; - f64 a[]; -} F64Arr; -typedef struct F64Slice { - struct Slice; - f64* a; -} F64Slice; - - -#define F64A_SZ(IA) fsizeof(F64Arr,a,f64,IA) -static B m_f64arrv(f64** p, usz ia) { - F64Arr* r = m_arr(F64A_SZ(ia), t_f64arr, ia); - arr_shVec((Arr*)r); - *p = r->a; - return taga(r); -} -static B m_f64arrc(f64** p, B x) { assert(isArr(x)); - F64Arr* r = m_arr(F64A_SZ(a(x)->ia), t_f64arr, a(x)->ia); - *p = r->a; - arr_shCopy((Arr*)r, x); - return taga(r); -} -static Arr* m_f64arrp(f64** p, usz ia) { - F64Arr* r = m_arr(F64A_SZ(ia), t_f64arr, ia); - *p = r->a; - return (Arr*)r; -} - -static f64* f64arr_ptr(B x) { VTY(x, t_f64arr); return c(F64Arr,x)->a; } -static f64* f64any_ptr(B x) { assert(isArr(x)); u8 t=v(x)->type; if(t==t_f64arr) return c(F64Arr,x)->a; assert(t==t_f64slice); return c(F64Slice,x)->a; } - B m_caf64(usz sz, f64* a); static F64Arr* toF64Arr(B x) { diff --git a/src/core/i32arr.h b/src/core/i32arr.h index 44e04cff..3fac3245 100644 --- a/src/core/i32arr.h +++ b/src/core/i32arr.h @@ -1,35 +1,3 @@ -typedef struct I32Arr { - struct Arr; - i32 a[]; -} I32Arr; -typedef struct I32Slice { - struct Slice; - i32* a; -} I32Slice; - - -#define I32A_SZ(IA) fsizeof(I32Arr,a,i32,IA) -static B m_i32arrv(i32** p, usz ia) { - I32Arr* r = m_arr(I32A_SZ(ia), t_i32arr, ia); - arr_shVec((Arr*)r); - *p = r->a; - return taga(r); -} -static B m_i32arrc(i32** p, B x) { assert(isArr(x)); - I32Arr* r = m_arr(I32A_SZ(a(x)->ia), t_i32arr, a(x)->ia); - *p = r->a; - arr_shCopy((Arr*)r, x); - return taga(r); -} -static Arr* m_i32arrp(i32** p, usz ia) { - I32Arr* r = m_arr(I32A_SZ(ia), t_i32arr, ia); - *p = r->a; - return (Arr*)r; -} - -static i32* i32arr_ptr(B x) { VTY(x, t_i32arr); return c(I32Arr,x)->a; } -static i32* i32any_ptr(B x) { assert(isArr(x)); u8 t=v(x)->type; if(t==t_i32arr) return c(I32Arr,x)->a; assert(t==t_i32slice); return c(I32Slice,x)->a; } - B m_cai32(usz ia, i32* a); static I32Arr* toI32Arr(B x) { // assumes it's possible diff --git a/src/core/tyarrTemplate.h b/src/core/tyarrTemplate.h new file mode 100644 index 00000000..6494ef50 --- /dev/null +++ b/src/core/tyarrTemplate.h @@ -0,0 +1,42 @@ +#define T_ARR TP(t_,arr) +#define T_SLICE TP(t_,slice) +#define TArr JOIN(TU,Arr) +#define TSlice JOIN(TU,Slice) +#define TEl JOIN(TU,Atom) + +typedef struct TArr { + struct Arr; + TEl a[]; +} TArr; +typedef struct TSlice { + struct Slice; + TEl* a; +} TSlice; + + +static B TP(m_,arrv) (TEl** p, usz ia) { + TArr* r = m_arr(TYARR_SZ2(TU,ia), T_ARR, ia); + arr_shVec((Arr*)r); + *p = r->a; + return taga(r); +} +static B TP(m_,arrc) (TEl** p, B x) { assert(isArr(x)); + TArr* r = m_arr(TYARR_SZ2(TU,a(x)->ia), T_ARR, a(x)->ia); + *p = r->a; + arr_shCopy((Arr*)r, x); + return taga(r); +} +static Arr* TP(m_,arrp) (TEl** p, usz ia) { + TArr* r = m_arr(TYARR_SZ2(TU,ia), T_ARR, ia); + *p = r->a; + return (Arr*)r; +} + +static TEl* TP(,arr_ptr) (B x) { VTY(x, T_ARR); return c(TArr,x)->a; } +static TEl* TP(,any_ptr) (B x) { assert(isArr(x)); u8 t=v(x)->type; if(t==T_ARR) return c(TArr,x)->a; assert(t==T_SLICE); return c(TSlice,x)->a; } + +#undef TEl +#undef TSlice +#undef TArr +#undef TU +#undef TP diff --git a/src/h.h b/src/h.h index 64744a8a..510c1533 100644 --- a/src/h.h +++ b/src/h.h @@ -118,6 +118,9 @@ #define N64d "%"SCNd64 #define N64u "%"SCNu64 +#define JOIN0(A,B) A##B +#define JOIN(A,B) JOIN0(A,B) + typedef u32 usz; #define USZ_MAX ((u32)((1LL<<32)-1)) // typedef u64 usz; diff --git a/src/utils/mut.h b/src/utils/mut.h index f5caf99c..9dfe0444 100644 --- a/src/utils/mut.h +++ b/src/utils/mut.h @@ -33,9 +33,9 @@ static void mut_init(Mut* m, u8 n) { usz sz; // hack around inlining of the allocator too many times switch(n) { default: UD; - 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_i32: ty = t_i32arr; sz = TYARR_SZ(I32,ia); break; + case el_f64: ty = t_f64arr; sz = TYARR_SZ(F64,ia); break; + case el_c32: ty = t_c32arr; sz = TYARR_SZ(C32,ia); break; case el_B:; HArr_p t = m_harrUp(ia); m->val = (Arr*)t.c; @@ -391,17 +391,17 @@ static inline bool inplace_add(B w, B x) { // fails if fills wouldn't be correct if (v(w)->refc==1) { u64 wsz = mm_size(v(w)); u8 wt = v(w)->type; - if (wt==t_i32arr && fsizeof(I32Arr,a,i32,ria)ia = ria; i32arr_ptr(w)[wia] = o2iu(x); return true; } - if (wt==t_c32arr && fsizeof(C32Arr,a,u32,ria)ia = ria; c32arr_ptr(w)[wia] = o2cu(x); return true; } - if (wt==t_f64arr && fsizeof(F64Arr,a,f64,ria)ia = ria; f64arr_ptr(w)[wia] = o2fu(x); return true;