refactor unroll/vectorization disabling macros

This commit is contained in:
dzaima 2022-09-25 14:49:57 +03:00
parent 2bb6750d7d
commit 5c87c291f9
7 changed files with 20 additions and 16 deletions

View File

@ -460,7 +460,7 @@ static B m1c2(B t, B f, B w, B x) { // consumes w,x
ShArr* X##_csh; \ ShArr* X##_csh; \
if (X##_cr>1) { \ if (X##_cr>1) { \
X##_csh = m_shArr(X##_cr); \ X##_csh = m_shArr(X##_cr); \
NOUNROLL for (usz i = 0; i < X##_cr; i++) { \ PLAINLOOP for (usz i = 0; i < X##_cr; i++) { \
usz v = SH(X)[i+1]; \ usz v = SH(X)[i+1]; \
X##_csz*= v; \ X##_csz*= v; \
X##_csh->a[i] = v; \ X##_csh->a[i] = v; \

View File

@ -333,11 +333,11 @@ B rand_range_c2(B t, B w, B x) {
if (max<1) { if (max<1) {
if (max!=0) thrM("(rand).Range: 𝕩 cannot be negative"); if (max!=0) thrM("(rand).Range: 𝕩 cannot be negative");
f64* rp; r = m_f64arrp(&rp, am); f64* rp; r = m_f64arrp(&rp, am);
NOUNROLL for (usz i = 0; i < am; i++) rp[i] = wy2u01(wyrand(&seed)); PLAINLOOP for (usz i = 0; i < am; i++) rp[i] = wy2u01(wyrand(&seed));
} else if (max > (1ULL<<31)) { } else if (max > (1ULL<<31)) {
if (max >= 1LL<<53) thrM("(rand).Range: 𝕩 must be less than 2⋆53"); if (max >= 1LL<<53) thrM("(rand).Range: 𝕩 must be less than 2⋆53");
f64* rp; r = m_f64arrp(&rp, am); f64* rp; r = m_f64arrp(&rp, am);
NOUNROLL for (usz i = 0; i < am; i++) rp[i] = wy2u0k(wyrand(&seed), max); PLAINLOOP for (usz i = 0; i < am; i++) rp[i] = wy2u0k(wyrand(&seed), max);
} else { } else {
u8 t; usz u64am; u8 t; usz u64am;
@ -351,9 +351,9 @@ B rand_range_c2(B t, B w, B x) {
r = m_arr(offsetof(TyArr,a) + (u64am<<3), t, am); r = m_arr(offsetof(TyArr,a) + (u64am<<3), t, am);
void* rp = ((TyArr*)r)->a; void* rp = ((TyArr*)r)->a;
if (max & (max-1)) { // not power of two if (max & (max-1)) { // not power of two
if (t==t_i32arr) NOUNROLL for (usz i = 0; i < am; i++) ((i32*)rp)[i] = wy2u0k(wyrand(&seed), max); if (t==t_i32arr) PLAINLOOP for (usz i = 0; i < am; i++) ((i32*)rp)[i] = wy2u0k(wyrand(&seed), max);
else if (t==t_i16arr) NOUNROLL for (usz i = 0; i < am; i++) ((i16*)rp)[i] = wy2u0k(wyrand(&seed), max); else if (t==t_i16arr) PLAINLOOP for (usz i = 0; i < am; i++) ((i16*)rp)[i] = wy2u0k(wyrand(&seed), max);
else if (t==t_i8arr) NOUNROLL for (usz i = 0; i < am; i++) (( i8*)rp)[i] = wy2u0k(wyrand(&seed), max); else if (t==t_i8arr) PLAINLOOP for (usz i = 0; i < am; i++) (( i8*)rp)[i] = wy2u0k(wyrand(&seed), max);
else UD; // bitarr will be max==2, i.e. a power of two else UD; // bitarr will be max==2, i.e. a power of two
} else { } else {
u64 mask; u64 mask;
@ -364,7 +364,7 @@ B rand_range_c2(B t, B w, B x) {
if (t==t_i16arr) { goto end; } mask|= mask<<8; if (t==t_i16arr) { goto end; } mask|= mask<<8;
end: end:
NOUNROLL for (usz i = 0; i < u64am; i++) ((u64*)rp)[i] = wyrand(&seed) & mask; PLAINLOOP for (usz i = 0; i < u64am; i++) ((u64*)rp)[i] = wyrand(&seed) & mask;
} }
} }

View File

@ -86,12 +86,12 @@ static void arr_shCopy(Arr* n, B o) { // copy shape & rank from o to n
} }
static void shcpy(usz* dst, usz* src, size_t len) { static void shcpy(usz* dst, usz* src, size_t len) {
// memcpy(dst, src, len*sizeof(usz)); // memcpy(dst, src, len*sizeof(usz));
NOUNROLL for (size_t i = 0; i < len; i++) dst[i] = src[i]; PLAINLOOP for (size_t i = 0; i < len; i++) dst[i] = src[i];
} }
static usz shProd(usz* sh, usz s, usz e) { static usz shProd(usz* sh, usz s, usz e) {
usz r = 1; usz r = 1;
NOUNROLL for (i32 i = s; i < e; i++) r*= sh[i]; PLAINLOOP for (i32 i = s; i < e; i++) r*= sh[i];
return r; return r;
} }
static usz arr_csz(B x) { static usz arr_csz(B x) {
@ -101,7 +101,7 @@ static usz arr_csz(B x) {
} }
static bool eqShPart(usz* w, usz* x, usz len) { static bool eqShPart(usz* w, usz* x, usz len) {
// return memcmp(w, x, len*sizeof(usz))==0; // return memcmp(w, x, len*sizeof(usz))==0;
NOUNROLL for (i32 i = 0; i < len; i++) if (w[i]!=x[i]) return false; PLAINLOOP for (i32 i = 0; i < len; i++) if (w[i]!=x[i]) return false;
return true; return true;
} }
static bool eqShape(B w, B x) { assert(isArr(w)); assert(isArr(x)); static bool eqShape(B w, B x) { assert(isArr(w)); assert(isArr(x));

View File

@ -108,7 +108,7 @@ BQNV bqn_makeChar(uint32_t c) { return makeX(m_c32(c)); }
static usz calcIA(size_t rank, const size_t* shape) { static usz calcIA(size_t rank, const size_t* shape) {
if (rank>UR_MAX) thrM("Rank too large"); if (rank>UR_MAX) thrM("Rank too large");
usz r = 1; usz r = 1;
NOUNROLL for (size_t i = 0; i < rank; i++) if (mulOn(r, shape[i])) thrM("Size too large"); PLAINLOOP for (size_t i = 0; i < rank; i++) if (mulOn(r, shape[i])) thrM("Size too large");
return r; return r;
} }
static void copyBData(B* r, const BQNV* data, usz ia) { static void copyBData(B* r, const BQNV* data, usz ia) {
@ -123,7 +123,7 @@ static void copyBData(B* r, const BQNV* data, usz ia) {
} }
} }
#define CPYSH(R) usz* sh = arr_shAlloc((Arr*)(R), r0); \ #define CPYSH(R) usz* sh = arr_shAlloc((Arr*)(R), r0); \
if (sh) NOUNROLL for (size_t i = 0; RARE(i < r0); i++) sh[i] = sh0[i]; if (sh) PLAINLOOP for (size_t i = 0; RARE(i < r0); i++) sh[i] = sh0[i];
BQNV bqn_makeI8Arr (size_t r0, const size_t* sh0, const i8* data) { usz ia=calcIA(r0,sh0); i8* rp; Arr* r = m_i8arrp (&rp,ia); CPYSH(r); memcpy(rp,data,ia*1); return makeX(taga(r)); } BQNV bqn_makeI8Arr (size_t r0, const size_t* sh0, const i8* data) { usz ia=calcIA(r0,sh0); i8* rp; Arr* r = m_i8arrp (&rp,ia); CPYSH(r); memcpy(rp,data,ia*1); return makeX(taga(r)); }
BQNV bqn_makeI16Arr(size_t r0, const size_t* sh0, const i16* data) { usz ia=calcIA(r0,sh0); i16* rp; Arr* r = m_i16arrp(&rp,ia); CPYSH(r); memcpy(rp,data,ia*2); return makeX(taga(r)); } BQNV bqn_makeI16Arr(size_t r0, const size_t* sh0, const i16* data) { usz ia=calcIA(r0,sh0); i16* rp; Arr* r = m_i16arrp(&rp,ia); CPYSH(r); memcpy(rp,data,ia*2); return makeX(taga(r)); }

View File

@ -140,12 +140,16 @@ typedef double f64;
#define N64d "%"SCNd64 #define N64d "%"SCNd64
#define N64u "%"SCNu64 #define N64u "%"SCNu64
#if __clang__ #if __clang__
#define NOUNROLL _Pragma("clang loop unroll(disable)") _Pragma("clang loop vectorize(disable)") #define NOUNROLL _Pragma("clang loop unroll(disable)")
#define NOVECTORIZE _Pragma("clang loop vectorize(disable)")
#elif __GNUC__ #elif __GNUC__
#define NOUNROLL _Pragma("GCC unroll 1") #define NOUNROLL _Pragma("GCC unroll 1")
#define NOVECTORIZE
#else #else
#define NOUNROLL #define NOUNROLL
#define NOVECTORIZE
#endif #endif
#define PLAINLOOP NOUNROLL NOVECTORIZE
#define JOIN0(A,B) A##B #define JOIN0(A,B) A##B
#define JOIN(A,B) JOIN0(A,B) #define JOIN(A,B) JOIN0(A,B)

View File

@ -20,7 +20,7 @@ u64 alSize;
FORCE_INLINE void BN(splitTo)(EmptyValue* c, i64 from, i64 to, bool notEqual) { FORCE_INLINE void BN(splitTo)(EmptyValue* c, i64 from, i64 to, bool notEqual) {
c->mmInfo = MMI(to); c->mmInfo = MMI(to);
NOUNROLL while (from != to) { PLAINLOOP while (from != to) {
from--; from--;
EmptyValue* b = (EmptyValue*) (BSZ(from) + (u8*)c); EmptyValue* b = (EmptyValue*) (BSZ(from) + (u8*)c);
b->type = t_empty; b->type = t_empty;

View File

@ -629,7 +629,7 @@ FORCE_INLINE Scope* m_scopeI(Body* body, Scope* psc, u16 varAm, i32 initVarAm, B
} }
i = initVarAm; i = initVarAm;
} else { } else {
NOUNROLL while (i<initVarAm) { sc->vars[i] = initVars[i]; i++; } PLAINLOOP while (i<initVarAm) { sc->vars[i] = initVars[i]; i++; }
} }
// some bit of manual unrolling, but not too much // some bit of manual unrolling, but not too much
@ -637,7 +637,7 @@ FORCE_INLINE Scope* m_scopeI(Body* body, Scope* psc, u16 varAm, i32 initVarAm, B
if (left==1) sc->vars[i] = bi_noVar; if (left==1) sc->vars[i] = bi_noVar;
else if (left>=2) { else if (left>=2) {
B* vars = sc->vars+i; B* vars = sc->vars+i;
NOUNROLL for (u32 i = 0; i < (left>>1); i++) { *(vars++) = bi_noVar; *(vars++) = bi_noVar; } PLAINLOOP for (u32 i = 0; i < (left>>1); i++) { *(vars++) = bi_noVar; *(vars++) = bi_noVar; }
if (left&1) *vars = bi_noVar; if (left&1) *vars = bi_noVar;
} }