various changes on filling arrays with the same element

This commit is contained in:
dzaima 2023-07-12 15:53:58 +03:00
parent 5b7330906a
commit 9cfc3f3f25
13 changed files with 90 additions and 85 deletions

View File

@ -187,7 +187,7 @@ static B modint_AS(B w, B xv) { return modint_AA(w, C2(shape, C1(fne, incG(w))
if (q_i32(w)) {
i32 wi32 = o2iG(w);
if (wi32>0 && (wi32&(wi32-1))==0) {
if (wi32==1) { Arr* ra=allZeroes(IA(x)); arr_shCopy(ra, x); r = taga(ra); decG(x); return r; }
if (wi32==1) return i64EachDec(0, x);
if (xe==el_bit) return x; // if n>1 (true from the above), 0‿1 ≡ (2⋆n)|0‿1
u8 elw = elWidth(xe);
u32 mask0 = (u32)wi32;

View File

@ -3,9 +3,7 @@
#include "../utils/calls.h"
static NOINLINE void fillBits(u64* dst, u64 sz, bool v) {
u64 x = 0-(u64)v;
u64 am = (sz+63)/64; assert(am>0);
for (usz i = 0; i < am; i++) dst[i] = x;
memset((u8*)dst, v?0xff:0, BIT_N(sz)*8);
}
static NOINLINE void fillBitsDec(u64* dst, u64 sz, bool v, u64 x) {
dec(b(x));

View File

@ -85,9 +85,7 @@ NOINLINE B list_range(B x) {
ud_rec(rp, 0, xia, pos, sh);
fillarr_setFill(r, incG(rp[0]));
} else {
i32* fp;
fillarr_setFill(r, m_i32arrv(&fp, xia));
for (usz i = 0; i < xia; i++) fp[i] = 0;
fillarr_setFill(r, taga(arr_shVec(allZeroes(xia))));
}
return taga(r);
}

View File

@ -414,11 +414,9 @@ B GRADE_CAT(c2)(B t, B w, B x) {
u8 we = TI(w,elType); usz wia = IA(w);
u8 xe = TI(x,elType); usz xia = IA(x);
B r;
if (wia==0 | xia==0) {
Arr* ra=allZeroes(xia); arr_shCopy(ra, x);
r=taga(ra); goto done;
}
B r; Arr* ra;
if (wia==0 | xia==0) goto zeroes_done;
if (wia==1) {
B c = IGet(w, 0);
if (LIKELY(we<el_B & xe<el_B)) {
@ -504,19 +502,14 @@ B GRADE_CAT(c2)(B t, B w, B x) {
w=toI32Any(w); x=toI32Any(x);
#endif
} else {
// TODO pull copy-scalar part out of Reshape
i32* rp; r = m_i32arrc(&rp, x);
for (u64 i=0; i<xia; i++) rp[i]=wia;
goto done;
ra = reshape_one(xia, m_f64(wia));
goto copysh_done;
}
} else { // w is character
if (elNum(xe)) {
Arr* ra=allZeroes(xia); arr_shCopy(ra, x);
r=taga(ra); goto done;
} else {
we = el_c32;
w=toC32Any(w); x=toC32Any(x);
}
if (elNum(xe)) goto zeroes_done;
we = el_c32;
w=toC32Any(w); x=toC32Any(x);
}
#if SINGELI
@ -554,9 +547,17 @@ B GRADE_CAT(c2)(B t, B w, B x) {
rp[i] = s;
}
}
done:
done:
decG(w);decG(x);
return r;
zeroes_done:;
ra = allZeroes(xia);
copysh_done:;
r = taga(arr_shCopy(ra, x));
goto done;
}
#undef GRADE_CHR
#undef LE_C2

View File

@ -304,9 +304,9 @@ B scan_c2(Md1D* d, B w, B x) { B f = d->f;
if (xe==el_bit && q_bit(w)) {
// ⌊ & ⌈ handled above
if (rtid==n_or ) { if (!o2bG(w)) return scan_or(x, ia); B r = taga(arr_shVec(allOnes (ia))); decG(x); return r; } //
if (rtid==n_and | rtid==n_mul) { if ( o2bG(w)) return scan_and(x, ia); B r = taga(arr_shVec(allZeroes(ia))); decG(x); return r; } // ∧×
if (rtid==n_lt) return scan_lt(x, bitx(w), ia); // <
if (rtid==n_or ) return !o2bG(w)? scan_or(x, ia) : i64EachDec(1, x); //
if (rtid==n_and | rtid==n_mul) return o2bG(w)? scan_and(x, ia) : i64EachDec(0, x); // ∧×
if (rtid==n_lt) return scan_lt(x, bitx(w), ia); // <
}
}
base:;

View File

@ -218,8 +218,7 @@ B indexOf_c2(B t, B w, B x) {
u8 we = TI(w,elType); usz wia = IA(w);
u8 xe = TI(x,elType); usz xia = IA(x);
if (wia==0 || xia==0) {
B r=taga(arr_shCopy(allZeroes(xia), x));
decG(w); decG(x); return r;
decG(w); return i64EachDec(0, x);
}
if (elNum(we) && elNum(xe)) { tyEls:
@ -303,8 +302,7 @@ B memberOf_c2(B t, B w, B x) {
u8 we = TI(w,elType); usz wia = IA(w);
u8 xe = TI(x,elType); usz xia = IA(x);
if (wia==0 || xia==0) {
r=taga(arr_shCopy(allZeroes(wia), w));
decG(w); goto dec_x;
decG(x); return i64EachDec(0, w);
}
if (elNum(we) && elNum(xe)) { tyEls:

View File

@ -124,6 +124,49 @@ static void fill_words(void* rp, u64 v, u64 bytes) {
for (usz i=0; i<wds; i++) p[i] = v;
if (ext) memcpy(p+wds, &v, ext);
}
NOINLINE B i64EachDec(i64 v, B x) {
B r = taga(arr_shCopy(reshape_one(IA(x), m_f64(v)), x));
decG(x);
return r;
}
NOINLINE Arr* reshape_one(usz nia, B x) {
Arr* r;
#define FILL(E,T,V) T* rp; r = m_##E##arrp(&rp,nia); fill_words(rp, V, (u64)nia*sizeof(T));
if (isF64(x)) {
i32 n = (i32)x.f;
if (RARE(n!=x.f)) {
FILL(f64,f64,x.u)
} else if (n==(i8)n) { // memset can be faster than writing words
u8 b = n;
void* rp; u64 nb;
if (b <= 1) { u64* rp0; r = m_bitarrp(&rp0,nia); rp=rp0; nb = 8*BIT_N(nia); b=-n; }
else { i8* rp0; r = m_i8arrp (&rp0,nia); rp=rp0; nb = nia; }
memset(rp, b, nb);
} else {
if(n==(i16)n) { FILL(i16,i16,(u16)n*0x0001000100010001U) }
else { FILL(i32,i32,(u32)n*0x0000000100000001U) }
}
} else if (isC32(x)) {
u32 c = o2cG(x);
if (c==(u8 )c) { u8* rp; r = m_c8arrp(&rp,nia); memset(rp, c, nia); }
else if (c==(u16)c) { FILL(c16,u16,c*0x0001000100010001U) }
else { FILL(c32,u32,c*0x0000000100000001U) }
} else {
incBy(x, nia); // in addition with the existing reference, this covers the filled amount & asFill
B rf = asFill(x);
r = m_fillarrp(nia);
if (sizeof(B)==8) fill_words(fillarr_ptr(r), x.u, (u64)nia*8);
else for (usz i = 0; i < nia; i++) fillarr_ptr(r)[i] = x;
fillarr_setFill(r, rf);
NOGC_E;
}
#undef FILL
return r;
}
B shape_c2(B t, B w, B x) {
usz xia = isArr(x)? IA(x) : 1;
usz nia = 1;
@ -282,37 +325,8 @@ B shape_c2(B t, B w, B x) {
}
}
} else {
unit:
#define FILL(E,T,V) T* rp; r = m_##E##arrp(&rp,nia); fill_words(rp, V, (u64)nia*sizeof(T));
if (isF64(x)) {
i32 n = (i32)x.f;
if (RARE(n!=x.f)) {
FILL(f64,f64,x.u)
} else if (n==(i8)n) { // memset can be faster than writing words
u8 b = n;
i8* rp; u64 nb = nia;
if (b <= 1) { r = m_bitarrp((u64**)&rp,nia); nb = 8*BIT_N(nia); b=-b; }
else { r = m_i8arrp ( &rp,nia); }
memset(rp, b, nb);
} else {
if(n==(i16)n) { FILL(i16,i16,(u16)n*0x0001000100010001U) }
else { FILL(i32,i32,(u32)n*0x0000000100000001U) }
}
} else if (isC32(x)) {
u32 c = o2cG(x);
if (c==(u8 )c) { u8* rp; r = m_c8arrp(&rp,nia); memset(rp, c, nia); }
else if (c==(u16)c) { FILL(c16,u16,c*0x0001000100010001U) }
else { FILL(c32,u32,c*0x0000000100000001U) }
} else {
incBy(x, nia); // in addition with the existing reference, this covers the filled amount & asFill
B rf = asFill(x);
r = m_fillarrp(nia);
if (sizeof(B)==8) fill_words(fillarr_ptr(r), x.u, (u64)nia*8);
else for (usz i = 0; i < nia; i++) fillarr_ptr(r)[i] = x;
fillarr_setFill(r, rf);
NOGC_E;
}
#undef FILL
unit:;
r = reshape_one(nia, x);
}
return taga(arr_shSetUO(r,nr,sh));
}

View File

@ -603,7 +603,7 @@ B rand_subset_c2(B t, B w, B x) {
for (u64 xt=xi/4; xt>=(u64)wi; xt>>=1) sh++;
u64 sz = ((xi-1)>>sh)+1 + wi;
TALLOC(i32, hash, sz);
for (u64 i = 0; i < sz; i++) hash[i] = xi;
FILL_TO(hash, el_i32, 0, x, sz);
for (i32 i = xi-wi; i < xi; i++) {
i32 j = wy2u0k(wyrand(&seed), i+1);
u64 p = (u64)j >> sh;

View File

@ -4,12 +4,7 @@ B asFill(B x) { // consumes
if (isArr(x)) {
u8 xe = TI(x,elType);
usz ia = IA(x);
if (elNum(xe)) {
Arr* r = allZeroes(ia);
arr_shCopy(r, x);
decG(x);
return taga(r);
}
if (elNum(xe)) return i64EachDec(0, x);
if (elChr(xe)) {
u8* rp; B r = m_c8arrc(&rp, x);
for (usz i = 0; i < ia; i++) rp[i] = ' ';

View File

@ -148,8 +148,14 @@ static bool ptr_eqShape(usz* wsh, ur wr, usz* xsh, ur xr) {
static bool eqShape(B w, B x) { assert(isArr(w) && isArr(x)); return ptr_eqShape(SH(w), RNK(w), SH(x), RNK(x)); }
B bit_sel(B b, B e0, B e1); // consumes b; b must be bitarr; b⊏e0‿e1
Arr* allZeroes(usz ia);
Arr* allOnes(usz ia);
Arr* allZeroes(usz ia); // ia⥊0 with undefined shape
Arr* allOnes(usz ia); // ia⥊1 with undefined shape
Arr* reshape_one(usz nia, B x); // nia⥊1↑⥊x with undefined shape
B i64EachDec(i64 v, B x); // v¨ x; consumes x
B bit_negate(B x); // consumes
void bit_negatePtr(u64* rp, u64* xp, usz count); // count is number of u64-s
B widenBitArr(B x, ur axis); // consumes x, assumes bitarr; returns some array with cell size padded to the nearest of 8,16,32,64 if ≤64 bits, or a multiple of 64 bits otherwise

View File

@ -329,14 +329,14 @@ BQNFFIEnt ffi_parseTypeStr(u32** src, bool inPtr, bool top) { // parse actual ty
rt = ffi_type_pointer;
} else { // largely copy-pasted from `case '{':`
BQNFFIEnt* rp; ro = m_bqnFFIType(&rp, cty_starr, n+1);
for (int i = 0; i < n; i++) rp[i] = e;
PLAINLOOP for (int i = 0; i < n; i++) rp[i] = e;
incBy(e.o, n-1);
rp[n].structData = NULL;
TAlloc* ao = ARBOBJ(sizeof(ffi_type*) * (n+1));
rp[n].structData = ao;
ffi_type** els = rt.elements = (ffi_type**) ao->data;
for (usz i = 0; i < n; i++) els[i] = &rp[i].t;
PLAINLOOP for (usz i = 0; i < n; i++) els[i] = &rp[i].t;
els[n] = NULL;
rt.type = FFI_TYPE_STRUCT;
@ -344,7 +344,7 @@ BQNFFIEnt ffi_parseTypeStr(u32** src, bool inPtr, bool top) { // parse actual ty
TALLOC(size_t, offsets, n);
if (ffi_get_struct_offsets(FFI_DEFAULT_ABI, &rt, offsets) != FFI_OK) thrM("FFI: Failed getting array offsets");
if (rt.size>=U16_MAX) thrM("FFI: Array too large; limit is 65534 bytes");
for (usz i = 0; i < n; i++) rp[i].offset = offsets[i];
PLAINLOOP for (usz i = 0; i < n; i++) rp[i].offset = offsets[i];
c(BQNFFIType, ro)->structSize = rt.size;
TFREE(offsets);
}
@ -415,7 +415,7 @@ BQNFFIEnt ffi_parseTypeStr(u32** src, bool inPtr, bool top) { // parse actual ty
TAlloc* ao = ARBOBJ(sizeof(ffi_type*) * (n+1));
rp[n].structData = ao;
ffi_type** els = rt.elements = (ffi_type**) ao->data;
for (usz i = 0; i < n; i++) els[i] = &rp[i].t;
PLAINLOOP for (usz i = 0; i < n; i++) els[i] = &rp[i].t;
els[n] = NULL;
TSFREE(es);
rt.type = FFI_TYPE_STRUCT;
@ -423,7 +423,7 @@ BQNFFIEnt ffi_parseTypeStr(u32** src, bool inPtr, bool top) { // parse actual ty
TALLOC(size_t, offsets, n);
if (ffi_get_struct_offsets(FFI_DEFAULT_ABI, &rt, offsets) != FFI_OK) thrM("FFI: Failed getting struct offsets");
if (rt.size>=U16_MAX) thrM("FFI: Struct too large; limit is 65534 bytes");
for (usz i = 0; i < n; i++) rp[i].offset = offsets[i];
PLAINLOOP for (usz i = 0; i < n; i++) rp[i].offset = offsets[i];
c(BQNFFIType, ro)->structSize = rt.size;
TFREE(offsets);
break;
@ -919,7 +919,7 @@ B ffiload_c2(B t, B w, B x) {
if (sz<16) sz = 16;
TAlloc* argsRaw = ARBOBJ(sz);
ffi_type** argsRawArr = (ffi_type**)argsRaw->data;
for (usz i = 0; i < argn; i++) argsRawArr[i] = &args[i+1].t;
PLAINLOOP for (usz i = 0; i < argn; i++) argsRawArr[i] = &args[i+1].t;
// for (usz i = 0; i < argn; i++) {
// ffi_type c = *argsRawArr[i];
// printf("%zu %d %d %p\n", c.size, c.alignment, c.type, c.elements);

View File

@ -164,7 +164,7 @@ static NOINLINE i64 readInt(char** p) {
static B sysvalNames, sysvalNamesNorm;
NOINLINE void fill_color(ReplxxColor* cols, int s, int e, ReplxxColor col) {
for (int i = s; i < e; i++) cols[i] = col;
PLAINLOOP for (int i = s; i < e; i++) cols[i] = col;
}
static bool chr_nl(u32 c) { return c==10 || c==13; }
static bool chr_dig(u32 c) { return c>='0' && c<='9'; }

View File

@ -35,13 +35,8 @@ NOINLINE B bit_sel(B b, B e0, B e1) {
case el_f64: goto t_f64;
}
t_bit:
if (f0) {
if (f1) { Arr* a = allOnes(ia); arr_shCopy(a, b); r = taga(a); goto dec_ret; }
else return bit_negate(b);
} else {
if (f1) return b;
else { Arr* a = allZeroes(ia); arr_shCopy(a, b); r = taga(a); goto dec_ret; }
}
if (f0) return f1? i64EachDec(1, b) : bit_negate(b);
else return f1? b : i64EachDec(0, b);
t_i8: type=t_i8arr; width=0; e0i=( u8)( i8)f0; e1i=( u8)( i8)f1; goto sel;
t_i16: type=t_i16arr; width=1; e0i=(u16)(i16)f0; e1i=(u16)(i16)f1; goto sel;
t_i32: type=t_i32arr; width=2; e0i=(u32)(i32)f0; e1i=(u32)(i32)f1; goto sel;