// Dyadic search functions: Member Of (∊), Index of (⊐), Progressive Index of (βŠ’) // π•¨βŠunit or unitβˆŠπ•©: scalar loop with early-exit // SHOULD use simd // SHOULD unify implementations // π•©βŠ’unit or π•¨βŠ’π•© where 1β‰₯≠𝕩: defer to π•¨βŠπ•© // Both arguments with rankβ‰₯1: // High-rank inputs: // Convert to a (lower-rank) typed integer array if cells are ≀62 bits // COULD have special hashing for equal type >64 bit cells, skipping squeezing // COULD try conditionally squeezing ahead-of-time, and not squeezing in bqn_hash // p⊐n & n∊p with short p & long n: n⊸=Β¨ p // bitarrβŠπ•©: more special arithmetic // SHOULD have impls for long p & short n // ≀16-bit elements: lookup tables // Character elements: reinterpret as integer elements // Otherwise, generic hashtable // SHOULD handle up to 64 bit cells via proper typed hash tables // SHOULD have fast path when cell sizes or element types doesn't match #include "../core.h" #include "../utils/hash.h" #include "../utils/talloc.h" #include "../utils/calls.h" RangeFn getRange_fns[el_f64+1]; #if SINGELI extern RangeFn* const simd_getRangeRaw; #define SINGELI_FILE search #include "../utils/includeSingeli.h" #else #define GETRANGE(T,X) bool getRange_##T(void* x0, i64* res, u64 ia) { \ assert(ia>0); T* x=x0; T min=*x,max=min; \ for (ux i=1; imax)max=c;} \ } \ res[0]=min; res[1]=max; return 1; \ } GETRANGE(i8,) GETRANGE(i16,) GETRANGE(i32,) GETRANGE(f64, if (!q_fi64(c)) return 0) #endif #define C2i(F, W, X) C2(F, m_i32(W), X) extern B eq_c2(B,B,B); extern B ne_c2(B,B,B); extern B or_c2(B,B,B); extern B add_c2(B,B,B); extern B sub_c2(B,B,B); extern B mul_c2(B,B,B); static u64 elRange(u8 eltype) { return 1ull<<(1<ft? it : ft; /* Table allocation width */ \ TALLOC(TY, tab0, t); TY* tab = tab0 + t/2; \ usz m=IN##ia, n=FOR##ia; \ void* ip = tyany_ptr(IN); \ void* fp = tyany_ptr(FOR); \ /* Initialize */ \ if (IN.u != FOR.u) { \ if (FOR##e==el_i16 && n1) { rsh=m_shArr(co); shcpy(rsh->a,SH(x),co); } B r0 = widenBitArr(x, co); usz csz = shProd(SH(r0),co,RNK(r0)) << elWidthLogBits(TI(r0,elType)); u8 t; if (csz==8) t = t_i8slice; else if (csz==16) t = t_i16slice; else if (csz==32) t = t_i32slice; else if (csz==64) t = t_f64slice; else UD; TySlice* r = m_arr(sizeof(TySlice), t, ria); r->p = a(r0); r->a = tyany_ptr(r0); if (co>=1) arr_shSetUO((Arr*)r, co, rsh); else arr_shVec((Arr*)r); return taga(r); } static NOINLINE B cpyToElLog(B x, u8 xe, u8 lb) { switch(lb) { default: UD; case 0: return taga(cpyBitArr(x)); case 3: return taga(elNum(xe)? cpyI8Arr(x) : cpyC8Arr(x)); case 4: return taga(elNum(xe)? cpyI16Arr(x) : cpyC16Arr(x)); case 5: return taga(elNum(xe)? cpyI32Arr(x) : cpyC32Arr(x)); case 6: return taga(cpyF64Arr(x)); } } static NOINLINE B2 splitCells(B n, B p, u8 mode) { // 0:∊ 1:⊐ 2:βŠ’ #define SYMB (mode==0? "∊" : mode==1? "⊐" : "βŠ’") #define ARG_N (mode? "𝕩" : "𝕨") #define ARG_P (mode? "𝕨" : "𝕩") if (isAtm(p) || RNK(p)==0) thrF("%U: %U cannot have rank 0", SYMB, ARG_P); ur pr = RNK(p); if (isAtm(n)) n = m_unit(n); ur nr = RNK(n); if (nr < pr-1) thrF("%U: Rank of %U must be at least the cell rank of %U (%H ≑ ≒𝕨, %H ≑ ≒𝕩)", SYMB, ARG_N, ARG_P, mode?p:n, mode?n:p); ur pcr = pr-1; ur nco = nr-pcr; if (nco>0 && eqShPart(SH(n)+nco, SH(p)+1, pcr)) { u8 ne = TI(n,elType); u8 pe = TI(p,elType); if (nepeb? neb : peb; ux rb = csz<xe) x=taga(cpyC16Arr(x)); \ else w=taga(cpyC16Arr(w)); \ we = xe = el_i16; \ goto tyEls; \ } \ we-=el_c8-el_i8; xe-=el_c8-el_i8; goto tyEls; \ } B indexOf_c2(B t, B w, B x) { if (RARE(!isArr(w) || RNK(w)!=1)) { B2 t = splitCells(x, w, 1); w = t.p; x = t.n; } if (!isArr(x) || RNK(x)==0) { B el = isArr(x)? IGetU(x,0) : x; usz res = indexOfOne(w, el); decG(w); dec(x); B r = m_vec1(m_f64(res)); arr_shAtm(a(r)); // replaces shape return r; } else { u8 we = TI(w,elType); usz wia = IA(w); u8 xe = TI(x,elType); usz xia = IA(x); if (wia==0 || xia==0) { decG(w); return i64EachDec(0, x); } if (elNum(we) && elNum(xe)) { tyEls: if (we==el_bit) { u64* wp = bitarr_ptr(w); u64 w0 = 1 & wp[0]; u64 i = bit_find(wp, wia, !w0); decG(w); if (i!=wia) incG(x); B r = C2i(mul, wia , C2i(ne, w0, x)) ; return i==wia? r : C2(sub, r, C2i(mul, wia-i, C2i(eq, !w0, x))); } if (wia<=(we<=el_i16?4:16) && xia>16) { SGetU(w); #define XEQ(I) C2(ne, GetU(w,I), incG(x)) B r = XEQ(wia-1); for (usz i=wia-1; i--; ) r = C2(mul, XEQ(i), C2i(add, 1, r)); #undef XEQ decG(w); decG(x); return r; } if (xia+wia>20 && we<=el_i16 && xe<=el_i16) { #if SINGELI if (wia>256 && we==el_i8 && xe==el_i8) { TALLOC(u8, tab, 256*(1+sizeof(usz))); usz* ind = (usz*)(tab+256); void* fp = tyany_ptr(x); simd_index_tab_u8(tyany_ptr(w), wia, fp, xia, tab, ind); decG(w); i32* rp; B r = m_i32arrc(&rp, x); for (usz i=0; ia[p].val = i; } for (usz i = 0; i < xia; i++) rp[i] = getD_b2i(map, GetU(x,i), wia); free_b2i(map); decG(w); decG(x); return reduceI32Width(r, wia); } } B enclosed_0, enclosed_1; B memberOf_c2(B t, B w, B x) { if (isAtm(x) || RNK(x)!=1) { B2 t = splitCells(w, x, false); w = t.n; x = t.p; } if (isAtm(w)) goto single; ur wr = RNK(w); if (wr>0) goto many; B w0 = IGet(w, 0); dec(w); w = w0; goto single; B r; single: { r = incG(indexOfOne(x,w)==IA(x)? enclosed_0 : enclosed_1); dec(w); goto dec_x; } many: { u8 we = TI(w,elType); usz wia = IA(w); u8 xe = TI(x,elType); usz xia = IA(x); if (wia==0 || xia==0) { decG(x); return i64EachDec(0, w); } if (elNum(we) && elNum(xe)) { tyEls: #define WEQ(V) C2(eq, incG(w), V) if (xe==el_bit) { u64* xp = bitarr_ptr(x); u64 x0 = 1 & xp[0]; r = WEQ(m_usz(x0)); if (bit_has(xp, xia, !x0)) r = C2(or, r, WEQ(m_usz(!x0))); decG(w); goto dec_x; } u8 me = we>xe?we:xe; if (xia<=(me==el_i8?1:me==el_i16?4:16) && wia>16) { SGetU(x); r = WEQ(GetU(x,0)); for (usz i=1; i20 && we<=el_i16 && xe<=el_i16) { #if SINGELI if (we==el_i8 && xe==el_i8) { TALLOC(u8, tab, 256); u64* rp; r = m_bitarrc(&rp, w); simd_member_u8(tyany_ptr(x), xia, tyany_ptr(w), wia, rp, tab); TFREE(tab); decG(w); goto dec_x; } #endif TABLE(x, w, i8, 0, 1) return taga(cpyBitArr(r)); } } else { CHR_TO_INT; } H_Sb* set = m_Sb(64); SGetU(x) SGetU(w) bool had; for (usz i = 0; i < xia; i++) mk_Sb(&set, GetU(x,i), &had); u64* rp; r = m_bitarrc(&rp, w); for (usz i = 0; i < wia; i++) bitp_set(rp, i, has_Sb(set, GetU(w,i))); free_Sb(set); decG(w); goto dec_x; } dec_x:; decG(x); return r; } #undef CHR_TO_INT B count_c2(B t, B w, B x) { if (RARE(!isArr(w) || RNK(w)!=1)) { B2 t = splitCells(x, w, 2); w = t.p; x = t.n; } if (!isArr(x) || IA(x)<=1 || IA(w)==0) return C2(indexOf, w, x); u8 we = TI(w,elType); usz wia = IA(w); u8 xe = TI(x,elType); usz xia = IA(x); i32* rp; B r = m_i32arrc(&rp, x); TALLOC(usz, wnext, wia+1); wnext[wia] = wia; if (we<=el_i16 && xe<=el_i16) { if (we==el_bit) { w = toI8Any(w); we = TI(w,elType); } if (xe==el_bit) { x = toI8Any(x); xe = TI(x,elType); } el8or16:; usz it = elRange(we); // Range of writes usz ft = elRange(xe); // Range of lookups usz t = it>ft? it : ft; // Table allocation width TALLOC(i32, tab0, t); i32* tab = tab0 + t/2; usz m=wia, n=xia; void* ip = tyany_ptr(w); void* fp = tyany_ptr(x); // Initialize if (xe==el_i16 && n=el_c8 && we<=el_c16 && xe>=el_c8 && xe<=el_c16) { we-= el_c8-el_i8; xe-= el_c8-el_i8; goto el8or16; } else { H_b2i* map = m_b2i(64); SGetU(x) SGetU(w) for (usz i = wia; i--; ) { bool had; u64 p = mk_b2i(&map, GetU(w,i), &had); wnext[i] = had ? map->a[p].val : wia; map->a[p].val = i; } for (usz i = 0; i < xia; i++) { bool had; u64 p = getQ_b2i(map, GetU(x,i), &had); usz j = wia; if (had) { j = map->a[p].val; map->a[p].val = wnext[j]; } rp[i] = j; } free_b2i(map); } TFREE(wnext); decG(w); decG(x); return reduceI32Width(r, wia); } // if nanBad and input contains a NaN, doesn't consume and returns m_f64(0) // otherwise, consumes and returns an array with -0 (and NaNs if !nanBad) normalized B asNormalized(B x, usz n, bool nanBad) { f64* fp = f64any_ptr(x); ux i = 0; #if SINGELI_SIMD i = simd_search_normalizable(fp, n); if (i!=n) goto some; #else for (; i < n; i++) if (r_f64u(fp[i])==r_f64u(-0.0) || fp[i]!=fp[i]) goto some; #endif return x; some:; f64* rp; B r; if (TY(x)==t_f64arr && reusable(x)) { rp = fp; r = x; } else { r = m_f64arrc(&rp, x); COPY_TO(rp, el_f64, 0, x, 0, i); } if (nanBad) { #if SINGELI_SIMD if (RARE(simd_copy_ordered(rp+i, fp+i, n-i))) goto bad; #else for (; i < n; i++) { if (RARE(fp[i]!=fp[i])) goto bad; rp[i] = fp[i]+0.0; } #endif } else { for (; i < n; i++) rp[i] = normalizeFloat(fp[i]); } if (r.u!=x.u) decG(x); return r; bad: if (r.u!=x.u) mm_free(v(r)); return m_f64(0); } bool getRangeBool(void* xp, i64* res, u64 ia) { assert(ia>0); u64 x0 = 1 & ((u64*)xp)[0]; if (bit_has(xp, ia, !x0)) { res[0]=0; res[1]=1; } else { res[0]=res[1]=x0; } return true; } void search_init(void) { { u64* p; Arr* a=m_bitarrp(&p, 1); arr_shAtm(a); *p= 0; gc_add(enclosed_0=taga(a)); } { u64* p; Arr* a=m_bitarrp(&p, 1); arr_shAtm(a); *p=~0ULL; gc_add(enclosed_1=taga(a)); } getRange_fns[0] = getRangeBool; #if SINGELI for (i32 i=0; i<4; i++) getRange_fns[i+1] = simd_getRangeRaw[i]; #else getRange_fns[1] = getRange_i8; getRange_fns[2] = getRange_i16; getRange_fns[3] = getRange_i32; getRange_fns[4] = getRange_f64; #endif }