From ff9919dde2f720422f2332b52e9d21a4601dde89 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Mon, 3 Jul 2023 16:01:52 -0400 Subject: [PATCH] AVX2 binary search on one lane of i8 (unused for now) --- src/singeli/src/bins.singeli | 53 +++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/src/singeli/src/bins.singeli b/src/singeli/src/bins.singeli index f7a24b53..b5c7dfa8 100644 --- a/src/singeli/src/bins.singeli +++ b/src/singeli/src/bins.singeli @@ -1,7 +1,46 @@ include './base' +if (hasarch{'AVX2'}) { + include './sse' + include './avx' + include './avx2' +} +include './mask' include 'util/tup' -def bin_search{lt, w, wn, x, n, res} = { +def ceil_log2{n:u64} = 64 - clz{n+1} + +def bin_search_vec{up, w:*i8, wn, x:*i8, n, res:*i8 & hasarch{'AVX2'}} = { + def T = i8; def I = u8 + def lt = if (up) <; else > + def pre = (if (up) minvalue else maxvalue){T} + def vl = 32 + def V = [vl]T; def H = [vl/2]T + def U = [vl]I + log := ceil_log2{wn-1} + l := 1<> 1) # Type doesn't matter but u8 would fail + } + store{*U~~(res+j), 0, s - off} + j += vl + } + if (j != n) { j = n-vl; goto{tail} } +} + +def bin_search_branchless{up, w, wn, x, n, res} = { + def lt = if (up) <; else > ws := w - 1 l0 := wn + 1 # Take a list of indices in x/res to allow unrolling @@ -24,11 +63,17 @@ def bin_search{lt, w, wn, x, n, res} = { each{searches, tup{4, 1}} } -fn bins_branchless{T, up}(w:*void, wn:u64, x:*void, xn:u64, r:*i32) : void = { - bin_search{if (up) <; else >, *T~~w, wn, *T~~x, xn, r} +fn bins{T, up}(w:*void, wn:u64, x:*void, xn:u64, r:*i32) : void = { + if (hasarch{'AVX2'} and T==i8 and wn<16 and xn>=32) { + bin_search_vec{up, *T~~w, wn, *T~~x, xn, *i8~~r} + # Slow and useless: need to allocate i8 result + j:=xn; while (j > 0) { --j; store{r, j, cast_i{i32, load{*i8~~r, j}}} } + } else { + bin_search_branchless{up, *T~~w, wn, *T~~x, xn, r} + } } exportT{ 'si_bins', - join{table{bins_branchless, tup{i8,i16,i32,f64}, tup{1,0}}} + join{table{bins, tup{i8,i16,i32,f64}, tup{1,0}}} }