From c1ff579e2f9efe841851c0dd668ea593b7a4d00a Mon Sep 17 00:00:00 2001 From: Yikai-Liao Date: Wed, 25 Nov 2026 17:32:18 +0800 Subject: [PATCH] Use native x86 shuffles for reverse() Lower all non-mask x86 reverse() implementations (8/16/32/64-bit elements across 128/256/512-bit vectors) directly to native shuffle/permute intrinsics instead of SSE2 scalar fallbacks or byte-level swizzle_dyn. Vector-backed masks on SSE2/SSE4.2/AVX2 inherit this via reverse_vector_mask_method, and wider-than-native vectors inherit it via generic_op split/combine. --- fearless_simd/src/generated/avx2.rs | 287 +++++++++++----- fearless_simd/src/generated/avx512.rs | 460 +++++++++++++++++--------- fearless_simd/src/generated/sse2.rs | 108 ++++-- fearless_simd/src/generated/sse4_2.rs | 118 +++++-- fearless_simd_gen/src/mk_x86.rs | 119 +++++-- 5 files changed, 767 insertions(+), 325 deletions(-) diff --git a/fearless_simd/src/generated/avx2.rs b/fearless_simd/src/generated/avx2.rs index 4a0bd797..616c820b 100644 --- a/fearless_simd/src/generated/avx2.rs +++ b/fearless_simd/src/generated/avx2.rs @@ -145,9 +145,14 @@ impl Simd for Avx2 { } #[inline(always)] fn reverse_f32x4(self, a: f32x4) -> f32x4 { - let indices: u8x16 = - [12, 13, 14, 15, 8, 9, 10, 11, 4, 5, 6, 7, 0, 1, 2, 3].simd_into(self); - self.swizzle_dyn_f32x4(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: f32x4) -> f32x4 { + let a = a.into(); + _mm_shuffle_ps::<0b00_01_10_11>(a, a).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_f32x4(self, a: f32x4, b: f32x4) -> f32x4 { @@ -861,9 +866,17 @@ impl Simd for Avx2 { } #[inline(always)] fn reverse_i8x16(self, a: i8x16) -> i8x16 { - let indices: u8x16 = - [15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0].simd_into(self); - self.swizzle_dyn_i8x16(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: i8x16) -> i8x16 { + _mm_shuffle_epi8( + a.into(), + _mm_setr_epi8(15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0), + ) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_i8x16(self, a: i8x16, b: i8x16) -> i8x16 { @@ -1547,9 +1560,17 @@ impl Simd for Avx2 { } #[inline(always)] fn reverse_u8x16(self, a: u8x16) -> u8x16 { - let indices: u8x16 = - [15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0].simd_into(self); - self.swizzle_dyn_u8x16(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: u8x16) -> u8x16 { + _mm_shuffle_epi8( + a.into(), + _mm_setr_epi8(15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0), + ) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_u8x16(self, a: u8x16, b: u8x16) -> u8x16 { @@ -2597,9 +2618,17 @@ impl Simd for Avx2 { } #[inline(always)] fn reverse_i16x8(self, a: i16x8) -> i16x8 { - let indices: u8x16 = - [14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1].simd_into(self); - self.swizzle_dyn_i16x8(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: i16x8) -> i16x8 { + _mm_shuffle_epi8( + a.into(), + _mm_setr_epi8(14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1), + ) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_i16x8(self, a: i16x8, b: i16x8) -> i16x8 { @@ -3273,9 +3302,17 @@ impl Simd for Avx2 { } #[inline(always)] fn reverse_u16x8(self, a: u16x8) -> u16x8 { - let indices: u8x16 = - [14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1].simd_into(self); - self.swizzle_dyn_u16x8(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: u16x8) -> u16x8 { + _mm_shuffle_epi8( + a.into(), + _mm_setr_epi8(14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1), + ) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_u16x8(self, a: u16x8, b: u16x8) -> u16x8 { @@ -4199,9 +4236,13 @@ impl Simd for Avx2 { } #[inline(always)] fn reverse_i32x4(self, a: i32x4) -> i32x4 { - let indices: u8x16 = - [12, 13, 14, 15, 8, 9, 10, 11, 4, 5, 6, 7, 0, 1, 2, 3].simd_into(self); - self.swizzle_dyn_i32x4(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: i32x4) -> i32x4 { + _mm_shuffle_epi32::<0b00_01_10_11>(a.into()).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_i32x4(self, a: i32x4, b: i32x4) -> i32x4 { @@ -4857,9 +4898,13 @@ impl Simd for Avx2 { } #[inline(always)] fn reverse_u32x4(self, a: u32x4) -> u32x4 { - let indices: u8x16 = - [12, 13, 14, 15, 8, 9, 10, 11, 4, 5, 6, 7, 0, 1, 2, 3].simd_into(self); - self.swizzle_dyn_u32x4(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: u32x4) -> u32x4 { + _mm_shuffle_epi32::<0b00_01_10_11>(a.into()).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_u32x4(self, a: u32x4, b: u32x4) -> u32x4 { @@ -5755,9 +5800,14 @@ impl Simd for Avx2 { } #[inline(always)] fn reverse_f64x2(self, a: f64x2) -> f64x2 { - let indices: u8x16 = - [8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7].simd_into(self); - self.swizzle_dyn_f64x2(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: f64x2) -> f64x2 { + let a = a.into(); + _mm_shuffle_pd::<0b01>(a, a).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_f64x2(self, a: f64x2, b: f64x2) -> f64x2 { @@ -6471,9 +6521,13 @@ impl Simd for Avx2 { } #[inline(always)] fn reverse_i64x2(self, a: i64x2) -> i64x2 { - let indices: u8x16 = - [8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7].simd_into(self); - self.swizzle_dyn_i64x2(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: i64x2) -> i64x2 { + _mm_shuffle_epi32::<0b01_00_11_10>(a.into()).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_i64x2(self, a: i64x2, b: i64x2) -> i64x2 { @@ -7080,9 +7134,13 @@ impl Simd for Avx2 { } #[inline(always)] fn reverse_u64x2(self, a: u64x2) -> u64x2 { - let indices: u8x16 = - [8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7].simd_into(self); - self.swizzle_dyn_u64x2(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: u64x2) -> u64x2 { + _mm_shuffle_epi32::<0b01_00_11_10>(a.into()).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_u64x2(self, a: u64x2, b: u64x2) -> u64x2 { @@ -7928,12 +7986,14 @@ impl Simd for Avx2 { } #[inline(always)] fn reverse_f32x8(self, a: f32x8) -> f32x8 { - let indices: u8x32 = [ - 28, 29, 30, 31, 24, 25, 26, 27, 20, 21, 22, 23, 16, 17, 18, 19, 12, 13, 14, 15, 8, 9, - 10, 11, 4, 5, 6, 7, 0, 1, 2, 3, - ] - .simd_into(self); - self.swizzle_dyn_f32x8(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: f32x8) -> f32x8 { + _mm256_permutevar8x32_ps(a.into(), _mm256_setr_epi32(7, 6, 5, 4, 3, 2, 1, 0)) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_f32x8(self, a: f32x8, b: f32x8) -> f32x8 { @@ -8588,12 +8648,22 @@ impl Simd for Avx2 { } #[inline(always)] fn reverse_i8x32(self, a: i8x32) -> i8x32 { - let indices: u8x32 = [ - 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, - 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, - ] - .simd_into(self); - self.swizzle_dyn_i8x32(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: i8x32) -> i8x32 { + let a = a.into(); + let swapped = _mm256_permute2x128_si256::<0x01>(a, a); + _mm256_shuffle_epi8( + swapped, + _mm256_setr_epi8( + 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, + 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, + ), + ) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_i8x32(self, a: i8x32, b: i8x32) -> i8x32 { @@ -9221,12 +9291,22 @@ impl Simd for Avx2 { } #[inline(always)] fn reverse_u8x32(self, a: u8x32) -> u8x32 { - let indices: u8x32 = [ - 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, - 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, - ] - .simd_into(self); - self.swizzle_dyn_u8x32(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: u8x32) -> u8x32 { + let a = a.into(); + let swapped = _mm256_permute2x128_si256::<0x01>(a, a); + _mm256_shuffle_epi8( + swapped, + _mm256_setr_epi8( + 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, + 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, + ), + ) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_u8x32(self, a: u8x32, b: u8x32) -> u8x32 { @@ -10394,12 +10474,22 @@ impl Simd for Avx2 { } #[inline(always)] fn reverse_i16x16(self, a: i16x16) -> i16x16 { - let indices: u8x32 = [ - 30, 31, 28, 29, 26, 27, 24, 25, 22, 23, 20, 21, 18, 19, 16, 17, 14, 15, 12, 13, 10, 11, - 8, 9, 6, 7, 4, 5, 2, 3, 0, 1, - ] - .simd_into(self); - self.swizzle_dyn_i16x16(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: i16x16) -> i16x16 { + let a = a.into(); + let swapped = _mm256_permute2x128_si256::<0x01>(a, a); + _mm256_shuffle_epi8( + swapped, + _mm256_setr_epi8( + 14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1, 14, 15, 12, 13, 10, + 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1, + ), + ) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_i16x16(self, a: i16x16, b: i16x16) -> i16x16 { @@ -11042,12 +11132,22 @@ impl Simd for Avx2 { } #[inline(always)] fn reverse_u16x16(self, a: u16x16) -> u16x16 { - let indices: u8x32 = [ - 30, 31, 28, 29, 26, 27, 24, 25, 22, 23, 20, 21, 18, 19, 16, 17, 14, 15, 12, 13, 10, 11, - 8, 9, 6, 7, 4, 5, 2, 3, 0, 1, - ] - .simd_into(self); - self.swizzle_dyn_u16x16(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: u16x16) -> u16x16 { + let a = a.into(); + let swapped = _mm256_permute2x128_si256::<0x01>(a, a); + _mm256_shuffle_epi8( + swapped, + _mm256_setr_epi8( + 14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1, 14, 15, 12, 13, 10, + 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1, + ), + ) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_u16x16(self, a: u16x16, b: u16x16) -> u16x16 { @@ -11955,12 +12055,14 @@ impl Simd for Avx2 { } #[inline(always)] fn reverse_i32x8(self, a: i32x8) -> i32x8 { - let indices: u8x32 = [ - 28, 29, 30, 31, 24, 25, 26, 27, 20, 21, 22, 23, 16, 17, 18, 19, 12, 13, 14, 15, 8, 9, - 10, 11, 4, 5, 6, 7, 0, 1, 2, 3, - ] - .simd_into(self); - self.swizzle_dyn_i32x8(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: i32x8) -> i32x8 { + _mm256_permutevar8x32_epi32(a.into(), _mm256_setr_epi32(7, 6, 5, 4, 3, 2, 1, 0)) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_i32x8(self, a: i32x8, b: i32x8) -> i32x8 { @@ -12616,12 +12718,14 @@ impl Simd for Avx2 { } #[inline(always)] fn reverse_u32x8(self, a: u32x8) -> u32x8 { - let indices: u8x32 = [ - 28, 29, 30, 31, 24, 25, 26, 27, 20, 21, 22, 23, 16, 17, 18, 19, 12, 13, 14, 15, 8, 9, - 10, 11, 4, 5, 6, 7, 0, 1, 2, 3, - ] - .simd_into(self); - self.swizzle_dyn_u32x8(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: u32x8) -> u32x8 { + _mm256_permutevar8x32_epi32(a.into(), _mm256_setr_epi32(7, 6, 5, 4, 3, 2, 1, 0)) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_u32x8(self, a: u32x8, b: u32x8) -> u32x8 { @@ -13527,12 +13631,13 @@ impl Simd for Avx2 { } #[inline(always)] fn reverse_f64x4(self, a: f64x4) -> f64x4 { - let indices: u8x32 = [ - 24, 25, 26, 27, 28, 29, 30, 31, 16, 17, 18, 19, 20, 21, 22, 23, 8, 9, 10, 11, 12, 13, - 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, - ] - .simd_into(self); - self.swizzle_dyn_f64x4(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: f64x4) -> f64x4 { + _mm256_permute4x64_pd::<0b00_01_10_11>(a.into()).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_f64x4(self, a: f64x4, b: f64x4) -> f64x4 { @@ -14226,12 +14331,13 @@ impl Simd for Avx2 { } #[inline(always)] fn reverse_i64x4(self, a: i64x4) -> i64x4 { - let indices: u8x32 = [ - 24, 25, 26, 27, 28, 29, 30, 31, 16, 17, 18, 19, 20, 21, 22, 23, 8, 9, 10, 11, 12, 13, - 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, - ] - .simd_into(self); - self.swizzle_dyn_i64x4(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: i64x4) -> i64x4 { + _mm256_permute4x64_epi64::<0b00_01_10_11>(a.into()).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_i64x4(self, a: i64x4, b: i64x4) -> i64x4 { @@ -14855,12 +14961,13 @@ impl Simd for Avx2 { } #[inline(always)] fn reverse_u64x4(self, a: u64x4) -> u64x4 { - let indices: u8x32 = [ - 24, 25, 26, 27, 28, 29, 30, 31, 16, 17, 18, 19, 20, 21, 22, 23, 8, 9, 10, 11, 12, 13, - 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, - ] - .simd_into(self); - self.swizzle_dyn_u64x4(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: u64x4) -> u64x4 { + _mm256_permute4x64_epi64::<0b00_01_10_11>(a.into()).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_u64x4(self, a: u64x4, b: u64x4) -> u64x4 { diff --git a/fearless_simd/src/generated/avx512.rs b/fearless_simd/src/generated/avx512.rs index 5a880cf3..a5aeaf8a 100644 --- a/fearless_simd/src/generated/avx512.rs +++ b/fearless_simd/src/generated/avx512.rs @@ -419,9 +419,14 @@ impl Simd for Avx512 { } #[inline(always)] fn reverse_f32x4(self, a: f32x4) -> f32x4 { - let indices: u8x16 = - [12, 13, 14, 15, 8, 9, 10, 11, 4, 5, 6, 7, 0, 1, 2, 3].simd_into(self); - self.swizzle_dyn_f32x4(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: f32x4) -> f32x4 { + let a = a.into(); + _mm_shuffle_ps::<0b00_01_10_11>(a, a).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_f32x4(self, a: f32x4, b: f32x4) -> f32x4 { @@ -1170,9 +1175,17 @@ impl Simd for Avx512 { } #[inline(always)] fn reverse_i8x16(self, a: i8x16) -> i8x16 { - let indices: u8x16 = - [15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0].simd_into(self); - self.swizzle_dyn_i8x16(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: i8x16) -> i8x16 { + _mm_shuffle_epi8( + a.into(), + _mm_setr_epi8(15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0), + ) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_i8x16(self, a: i8x16, b: i8x16) -> i8x16 { @@ -1870,9 +1883,17 @@ impl Simd for Avx512 { } #[inline(always)] fn reverse_u8x16(self, a: u8x16) -> u8x16 { - let indices: u8x16 = - [15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0].simd_into(self); - self.swizzle_dyn_u8x16(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: u8x16) -> u8x16 { + _mm_shuffle_epi8( + a.into(), + _mm_setr_epi8(15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0), + ) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_u8x16(self, a: u8x16, b: u8x16) -> u8x16 { @@ -2849,9 +2870,17 @@ impl Simd for Avx512 { } #[inline(always)] fn reverse_i16x8(self, a: i16x8) -> i16x8 { - let indices: u8x16 = - [14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1].simd_into(self); - self.swizzle_dyn_i16x8(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: i16x8) -> i16x8 { + _mm_shuffle_epi8( + a.into(), + _mm_setr_epi8(14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1), + ) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_i16x8(self, a: i16x8, b: i16x8) -> i16x8 { @@ -3540,9 +3569,17 @@ impl Simd for Avx512 { } #[inline(always)] fn reverse_u16x8(self, a: u16x8) -> u16x8 { - let indices: u8x16 = - [14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1].simd_into(self); - self.swizzle_dyn_u16x8(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: u16x8) -> u16x8 { + _mm_shuffle_epi8( + a.into(), + _mm_setr_epi8(14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1), + ) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_u16x8(self, a: u16x8, b: u16x8) -> u16x8 { @@ -4414,9 +4451,13 @@ impl Simd for Avx512 { } #[inline(always)] fn reverse_i32x4(self, a: i32x4) -> i32x4 { - let indices: u8x16 = - [12, 13, 14, 15, 8, 9, 10, 11, 4, 5, 6, 7, 0, 1, 2, 3].simd_into(self); - self.swizzle_dyn_i32x4(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: i32x4) -> i32x4 { + _mm_shuffle_epi32::<0b00_01_10_11>(a.into()).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_i32x4(self, a: i32x4, b: i32x4) -> i32x4 { @@ -5072,9 +5113,13 @@ impl Simd for Avx512 { } #[inline(always)] fn reverse_u32x4(self, a: u32x4) -> u32x4 { - let indices: u8x16 = - [12, 13, 14, 15, 8, 9, 10, 11, 4, 5, 6, 7, 0, 1, 2, 3].simd_into(self); - self.swizzle_dyn_u32x4(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: u32x4) -> u32x4 { + _mm_shuffle_epi32::<0b00_01_10_11>(a.into()).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_u32x4(self, a: u32x4, b: u32x4) -> u32x4 { @@ -5935,9 +5980,14 @@ impl Simd for Avx512 { } #[inline(always)] fn reverse_f64x2(self, a: f64x2) -> f64x2 { - let indices: u8x16 = - [8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7].simd_into(self); - self.swizzle_dyn_f64x2(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: f64x2) -> f64x2 { + let a = a.into(); + _mm_shuffle_pd::<0b01>(a, a).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_f64x2(self, a: f64x2, b: f64x2) -> f64x2 { @@ -6676,9 +6726,13 @@ impl Simd for Avx512 { } #[inline(always)] fn reverse_i64x2(self, a: i64x2) -> i64x2 { - let indices: u8x16 = - [8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7].simd_into(self); - self.swizzle_dyn_i64x2(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: i64x2) -> i64x2 { + _mm_shuffle_epi32::<0b01_00_11_10>(a.into()).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_i64x2(self, a: i64x2, b: i64x2) -> i64x2 { @@ -7262,9 +7316,13 @@ impl Simd for Avx512 { } #[inline(always)] fn reverse_u64x2(self, a: u64x2) -> u64x2 { - let indices: u8x16 = - [8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7].simd_into(self); - self.swizzle_dyn_u64x2(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: u64x2) -> u64x2 { + _mm_shuffle_epi32::<0b01_00_11_10>(a.into()).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_u64x2(self, a: u64x2, b: u64x2) -> u64x2 { @@ -8039,12 +8097,14 @@ impl Simd for Avx512 { } #[inline(always)] fn reverse_f32x8(self, a: f32x8) -> f32x8 { - let indices: u8x32 = [ - 28, 29, 30, 31, 24, 25, 26, 27, 20, 21, 22, 23, 16, 17, 18, 19, 12, 13, 14, 15, 8, 9, - 10, 11, 4, 5, 6, 7, 0, 1, 2, 3, - ] - .simd_into(self); - self.swizzle_dyn_f32x8(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: f32x8) -> f32x8 { + _mm256_permutevar8x32_ps(a.into(), _mm256_setr_epi32(7, 6, 5, 4, 3, 2, 1, 0)) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_f32x8(self, a: f32x8, b: f32x8) -> f32x8 { @@ -8766,12 +8826,20 @@ impl Simd for Avx512 { } #[inline(always)] fn reverse_i8x32(self, a: i8x32) -> i8x32 { - let indices: u8x32 = [ - 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, - 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, - ] - .simd_into(self); - self.swizzle_dyn_i8x32(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: i8x32) -> i8x32 { + _mm256_permutexvar_epi8( + _mm256_setr_epi8( + 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, + 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, + ), + a.into(), + ) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_i8x32(self, a: i8x32, b: i8x32) -> i8x32 { @@ -9444,12 +9512,20 @@ impl Simd for Avx512 { } #[inline(always)] fn reverse_u8x32(self, a: u8x32) -> u8x32 { - let indices: u8x32 = [ - 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, - 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, - ] - .simd_into(self); - self.swizzle_dyn_u8x32(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: u8x32) -> u8x32 { + _mm256_permutexvar_epi8( + _mm256_setr_epi8( + 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, + 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, + ), + a.into(), + ) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_u8x32(self, a: u8x32, b: u8x32) -> u8x32 { @@ -10415,12 +10491,17 @@ impl Simd for Avx512 { } #[inline(always)] fn reverse_i16x16(self, a: i16x16) -> i16x16 { - let indices: u8x32 = [ - 30, 31, 28, 29, 26, 27, 24, 25, 22, 23, 20, 21, 18, 19, 16, 17, 14, 15, 12, 13, 10, 11, - 8, 9, 6, 7, 4, 5, 2, 3, 0, 1, - ] - .simd_into(self); - self.swizzle_dyn_i16x16(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: i16x16) -> i16x16 { + _mm256_permutexvar_epi16( + _mm256_setr_epi16(15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0), + a.into(), + ) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_i16x16(self, a: i16x16, b: i16x16) -> i16x16 { @@ -11099,12 +11180,17 @@ impl Simd for Avx512 { } #[inline(always)] fn reverse_u16x16(self, a: u16x16) -> u16x16 { - let indices: u8x32 = [ - 30, 31, 28, 29, 26, 27, 24, 25, 22, 23, 20, 21, 18, 19, 16, 17, 14, 15, 12, 13, 10, 11, - 8, 9, 6, 7, 4, 5, 2, 3, 0, 1, - ] - .simd_into(self); - self.swizzle_dyn_u16x16(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: u16x16) -> u16x16 { + _mm256_permutexvar_epi16( + _mm256_setr_epi16(15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0), + a.into(), + ) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_u16x16(self, a: u16x16, b: u16x16) -> u16x16 { @@ -11979,12 +12065,14 @@ impl Simd for Avx512 { } #[inline(always)] fn reverse_i32x8(self, a: i32x8) -> i32x8 { - let indices: u8x32 = [ - 28, 29, 30, 31, 24, 25, 26, 27, 20, 21, 22, 23, 16, 17, 18, 19, 12, 13, 14, 15, 8, 9, - 10, 11, 4, 5, 6, 7, 0, 1, 2, 3, - ] - .simd_into(self); - self.swizzle_dyn_i32x8(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: i32x8) -> i32x8 { + _mm256_permutevar8x32_epi32(a.into(), _mm256_setr_epi32(7, 6, 5, 4, 3, 2, 1, 0)) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_i32x8(self, a: i32x8, b: i32x8) -> i32x8 { @@ -12627,12 +12715,14 @@ impl Simd for Avx512 { } #[inline(always)] fn reverse_u32x8(self, a: u32x8) -> u32x8 { - let indices: u8x32 = [ - 28, 29, 30, 31, 24, 25, 26, 27, 20, 21, 22, 23, 16, 17, 18, 19, 12, 13, 14, 15, 8, 9, - 10, 11, 4, 5, 6, 7, 0, 1, 2, 3, - ] - .simd_into(self); - self.swizzle_dyn_u32x8(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: u32x8) -> u32x8 { + _mm256_permutevar8x32_epi32(a.into(), _mm256_setr_epi32(7, 6, 5, 4, 3, 2, 1, 0)) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_u32x8(self, a: u32x8, b: u32x8) -> u32x8 { @@ -13479,12 +13569,13 @@ impl Simd for Avx512 { } #[inline(always)] fn reverse_f64x4(self, a: f64x4) -> f64x4 { - let indices: u8x32 = [ - 24, 25, 26, 27, 28, 29, 30, 31, 16, 17, 18, 19, 20, 21, 22, 23, 8, 9, 10, 11, 12, 13, - 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, - ] - .simd_into(self); - self.swizzle_dyn_f64x4(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: f64x4) -> f64x4 { + _mm256_permute4x64_pd::<0b00_01_10_11>(a.into()).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_f64x4(self, a: f64x4, b: f64x4) -> f64x4 { @@ -14193,12 +14284,13 @@ impl Simd for Avx512 { } #[inline(always)] fn reverse_i64x4(self, a: i64x4) -> i64x4 { - let indices: u8x32 = [ - 24, 25, 26, 27, 28, 29, 30, 31, 16, 17, 18, 19, 20, 21, 22, 23, 8, 9, 10, 11, 12, 13, - 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, - ] - .simd_into(self); - self.swizzle_dyn_i64x4(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: i64x4) -> i64x4 { + _mm256_permute4x64_epi64::<0b00_01_10_11>(a.into()).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_i64x4(self, a: i64x4, b: i64x4) -> i64x4 { @@ -14777,12 +14869,13 @@ impl Simd for Avx512 { } #[inline(always)] fn reverse_u64x4(self, a: u64x4) -> u64x4 { - let indices: u8x32 = [ - 24, 25, 26, 27, 28, 29, 30, 31, 16, 17, 18, 19, 20, 21, 22, 23, 8, 9, 10, 11, 12, 13, - 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, - ] - .simd_into(self); - self.swizzle_dyn_u64x4(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: u64x4) -> u64x4 { + _mm256_permute4x64_epi64::<0b00_01_10_11>(a.into()).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_u64x4(self, a: u64x4, b: u64x4) -> u64x4 { @@ -15561,13 +15654,17 @@ impl Simd for Avx512 { } #[inline(always)] fn reverse_f32x16(self, a: f32x16) -> f32x16 { - let indices: u8x64 = [ - 60, 61, 62, 63, 56, 57, 58, 59, 52, 53, 54, 55, 48, 49, 50, 51, 44, 45, 46, 47, 40, 41, - 42, 43, 36, 37, 38, 39, 32, 33, 34, 35, 28, 29, 30, 31, 24, 25, 26, 27, 20, 21, 22, 23, - 16, 17, 18, 19, 12, 13, 14, 15, 8, 9, 10, 11, 4, 5, 6, 7, 0, 1, 2, 3, - ] - .simd_into(self); - self.swizzle_dyn_f32x16(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: f32x16) -> f32x16 { + _mm512_permutexvar_ps( + _mm512_setr_epi32(15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0), + a.into(), + ) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_f32x16(self, a: f32x16, b: f32x16) -> f32x16 { @@ -16313,13 +16410,22 @@ impl Simd for Avx512 { } #[inline(always)] fn reverse_i8x64(self, a: i8x64) -> i8x64 { - let indices: u8x64 = [ - 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, - 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, - 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, - ] - .simd_into(self); - self.swizzle_dyn_i8x64(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: i8x64) -> i8x64 { + _mm512_permutexvar_epi8( + _mm512_set_epi8( + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, + 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, + 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, + 59, 60, 61, 62, 63, + ), + a.into(), + ) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_i8x64(self, a: i8x64, b: i8x64) -> i8x64 { @@ -16981,13 +17087,22 @@ impl Simd for Avx512 { } #[inline(always)] fn reverse_u8x64(self, a: u8x64) -> u8x64 { - let indices: u8x64 = [ - 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, - 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, - 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, - ] - .simd_into(self); - self.swizzle_dyn_u8x64(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: u8x64) -> u8x64 { + _mm512_permutexvar_epi8( + _mm512_set_epi8( + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, + 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, + 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, + 59, 60, 61, 62, 63, + ), + a.into(), + ) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_u8x64(self, a: u8x64, b: u8x64) -> u8x64 { @@ -17932,13 +18047,20 @@ impl Simd for Avx512 { } #[inline(always)] fn reverse_i16x32(self, a: i16x32) -> i16x32 { - let indices: u8x64 = [ - 62, 63, 60, 61, 58, 59, 56, 57, 54, 55, 52, 53, 50, 51, 48, 49, 46, 47, 44, 45, 42, 43, - 40, 41, 38, 39, 36, 37, 34, 35, 32, 33, 30, 31, 28, 29, 26, 27, 24, 25, 22, 23, 20, 21, - 18, 19, 16, 17, 14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1, - ] - .simd_into(self); - self.swizzle_dyn_i16x32(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: i16x32) -> i16x32 { + _mm512_permutexvar_epi16( + _mm512_set_epi16( + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, + 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, + ), + a.into(), + ) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_i16x32(self, a: i16x32, b: i16x32) -> i16x32 { @@ -18620,13 +18742,20 @@ impl Simd for Avx512 { } #[inline(always)] fn reverse_u16x32(self, a: u16x32) -> u16x32 { - let indices: u8x64 = [ - 62, 63, 60, 61, 58, 59, 56, 57, 54, 55, 52, 53, 50, 51, 48, 49, 46, 47, 44, 45, 42, 43, - 40, 41, 38, 39, 36, 37, 34, 35, 32, 33, 30, 31, 28, 29, 26, 27, 24, 25, 22, 23, 20, 21, - 18, 19, 16, 17, 14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1, - ] - .simd_into(self); - self.swizzle_dyn_u16x32(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: u16x32) -> u16x32 { + _mm512_permutexvar_epi16( + _mm512_set_epi16( + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, + 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, + ), + a.into(), + ) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_u16x32(self, a: u16x32, b: u16x32) -> u16x32 { @@ -19517,13 +19646,17 @@ impl Simd for Avx512 { } #[inline(always)] fn reverse_i32x16(self, a: i32x16) -> i32x16 { - let indices: u8x64 = [ - 60, 61, 62, 63, 56, 57, 58, 59, 52, 53, 54, 55, 48, 49, 50, 51, 44, 45, 46, 47, 40, 41, - 42, 43, 36, 37, 38, 39, 32, 33, 34, 35, 28, 29, 30, 31, 24, 25, 26, 27, 20, 21, 22, 23, - 16, 17, 18, 19, 12, 13, 14, 15, 8, 9, 10, 11, 4, 5, 6, 7, 0, 1, 2, 3, - ] - .simd_into(self); - self.swizzle_dyn_i32x16(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: i32x16) -> i32x16 { + _mm512_permutexvar_epi32( + _mm512_setr_epi32(15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0), + a.into(), + ) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_i32x16(self, a: i32x16, b: i32x16) -> i32x16 { @@ -20188,13 +20321,17 @@ impl Simd for Avx512 { } #[inline(always)] fn reverse_u32x16(self, a: u32x16) -> u32x16 { - let indices: u8x64 = [ - 60, 61, 62, 63, 56, 57, 58, 59, 52, 53, 54, 55, 48, 49, 50, 51, 44, 45, 46, 47, 40, 41, - 42, 43, 36, 37, 38, 39, 32, 33, 34, 35, 28, 29, 30, 31, 24, 25, 26, 27, 20, 21, 22, 23, - 16, 17, 18, 19, 12, 13, 14, 15, 8, 9, 10, 11, 4, 5, 6, 7, 0, 1, 2, 3, - ] - .simd_into(self); - self.swizzle_dyn_u32x16(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: u32x16) -> u32x16 { + _mm512_permutexvar_epi32( + _mm512_setr_epi32(15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0), + a.into(), + ) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_u32x16(self, a: u32x16, b: u32x16) -> u32x16 { @@ -21054,13 +21191,14 @@ impl Simd for Avx512 { } #[inline(always)] fn reverse_f64x8(self, a: f64x8) -> f64x8 { - let indices: u8x64 = [ - 56, 57, 58, 59, 60, 61, 62, 63, 48, 49, 50, 51, 52, 53, 54, 55, 40, 41, 42, 43, 44, 45, - 46, 47, 32, 33, 34, 35, 36, 37, 38, 39, 24, 25, 26, 27, 28, 29, 30, 31, 16, 17, 18, 19, - 20, 21, 22, 23, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, - ] - .simd_into(self); - self.swizzle_dyn_f64x8(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: f64x8) -> f64x8 { + _mm512_permutexvar_pd(_mm512_setr_epi64(7, 6, 5, 4, 3, 2, 1, 0), a.into()) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_f64x8(self, a: f64x8, b: f64x8) -> f64x8 { @@ -21781,13 +21919,14 @@ impl Simd for Avx512 { } #[inline(always)] fn reverse_i64x8(self, a: i64x8) -> i64x8 { - let indices: u8x64 = [ - 56, 57, 58, 59, 60, 61, 62, 63, 48, 49, 50, 51, 52, 53, 54, 55, 40, 41, 42, 43, 44, 45, - 46, 47, 32, 33, 34, 35, 36, 37, 38, 39, 24, 25, 26, 27, 28, 29, 30, 31, 16, 17, 18, 19, - 20, 21, 22, 23, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, - ] - .simd_into(self); - self.swizzle_dyn_i64x8(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: i64x8) -> i64x8 { + _mm512_permutexvar_epi64(_mm512_setr_epi64(7, 6, 5, 4, 3, 2, 1, 0), a.into()) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_i64x8(self, a: i64x8, b: i64x8) -> i64x8 { @@ -22375,13 +22514,14 @@ impl Simd for Avx512 { } #[inline(always)] fn reverse_u64x8(self, a: u64x8) -> u64x8 { - let indices: u8x64 = [ - 56, 57, 58, 59, 60, 61, 62, 63, 48, 49, 50, 51, 52, 53, 54, 55, 40, 41, 42, 43, 44, 45, - 46, 47, 32, 33, 34, 35, 36, 37, 38, 39, 24, 25, 26, 27, 28, 29, 30, 31, 16, 17, 18, 19, - 20, 21, 22, 23, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, - ] - .simd_into(self); - self.swizzle_dyn_u64x8(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: u64x8) -> u64x8 { + _mm512_permutexvar_epi64(_mm512_setr_epi64(7, 6, 5, 4, 3, 2, 1, 0), a.into()) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_u64x8(self, a: u64x8, b: u64x8) -> u64x8 { diff --git a/fearless_simd/src/generated/sse2.rs b/fearless_simd/src/generated/sse2.rs index 13701067..17817d4f 100644 --- a/fearless_simd/src/generated/sse2.rs +++ b/fearless_simd/src/generated/sse2.rs @@ -149,7 +149,14 @@ impl Simd for Sse2 { } #[inline(always)] fn reverse_f32x4(self, a: f32x4) -> f32x4 { - [a[3usize], a[2usize], a[1usize], a[0usize]].simd_into(self) + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse2, a: f32x4) -> f32x4 { + let a = a.into(); + _mm_shuffle_ps::<0b00_01_10_11>(a, a).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_f32x4(self, a: f32x4, b: f32x4) -> f32x4 { @@ -892,12 +899,16 @@ impl Simd for Sse2 { } #[inline(always)] fn reverse_i8x16(self, a: i8x16) -> i8x16 { - [ - a[15usize], a[14usize], a[13usize], a[12usize], a[11usize], a[10usize], a[9usize], - a[8usize], a[7usize], a[6usize], a[5usize], a[4usize], a[3usize], a[2usize], a[1usize], - a[0usize], - ] - .simd_into(self) + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse2, a: i8x16) -> i8x16 { + let a = _mm_shufflelo_epi16::<0b00_01_10_11>(a.into()); + let a = _mm_shufflehi_epi16::<0b00_01_10_11>(a); + let a = _mm_shuffle_epi32::<0b01_00_11_10>(a); + _mm_or_si128(_mm_slli_epi16::<8>(a), _mm_srli_epi16::<8>(a)).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_i8x16(self, a: i8x16, b: i8x16) -> i8x16 { @@ -1710,12 +1721,16 @@ impl Simd for Sse2 { } #[inline(always)] fn reverse_u8x16(self, a: u8x16) -> u8x16 { - [ - a[15usize], a[14usize], a[13usize], a[12usize], a[11usize], a[10usize], a[9usize], - a[8usize], a[7usize], a[6usize], a[5usize], a[4usize], a[3usize], a[2usize], a[1usize], - a[0usize], - ] - .simd_into(self) + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse2, a: u8x16) -> u8x16 { + let a = _mm_shufflelo_epi16::<0b00_01_10_11>(a.into()); + let a = _mm_shufflehi_epi16::<0b00_01_10_11>(a); + let a = _mm_shuffle_epi32::<0b01_00_11_10>(a); + _mm_or_si128(_mm_slli_epi16::<8>(a), _mm_srli_epi16::<8>(a)).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_u8x16(self, a: u8x16, b: u8x16) -> u8x16 { @@ -2847,10 +2862,15 @@ impl Simd for Sse2 { } #[inline(always)] fn reverse_i16x8(self, a: i16x8) -> i16x8 { - [ - a[7usize], a[6usize], a[5usize], a[4usize], a[3usize], a[2usize], a[1usize], a[0usize], - ] - .simd_into(self) + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse2, a: i16x8) -> i16x8 { + let a = _mm_shufflelo_epi16::<0b00_01_10_11>(a.into()); + let a = _mm_shufflehi_epi16::<0b00_01_10_11>(a); + _mm_shuffle_epi32::<0b01_00_11_10>(a).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_i16x8(self, a: i16x8, b: i16x8) -> i16x8 { @@ -3508,10 +3528,15 @@ impl Simd for Sse2 { } #[inline(always)] fn reverse_u16x8(self, a: u16x8) -> u16x8 { - [ - a[7usize], a[6usize], a[5usize], a[4usize], a[3usize], a[2usize], a[1usize], a[0usize], - ] - .simd_into(self) + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse2, a: u16x8) -> u16x8 { + let a = _mm_shufflelo_epi16::<0b00_01_10_11>(a.into()); + let a = _mm_shufflehi_epi16::<0b00_01_10_11>(a); + _mm_shuffle_epi32::<0b01_00_11_10>(a).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_u16x8(self, a: u16x8, b: u16x8) -> u16x8 { @@ -4536,7 +4561,13 @@ impl Simd for Sse2 { } #[inline(always)] fn reverse_i32x4(self, a: i32x4) -> i32x4 { - [a[3usize], a[2usize], a[1usize], a[0usize]].simd_into(self) + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse2, a: i32x4) -> i32x4 { + _mm_shuffle_epi32::<0b00_01_10_11>(a.into()).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_i32x4(self, a: i32x4, b: i32x4) -> i32x4 { @@ -5207,7 +5238,13 @@ impl Simd for Sse2 { } #[inline(always)] fn reverse_u32x4(self, a: u32x4) -> u32x4 { - [a[3usize], a[2usize], a[1usize], a[0usize]].simd_into(self) + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse2, a: u32x4) -> u32x4 { + _mm_shuffle_epi32::<0b00_01_10_11>(a.into()).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_u32x4(self, a: u32x4, b: u32x4) -> u32x4 { @@ -6168,7 +6205,14 @@ impl Simd for Sse2 { } #[inline(always)] fn reverse_f64x2(self, a: f64x2) -> f64x2 { - [a[1usize], a[0usize]].simd_into(self) + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse2, a: f64x2) -> f64x2 { + let a = a.into(); + _mm_shuffle_pd::<0b01>(a, a).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_f64x2(self, a: f64x2, b: f64x2) -> f64x2 { @@ -6821,7 +6865,13 @@ impl Simd for Sse2 { } #[inline(always)] fn reverse_i64x2(self, a: i64x2) -> i64x2 { - [a[1usize], a[0usize]].simd_into(self) + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse2, a: i64x2) -> i64x2 { + _mm_shuffle_epi32::<0b01_00_11_10>(a.into()).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_i64x2(self, a: i64x2, b: i64x2) -> i64x2 { @@ -7340,7 +7390,13 @@ impl Simd for Sse2 { } #[inline(always)] fn reverse_u64x2(self, a: u64x2) -> u64x2 { - [a[1usize], a[0usize]].simd_into(self) + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse2, a: u64x2) -> u64x2 { + _mm_shuffle_epi32::<0b01_00_11_10>(a.into()).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_u64x2(self, a: u64x2, b: u64x2) -> u64x2 { diff --git a/fearless_simd/src/generated/sse4_2.rs b/fearless_simd/src/generated/sse4_2.rs index e22afd37..9da8c1c7 100644 --- a/fearless_simd/src/generated/sse4_2.rs +++ b/fearless_simd/src/generated/sse4_2.rs @@ -149,9 +149,14 @@ impl Simd for Sse4_2 { } #[inline(always)] fn reverse_f32x4(self, a: f32x4) -> f32x4 { - let indices: u8x16 = - [12, 13, 14, 15, 8, 9, 10, 11, 4, 5, 6, 7, 0, 1, 2, 3].simd_into(self); - self.swizzle_dyn_f32x4(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse4_2, a: f32x4) -> f32x4 { + let a = a.into(); + _mm_shuffle_ps::<0b00_01_10_11>(a, a).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_f32x4(self, a: f32x4, b: f32x4) -> f32x4 { @@ -855,9 +860,17 @@ impl Simd for Sse4_2 { } #[inline(always)] fn reverse_i8x16(self, a: i8x16) -> i8x16 { - let indices: u8x16 = - [15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0].simd_into(self); - self.swizzle_dyn_i8x16(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse4_2, a: i8x16) -> i8x16 { + _mm_shuffle_epi8( + a.into(), + _mm_setr_epi8(15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0), + ) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_i8x16(self, a: i8x16, b: i8x16) -> i8x16 { @@ -1533,9 +1546,17 @@ impl Simd for Sse4_2 { } #[inline(always)] fn reverse_u8x16(self, a: u8x16) -> u8x16 { - let indices: u8x16 = - [15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0].simd_into(self); - self.swizzle_dyn_u8x16(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse4_2, a: u8x16) -> u8x16 { + _mm_shuffle_epi8( + a.into(), + _mm_setr_epi8(15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0), + ) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_u8x16(self, a: u8x16, b: u8x16) -> u8x16 { @@ -2580,9 +2601,17 @@ impl Simd for Sse4_2 { } #[inline(always)] fn reverse_i16x8(self, a: i16x8) -> i16x8 { - let indices: u8x16 = - [14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1].simd_into(self); - self.swizzle_dyn_i16x8(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse4_2, a: i16x8) -> i16x8 { + _mm_shuffle_epi8( + a.into(), + _mm_setr_epi8(14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1), + ) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_i16x8(self, a: i16x8, b: i16x8) -> i16x8 { @@ -3234,9 +3263,17 @@ impl Simd for Sse4_2 { } #[inline(always)] fn reverse_u16x8(self, a: u16x8) -> u16x8 { - let indices: u8x16 = - [14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1].simd_into(self); - self.swizzle_dyn_u16x8(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse4_2, a: u16x8) -> u16x8 { + _mm_shuffle_epi8( + a.into(), + _mm_setr_epi8(14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1), + ) + .simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_u16x8(self, a: u16x8, b: u16x8) -> u16x8 { @@ -4162,9 +4199,13 @@ impl Simd for Sse4_2 { } #[inline(always)] fn reverse_i32x4(self, a: i32x4) -> i32x4 { - let indices: u8x16 = - [12, 13, 14, 15, 8, 9, 10, 11, 4, 5, 6, 7, 0, 1, 2, 3].simd_into(self); - self.swizzle_dyn_i32x4(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse4_2, a: i32x4) -> i32x4 { + _mm_shuffle_epi32::<0b00_01_10_11>(a.into()).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_i32x4(self, a: i32x4, b: i32x4) -> i32x4 { @@ -4821,9 +4862,13 @@ impl Simd for Sse4_2 { } #[inline(always)] fn reverse_u32x4(self, a: u32x4) -> u32x4 { - let indices: u8x16 = - [12, 13, 14, 15, 8, 9, 10, 11, 4, 5, 6, 7, 0, 1, 2, 3].simd_into(self); - self.swizzle_dyn_u32x4(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse4_2, a: u32x4) -> u32x4 { + _mm_shuffle_epi32::<0b00_01_10_11>(a.into()).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_u32x4(self, a: u32x4, b: u32x4) -> u32x4 { @@ -5713,9 +5758,14 @@ impl Simd for Sse4_2 { } #[inline(always)] fn reverse_f64x2(self, a: f64x2) -> f64x2 { - let indices: u8x16 = - [8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7].simd_into(self); - self.swizzle_dyn_f64x2(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse4_2, a: f64x2) -> f64x2 { + let a = a.into(); + _mm_shuffle_pd::<0b01>(a, a).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_f64x2(self, a: f64x2, b: f64x2) -> f64x2 { @@ -6359,9 +6409,13 @@ impl Simd for Sse4_2 { } #[inline(always)] fn reverse_i64x2(self, a: i64x2) -> i64x2 { - let indices: u8x16 = - [8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7].simd_into(self); - self.swizzle_dyn_i64x2(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse4_2, a: i64x2) -> i64x2 { + _mm_shuffle_epi32::<0b01_00_11_10>(a.into()).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_i64x2(self, a: i64x2, b: i64x2) -> i64x2 { @@ -6968,9 +7022,13 @@ impl Simd for Sse4_2 { } #[inline(always)] fn reverse_u64x2(self, a: u64x2) -> u64x2 { - let indices: u8x16 = - [8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7].simd_into(self); - self.swizzle_dyn_u64x2(a, indices) + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse4_2, a: u64x2) -> u64x2 { + _mm_shuffle_epi32::<0b01_00_11_10>(a.into()).simd_into(token) + } + ); + kernel(self, a) } #[inline(always)] fn slide_u64x2(self, a: u64x2, b: u64x2) -> u64x2 { diff --git a/fearless_simd_gen/src/mk_x86.rs b/fearless_simd_gen/src/mk_x86.rs index 5fb6eda2..8f42e0f5 100644 --- a/fearless_simd_gen/src/mk_x86.rs +++ b/fearless_simd_gen/src/mk_x86.rs @@ -12,7 +12,7 @@ use crate::generic::{ generic_classify, generic_mask_from_bitmask, generic_mask_from_vector, generic_mask_set, generic_mask_to_vector, generic_op, generic_op_name, generic_round, generic_to_degrees_radians, integer_lane_mask_rotate, integer_lane_mask_splat_arg, recursive_swizzle_dyn_precise_body, - reverse_method, reverse_vector_mask_method, + reverse_vector_mask_method, }; use crate::level::Level; use crate::ops::{ @@ -2043,6 +2043,104 @@ impl X86 { } } + fn handle_reverse(&self, op: Op, method_sig: TokenStream, vec_ty: &VecType) -> TokenStream { + if vec_ty.scalar == ScalarType::Mask { + if *self == Self::Avx512 { + let shift = avx512_mask_register_bits(vec_ty) - vec_ty.len; + let result = avx512_mask_value(vec_ty, quote! { a.val.reverse_bits() >> #shift }); + return quote! { + #method_sig { + #result + } + }; + } + return reverse_vector_mask_method(op, vec_ty); + } + + // Reverse element order within each 128-bit lane while keeping byte order + // within each element intact. + let reverse_lane_bytes = |n_bytes: usize| { + let elem_bytes = vec_ty.scalar_bits / 8; + let elems_per_lane = 16 / elem_bytes; + (0..n_bytes).map(move |out_byte| { + let byte_in_lane = out_byte % 16; + let out_elem = byte_in_lane / elem_bytes; + let byte_in_elem = byte_in_lane % elem_bytes; + let in_byte = (elems_per_lane - 1 - out_elem) * elem_bytes + byte_in_elem; + signed_literal(in_byte as u64, 8) + }) + }; + + self.kernel_method(op, vec_ty, |token| { + match (*self, vec_ty.n_bits(), vec_ty.scalar_bits, vec_ty.scalar) { + // Reverse 16-bit elements within each 64-bit half, swap the halves, + // and swap bytes within each 16-bit element for 8-bit lanes. + (Self::Sse2, 128, 8, _) => quote! { + let a = _mm_shufflelo_epi16::<0b00_01_10_11>(a.into()); + let a = _mm_shufflehi_epi16::<0b00_01_10_11>(a); + let a = _mm_shuffle_epi32::<0b01_00_11_10>(a); + _mm_or_si128(_mm_slli_epi16::<8>(a), _mm_srli_epi16::<8>(a)).simd_into(#token) + }, + (Self::Sse2, 128, 16, _) => quote! { + let a = _mm_shufflelo_epi16::<0b00_01_10_11>(a.into()); + let a = _mm_shufflehi_epi16::<0b00_01_10_11>(a); + _mm_shuffle_epi32::<0b01_00_11_10>(a).simd_into(#token) + }, + (_, 128, 8 | 16, _) => { + let mask = reverse_lane_bytes(16); + quote! { + _mm_shuffle_epi8(a.into(), _mm_setr_epi8(#(#mask),*)).simd_into(#token) + } + } + (_, 128, 32, ScalarType::Float) => quote! { + let a = a.into(); + _mm_shuffle_ps::<0b00_01_10_11>(a, a).simd_into(#token) + }, + (_, 128, 32, ScalarType::Int | ScalarType::Unsigned) => quote! { + _mm_shuffle_epi32::<0b00_01_10_11>(a.into()).simd_into(#token) + }, + (_, 128, 64, ScalarType::Float) => quote! { + let a = a.into(); + _mm_shuffle_pd::<0b01>(a, a).simd_into(#token) + }, + (_, 128, 64, ScalarType::Int | ScalarType::Unsigned) => quote! { + _mm_shuffle_epi32::<0b01_00_11_10>(a.into()).simd_into(#token) + }, + // `vpshufb` operates within each 128-bit lane, so AVX2 swaps halves first. + (Self::Avx2, 256, 8 | 16, _) => { + let mask = reverse_lane_bytes(32); + quote! { + let a = a.into(); + let swapped = _mm256_permute2x128_si256::<0x01>(a, a); + _mm256_shuffle_epi8(swapped, _mm256_setr_epi8(#(#mask),*)).simd_into(#token) + } + } + (_, 256, 32, _) => { + let permute = simple_sign_unaware_intrinsic("permutevar8x32", vec_ty); + let indices = avx512_index_vector(vec_ty, (0..vec_ty.len).rev()); + quote! { + #permute(a.into(), #indices).simd_into(#token) + } + } + (_, 256, 64, _) => { + let permute = simple_sign_unaware_intrinsic("permute4x64", vec_ty); + quote! { + #permute::<0b00_01_10_11>(a.into()).simd_into(#token) + } + } + // AVX-512 BW/VBMI permutes 8/16-bit elements across the full register directly. + (Self::Avx512, 256, 8 | 16, _) | (Self::Avx512, 512, _, _) => { + let permute = avx512_permutexvar_intrinsic(vec_ty); + let indices = avx512_index_vector(vec_ty, (0..vec_ty.len).rev()); + quote! { + #permute(#indices, a.into()).simd_into(#token) + } + } + _ => unreachable!(), + } + }) + } + fn handle_swap_bytes(&self, op: Op, vec_ty: &VecType) -> TokenStream { let method_sig = op.simd_trait_method_sig(vec_ty); // swap_bytes is a noop for i8/u8 @@ -2778,24 +2876,7 @@ impl X86 { } if method == "reverse" { - if vec_ty.scalar == ScalarType::Mask { - if *self == Self::Avx512 { - let shift = avx512_mask_register_bits(vec_ty) - vec_ty.len; - let result = - avx512_mask_value(vec_ty, quote! { a.val.reverse_bits() >> #shift }); - return quote! { - #method_sig { - #result - } - }; - } - return reverse_vector_mask_method(op, vec_ty); - } - return if *self == Self::Sse2 { - fallback_method(op, vec_ty) - } else { - reverse_method(op, vec_ty) - }; + return self.handle_reverse(op, method_sig, vec_ty); } if method == "count_zeros" {