From 55263bcbc48c9aa2809d2328722019347fbc1720 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Tue, 6 Sep 2022 12:40:18 -0400 Subject: [PATCH 1/5] Handle bit selection from arrays with under 2 elements --- src/builtins/select.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/builtins/select.c b/src/builtins/select.c index e70f6de1..549e4137 100644 --- a/src/builtins/select.c +++ b/src/builtins/select.c @@ -63,6 +63,7 @@ B select_c2(B t, B w, B x) { return r; } usz xia = IA(x); + if (xia==0) thrM("โŠ: Indexing out-of-bounds (0โ‰กโ‰ ๐•ฉ)"); u8 xe = TI(x,elType); u8 we = TI(w,elType); #if SINGELI @@ -100,7 +101,12 @@ B select_c2(B t, B w, B x) { for (usz i=0; i < wia; i++) HARR_ADD(r, i, Get(x, WRAP(wp[i], xia, thrF("โŠ: Indexing out-of-bounds (%iโˆŠ๐•จ, %sโ‰กโ‰ ๐•ฉ)", wp[i], xia)))); \ decG(x); return withFill(HARR_FCD(r,w),xf); \ } - if (we==el_bit && xia>=2) { + if (we==el_bit) { + if (xia<2) { + u64* wp=bitarr_ptr(w); + usz i; for (i=0; i Date: Tue, 6 Sep 2022 12:42:19 -0400 Subject: [PATCH 2/5] Faster selection from boolean list --- src/builtins/select.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/builtins/select.c b/src/builtins/select.c index 549e4137..a7d6b732 100644 --- a/src/builtins/select.c +++ b/src/builtins/select.c @@ -82,8 +82,15 @@ B select_c2(B t, B w, B x) { #define TYPE(W) { W* wp = W##any_ptr(w); \ switch(xe) { default: UD; \ case el_bit: { u64* xp=bitarr_ptr(x); \ - u64* rp; r = m_bitarrc(&rp, w); \ - for (usz i = 0; i < wia; i++) bitp_set(rp, i, bitp_get(xp, WRAP(wp[i], xia, thrF("โŠ: Indexing out-of-bounds (%iโˆŠ๐•จ, %sโ‰กโ‰ ๐•ฉ)", wp[i], xia)))); \ + u64* rp; r = m_bitarrc(&rp, w); \ + u64 b=0; \ + for (usz i = wia; ; ) { \ + i--; \ + usz n = WRAP(wp[i], xia, thrF("โŠ: Indexing out-of-bounds (%iโˆŠ๐•จ, %sโ‰กโ‰ ๐•ฉ)", wp[i], xia)); \ + b <<= 1; \ + b |= (-(xp[n/64] & (1ull<<(n%64)))) >> 63; \ + if (i%64 == 0) { rp[i/64]=b; if (!i) break; } \ + } \ goto dec_ret; \ } \ case el_i8: case el_c8: CASE(u8 ,el2t(xe)) \ From f877a45a634ec5bcb54228aaa1da9b0282c1fc52 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Tue, 6 Sep 2022 16:06:00 -0400 Subject: [PATCH 3/5] Vectorize range checks for 1- and 2-byte indices --- src/builtins/select.c | 66 ++++++++++++++++++++++++++----------------- src/utils/talloc.h | 5 ++-- 2 files changed, 43 insertions(+), 28 deletions(-) diff --git a/src/builtins/select.c b/src/builtins/select.c index a7d6b732..9935c741 100644 --- a/src/builtins/select.c +++ b/src/builtins/select.c @@ -73,33 +73,46 @@ B select_c2(B t, B w, B x) { // if (we==el_i32 && xe==el_i32) { i32* rp; r = m_i32arrc(&rp, w); if (!avx2_select_i32_32((u8*)i32any_ptr(w), (u8*)i32any_ptr(x), (u8*)rp, wia, xia)) thrM("โŠ: Indexing out-of-bounds"); goto dec_ret; } // if (we==el_i32 && xe==el_f64) { f64* rp; r = m_f64arrc(&rp, w); if (!avx2_select_i32_64((u8*)i32any_ptr(w), (u8*)f64any_ptr(x), (u8*)rp, wia, xia)) thrM("โŠ: Indexing out-of-bounds"); goto dec_ret; } #endif - #define CASE(E,TY) { \ - E* rp = m_tyarrc(&r, sizeof(E), w, TY); \ - E* xp = tyany_ptr(x); \ - for (usz i = 0; i < wia; i++) rp[i] = xp[WRAP(wp[i], xia, thrF("โŠ: Indexing out-of-bounds (%iโˆŠ๐•จ, %sโ‰กโ‰ ๐•ฉ)", wp[i], xia))]; \ - goto dec_ret; \ - } + #define CASE(S,E) case S: for (usz i=i0; i> 63; \ - if (i%64 == 0) { rp[i/64]=b; if (!i) break; } \ - } \ - goto dec_ret; \ - } \ - case el_i8: case el_c8: CASE(u8 ,el2t(xe)) \ - case el_i16:case el_c16:CASE(u16,el2t(xe)) \ - case el_i32:case el_c32:CASE(u32,el2t(xe)) \ - case el_f64: CASE(f64,t_f64arr) \ - case el_B:; \ - } \ - M_HARR(r, wia); \ + if (xe==el_bit) { u64* xp=bitarr_ptr(x); \ + u64* rp; r = m_bitarrc(&rp, w); \ + u64 b=0; \ + for (usz i = wia; ; ) { \ + i--; \ + usz n = WRAP(wp[i], xia, thrF("โŠ: Indexing out-of-bounds (%iโˆŠ๐•จ, %sโ‰กโ‰ ๐•ฉ)", wp[i], xia)); \ + b <<= 1; \ + b |= (-(xp[n/64] & (1ull<<(n%64)))) >> 63; \ + if (i%64 == 0) { rp[i/64]=b; if (!i) break; } \ + } \ + goto dec_ret; \ + } \ + if (xe!=el_B) { \ + usz xw = elWidth(xe); \ + void* rp = m_tyarrc(&r, xw, w, el2t(xe)); \ + void* xp = tyany_ptr(x); \ + if (sizeof(W) >= 4) { \ + switch(xw) { default:UD; CASEW(1,u8); CASEW(2,u16); CASEW(4,u32); CASEW(8,f64); } \ + } else { \ + W* wt = NULL; \ + for (usz bl=(1<<14)/sizeof(W), i0=0, i1=0; i0wia) i1=wia; \ + W min=wp[i0], max=min; for (usz i=i0+1; imax) max=e; if (e=(i64)xia) thrF("โŠ: Indexing out-of-bounds (%iโˆŠ๐•จ, %sโ‰กโ‰ ๐•ฉ)", max, xia); \ + W* ip=wp; usz off=xia; \ + if (max>=0) { off=0; if (RARE(min<0)) { \ + if (!wt) wt=TALLOCP(W,i1-i0); ip=wt-i0; \ + for (usz i=i0; idata; // +8 so mm is happy +#define TALLOCP(T,AM) ((T*) ((TAlloc*)mm_alloc(TOFF + (AM)*sizeof(T) + 8, t_temp))->data) // +8 so mm is happy +#define TALLOC(T,N,AM) T* N = TALLOCP(T,AM); #define TOBJ(N) (void*)((u8*)(N) - TOFF) #define TFREE(N) mm_free((Value*)TOBJ(N)); #define TREALLOC(N, AM) talloc_realloc(TOBJ(N), AM) @@ -35,4 +36,4 @@ typedef struct TStack { #define TSSIZE(N) (N##_o->size) TStack* ts_e(TStack* o, u32 elsz, u64 am); -#define ARBOBJ(SZ) (TAlloc*)mm_alloc(sizeof(TAlloc)+(SZ), t_arbObj) \ No newline at end of file +#define ARBOBJ(SZ) (TAlloc*)mm_alloc(sizeof(TAlloc)+(SZ), t_arbObj) From a43a42e4d1f4e22fe08a62c1f04d3c26e0949904 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Tue, 6 Sep 2022 16:30:03 -0400 Subject: [PATCH 4/5] Select from small boolean list by copying to bytes --- src/builtins/select.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/builtins/select.c b/src/builtins/select.c index 9935c741..b4a98452 100644 --- a/src/builtins/select.c +++ b/src/builtins/select.c @@ -121,6 +121,9 @@ B select_c2(B t, B w, B x) { for (usz i=0; i < wia; i++) HARR_ADD(r, i, Get(x, WRAP(wp[i], xia, thrF("โŠ: Indexing out-of-bounds (%iโˆŠ๐•จ, %sโ‰กโ‰ ๐•ฉ)", wp[i], xia)))); \ decG(x); return withFill(HARR_FCD(r,w),xf); \ } + if (xe==el_bit && wia>=256 && wia/4>=xia && we!=el_bit) { + return taga(cpyBitArr(select_c2(m_f64(0), w, taga(cpyI8Arr(x))))); + } if (we==el_bit) { if (xia<2) { u64* wp=bitarr_ptr(w); From b784dac9e1aa42f7429478738e080c5ff8791126 Mon Sep 17 00:00:00 2001 From: dzaima Date: Wed, 7 Sep 2022 00:27:23 +0300 Subject: [PATCH 5/5] make clang not go crazy --- src/builtins/select.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/builtins/select.c b/src/builtins/select.c index b4a98452..2397128e 100644 --- a/src/builtins/select.c +++ b/src/builtins/select.c @@ -104,7 +104,7 @@ B select_c2(B t, B w, B x) { W* ip=wp; usz off=xia; \ if (max>=0) { off=0; if (RARE(min<0)) { \ if (!wt) wt=TALLOCP(W,i1-i0); ip=wt-i0; \ - for (usz i=i0; i