From 302c2f926f89f5972eb3aad4ed28fa1aa1fc5106 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Fri, 7 Mar 2025 15:26:50 -0500 Subject: [PATCH] AVX2 k/bool for odd 64= 2}; assert{wv <= 64} + assert{wv >= 2} nw := cdiv{rlen, 64} if (wv&1 == 0) { p := ctz{wv | 8} # Power of two for second replicate @@ -555,14 +555,15 @@ def proc_mod_dat{swap_data:W} = { else tup{8, get_byteperm{}} } # Fill in higher steps - def get_mod_permuter{} = { + def get_mod_permuter{width} = { def get_swap{l} = { def mask = extract_modperm_mask{swap_data, swap_lane, l} modperm_shuf_step{., l, mask} } - def swap_x = on_len_range{get_swap, partwidth, ww} + def swap_x = on_len_range{get_swap, partwidth, width} {x} => partperm{swap_x{x}} } + def get_mod_permuter{} = get_mod_permuter{ww} tup{partperm, get_mod_permuter} } @@ -577,15 +578,18 @@ def rep_const_bool_odd{k, x, r, nw} = { # Modular permutation: small-k cases may use a limited permutation # on bytes or 32-bit ints; general case uses the whole thing swtab:*W = each{modperm_dat{W, .}, 1+2*iota{32}} - swap_data := load{swtab, k>>1} + swap_data := load{swtab, (k%64)>>1} def {partperm, get_full_permute} = proc_mod_dat{swap_data} def sp_max = if (any_sel) 8 else 4 if (k < sp_max) { rep_const_bool_small_odd{W, sp_max, k, getter{partperm}, output} - } else { + } else if (not avx2 or k < 64) { def get_swap_x = getter{get_full_permute{}} rep_const_bool_odd_mask4{W, k, get_swap_x, output, cdiv{nw, vcount{W}}} + } else { + def get_swap_x = getter{get_full_permute{64}} + rep_const_bool_odd_loose_mask{W, k, get_swap_x, output} } flush{} } @@ -599,6 +603,7 @@ def rep_const_bool_odd_mask4{ } = { def ifvec{g} = match (M) { {[_](u64)} => g; {_} => ({v}=>v) } def scal = ifvec{{v} => M**v} + assert{k < 64} # Fundamental operation: shifts act as order-k cyclic group on masks def advance{m, sh} = advance_spaced_mask{k, m, sh} @@ -729,4 +734,55 @@ def rep_const_bool_small_odd{W=[wl](u64), max_wv, wv, get_perm_x, output} = { } } +# Odd factors larger than 64 +# AVX2-only because scalar should be about as good otherwise +def rep_const_bool_odd_loose_mask{V=[vl==4](u64), k, get_modperm_x, output if hasarch{'AVX2'}} = { + assert{k > 64} + # Distance from end to previous row boundary (-k <= q < 0) + q := -make{V, 64*(1+iota{vl})} + def q_mod{} = { d:=q+V**k; q = blend_top{q,d, d} } + o:u64 = width{V}; while (o>k) { o-=k; q_mod{} } + + km:= k%64 + i:usz = 0; iv:usz = 0 # Words and vectors completed + def step{perm, diff} = { + # Mod-64 mask with 1 bit per word + m:= V**1 << (q & V**63) + # Indicator of which bits are actual boundaries + def S = ty_s{V}; a:= S**(-65) < S~~q + q-= V**o; q_mod{} + # Set to bit from perm, but below a&m xor with diff to get previous + base:= (m & perm) == m + md:= (a & m) & diff + output{base ^ (md + (md==m))} + } + {xp, xd, perm, diff}:= 4**(V**0) + while (1) { + # get_modperm_x permutes each 64-bit word + # Each iteration of this loop handles one permuted word + def first = shuf{., 4**0} + if (i%4 == 0) { + xp = get_modperm_x{} + # Shift by 1, or k%64 in mod-space + # Then the low bit of each word has to be moved to the next + # As before, first bit is wrong but unused + xl:= xp>>(64-km) + xo:= (xp<