From 0602927b17d6c1ea4709898151505f2a3b7d6794 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Mon, 5 Aug 2024 09:57:05 -0400 Subject: [PATCH] Move pdep-based k/bool code to Singeli --- src/builtins/slash.c | 37 ++++------------------------ src/singeli/src/replicate.singeli | 40 +++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 32 deletions(-) diff --git a/src/builtins/slash.c b/src/builtins/slash.c index bad784ba..98810542 100644 --- a/src/builtins/slash.c +++ b/src/builtins/slash.c @@ -82,6 +82,8 @@ extern void (*const si_scan_max_i32)(int32_t* v0,int32_t* v1,uint64_t v2); #define SINGELI_FILE slash #include "../utils/includeSingeli.h" + extern uint64_t* const si_spaced_masks; + #define get_spaced_mask(i) si_spaced_masks[i-1] #define SINGELI_FILE replicate #include "../utils/includeSingeli.h" #endif @@ -765,38 +767,9 @@ B slash_c2(B t, B w, B x) { if (xl == 0) { u64* xp = bitarr_ptr(x); u64* rp; r = m_bitarrv(&rp, s); - #if FAST_PDEP - if (wv <= 52) { - #if SINGELI - u64 m = si_spaced_masks[wv-1]; - #else - u64 m = (u64)-1 / (((u64)1<>= d; - if ((j&(wv-1))==0) xw = xp[++i]; - u64 rw = _pdep_u64(xw, m); - rp[j] = (rw<> (xi%8); - u64 ex = (xw&mt)<>o|(xw&1)); - o += q; - bool oo = o>=wv; xi+=d+oo; o-=wv&-oo; - } - } - goto atmW_maybesh; - } + #if SINGELI + if (si_constrep_bool(wv, xp, rp, s)) ; // Handles small wv + else #endif if (wv <= 256) { BOOL_REP_XOR_SCAN(wv) } else { BOOL_REP_OVER(wv, xlen) } diff --git a/src/singeli/src/replicate.singeli b/src/singeli/src/replicate.singeli index d4fe0aba..c789c571 100644 --- a/src/singeli/src/replicate.singeli +++ b/src/singeli/src/replicate.singeli @@ -1,5 +1,7 @@ include './base' +if_inline (hasarch{'BMI2'}) include './bmi2' include './mask' +include './spaced' def ind_types = tup{i8, i16, i32} def dat_types = tup{...ind_types, u64} @@ -284,3 +286,41 @@ fn rep_const{T}(wv:u64, x:*void, r:*void, n:u64) : void = { } exportT{'si_constrep', each{rep_const, dat_types}} + + + +# Constant replicate on boolean +fn rep_const_bool{}(wv:usz, x:*u64, r:*u64, rlen:usz) : u1 = 0 +fn rep_const_bool{if hasarch{'BMI2'}}(wv:usz, x:*u64, r:*u64, rlen:usz) : u1 = { + if (wv > 52) return{0} + m:u64 = spaced_mask_of{wv} + xw:u64 = 0 + d := cast_i{usz, popc{m}} # == 64/wv + nw := cdiv{rlen, 64} + if (m&1 != 0) { # Power of two + i := -usz~~1 + @for (r over j to nw) { + xw >>= d + if ((j&(wv-1))==0) { ++i; xw = load{x, i} } + rw := pdep{xw, m} + r = (rw<> (xi%8) + ex := (xw & mt) << tsh + rw := pdep{xw, m} + r = ((rw-ex)<<(wv-o)) - (rw>>o|(xw&1)) + o += q + oo := o>=wv; xi+=d+promote{usz,oo}; o-=wv&-oo + } + } + 1 +} +export{'si_constrep_bool', rep_const_bool{}}