better ARR_BPTR_NEVER

This commit is contained in:
dzaima 2024-09-12 05:17:48 +03:00
parent 41785cb4bf
commit da29b285dd
4 changed files with 20 additions and 22 deletions

View File

@ -330,7 +330,8 @@ if (TI(x,elType)!=el_B) void* xp = tyany_ptr(x); // alternative equivalent check
if (TY(x)==t_harr) B* xp = harr_ptr(x); // similarly, hslice_ptr, fillarrv_ptr, fillslicev_ptr for specific types
B* xp = arr_bptr(x); // will return NULL if the array isn't backed by a contiguous B*
// while currently there are no types with eltype el_B for which arr_bptr isn't NULL, that could change in the future and so should still be handled
// testable with f=-DARR_BPTR_NEVER, which makes arr_bptr
// testable with f=-DARR_BPTR_NEVER, which makes arr_bptr & arrv_bptr always return NULL
// use arr_bptrG & arrv_bptrG if you know that the input is B*-backed and want to rely on it (never returns NULL; unaffected by ARR_BPTR_NEVER)
// most arr_* fns have a arrv_* variant that takes an untagged pointer instead of B

View File

@ -1,24 +1,23 @@
#pragma once
static B* arr_bptr(B x) { assert(isArr(x));
#if !ARR_BPTR_NEVER
if (TY(x)==t_harr) return harr_ptr(x);
if (TY(x)==t_fillarr) return fillarrv_ptr(a(x));
if (TY(x)==t_hslice) return hslice_ptr(x);
if (TY(x)==t_fillslice) return fillslicev_ptr(a(x));
#endif
return NULL;
}
static B* arrv_bptr(Arr* x) {
#if !ARR_BPTR_NEVER
if (PTY(x)==t_harr) return harrv_ptr(x);
if (PTY(x)==t_fillarr) return fillarrv_ptr(x);
if (PTY(x)==t_hslice) return hslicev_ptr(x);
if (PTY(x)==t_fillslice) return fillslicev_ptr(x);
#endif
return NULL;
#define ARRV_BPTR_BODY switch (PTY(xa)) { \
case t_harr: return harrv_ptr(xa); \
case t_fillarr: return fillarrv_ptr(xa); \
case t_hslice: return hslicev_ptr(xa); \
case t_fillslice: return fillslicev_ptr(xa); \
}
static B* arrv_bptrG(Arr* xa) { ARRV_BPTR_BODY; UD; }
static B* arr_bptrG(B x) { Arr* xa=a(x); ARRV_BPTR_BODY; UD; }
#if ARR_BPTR_NEVER
static B* arr_bptr(B x) { return NULL; }
static B* arrv_bptr(Arr* x) { return NULL; }
#else
static B* arr_bptr(B x) { Arr* xa=a(x); ARRV_BPTR_BODY; return NULL; }
static B* arrv_bptr(Arr* xa) { ARRV_BPTR_BODY; return NULL; }
#endif
static void* tyarrv_ptr(TyArr* x) {
assert(IS_ANY_ARR(PTY(x)) && !IS_SLICE(PTY(x)));
return x->a;

View File

@ -112,7 +112,7 @@ static B toI8Any (B x) { u8 t=TY(x); return t==t_i8arr || t==t_i8slice ? x : ta
static B toI16Any(B x) { u8 t=TY(x); return t==t_i16arr || t==t_i16slice? x : taga(cpyI16Arr(x)); }
static B toI32Any(B x) { u8 t=TY(x); return t==t_i32arr || t==t_i32slice? x : taga(cpyI32Arr(x)); }
static B toF64Any(B x) { u8 t=TY(x); return t==t_f64arr || t==t_f64slice? x : taga(cpyF64Arr(x)); }
static B toBitAny(B x) { return taga(toBitArr(x)); }
B m_cai32(usz ia, i32* a);
B m_caf64(usz sz, f64* a);

View File

@ -295,13 +295,11 @@ DEF_G(void, copy, B, (void* a, usz ms, B x, usz xs, usz l), ms, x, x
case t_c8arr: case t_c8slice: { u8* xp = c8any_ptr (x); vfor (usz i = 0; i < l; i++) mpo[i] = m_c32(xp[i+xs]); return; }
case t_c16arr: case t_c16slice: { u16* xp = c16any_ptr(x); vfor (usz i = 0; i < l; i++) mpo[i] = m_c32(xp[i+xs]); return; }
case t_c32arr: case t_c32slice: { u32* xp = c32any_ptr(x); vfor (usz i = 0; i < l; i++) mpo[i] = m_c32(xp[i+xs]); return; }
#if !ARR_BPTR_NEVER
case t_harr: case t_hslice: case t_fillarr: case t_fillslice:;
B* xp = arr_bptr(x)+xs;
B* xp = arr_bptrG(x)+xs;
for (usz i = 0; i < l; i++) inc(xp[i]);
memcpy(mpo, xp, l*sizeof(B));
return;
#endif
case t_f64arr: case t_f64slice:
assert(sizeof(B)==sizeof(f64));
memcpy(mpo, f64any_ptr(x)+xs, l*sizeof(B));