1use crate::simd::Simd;
2use core::mem;
3
4impl<const N: usize> Simd<u8, N> {
5 #[inline]
14 pub fn swizzle_dyn(self, idxs: Simd<u8, N>) -> Self {
15 #![allow(unused_imports, unused_unsafe)]
16 #[cfg(all(
17 target_arch = "arm",
18 target_feature = "v7",
19 target_feature = "neon",
20 target_endian = "little"
21 ))]
22 use core::arch::arm::{uint8x8_t, vtbl1_u8};
23 #[cfg(target_arch = "wasm32")]
24 use core::arch::wasm32 as wasm;
25 #[cfg(target_arch = "wasm64")]
26 use core::arch::wasm64 as wasm;
27 #[cfg(target_arch = "x86")]
28 use core::arch::x86;
29 #[cfg(target_arch = "x86_64")]
30 use core::arch::x86_64 as x86;
31 unsafe {
33 #[allow(
34 unreachable_patterns,
35 reason = "avoids writing verbose cfg(not), earlier branches take priority"
36 )]
37 match N {
38 #[cfg(all(
40 any(target_arch = "aarch64", target_arch = "arm64ec"),
41 target_feature = "neon",
42 target_endian = "little"
43 ))]
44 8 | 16 | 24 | 32 | 48 | 64 => aarch64_swizzle(self, idxs),
45
46 #[cfg(all(
48 target_arch = "arm",
49 target_feature = "v7",
50 target_feature = "neon",
51 target_endian = "little"
52 ))]
53 16 => transize(armv7_neon_swizzle_u8x16, self, idxs),
54
55 #[cfg(target_feature = "simd128")]
57 16 => transize(wasm::i8x16_swizzle, self, idxs),
58 #[cfg(target_feature = "simd128")]
59 32 => transize(swizzle_dyn_split::<32, 16>, self, idxs),
60
61 #[cfg(all(target_arch = "loongarch64", target_feature = "lsx"))]
63 16 => transize(loong64_lsx_swizzle, self, idxs),
64 #[cfg(all(target_arch = "loongarch64", target_feature = "lasx"))]
65 32 => transize(loong64_lasx_swizzle, self, idxs),
66 #[cfg(all(target_arch = "loongarch64", target_feature = "lsx"))]
67 32 => transize(swizzle_dyn_split::<32, 16>, self, idxs),
68 #[cfg(all(target_arch = "loongarch64", target_feature = "lasx"))]
69 64 => transize(swizzle_dyn_split::<64, 32>, self, idxs),
70
71 #[cfg(target_feature = "ssse3")]
73 16 => transize(x86::_mm_shuffle_epi8, self, zeroing_idxs(idxs)),
74 #[cfg(all(target_feature = "avx512vl", target_feature = "avx512vbmi"))]
75 32 => {
76 let swizzler = |bytes, idxs| {
77 let idxs =
80 x86::_mm256_min_epu8(idxs, Simd::<u8, 32>::splat(N as u8).into());
81 x86::_mm256_permutex2var_epi8(bytes, idxs, x86::_mm256_setzero_si256())
82 };
83 transize(swizzler, self, idxs)
84 }
85 #[cfg(target_feature = "avx2")]
86 32 => transize(avx2_pshufb, self, idxs),
87 #[cfg(target_feature = "ssse3")]
88 32 => transize(swizzle_dyn_split::<32, 16>, self, idxs),
89 #[cfg(all(target_feature = "avx512vl", target_feature = "avx512vbmi"))]
91 64 => {
92 let swizzler = |bytes, idxs| {
93 let idxs =
96 x86::_mm512_min_epu8(idxs, Simd::<u8, 64>::splat(N as u8).into());
97 x86::_mm512_permutex2var_epi8(bytes, idxs, x86::_mm512_setzero_si512())
98 };
99 transize(swizzler, self, idxs)
100 }
101 #[cfg(target_feature = "avx2")]
102 64 => transize(swizzle_dyn_split::<64, 32>, self, idxs),
103
104 _ => {
106 let mut array = [0; N];
107 for (i, k) in idxs.to_array().into_iter().enumerate() {
108 if (k as usize) < N {
109 array[i] = self[k as usize];
110 };
111 }
112 array.into()
113 }
114 }
115 }
116 }
117}
118
119#[allow(dead_code, reason = "only used on some targets/features")]
120fn swizzle_dyn_split<const N: usize, const HALF: usize>(
123 bytes: Simd<u8, N>,
124 idxs: Simd<u8, N>,
125) -> Simd<u8, N> {
126 let table_low = bytes.extract::<0, HALF>();
127 let table_high = bytes.extract::<HALF, HALF>();
128 let idxs_low = idxs.extract::<0, HALF>();
129 let idxs_high = idxs.extract::<HALF, HALF>();
130 let table_high_offset = Simd::<u8, HALF>::splat(HALF as u8);
131
132 let output_low_from_low = table_low.swizzle_dyn(idxs_low);
133 let output_low_from_high = table_high.swizzle_dyn(idxs_low - table_high_offset);
134 let output_low = output_low_from_low | output_low_from_high;
135
136 let output_high_from_low = table_low.swizzle_dyn(idxs_high);
137 let output_high_from_high = table_high.swizzle_dyn(idxs_high - table_high_offset);
138 let output_high = output_high_from_low | output_high_from_high;
139
140 use crate::simd::Swizzle;
145 struct CombineHalves;
146 impl<const N: usize> Swizzle<N> for CombineHalves {
147 const INDEX: [usize; N] = const {
148 let mut index = [0; N];
149 let mut i = 0;
150 while i < N {
151 index[i] = i;
152 i += 1;
153 }
154 index
155 };
156 }
157
158 CombineHalves::concat_swizzle(output_low, output_high)
159}
160
161#[cfg(all(
167 target_arch = "arm",
168 target_feature = "v7",
169 target_feature = "neon",
170 target_endian = "little"
171))]
172unsafe fn armv7_neon_swizzle_u8x16(bytes: Simd<u8, 16>, idxs: Simd<u8, 16>) -> Simd<u8, 16> {
173 use core::arch::arm::{uint8x8x2_t, vcombine_u8, vget_high_u8, vget_low_u8, vtbl2_u8};
174 unsafe {
176 let bytes = uint8x8x2_t(vget_low_u8(bytes.into()), vget_high_u8(bytes.into()));
177 let lo = vtbl2_u8(bytes, vget_low_u8(idxs.into()));
178 let hi = vtbl2_u8(bytes, vget_high_u8(idxs.into()));
179 vcombine_u8(lo, hi).into()
180 }
181}
182
183#[cfg(all(
188 any(target_arch = "aarch64", target_arch = "arm64ec"),
189 target_feature = "neon",
190 target_endian = "little"
191))]
192unsafe fn aarch64_swizzle<const N: usize>(bytes: Simd<u8, N>, idxs: Simd<u8, N>) -> Simd<u8, N> {
193 use core::arch::aarch64::*;
194 use core::mem::transmute_copy;
195
196 unsafe {
198 match N {
199 8 => transmute_copy(&vtbl1_u8(transmute_copy(&bytes), transmute_copy(&idxs))),
200 16 => transmute_copy(&vqtbl1q_u8(transmute_copy(&bytes), transmute_copy(&idxs))),
201 24 => {
202 let bytes: uint8x8x3_t = transmute_copy(&bytes);
203 let idxs: uint8x8x3_t = transmute_copy(&idxs);
204
205 let ret0 = vtbl3_u8(bytes, idxs.0);
206 let ret1 = vtbl3_u8(bytes, idxs.1);
207 let ret2 = vtbl3_u8(bytes, idxs.2);
208
209 let ret = uint8x8x3_t(ret0, ret1, ret2);
210 transmute_copy(&ret)
211 }
212 32 => {
213 let bytes: uint8x16x2_t = transmute_copy(&bytes);
214 let idxs: uint8x16x2_t = transmute_copy(&idxs);
215
216 let ret0 = vqtbl2q_u8(bytes, idxs.0);
217 let ret1 = vqtbl2q_u8(bytes, idxs.1);
218
219 let ret = uint8x16x2_t(ret0, ret1);
220 transmute_copy(&ret)
221 }
222 48 => {
223 let bytes: uint8x16x3_t = transmute_copy(&bytes);
224 let idxs: uint8x16x3_t = transmute_copy(&idxs);
225
226 let ret0 = vqtbl3q_u8(bytes, idxs.0);
227 let ret1 = vqtbl3q_u8(bytes, idxs.1);
228 let ret2 = vqtbl3q_u8(bytes, idxs.2);
229
230 let ret = uint8x16x3_t(ret0, ret1, ret2);
231 transmute_copy(&ret)
232 }
233 64 => {
234 let bytes: uint8x16x4_t = transmute_copy(&bytes);
235 let idxs: uint8x16x4_t = transmute_copy(&idxs);
236
237 let ret0 = vqtbl4q_u8(bytes, idxs.0);
238 let ret1 = vqtbl4q_u8(bytes, idxs.1);
239 let ret2 = vqtbl4q_u8(bytes, idxs.2);
240 let ret3 = vqtbl4q_u8(bytes, idxs.3);
241
242 let ret = uint8x16x4_t(ret0, ret1, ret2, ret3);
243 transmute_copy(&ret)
244 }
245 _ => unreachable!(),
246 }
247 }
248}
249
250#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
255#[target_feature(enable = "avx2")]
256#[allow(unused)]
257#[inline]
258#[allow(clippy::let_and_return)]
259unsafe fn avx2_pshufb(bytes: Simd<u8, 32>, idxs: Simd<u8, 32>) -> Simd<u8, 32> {
260 #[cfg(target_arch = "x86")]
261 use core::arch::x86;
262 #[cfg(target_arch = "x86_64")]
263 use core::arch::x86_64 as x86;
264 use x86::_mm256_permute2x128_si256 as avx2_cross_shuffle;
265 use x86::_mm256_shuffle_epi8 as avx2_half_pshufb;
266 unsafe {
268 let lolo = avx2_cross_shuffle::<0x00>(bytes.into(), bytes.into());
269 let hihi = avx2_cross_shuffle::<0x11>(bytes.into(), bytes.into());
270
271 let control = x86::_mm256_adds_epu8(idxs.into(), x86::_mm256_set1_epi8(0x60));
275
276 let select_high = x86::_mm256_slli_epi16::<3>(control);
278 let from_low = avx2_half_pshufb(lolo, control);
279 let from_high = avx2_half_pshufb(hihi, control);
280 x86::_mm256_blendv_epi8(from_low, from_high, select_high).into()
281 }
282}
283
284#[cfg(all(target_arch = "loongarch64", target_feature = "lsx"))]
289unsafe fn loong64_lsx_swizzle(bytes: Simd<u8, 16>, idxs: Simd<u8, 16>) -> Simd<u8, 16> {
290 use core::arch::loongarch64::{lsx_vand_v, lsx_vshuf_b, lsx_vslei_bu};
291 unsafe {
293 let bytes = lsx_vshuf_b(bytes.into(), bytes.into(), idxs.into());
294 let mask = lsx_vslei_bu::<15>(idxs.into());
295 lsx_vand_v(bytes, mask).into()
296 }
297}
298
299#[cfg(all(target_arch = "loongarch64", target_feature = "lasx"))]
304unsafe fn loong64_lasx_swizzle(bytes: Simd<u8, 32>, idxs: Simd<u8, 32>) -> Simd<u8, 32> {
305 use core::arch::loongarch64::{lasx_xvand_v, lasx_xvpermi_q, lasx_xvshuf_b, lasx_xvslei_bu};
306 unsafe {
308 let lolo = lasx_xvpermi_q::<0x00>(bytes.into(), bytes.into());
309 let hihi = lasx_xvpermi_q::<0x11>(bytes.into(), bytes.into());
310 let bytes = lasx_xvshuf_b(hihi, lolo, idxs.into());
311 let mask = lasx_xvslei_bu::<31>(idxs.into());
312 lasx_xvand_v(bytes, mask).into()
313 }
314}
315
316#[allow(dead_code)]
324#[inline(always)]
325unsafe fn transize<T, const N: usize>(
326 f: unsafe fn(T, T) -> T,
327 a: Simd<u8, N>,
328 b: Simd<u8, N>,
329) -> Simd<u8, N> {
330 unsafe { mem::transmute_copy(&f(mem::transmute_copy(&a), mem::transmute_copy(&b))) }
332}
333
334#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
336#[allow(unused)]
337#[inline(always)]
338fn zeroing_idxs<const N: usize>(idxs: Simd<u8, N>) -> Simd<u8, N> {
339 let zeroing_bits = idxs + Simd::splat((127 - N + 1) as u8);
342 idxs | zeroing_bits
343}