From 156dc7599c7802a8f115cf245b320ef5944a244b Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Sat, 28 Nov 2026 01:34:39 +0100 Subject: [PATCH 1/4] Add `SimdInt::saturating_neg` --- fearless_simd/src/generated/avx2.rs | 186 +++++++++ fearless_simd/src/generated/avx512.rs | 276 +++++++++++++ fearless_simd/src/generated/fallback.rs | 70 ++++ fearless_simd/src/generated/neon.rs | 84 ++++ fearless_simd/src/generated/simd_trait.rs | 120 ++++++ fearless_simd/src/generated/simd_types.rs | 96 +++++ fearless_simd/src/generated/sse2.rs | 86 +++++ fearless_simd/src/generated/sse4_2.rs | 93 +++++ fearless_simd/src/generated/wasm.rs | 36 ++ fearless_simd_gen/src/mk_fallback.rs | 30 ++ fearless_simd_gen/src/mk_neon.rs | 21 + fearless_simd_gen/src/mk_wasm.rs | 31 ++ fearless_simd_gen/src/mk_x86.rs | 87 +++++ fearless_simd_gen/src/ops.rs | 7 + fearless_simd_tests/tests/harness/ops/mod.rs | 1 + .../tests/harness/ops/saturating_neg.rs | 362 ++++++++++++++++++ 16 files changed, 1586 insertions(+) create mode 100644 fearless_simd_tests/tests/harness/ops/saturating_neg.rs diff --git a/fearless_simd/src/generated/avx2.rs b/fearless_simd/src/generated/avx2.rs index 4a0bd797..5af8706b 100644 --- a/fearless_simd/src/generated/avx2.rs +++ b/fearless_simd/src/generated/avx2.rs @@ -1021,6 +1021,18 @@ impl Simd for Avx2 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_i8x16(self, a: i8x16) -> i8x16 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: i8x16) -> i8x16 { + let a = a.into(); + let bounded = _mm_max_epi8(a, _mm_set1_epi8(i8::MIN + 1)); + _mm_sub_epi8(_mm_setzero_si128(), bounded).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_i8x16(self, a: i8x16, b: i8x16) -> i8x16 { crate::kernel!( #[inline(always)] @@ -1835,6 +1847,17 @@ impl Simd for Avx2 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_u8x16(self, a: u8x16) -> u8x16 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: u8x16) -> u8x16 { + let _ = a; + _mm_setzero_si128().simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u8x16(self, a: u8x16, b: u8x16) -> u8x16 { crate::kernel!( #[inline(always)] @@ -2746,6 +2769,18 @@ impl Simd for Avx2 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_i16x8(self, a: i16x8) -> i16x8 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: i16x8) -> i16x8 { + let a = a.into(); + let bounded = _mm_max_epi16(a, _mm_set1_epi16(i16::MIN + 1)); + _mm_sub_epi16(_mm_setzero_si128(), bounded).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_i16x8(self, a: i16x8, b: i16x8) -> i16x8 { crate::kernel!( #[inline(always)] @@ -3422,6 +3457,17 @@ impl Simd for Avx2 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_u16x8(self, a: u16x8) -> u16x8 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: u16x8) -> u16x8 { + let _ = a; + _mm_setzero_si128().simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u16x8(self, a: u16x8, b: u16x8) -> u16x8 { crate::kernel!( #[inline(always)] @@ -4372,6 +4418,18 @@ impl Simd for Avx2 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_i32x4(self, a: i32x4) -> i32x4 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: i32x4) -> i32x4 { + let a = a.into(); + let bounded = _mm_max_epi32(a, _mm_set1_epi32(i32::MIN + 1)); + _mm_sub_epi32(_mm_setzero_si128(), bounded).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_i32x4(self, a: i32x4, b: i32x4) -> i32x4 { crate::kernel!( #[inline(always)] @@ -5015,6 +5073,17 @@ impl Simd for Avx2 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_u32x4(self, a: u32x4) -> u32x4 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: u32x4) -> u32x4 { + let _ = a; + _mm_setzero_si128().simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u32x4(self, a: u32x4, b: u32x4) -> u32x4 { crate::kernel!( #[inline(always)] @@ -6640,6 +6709,19 @@ impl Simd for Avx2 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_i64x2(self, a: i64x2) -> i64x2 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: i64x2) -> i64x2 { + let a = a.into(); + let neg_a = _mm_sub_epi64(_mm_setzero_si128(), a); + let is_min = _mm_cmpeq_epi64(neg_a, _mm_set1_epi64x(i64::MIN)); + _mm_xor_si128(neg_a, is_min).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_i64x2(self, a: i64x2, b: i64x2) -> i64x2 { crate::kernel!( #[inline(always)] @@ -7243,6 +7325,17 @@ impl Simd for Avx2 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_u64x2(self, a: u64x2) -> u64x2 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: u64x2) -> u64x2 { + let _ = a; + _mm_setzero_si128().simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u64x2(self, a: u64x2, b: u64x2) -> u64x2 { crate::kernel!( #[inline(always)] @@ -8775,6 +8868,18 @@ impl Simd for Avx2 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_i8x32(self, a: i8x32) -> i8x32 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: i8x32) -> i8x32 { + let a = a.into(); + let bounded = _mm256_max_epi8(a, _mm256_set1_epi8(i8::MIN + 1)); + _mm256_sub_epi8(_mm256_setzero_si256(), bounded).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_i8x32(self, a: i8x32, b: i8x32) -> i8x32 { crate::kernel!( #[inline(always)] @@ -9697,6 +9802,17 @@ impl Simd for Avx2 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_u8x32(self, a: u8x32) -> u8x32 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: u8x32) -> u8x32 { + let _ = a; + _mm256_setzero_si256().simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u8x32(self, a: u8x32, b: u8x32) -> u8x32 { crate::kernel!( #[inline(always)] @@ -10588,6 +10704,18 @@ impl Simd for Avx2 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_i16x16(self, a: i16x16) -> i16x16 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: i16x16) -> i16x16 { + let a = a.into(); + let bounded = _mm256_max_epi16(a, _mm256_set1_epi16(i16::MIN + 1)); + _mm256_sub_epi16(_mm256_setzero_si256(), bounded).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_i16x16(self, a: i16x16, b: i16x16) -> i16x16 { crate::kernel!( #[inline(always)] @@ -11236,6 +11364,17 @@ impl Simd for Avx2 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_u16x16(self, a: u16x16) -> u16x16 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: u16x16) -> u16x16 { + let _ = a; + _mm256_setzero_si256().simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u16x16(self, a: u16x16, b: u16x16) -> u16x16 { crate::kernel!( #[inline(always)] @@ -12209,6 +12348,18 @@ impl Simd for Avx2 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_i32x8(self, a: i32x8) -> i32x8 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: i32x8) -> i32x8 { + let a = a.into(); + let bounded = _mm256_max_epi32(a, _mm256_set1_epi32(i32::MIN + 1)); + _mm256_sub_epi32(_mm256_setzero_si256(), bounded).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_i32x8(self, a: i32x8, b: i32x8) -> i32x8 { crate::kernel!( #[inline(always)] @@ -12853,6 +13004,17 @@ impl Simd for Avx2 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_u32x8(self, a: u32x8) -> u32x8 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: u32x8) -> u32x8 { + let _ = a; + _mm256_setzero_si256().simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u32x8(self, a: u32x8, b: u32x8) -> u32x8 { crate::kernel!( #[inline(always)] @@ -14476,6 +14638,19 @@ impl Simd for Avx2 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_i64x4(self, a: i64x4) -> i64x4 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: i64x4) -> i64x4 { + let a = a.into(); + let neg_a = _mm256_sub_epi64(_mm256_setzero_si256(), a); + let is_min = _mm256_cmpeq_epi64(neg_a, _mm256_set1_epi64x(i64::MIN)); + _mm256_xor_si256(neg_a, is_min).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_i64x4(self, a: i64x4, b: i64x4) -> i64x4 { crate::kernel!( #[inline(always)] @@ -15099,6 +15274,17 @@ impl Simd for Avx2 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_u64x4(self, a: u64x4) -> u64x4 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: u64x4) -> u64x4 { + let _ = a; + _mm256_setzero_si256().simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u64x4(self, a: u64x4, b: u64x4) -> u64x4 { crate::kernel!( #[inline(always)] diff --git a/fearless_simd/src/generated/avx512.rs b/fearless_simd/src/generated/avx512.rs index 5a880cf3..d8207813 100644 --- a/fearless_simd/src/generated/avx512.rs +++ b/fearless_simd/src/generated/avx512.rs @@ -1336,6 +1336,18 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_i8x16(self, a: i8x16) -> i8x16 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: i8x16) -> i8x16 { + let a = a.into(); + let bounded = _mm_max_epi8(a, _mm_set1_epi8(i8::MIN + 1)); + _mm_sub_epi8(_mm_setzero_si128(), bounded).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_i8x16(self, a: i8x16, b: i8x16) -> i8x16 { crate::kernel!( #[inline(always)] @@ -2140,6 +2152,17 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_u8x16(self, a: u8x16) -> u8x16 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: u8x16) -> u8x16 { + let _ = a; + _mm_setzero_si128().simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u8x16(self, a: u8x16, b: u8x16) -> u8x16 { crate::kernel!( #[inline(always)] @@ -3017,6 +3040,18 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_i16x8(self, a: i16x8) -> i16x8 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: i16x8) -> i16x8 { + let a = a.into(); + let bounded = _mm_max_epi16(a, _mm_set1_epi16(i16::MIN + 1)); + _mm_sub_epi16(_mm_setzero_si128(), bounded).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_i16x8(self, a: i16x8, b: i16x8) -> i16x8 { crate::kernel!( #[inline(always)] @@ -3708,6 +3743,17 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_u16x8(self, a: u16x8) -> u16x8 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: u16x8) -> u16x8 { + let _ = a; + _mm_setzero_si128().simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u16x8(self, a: u16x8, b: u16x8) -> u16x8 { crate::kernel!( #[inline(always)] @@ -4594,6 +4640,18 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_i32x4(self, a: i32x4) -> i32x4 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: i32x4) -> i32x4 { + let a = a.into(); + let bounded = _mm_max_epi32(a, _mm_set1_epi32(i32::MIN + 1)); + _mm_sub_epi32(_mm_setzero_si128(), bounded).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_i32x4(self, a: i32x4, b: i32x4) -> i32x4 { crate::kernel!( #[inline(always)] @@ -5245,6 +5303,17 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_u32x4(self, a: u32x4) -> u32x4 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: u32x4) -> u32x4 { + let _ = a; + _mm_setzero_si128().simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u32x4(self, a: u32x4, b: u32x4) -> u32x4 { crate::kernel!( #[inline(always)] @@ -6856,6 +6925,18 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_i64x2(self, a: i64x2) -> i64x2 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: i64x2) -> i64x2 { + let a = a.into(); + let bounded = _mm_max_epi64(a, _mm_set1_epi64x(i64::MIN + 1)); + _mm_sub_epi64(_mm_setzero_si128(), bounded).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_i64x2(self, a: i64x2, b: i64x2) -> i64x2 { crate::kernel!( #[inline(always)] @@ -7435,6 +7516,17 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_u64x2(self, a: u64x2) -> u64x2 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: u64x2) -> u64x2 { + let _ = a; + _mm_setzero_si128().simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u64x2(self, a: u64x2, b: u64x2) -> u64x2 { crate::kernel!( #[inline(always)] @@ -8973,6 +9065,18 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_i8x32(self, a: i8x32) -> i8x32 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: i8x32) -> i8x32 { + let a = a.into(); + let bounded = _mm256_max_epi8(a, _mm256_set1_epi8(i8::MIN + 1)); + _mm256_sub_epi8(_mm256_setzero_si256(), bounded).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_i8x32(self, a: i8x32, b: i8x32) -> i8x32 { crate::kernel!( #[inline(always)] @@ -9755,6 +9859,17 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_u8x32(self, a: u8x32) -> u8x32 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: u8x32) -> u8x32 { + let _ = a; + _mm256_setzero_si256().simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u8x32(self, a: u8x32, b: u8x32) -> u8x32 { crate::kernel!( #[inline(always)] @@ -10633,6 +10748,18 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_i16x16(self, a: i16x16) -> i16x16 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: i16x16) -> i16x16 { + let a = a.into(); + let bounded = _mm256_max_epi16(a, _mm256_set1_epi16(i16::MIN + 1)); + _mm256_sub_epi16(_mm256_setzero_si256(), bounded).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_i16x16(self, a: i16x16, b: i16x16) -> i16x16 { crate::kernel!( #[inline(always)] @@ -11317,6 +11444,17 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_u16x16(self, a: u16x16) -> u16x16 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: u16x16) -> u16x16 { + let _ = a; + _mm256_setzero_si256().simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u16x16(self, a: u16x16, b: u16x16) -> u16x16 { crate::kernel!( #[inline(always)] @@ -12209,6 +12347,18 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_i32x8(self, a: i32x8) -> i32x8 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: i32x8) -> i32x8 { + let a = a.into(); + let bounded = _mm256_max_epi32(a, _mm256_set1_epi32(i32::MIN + 1)); + _mm256_sub_epi32(_mm256_setzero_si256(), bounded).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_i32x8(self, a: i32x8, b: i32x8) -> i32x8 { crate::kernel!( #[inline(always)] @@ -12846,6 +12996,17 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_u32x8(self, a: u32x8) -> u32x8 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: u32x8) -> u32x8 { + let _ = a; + _mm256_setzero_si256().simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u32x8(self, a: u32x8, b: u32x8) -> u32x8 { crate::kernel!( #[inline(always)] @@ -14423,6 +14584,18 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_i64x4(self, a: i64x4) -> i64x4 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: i64x4) -> i64x4 { + let a = a.into(); + let bounded = _mm256_max_epi64(a, _mm256_set1_epi64x(i64::MIN + 1)); + _mm256_sub_epi64(_mm256_setzero_si256(), bounded).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_i64x4(self, a: i64x4, b: i64x4) -> i64x4 { crate::kernel!( #[inline(always)] @@ -14996,6 +15169,17 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_u64x4(self, a: u64x4) -> u64x4 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: u64x4) -> u64x4 { + let _ = a; + _mm256_setzero_si256().simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u64x4(self, a: u64x4, b: u64x4) -> u64x4 { crate::kernel!( #[inline(always)] @@ -16523,6 +16707,18 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_i8x64(self, a: i8x64) -> i8x64 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: i8x64) -> i8x64 { + let a = a.into(); + let bounded = _mm512_max_epi8(a, _mm512_set1_epi8(i8::MIN + 1)); + _mm512_sub_epi8(_mm512_setzero_si512(), bounded).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_i8x64(self, a: i8x64, b: i8x64) -> i8x64 { crate::kernel!( #[inline(always)] @@ -17294,6 +17490,17 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_u8x64(self, a: u8x64) -> u8x64 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: u8x64) -> u8x64 { + let _ = a; + _mm512_setzero_si512().simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u8x64(self, a: u8x64, b: u8x64) -> u8x64 { crate::kernel!( #[inline(always)] @@ -18154,6 +18361,18 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_i16x32(self, a: i16x32) -> i16x32 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: i16x32) -> i16x32 { + let a = a.into(); + let bounded = _mm512_max_epi16(a, _mm512_set1_epi16(i16::MIN + 1)); + _mm512_sub_epi16(_mm512_setzero_si512(), bounded).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_i16x32(self, a: i16x32, b: i16x32) -> i16x32 { crate::kernel!( #[inline(always)] @@ -18842,6 +19061,17 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_u16x32(self, a: u16x32) -> u16x32 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: u16x32) -> u16x32 { + let _ = a; + _mm512_setzero_si512().simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u16x32(self, a: u16x32, b: u16x32) -> u16x32 { crate::kernel!( #[inline(always)] @@ -19755,6 +19985,18 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_i32x16(self, a: i32x16) -> i32x16 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: i32x16) -> i32x16 { + let a = a.into(); + let bounded = _mm512_max_epi32(a, _mm512_set1_epi32(i32::MIN + 1)); + _mm512_sub_epi32(_mm512_setzero_si512(), bounded).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_i32x16(self, a: i32x16, b: i32x16) -> i32x16 { crate::kernel!( #[inline(always)] @@ -20415,6 +20657,17 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_u32x16(self, a: u32x16) -> u32x16 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: u32x16) -> u32x16 { + let _ = a; + _mm512_setzero_si512().simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u32x16(self, a: u32x16, b: u32x16) -> u32x16 { crate::kernel!( #[inline(always)] @@ -22015,6 +22268,18 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_i64x8(self, a: i64x8) -> i64x8 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: i64x8) -> i64x8 { + let a = a.into(); + let bounded = _mm512_max_epi64(a, _mm512_set1_epi64(i64::MIN + 1)); + _mm512_sub_epi64(_mm512_setzero_si512(), bounded).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_i64x8(self, a: i64x8, b: i64x8) -> i64x8 { crate::kernel!( #[inline(always)] @@ -22598,6 +22863,17 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_u64x8(self, a: u64x8) -> u64x8 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: u64x8) -> u64x8 { + let _ = a; + _mm512_setzero_si512().simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u64x8(self, a: u64x8, b: u64x8) -> u64x8 { crate::kernel!( #[inline(always)] diff --git a/fearless_simd/src/generated/fallback.rs b/fearless_simd/src/generated/fallback.rs index 2c96eb24..02a538ab 100644 --- a/fearless_simd/src/generated/fallback.rs +++ b/fearless_simd/src/generated/fallback.rs @@ -908,6 +908,28 @@ impl Simd for Fallback { .simd_into(self) } #[inline(always)] + fn saturating_neg_i8x16(self, a: i8x16) -> i8x16 { + [ + a[0usize].saturating_neg(), + a[1usize].saturating_neg(), + a[2usize].saturating_neg(), + a[3usize].saturating_neg(), + a[4usize].saturating_neg(), + a[5usize].saturating_neg(), + a[6usize].saturating_neg(), + a[7usize].saturating_neg(), + a[8usize].saturating_neg(), + a[9usize].saturating_neg(), + a[10usize].saturating_neg(), + a[11usize].saturating_neg(), + a[12usize].saturating_neg(), + a[13usize].saturating_neg(), + a[14usize].saturating_neg(), + a[15usize].saturating_neg(), + ] + .simd_into(self) + } + #[inline(always)] fn mul_i8x16(self, a: i8x16, b: i8x16) -> i8x16 { [ i8::wrapping_mul(a[0usize], b[0usize]), @@ -2071,6 +2093,11 @@ impl Simd for Fallback { .simd_into(self) } #[inline(always)] + fn saturating_neg_u8x16(self, a: u8x16) -> u8x16 { + let _ = a; + [0; 16usize].simd_into(self) + } + #[inline(always)] fn mul_u8x16(self, a: u8x16, b: u8x16) -> u8x16 { [ u8::wrapping_mul(a[0usize], b[0usize]), @@ -3468,6 +3495,20 @@ impl Simd for Fallback { .simd_into(self) } #[inline(always)] + fn saturating_neg_i16x8(self, a: i16x8) -> i16x8 { + [ + a[0usize].saturating_neg(), + a[1usize].saturating_neg(), + a[2usize].saturating_neg(), + a[3usize].saturating_neg(), + a[4usize].saturating_neg(), + a[5usize].saturating_neg(), + a[6usize].saturating_neg(), + a[7usize].saturating_neg(), + ] + .simd_into(self) + } + #[inline(always)] fn mul_i16x8(self, a: i16x8, b: i16x8) -> i16x8 { [ i16::wrapping_mul(a[0usize], b[0usize]), @@ -4212,6 +4253,11 @@ impl Simd for Fallback { .simd_into(self) } #[inline(always)] + fn saturating_neg_u16x8(self, a: u16x8) -> u16x8 { + let _ = a; + [0; 8usize].simd_into(self) + } + #[inline(always)] fn mul_u16x8(self, a: u16x8, b: u16x8) -> u16x8 { [ u16::wrapping_mul(a[0usize], b[0usize]), @@ -5230,6 +5276,16 @@ impl Simd for Fallback { .simd_into(self) } #[inline(always)] + fn saturating_neg_i32x4(self, a: i32x4) -> i32x4 { + [ + a[0usize].saturating_neg(), + a[1usize].saturating_neg(), + a[2usize].saturating_neg(), + a[3usize].saturating_neg(), + ] + .simd_into(self) + } + #[inline(always)] fn mul_i32x4(self, a: i32x4, b: i32x4) -> i32x4 { [ i32::wrapping_mul(a[0usize], b[0usize]), @@ -5742,6 +5798,11 @@ impl Simd for Fallback { .simd_into(self) } #[inline(always)] + fn saturating_neg_u32x4(self, a: u32x4) -> u32x4 { + let _ = a; + [0; 4usize].simd_into(self) + } + #[inline(always)] fn mul_u32x4(self, a: u32x4, b: u32x4) -> u32x4 { [ u32::wrapping_mul(a[0usize], b[0usize]), @@ -6850,6 +6911,10 @@ impl Simd for Fallback { .simd_into(self) } #[inline(always)] + fn saturating_neg_i64x2(self, a: i64x2) -> i64x2 { + [a[0usize].saturating_neg(), a[1usize].saturating_neg()].simd_into(self) + } + #[inline(always)] fn mul_i64x2(self, a: i64x2, b: i64x2) -> i64x2 { [ i64::wrapping_mul(a[0usize], b[0usize]), @@ -7217,6 +7282,11 @@ impl Simd for Fallback { .simd_into(self) } #[inline(always)] + fn saturating_neg_u64x2(self, a: u64x2) -> u64x2 { + let _ = a; + [0; 2usize].simd_into(self) + } + #[inline(always)] fn mul_u64x2(self, a: u64x2, b: u64x2) -> u64x2 { [ u64::wrapping_mul(a[0usize], b[0usize]), diff --git a/fearless_simd/src/generated/neon.rs b/fearless_simd/src/generated/neon.rs index 4b8fcf03..cc0d41ad 100644 --- a/fearless_simd/src/generated/neon.rs +++ b/fearless_simd/src/generated/neon.rs @@ -898,6 +898,16 @@ impl Simd for Neon { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_i8x16(self, a: i8x16) -> i8x16 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Neon, a: i8x16) -> i8x16 { + vqnegvq_s8(a.into()).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_i8x16(self, a: i8x16, b: i8x16) -> i8x16 { crate::kernel!( #[inline(always)] @@ -1551,6 +1561,17 @@ impl Simd for Neon { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_u8x16(self, a: u8x16) -> u8x16 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Neon, a: u8x16) -> u8x16 { + let _ = a; + vdupq_n_u8(0).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u8x16(self, a: u8x16, b: u8x16) -> u8x16 { crate::kernel!( #[inline(always)] @@ -2351,6 +2372,16 @@ impl Simd for Neon { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_i16x8(self, a: i16x8) -> i16x8 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Neon, a: i16x8) -> i16x8 { + vqnegvq_s16(a.into()).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_i16x8(self, a: i16x8, b: i16x8) -> i16x8 { crate::kernel!( #[inline(always)] @@ -2938,6 +2969,17 @@ impl Simd for Neon { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_u16x8(self, a: u16x8) -> u16x8 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Neon, a: u16x8) -> u16x8 { + let _ = a; + vdupq_n_u16(0).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u16x8(self, a: u16x8, b: u16x8) -> u16x8 { crate::kernel!( #[inline(always)] @@ -3775,6 +3817,16 @@ impl Simd for Neon { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_i32x4(self, a: i32x4) -> i32x4 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Neon, a: i32x4) -> i32x4 { + vqnegvq_s32(a.into()).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_i32x4(self, a: i32x4, b: i32x4) -> i32x4 { crate::kernel!( #[inline(always)] @@ -4371,6 +4423,17 @@ impl Simd for Neon { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_u32x4(self, a: u32x4) -> u32x4 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Neon, a: u32x4) -> u32x4 { + let _ = a; + vdupq_n_u32(0).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u32x4(self, a: u32x4, b: u32x4) -> u32x4 { crate::kernel!( #[inline(always)] @@ -5838,6 +5901,16 @@ impl Simd for Neon { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_i64x2(self, a: i64x2) -> i64x2 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Neon, a: i64x2) -> i64x2 { + vqnegvq_s64(a.into()).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_i64x2(self, a: i64x2, b: i64x2) -> i64x2 { [ i64::wrapping_mul(a[0usize], b[0usize]), @@ -6353,6 +6426,17 @@ impl Simd for Neon { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_u64x2(self, a: u64x2) -> u64x2 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Neon, a: u64x2) -> u64x2 { + let _ = a; + vdupq_n_u64(0).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u64x2(self, a: u64x2, b: u64x2) -> u64x2 { [ u64::wrapping_mul(a[0usize], b[0usize]), diff --git a/fearless_simd/src/generated/simd_trait.rs b/fearless_simd/src/generated/simd_trait.rs index aa4ac8b4..60cd4188 100644 --- a/fearless_simd/src/generated/simd_trait.rs +++ b/fearless_simd/src/generated/simd_trait.rs @@ -557,6 +557,8 @@ pub trait Simd: fn sub_i8x16(self, a: i8x16, b: i8x16) -> i8x16; #[doc = "Subtract two vectors element-wise, saturating on overflow.\n\n\"Saturating\" means that if the result is not representable, the closest representable value (either `Element::MAX` or `Element::MIN`) is returned.\n\nOn x86 it is implemented in hardware only for 8-bit and 16-bit elements. For 32-bit and 64-bit vectors this operation is slower than wrapping subtraction on x86."] fn saturating_sub_i8x16(self, a: i8x16, b: i8x16) -> i8x16; + #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] + fn saturating_neg_i8x16(self, a: i8x16) -> i8x16; #[doc = "Multiply two vectors element-wise, wrapping on overflow."] fn mul_i8x16(self, a: i8x16, b: i8x16) -> i8x16; #[doc = "Compute the bitwise AND of two vectors."] @@ -722,6 +724,8 @@ pub trait Simd: fn sub_u8x16(self, a: u8x16, b: u8x16) -> u8x16; #[doc = "Subtract two vectors element-wise, saturating on overflow.\n\n\"Saturating\" means that if the result is not representable, the closest representable value (either `Element::MAX` or `Element::MIN`) is returned.\n\nOn x86 it is implemented in hardware only for 8-bit and 16-bit elements. For 32-bit and 64-bit vectors this operation is slower than wrapping subtraction on x86."] fn saturating_sub_u8x16(self, a: u8x16, b: u8x16) -> u8x16; + #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] + fn saturating_neg_u8x16(self, a: u8x16) -> u8x16; #[doc = "Multiply two vectors element-wise, wrapping on overflow."] fn mul_u8x16(self, a: u8x16, b: u8x16) -> u8x16; #[doc = "Compute the bitwise AND of two vectors."] @@ -963,6 +967,8 @@ pub trait Simd: fn sub_i16x8(self, a: i16x8, b: i16x8) -> i16x8; #[doc = "Subtract two vectors element-wise, saturating on overflow.\n\n\"Saturating\" means that if the result is not representable, the closest representable value (either `Element::MAX` or `Element::MIN`) is returned.\n\nOn x86 it is implemented in hardware only for 8-bit and 16-bit elements. For 32-bit and 64-bit vectors this operation is slower than wrapping subtraction on x86."] fn saturating_sub_i16x8(self, a: i16x8, b: i16x8) -> i16x8; + #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] + fn saturating_neg_i16x8(self, a: i16x8) -> i16x8; #[doc = "Multiply two vectors element-wise, wrapping on overflow."] fn mul_i16x8(self, a: i16x8, b: i16x8) -> i16x8; #[doc = "Compute the bitwise AND of two vectors."] @@ -1157,6 +1163,8 @@ pub trait Simd: fn sub_u16x8(self, a: u16x8, b: u16x8) -> u16x8; #[doc = "Subtract two vectors element-wise, saturating on overflow.\n\n\"Saturating\" means that if the result is not representable, the closest representable value (either `Element::MAX` or `Element::MIN`) is returned.\n\nOn x86 it is implemented in hardware only for 8-bit and 16-bit elements. For 32-bit and 64-bit vectors this operation is slower than wrapping subtraction on x86."] fn saturating_sub_u16x8(self, a: u16x8, b: u16x8) -> u16x8; + #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] + fn saturating_neg_u16x8(self, a: u16x8) -> u16x8; #[doc = "Multiply two vectors element-wise, wrapping on overflow."] fn mul_u16x8(self, a: u16x8, b: u16x8) -> u16x8; #[doc = "Compute the bitwise AND of two vectors."] @@ -1406,6 +1414,8 @@ pub trait Simd: fn sub_i32x4(self, a: i32x4, b: i32x4) -> i32x4; #[doc = "Subtract two vectors element-wise, saturating on overflow.\n\n\"Saturating\" means that if the result is not representable, the closest representable value (either `Element::MAX` or `Element::MIN`) is returned.\n\nOn x86 it is implemented in hardware only for 8-bit and 16-bit elements. For 32-bit and 64-bit vectors this operation is slower than wrapping subtraction on x86."] fn saturating_sub_i32x4(self, a: i32x4, b: i32x4) -> i32x4; + #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] + fn saturating_neg_i32x4(self, a: i32x4) -> i32x4; #[doc = "Multiply two vectors element-wise, wrapping on overflow."] fn mul_i32x4(self, a: i32x4, b: i32x4) -> i32x4; #[doc = "Compute the bitwise AND of two vectors."] @@ -1602,6 +1612,8 @@ pub trait Simd: fn sub_u32x4(self, a: u32x4, b: u32x4) -> u32x4; #[doc = "Subtract two vectors element-wise, saturating on overflow.\n\n\"Saturating\" means that if the result is not representable, the closest representable value (either `Element::MAX` or `Element::MIN`) is returned.\n\nOn x86 it is implemented in hardware only for 8-bit and 16-bit elements. For 32-bit and 64-bit vectors this operation is slower than wrapping subtraction on x86."] fn saturating_sub_u32x4(self, a: u32x4, b: u32x4) -> u32x4; + #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] + fn saturating_neg_u32x4(self, a: u32x4) -> u32x4; #[doc = "Multiply two vectors element-wise, wrapping on overflow."] fn mul_u32x4(self, a: u32x4, b: u32x4) -> u32x4; #[doc = "Compute the bitwise AND of two vectors."] @@ -2068,6 +2080,8 @@ pub trait Simd: fn sub_i64x2(self, a: i64x2, b: i64x2) -> i64x2; #[doc = "Subtract two vectors element-wise, saturating on overflow.\n\n\"Saturating\" means that if the result is not representable, the closest representable value (either `Element::MAX` or `Element::MIN`) is returned.\n\nOn x86 it is implemented in hardware only for 8-bit and 16-bit elements. For 32-bit and 64-bit vectors this operation is slower than wrapping subtraction on x86."] fn saturating_sub_i64x2(self, a: i64x2, b: i64x2) -> i64x2; + #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] + fn saturating_neg_i64x2(self, a: i64x2) -> i64x2; #[doc = "Multiply two vectors element-wise, wrapping on overflow."] fn mul_i64x2(self, a: i64x2, b: i64x2) -> i64x2; #[doc = "Compute the bitwise AND of two vectors."] @@ -2254,6 +2268,8 @@ pub trait Simd: fn sub_u64x2(self, a: u64x2, b: u64x2) -> u64x2; #[doc = "Subtract two vectors element-wise, saturating on overflow.\n\n\"Saturating\" means that if the result is not representable, the closest representable value (either `Element::MAX` or `Element::MIN`) is returned.\n\nOn x86 it is implemented in hardware only for 8-bit and 16-bit elements. For 32-bit and 64-bit vectors this operation is slower than wrapping subtraction on x86."] fn saturating_sub_u64x2(self, a: u64x2, b: u64x2) -> u64x2; + #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] + fn saturating_neg_u64x2(self, a: u64x2) -> u64x2; #[doc = "Multiply two vectors element-wise, wrapping on overflow."] fn mul_u64x2(self, a: u64x2, b: u64x2) -> u64x2; #[doc = "Compute the bitwise AND of two vectors."] @@ -3032,6 +3048,12 @@ pub trait Simd: self.saturating_sub_i8x16(a1, b1), ) } + #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_neg_i8x32(self, a: i8x32) -> i8x32 { + let (a0, a1) = self.split_i8x32(a); + self.combine_i8x16(self.saturating_neg_i8x16(a0), self.saturating_neg_i8x16(a1)) + } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] fn mul_i8x32(self, a: i8x32, b: i8x32) -> i8x32 { @@ -3436,6 +3458,12 @@ pub trait Simd: self.saturating_sub_u8x16(a1, b1), ) } + #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_neg_u8x32(self, a: u8x32) -> u8x32 { + let (a0, a1) = self.split_u8x32(a); + self.combine_u8x16(self.saturating_neg_u8x16(a0), self.saturating_neg_u8x16(a1)) + } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] fn mul_u8x32(self, a: u8x32, b: u8x32) -> u8x32 { @@ -3991,6 +4019,12 @@ pub trait Simd: self.saturating_sub_i16x8(a1, b1), ) } + #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_neg_i16x16(self, a: i16x16) -> i16x16 { + let (a0, a1) = self.split_i16x16(a); + self.combine_i16x8(self.saturating_neg_i16x8(a0), self.saturating_neg_i16x8(a1)) + } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] fn mul_i16x16(self, a: i16x16, b: i16x16) -> i16x16 { @@ -4441,6 +4475,12 @@ pub trait Simd: self.saturating_sub_u16x8(a1, b1), ) } + #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_neg_u16x16(self, a: u16x16) -> u16x16 { + let (a0, a1) = self.split_u16x16(a); + self.combine_u16x8(self.saturating_neg_u16x8(a0), self.saturating_neg_u16x8(a1)) + } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] fn mul_u16x16(self, a: u16x16, b: u16x16) -> u16x16 { @@ -5026,6 +5066,12 @@ pub trait Simd: self.saturating_sub_i32x4(a1, b1), ) } + #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_neg_i32x8(self, a: i32x8) -> i32x8 { + let (a0, a1) = self.split_i32x8(a); + self.combine_i32x4(self.saturating_neg_i32x4(a0), self.saturating_neg_i32x4(a1)) + } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] fn mul_i32x8(self, a: i32x8, b: i32x8) -> i32x8 { @@ -5478,6 +5524,12 @@ pub trait Simd: self.saturating_sub_u32x4(a1, b1), ) } + #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_neg_u32x8(self, a: u32x8) -> u32x8 { + let (a0, a1) = self.split_u32x8(a); + self.combine_u32x4(self.saturating_neg_u32x4(a0), self.saturating_neg_u32x4(a1)) + } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] fn mul_u32x8(self, a: u32x8, b: u32x8) -> u32x8 { @@ -6568,6 +6620,12 @@ pub trait Simd: self.saturating_sub_i64x2(a1, b1), ) } + #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_neg_i64x4(self, a: i64x4) -> i64x4 { + let (a0, a1) = self.split_i64x4(a); + self.combine_i64x2(self.saturating_neg_i64x2(a0), self.saturating_neg_i64x2(a1)) + } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] fn mul_i64x4(self, a: i64x4, b: i64x4) -> i64x4 { @@ -6969,6 +7027,12 @@ pub trait Simd: self.saturating_sub_u64x2(a1, b1), ) } + #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_neg_u64x4(self, a: u64x4) -> u64x4 { + let (a0, a1) = self.split_u64x4(a); + self.combine_u64x2(self.saturating_neg_u64x2(a0), self.saturating_neg_u64x2(a1)) + } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] fn mul_u64x4(self, a: u64x4, b: u64x4) -> u64x4 { @@ -7990,6 +8054,12 @@ pub trait Simd: self.saturating_sub_i8x32(a1, b1), ) } + #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_neg_i8x64(self, a: i8x64) -> i8x64 { + let (a0, a1) = self.split_i8x64(a); + self.combine_i8x32(self.saturating_neg_i8x32(a0), self.saturating_neg_i8x32(a1)) + } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] fn mul_i8x64(self, a: i8x64, b: i8x64) -> i8x64 { @@ -8392,6 +8462,12 @@ pub trait Simd: self.saturating_sub_u8x32(a1, b1), ) } + #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_neg_u8x64(self, a: u8x64) -> u8x64 { + let (a0, a1) = self.split_u8x64(a); + self.combine_u8x32(self.saturating_neg_u8x32(a0), self.saturating_neg_u8x32(a1)) + } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] fn mul_u8x64(self, a: u8x64, b: u8x64) -> u8x64 { @@ -8943,6 +9019,15 @@ pub trait Simd: self.saturating_sub_i16x16(a1, b1), ) } + #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_neg_i16x32(self, a: i16x32) -> i16x32 { + let (a0, a1) = self.split_i16x32(a); + self.combine_i16x16( + self.saturating_neg_i16x16(a0), + self.saturating_neg_i16x16(a1), + ) + } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] fn mul_i16x32(self, a: i16x32, b: i16x32) -> i16x32 { @@ -9397,6 +9482,15 @@ pub trait Simd: self.saturating_sub_u16x16(a1, b1), ) } + #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_neg_u16x32(self, a: u16x32) -> u16x32 { + let (a0, a1) = self.split_u16x32(a); + self.combine_u16x16( + self.saturating_neg_u16x16(a0), + self.saturating_neg_u16x16(a1), + ) + } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] fn mul_u16x32(self, a: u16x32, b: u16x32) -> u16x32 { @@ -9991,6 +10085,12 @@ pub trait Simd: self.saturating_sub_i32x8(a1, b1), ) } + #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_neg_i32x16(self, a: i32x16) -> i32x16 { + let (a0, a1) = self.split_i32x16(a); + self.combine_i32x8(self.saturating_neg_i32x8(a0), self.saturating_neg_i32x8(a1)) + } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] fn mul_i32x16(self, a: i32x16, b: i32x16) -> i32x16 { @@ -10445,6 +10545,12 @@ pub trait Simd: self.saturating_sub_u32x8(a1, b1), ) } + #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_neg_u32x16(self, a: u32x16) -> u32x16 { + let (a0, a1) = self.split_u32x16(a); + self.combine_u32x8(self.saturating_neg_u32x8(a0), self.saturating_neg_u32x8(a1)) + } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] fn mul_u32x16(self, a: u32x16, b: u32x16) -> u32x16 { @@ -11529,6 +11635,12 @@ pub trait Simd: self.saturating_sub_i64x4(a1, b1), ) } + #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_neg_i64x8(self, a: i64x8) -> i64x8 { + let (a0, a1) = self.split_i64x8(a); + self.combine_i64x4(self.saturating_neg_i64x4(a0), self.saturating_neg_i64x4(a1)) + } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] fn mul_i64x8(self, a: i64x8, b: i64x8) -> i64x8 { @@ -11928,6 +12040,12 @@ pub trait Simd: self.saturating_sub_u64x4(a1, b1), ) } + #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_neg_u64x8(self, a: u64x8) -> u64x8 { + let (a0, a1) = self.split_u64x8(a); + self.combine_u64x4(self.saturating_neg_u64x4(a0), self.saturating_neg_u64x4(a1)) + } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] fn mul_u64x8(self, a: u64x8, b: u64x8) -> u64x8 { @@ -13281,6 +13399,8 @@ pub trait SimdInt: fn saturating_add(self, rhs: impl SimdInto) -> Self; #[doc = "Subtract two vectors element-wise, saturating on overflow.\n\n\"Saturating\" means that if the result is not representable, the closest representable value (either `Element::MAX` or `Element::MIN`) is returned.\n\nOn x86 it is implemented in hardware only for 8-bit and 16-bit elements. For 32-bit and 64-bit vectors this operation is slower than wrapping subtraction on x86."] fn saturating_sub(self, rhs: impl SimdInto) -> Self; + #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] + fn saturating_neg(self) -> Self; } #[doc = r" Functionality implemented by SIMD masks."] #[doc = r""] diff --git a/fearless_simd/src/generated/simd_types.rs b/fearless_simd/src/generated/simd_types.rs index 9891b00e..f3e7c498 100644 --- a/fearless_simd/src/generated/simd_types.rs +++ b/fearless_simd/src/generated/simd_types.rs @@ -780,6 +780,10 @@ impl crate::SimdInt for i8x16 { self.simd .saturating_sub_i8x16(self, rhs.simd_into(self.simd)) } + #[inline(always)] + fn saturating_neg(self) -> Self { + self.simd.saturating_neg_i8x16(self) + } } impl SimdWiden for i8x16 { type Widened = i16x8; @@ -1169,6 +1173,10 @@ impl crate::SimdInt for u8x16 { self.simd .saturating_sub_u8x16(self, rhs.simd_into(self.simd)) } + #[inline(always)] + fn saturating_neg(self) -> Self { + self.simd.saturating_neg_u8x16(self) + } } impl SimdWiden for u8x16 { type Widened = u16x8; @@ -1693,6 +1701,10 @@ impl crate::SimdInt for i16x8 { self.simd .saturating_sub_i16x8(self, rhs.simd_into(self.simd)) } + #[inline(always)] + fn saturating_neg(self) -> Self { + self.simd.saturating_neg_i16x8(self) + } } impl SimdWiden for i16x8 { type Widened = i32x4; @@ -2089,6 +2101,10 @@ impl crate::SimdInt for u16x8 { self.simd .saturating_sub_u16x8(self, rhs.simd_into(self.simd)) } + #[inline(always)] + fn saturating_neg(self) -> Self { + self.simd.saturating_neg_u16x8(self) + } } impl SimdWiden for u16x8 { type Widened = u32x4; @@ -2623,6 +2639,10 @@ impl crate::SimdInt for i32x4 { self.simd .saturating_sub_i32x4(self, rhs.simd_into(self.simd)) } + #[inline(always)] + fn saturating_neg(self) -> Self { + self.simd.saturating_neg_i32x4(self) + } } impl SimdCvtTruncate> for i32x4 { #[doc = "Convert each floating-point element to a signed 32-bit integer, truncating towards zero.\n\nOut-of-range values or NaN will produce implementation-defined results."] @@ -3019,6 +3039,10 @@ impl crate::SimdInt for u32x4 { self.simd .saturating_sub_u32x4(self, rhs.simd_into(self.simd)) } + #[inline(always)] + fn saturating_neg(self) -> Self { + self.simd.saturating_neg_u32x4(self) + } } impl SimdCvtTruncate> for u32x4 { #[doc = "Convert each floating-point element to an unsigned 32-bit integer, truncating towards zero.\n\nOut-of-range values or NaN will produce implementation-defined results.\n\nOn x86 platforms below AVX-512, this operation will still be slower than converting to `i32`, because there is no native instruction for converting to `u32`.\nIf you know your values fit within range of an `i32`, you should convert to an `i32` and cast to your desired datatype afterwards."] @@ -3995,6 +4019,10 @@ impl crate::SimdInt for i64x2 { self.simd .saturating_sub_i64x2(self, rhs.simd_into(self.simd)) } + #[inline(always)] + fn saturating_neg(self) -> Self { + self.simd.saturating_neg_i64x2(self) + } } impl SimdCvtTruncate> for i64x2 { #[doc = "Convert each floating-point element to a signed 64-bit integer, truncating towards zero.\n\nOut-of-range values or NaN will produce implementation-defined results."] @@ -4365,6 +4393,10 @@ impl crate::SimdInt for u64x2 { self.simd .saturating_sub_u64x2(self, rhs.simd_into(self.simd)) } + #[inline(always)] + fn saturating_neg(self) -> Self { + self.simd.saturating_neg_u64x2(self) + } } impl SimdCvtTruncate> for u64x2 { #[doc = "Convert each floating-point element to an unsigned 64-bit integer, truncating towards zero.\n\nOut-of-range values or NaN will produce implementation-defined results."] @@ -5336,6 +5368,10 @@ impl crate::SimdInt for i8x32 { self.simd .saturating_sub_i8x32(self, rhs.simd_into(self.simd)) } + #[inline(always)] + fn saturating_neg(self) -> Self { + self.simd.saturating_neg_i8x32(self) + } } impl SimdWiden for i8x32 { type Widened = i16x16; @@ -5736,6 +5772,10 @@ impl crate::SimdInt for u8x32 { self.simd .saturating_sub_u8x32(self, rhs.simd_into(self.simd)) } + #[inline(always)] + fn saturating_neg(self) -> Self { + self.simd.saturating_neg_u8x32(self) + } } impl SimdWiden for u8x32 { type Widened = u16x16; @@ -6257,6 +6297,10 @@ impl crate::SimdInt for i16x16 { self.simd .saturating_sub_i16x16(self, rhs.simd_into(self.simd)) } + #[inline(always)] + fn saturating_neg(self) -> Self { + self.simd.saturating_neg_i16x16(self) + } } impl SimdWiden for i16x16 { type Widened = i32x8; @@ -6659,6 +6703,10 @@ impl crate::SimdInt for u16x16 { self.simd .saturating_sub_u16x16(self, rhs.simd_into(self.simd)) } + #[inline(always)] + fn saturating_neg(self) -> Self { + self.simd.saturating_neg_u16x16(self) + } } impl SimdWiden for u16x16 { type Widened = u32x8; @@ -7202,6 +7250,10 @@ impl crate::SimdInt for i32x8 { self.simd .saturating_sub_i32x8(self, rhs.simd_into(self.simd)) } + #[inline(always)] + fn saturating_neg(self) -> Self { + self.simd.saturating_neg_i32x8(self) + } } impl SimdCvtTruncate> for i32x8 { #[doc = "Convert each floating-point element to a signed 32-bit integer, truncating towards zero.\n\nOut-of-range values or NaN will produce implementation-defined results."] @@ -7605,6 +7657,10 @@ impl crate::SimdInt for u32x8 { self.simd .saturating_sub_u32x8(self, rhs.simd_into(self.simd)) } + #[inline(always)] + fn saturating_neg(self) -> Self { + self.simd.saturating_neg_u32x8(self) + } } impl SimdCvtTruncate> for u32x8 { #[doc = "Convert each floating-point element to an unsigned 32-bit integer, truncating towards zero.\n\nOut-of-range values or NaN will produce implementation-defined results.\n\nOn x86 platforms below AVX-512, this operation will still be slower than converting to `i32`, because there is no native instruction for converting to `u32`.\nIf you know your values fit within range of an `i32`, you should convert to an `i32` and cast to your desired datatype afterwards."] @@ -8578,6 +8634,10 @@ impl crate::SimdInt for i64x4 { self.simd .saturating_sub_i64x4(self, rhs.simd_into(self.simd)) } + #[inline(always)] + fn saturating_neg(self) -> Self { + self.simd.saturating_neg_i64x4(self) + } } impl SimdCvtTruncate> for i64x4 { #[doc = "Convert each floating-point element to a signed 64-bit integer, truncating towards zero.\n\nOut-of-range values or NaN will produce implementation-defined results."] @@ -8943,6 +9003,10 @@ impl crate::SimdInt for u64x4 { self.simd .saturating_sub_u64x4(self, rhs.simd_into(self.simd)) } + #[inline(always)] + fn saturating_neg(self) -> Self { + self.simd.saturating_neg_u64x4(self) + } } impl SimdCvtTruncate> for u64x4 { #[doc = "Convert each floating-point element to an unsigned 64-bit integer, truncating towards zero.\n\nOut-of-range values or NaN will produce implementation-defined results."] @@ -9952,6 +10016,10 @@ impl crate::SimdInt for i8x64 { self.simd .saturating_sub_i8x64(self, rhs.simd_into(self.simd)) } + #[inline(always)] + fn saturating_neg(self) -> Self { + self.simd.saturating_neg_i8x64(self) + } } impl SimdWiden for i8x64 { type Widened = i16x32; @@ -10378,6 +10446,10 @@ impl crate::SimdInt for u8x64 { self.simd .saturating_sub_u8x64(self, rhs.simd_into(self.simd)) } + #[inline(always)] + fn saturating_neg(self) -> Self { + self.simd.saturating_neg_u8x64(self) + } } impl SimdWiden for u8x64 { type Widened = u16x32; @@ -10902,6 +10974,10 @@ impl crate::SimdInt for i16x32 { self.simd .saturating_sub_i16x32(self, rhs.simd_into(self.simd)) } + #[inline(always)] + fn saturating_neg(self) -> Self { + self.simd.saturating_neg_i16x32(self) + } } impl SimdWiden for i16x32 { type Widened = i32x16; @@ -11314,6 +11390,10 @@ impl crate::SimdInt for u16x32 { self.simd .saturating_sub_u16x32(self, rhs.simd_into(self.simd)) } + #[inline(always)] + fn saturating_neg(self) -> Self { + self.simd.saturating_neg_u16x32(self) + } } impl SimdWiden for u16x32 { type Widened = u32x16; @@ -11853,6 +11933,10 @@ impl crate::SimdInt for i32x16 { self.simd .saturating_sub_i32x16(self, rhs.simd_into(self.simd)) } + #[inline(always)] + fn saturating_neg(self) -> Self { + self.simd.saturating_neg_i32x16(self) + } } impl SimdCvtTruncate> for i32x16 { #[doc = "Convert each floating-point element to a signed 32-bit integer, truncating towards zero.\n\nOut-of-range values or NaN will produce implementation-defined results."] @@ -12261,6 +12345,10 @@ impl crate::SimdInt for u32x16 { self.simd .saturating_sub_u32x16(self, rhs.simd_into(self.simd)) } + #[inline(always)] + fn saturating_neg(self) -> Self { + self.simd.saturating_neg_u32x16(self) + } } impl SimdCvtTruncate> for u32x16 { #[doc = "Convert each floating-point element to an unsigned 32-bit integer, truncating towards zero.\n\nOut-of-range values or NaN will produce implementation-defined results.\n\nOn x86 platforms below AVX-512, this operation will still be slower than converting to `i32`, because there is no native instruction for converting to `u32`.\nIf you know your values fit within range of an `i32`, you should convert to an `i32` and cast to your desired datatype afterwards."] @@ -13248,6 +13336,10 @@ impl crate::SimdInt for i64x8 { self.simd .saturating_sub_i64x8(self, rhs.simd_into(self.simd)) } + #[inline(always)] + fn saturating_neg(self) -> Self { + self.simd.saturating_neg_i64x8(self) + } } impl SimdCvtTruncate> for i64x8 { #[doc = "Convert each floating-point element to a signed 64-bit integer, truncating towards zero.\n\nOut-of-range values or NaN will produce implementation-defined results."] @@ -13619,6 +13711,10 @@ impl crate::SimdInt for u64x8 { self.simd .saturating_sub_u64x8(self, rhs.simd_into(self.simd)) } + #[inline(always)] + fn saturating_neg(self) -> Self { + self.simd.saturating_neg_u64x8(self) + } } impl SimdCvtTruncate> for u64x8 { #[doc = "Convert each floating-point element to an unsigned 64-bit integer, truncating towards zero.\n\nOut-of-range values or NaN will produce implementation-defined results."] diff --git a/fearless_simd/src/generated/sse2.rs b/fearless_simd/src/generated/sse2.rs index 13701067..5ee8a6f6 100644 --- a/fearless_simd/src/generated/sse2.rs +++ b/fearless_simd/src/generated/sse2.rs @@ -1076,6 +1076,19 @@ impl Simd for Sse2 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_i8x16(self, a: i8x16) -> i8x16 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse2, a: i8x16) -> i8x16 { + let a = a.into(); + let neg_a = _mm_sub_epi8(_mm_setzero_si128(), a); + let is_min = _mm_cmpeq_epi8(neg_a, _mm_set1_epi8(i8::MIN)); + _mm_xor_si128(neg_a, is_min).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_i8x16(self, a: i8x16, b: i8x16) -> i8x16 { crate::kernel!( #[inline(always)] @@ -2030,6 +2043,17 @@ impl Simd for Sse2 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_u8x16(self, a: u8x16) -> u8x16 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse2, a: u8x16) -> u8x16 { + let _ = a; + _mm_setzero_si128().simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u8x16(self, a: u8x16, b: u8x16) -> u8x16 { crate::kernel!( #[inline(always)] @@ -3015,6 +3039,18 @@ impl Simd for Sse2 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_i16x8(self, a: i16x8) -> i16x8 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse2, a: i16x8) -> i16x8 { + let a = a.into(); + let bounded = _mm_max_epi16(a, _mm_set1_epi16(i16::MIN + 1)); + _mm_sub_epi16(_mm_setzero_si128(), bounded).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_i16x8(self, a: i16x8, b: i16x8) -> i16x8 { crate::kernel!( #[inline(always)] @@ -3676,6 +3712,17 @@ impl Simd for Sse2 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_u16x8(self, a: u16x8) -> u16x8 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse2, a: u16x8) -> u16x8 { + let _ = a; + _mm_setzero_si128().simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u16x8(self, a: u16x8, b: u16x8) -> u16x8 { crate::kernel!( #[inline(always)] @@ -4685,6 +4732,19 @@ impl Simd for Sse2 { .simd_into(self) } #[inline(always)] + fn saturating_neg_i32x4(self, a: i32x4) -> i32x4 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse2, a: i32x4) -> i32x4 { + let a = a.into(); + let neg_a = _mm_sub_epi32(_mm_setzero_si128(), a); + let is_min = _mm_cmpeq_epi32(neg_a, _mm_set1_epi32(i32::MIN)); + _mm_xor_si128(neg_a, is_min).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_i32x4(self, a: i32x4, b: i32x4) -> i32x4 { [ i32::wrapping_mul(a[0usize], b[0usize]), @@ -5356,6 +5416,17 @@ impl Simd for Sse2 { .simd_into(self) } #[inline(always)] + fn saturating_neg_u32x4(self, a: u32x4) -> u32x4 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse2, a: u32x4) -> u32x4 { + let _ = a; + _mm_setzero_si128().simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u32x4(self, a: u32x4, b: u32x4) -> u32x4 { [ u32::wrapping_mul(a[0usize], b[0usize]), @@ -6950,6 +7021,10 @@ impl Simd for Sse2 { .simd_into(self) } #[inline(always)] + fn saturating_neg_i64x2(self, a: i64x2) -> i64x2 { + [a[0usize].saturating_neg(), a[1usize].saturating_neg()].simd_into(self) + } + #[inline(always)] fn mul_i64x2(self, a: i64x2, b: i64x2) -> i64x2 { crate::kernel!( #[inline(always)] @@ -7469,6 +7544,17 @@ impl Simd for Sse2 { .simd_into(self) } #[inline(always)] + fn saturating_neg_u64x2(self, a: u64x2) -> u64x2 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse2, a: u64x2) -> u64x2 { + let _ = a; + _mm_setzero_si128().simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u64x2(self, a: u64x2, b: u64x2) -> u64x2 { crate::kernel!( #[inline(always)] diff --git a/fearless_simd/src/generated/sse4_2.rs b/fearless_simd/src/generated/sse4_2.rs index e22afd37..1381d14f 100644 --- a/fearless_simd/src/generated/sse4_2.rs +++ b/fearless_simd/src/generated/sse4_2.rs @@ -1015,6 +1015,18 @@ impl Simd for Sse4_2 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_i8x16(self, a: i8x16) -> i8x16 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse4_2, a: i8x16) -> i8x16 { + let a = a.into(); + let bounded = _mm_max_epi8(a, _mm_set1_epi8(i8::MIN + 1)); + _mm_sub_epi8(_mm_setzero_si128(), bounded).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_i8x16(self, a: i8x16, b: i8x16) -> i8x16 { crate::kernel!( #[inline(always)] @@ -1829,6 +1841,17 @@ impl Simd for Sse4_2 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_u8x16(self, a: u8x16) -> u8x16 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse4_2, a: u8x16) -> u8x16 { + let _ = a; + _mm_setzero_si128().simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u8x16(self, a: u8x16, b: u8x16) -> u8x16 { crate::kernel!( #[inline(always)] @@ -2729,6 +2752,18 @@ impl Simd for Sse4_2 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_i16x8(self, a: i16x8) -> i16x8 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse4_2, a: i16x8) -> i16x8 { + let a = a.into(); + let bounded = _mm_max_epi16(a, _mm_set1_epi16(i16::MIN + 1)); + _mm_sub_epi16(_mm_setzero_si128(), bounded).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_i16x8(self, a: i16x8, b: i16x8) -> i16x8 { crate::kernel!( #[inline(always)] @@ -3383,6 +3418,17 @@ impl Simd for Sse4_2 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_u16x8(self, a: u16x8) -> u16x8 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse4_2, a: u16x8) -> u16x8 { + let _ = a; + _mm_setzero_si128().simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u16x8(self, a: u16x8, b: u16x8) -> u16x8 { crate::kernel!( #[inline(always)] @@ -4335,6 +4381,18 @@ impl Simd for Sse4_2 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_i32x4(self, a: i32x4) -> i32x4 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse4_2, a: i32x4) -> i32x4 { + let a = a.into(); + let bounded = _mm_max_epi32(a, _mm_set1_epi32(i32::MIN + 1)); + _mm_sub_epi32(_mm_setzero_si128(), bounded).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_i32x4(self, a: i32x4, b: i32x4) -> i32x4 { crate::kernel!( #[inline(always)] @@ -4979,6 +5037,17 @@ impl Simd for Sse4_2 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_u32x4(self, a: u32x4) -> u32x4 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse4_2, a: u32x4) -> u32x4 { + let _ = a; + _mm_setzero_si128().simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u32x4(self, a: u32x4, b: u32x4) -> u32x4 { crate::kernel!( #[inline(always)] @@ -6530,6 +6599,19 @@ impl Simd for Sse4_2 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_i64x2(self, a: i64x2) -> i64x2 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse4_2, a: i64x2) -> i64x2 { + let a = a.into(); + let neg_a = _mm_sub_epi64(_mm_setzero_si128(), a); + let is_min = _mm_cmpeq_epi64(neg_a, _mm_set1_epi64x(i64::MIN)); + _mm_xor_si128(neg_a, is_min).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_i64x2(self, a: i64x2, b: i64x2) -> i64x2 { crate::kernel!( #[inline(always)] @@ -7131,6 +7213,17 @@ impl Simd for Sse4_2 { kernel(self, a, b) } #[inline(always)] + fn saturating_neg_u64x2(self, a: u64x2) -> u64x2 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse4_2, a: u64x2) -> u64x2 { + let _ = a; + _mm_setzero_si128().simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] fn mul_u64x2(self, a: u64x2, b: u64x2) -> u64x2 { crate::kernel!( #[inline(always)] diff --git a/fearless_simd/src/generated/wasm.rs b/fearless_simd/src/generated/wasm.rs index e843e416..7cddeae8 100644 --- a/fearless_simd/src/generated/wasm.rs +++ b/fearless_simd/src/generated/wasm.rs @@ -669,6 +669,10 @@ impl Simd for WasmSimd128 { i8x16_sub_sat(a.into(), b.into()).simd_into(self) } #[inline(always)] + fn saturating_neg_i8x16(self, a: i8x16) -> i8x16 { + self.neg_i8x16(self.max_i8x16(a, self.splat(i8::MIN + 1))) + } + #[inline(always)] fn mul_i8x16(self, a: i8x16, b: i8x16) -> i8x16 { let low = i16x8_extmul_low_i8x16(a.into(), b.into()); let high = i16x8_extmul_high_i8x16(a.into(), b.into()); @@ -1237,6 +1241,11 @@ impl Simd for WasmSimd128 { u8x16_sub_sat(a.into(), b.into()).simd_into(self) } #[inline(always)] + fn saturating_neg_u8x16(self, a: u8x16) -> u8x16 { + let _ = a; + u32x4_splat(0).simd_into(self) + } + #[inline(always)] fn mul_u8x16(self, a: u8x16, b: u8x16) -> u8x16 { let low = u16x8_extmul_low_u8x16(a.into(), b.into()); let high = u16x8_extmul_high_u8x16(a.into(), b.into()); @@ -1854,6 +1863,10 @@ impl Simd for WasmSimd128 { i16x8_sub_sat(a.into(), b.into()).simd_into(self) } #[inline(always)] + fn saturating_neg_i16x8(self, a: i16x8) -> i16x8 { + self.neg_i16x8(self.max_i16x8(a, self.splat(i16::MIN + 1))) + } + #[inline(always)] fn mul_i16x8(self, a: i16x8, b: i16x8) -> i16x8 { i16x8_mul(a.into(), b.into()).simd_into(self) } @@ -2234,6 +2247,11 @@ impl Simd for WasmSimd128 { u16x8_sub_sat(a.into(), b.into()).simd_into(self) } #[inline(always)] + fn saturating_neg_u16x8(self, a: u16x8) -> u16x8 { + let _ = a; + u32x4_splat(0).simd_into(self) + } + #[inline(always)] fn mul_u16x8(self, a: u16x8, b: u16x8) -> u16x8 { u16x8_mul(a.into(), b.into()).simd_into(self) } @@ -2785,6 +2803,10 @@ impl Simd for WasmSimd128 { v128_bitselect(saturation, wrapped, overflow_mask).simd_into(self) } #[inline(always)] + fn saturating_neg_i32x4(self, a: i32x4) -> i32x4 { + self.neg_i32x4(self.max_i32x4(a, self.splat(i32::MIN + 1))) + } + #[inline(always)] fn mul_i32x4(self, a: i32x4, b: i32x4) -> i32x4 { i32x4_mul(a.into(), b.into()).simd_into(self) } @@ -3160,6 +3182,11 @@ impl Simd for WasmSimd128 { u32x4_sub(u32x4_max(a, b), b).simd_into(self) } #[inline(always)] + fn saturating_neg_u32x4(self, a: u32x4) -> u32x4 { + let _ = a; + u32x4_splat(0).simd_into(self) + } + #[inline(always)] fn mul_u32x4(self, a: u32x4, b: u32x4) -> u32x4 { u32x4_mul(a.into(), b.into()).simd_into(self) } @@ -4112,6 +4139,10 @@ impl Simd for WasmSimd128 { v128_bitselect(saturation, wrapped, overflow_mask).simd_into(self) } #[inline(always)] + fn saturating_neg_i64x2(self, a: i64x2) -> i64x2 { + self.neg_i64x2(self.max_i64x2(a, self.splat(i64::MIN + 1))) + } + #[inline(always)] fn mul_i64x2(self, a: i64x2, b: i64x2) -> i64x2 { i64x2_mul(a.into(), b.into()).simd_into(self) } @@ -4461,6 +4492,11 @@ impl Simd for WasmSimd128 { v128_andnot(wrapped, saturation_mask).simd_into(self) } #[inline(always)] + fn saturating_neg_u64x2(self, a: u64x2) -> u64x2 { + let _ = a; + u32x4_splat(0).simd_into(self) + } + #[inline(always)] fn mul_u64x2(self, a: u64x2, b: u64x2) -> u64x2 { u64x2_mul(a.into(), b.into()).simd_into(self) } diff --git a/fearless_simd_gen/src/mk_fallback.rs b/fearless_simd_gen/src/mk_fallback.rs index f94a7e7d..6698c6e4 100644 --- a/fearless_simd_gen/src/mk_fallback.rs +++ b/fearless_simd_gen/src/mk_fallback.rs @@ -163,6 +163,36 @@ impl Level for Fallback { }; } + if method == "saturating_neg" { + match vec_ty.scalar { + ScalarType::Unsigned => { + let n = vec_ty.len; + return quote! { + #method_sig { + let _ = a; + [0; #n].simd_into(self) + } + }; + } + ScalarType::Int => { + let items = make_list( + (0..vec_ty.len) + .map(|idx| { + let value = lane(quote! { a }, vec_ty, idx); + quote! { #value.saturating_neg() } + }) + .collect::>(), + ); + return quote! { + #method_sig { + #items.simd_into(self) + } + }; + } + _ => unreachable!(), + } + } + let items = make_list( (0..vec_ty.len) .map(|idx| { diff --git a/fearless_simd_gen/src/mk_neon.rs b/fearless_simd_gen/src/mk_neon.rs index 0334d002..946a5df7 100644 --- a/fearless_simd_gen/src/mk_neon.rs +++ b/fearless_simd_gen/src/mk_neon.rs @@ -341,6 +341,27 @@ impl Level for Neon { }); } + if method == "saturating_neg" { + match vec_ty.scalar { + ScalarType::Unsigned => { + return self.kernel_method(op, vec_ty, |token| { + let zero = format_ident!("vdupq_n_u{}", vec_ty.scalar_bits); + quote! { + let _ = a; + #zero(0).simd_into(#token) + } + }); + } + ScalarType::Int => { + let sat_neg = format_ident!("vqneg{}q_s{}", "v", vec_ty.scalar_bits); + return self.kernel_method(op, vec_ty, |token| { + quote! { #sat_neg(a.into()).simd_into(#token) } + }); + } + _ => unreachable!(), + } + } + if matches!(method, "to_degrees" | "to_radians") { return generic_to_degrees_radians(method_sig, method, vec_ty); } diff --git a/fearless_simd_gen/src/mk_wasm.rs b/fearless_simd_gen/src/mk_wasm.rs index f7f5326f..4a2e0452 100644 --- a/fearless_simd_gen/src/mk_wasm.rs +++ b/fearless_simd_gen/src/mk_wasm.rs @@ -554,6 +554,37 @@ impl Level for WasmSimd128 { }; } + if method == "saturating_neg" { + match vec_ty.scalar { + ScalarType::Unsigned => { + return quote! { + #method_sig { + let _ = a; + u32x4_splat(0).simd_into(self) + } + }; + } + ScalarType::Int => { + // Emulate saturating neg with -max(x, iN::MIN + 1) + let bound = match vec_ty.scalar_bits { + 8 => quote! { i8::MIN + 1 }, + 16 => quote! { i16::MIN + 1 }, + 32 => quote! { i32::MIN + 1 }, + 64 => quote! { i64::MIN + 1 }, + _ => unreachable!(), + }; + let max_op = generic_op_name("max", vec_ty); + let neg_op = generic_op_name("neg", vec_ty); + return quote! { + #method_sig { + self.#neg_op(self.#max_op(a, self.splat(#bound))) + } + }; + } + _ => unreachable!(), + } + } + if method == "round" { return generic_round(method_sig, vec_ty); } diff --git a/fearless_simd_gen/src/mk_x86.rs b/fearless_simd_gen/src/mk_x86.rs index 5fb6eda2..cc0d63bc 100644 --- a/fearless_simd_gen/src/mk_x86.rs +++ b/fearless_simd_gen/src/mk_x86.rs @@ -2777,6 +2777,93 @@ impl X86 { } } + if method == "saturating_neg" { + match vec_ty.scalar { + ScalarType::Unsigned => { + // For unsigned integers, saturating_neg always produces zero + return self.kernel_method(op, vec_ty, |token| { + let zero = intrinsic_ident("setzero", coarse_type(vec_ty), vec_ty.n_bits()); + quote! { + let _ = a; + #zero().simd_into(#token) + } + }); + } + ScalarType::Int => { + // x86 doesn't have saturating neg, so we emulate using saturating_neg(x) = -max(x, MIN+1). + // Unfortunately, the max-family of instructions have requirements: + // - SSE2: i16 available + // - SSE4.1, AVX2: i8, i16, i32 available + // - AVX512: i8, i16, i32, i64 available + + let can_use_max = match self { + Self::Sse2 => vec_ty.scalar_bits == 16, + Self::Sse4_2 | Self::Avx2 => matches!(vec_ty.scalar_bits, 8 | 16 | 32), + Self::Avx512 => matches!(vec_ty.scalar_bits, 8 | 16 | 32 | 64), + }; + + if can_use_max { + let min_plus_one = match vec_ty.scalar_bits { + 8 => quote! { i8::MIN + 1 }, + 16 => quote! { i16::MIN + 1 }, + 32 => quote! { i32::MIN + 1 }, + 64 => quote! { i64::MIN + 1 }, + _ => unreachable!(), + }; + let set1 = set1_intrinsic(vec_ty); + let max_intrinsic = simple_intrinsic("max", vec_ty); + let sub = simple_intrinsic("sub", vec_ty); + let zero = intrinsic_ident("setzero", coarse_type(vec_ty), vec_ty.n_bits()); + + return self.kernel_method(op, vec_ty, |token| { + quote! { + let a = a.into(); + let bounded = #max_intrinsic(a, #set1(#min_plus_one)); + #sub(#zero(), bounded).simd_into(#token) + } + }); + } + + // When max isn't avaialble, we use the following alternative formulation: + // Input: signed integer i: + // let a = -i (wrapping negation) + // return a ^ cmpeq(a, iN::MIN) + // This works because: + // - If i == iN::MIN, then a == iN::MIN (wrapping) and cmpeq produces -1, + // so a ^ -1 = !a = !iN::MIN = iN::MAX + // - Otherwise, cmpeq produces 0, so a ^ 0 = a + + // For SSE2 i64, cmpeq_epi64 isn't available, so fall back + if *self == Self::Sse2 && vec_ty.scalar_bits == 64 { + return fallback_method(op, vec_ty); + } + + let min_val = match vec_ty.scalar_bits { + 8 => quote! { i8::MIN }, + 16 => quote! { i16::MIN }, + 32 => quote! { i32::MIN }, + 64 => quote! { i64::MIN }, + _ => unreachable!(), + }; + let set1 = set1_intrinsic(vec_ty); + let cmpeq = simple_intrinsic("cmpeq", vec_ty); + let sub = simple_intrinsic("sub", vec_ty); + let xor = intrinsic_ident("xor", coarse_type(vec_ty), vec_ty.n_bits()); + let zero = intrinsic_ident("setzero", coarse_type(vec_ty), vec_ty.n_bits()); + + return self.kernel_method(op, vec_ty, |token| { + quote! { + let a = a.into(); + let neg_a = #sub(#zero(), a); + let is_min = #cmpeq(neg_a, #set1(#min_val)); + #xor(neg_a, is_min).simd_into(#token) + } + }); + } + _ => unreachable!(), + } + } + if method == "reverse" { if vec_ty.scalar == ScalarType::Mask { if *self == Self::Avx512 { diff --git a/fearless_simd_gen/src/ops.rs b/fearless_simd_gen/src/ops.rs index 413c4c54..8d7d3b23 100644 --- a/fearless_simd_gen/src/ops.rs +++ b/fearless_simd_gen/src/ops.rs @@ -1371,6 +1371,13 @@ assert_eq!(*i.reverse_bits(), [0xE6_A2_C4_80, 0xF7_B3_D5_91, 0xD0_50_90_10, 0xF0 On x86 it is implemented in hardware only for 8-bit and 16-bit elements. \ For 32-bit and 64-bit vectors this operation is slower than wrapping subtraction on x86.", ), + Op::new( + "saturating_neg", + OpKind::VecTraitMethod, + OpSig::Unary, + "Negate each element of the vector, saturating on overflow.\n\n\ + For unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`.", + ), Op::new( "mul", OpKind::Overloaded(CoreOpTrait::Mul), diff --git a/fearless_simd_tests/tests/harness/ops/mod.rs b/fearless_simd_tests/tests/harness/ops/mod.rs index 3478d066..32ed0735 100644 --- a/fearless_simd_tests/tests/harness/ops/mod.rs +++ b/fearless_simd_tests/tests/harness/ops/mod.rs @@ -81,6 +81,7 @@ mod rotate_elements_right; mod round; mod round_ties_even; mod saturating_add; +mod saturating_neg; mod saturating_sub; mod select; mod set; diff --git a/fearless_simd_tests/tests/harness/ops/saturating_neg.rs b/fearless_simd_tests/tests/harness/ops/saturating_neg.rs new file mode 100644 index 00000000..3f38b678 --- /dev/null +++ b/fearless_simd_tests/tests/harness/ops/saturating_neg.rs @@ -0,0 +1,362 @@ +// Copyright 2026 the Fearless_SIMD Authors +// SPDX-License-Identifier: Apache-2.0 OR MIT + +use fearless_simd::*; +use fearless_simd_dev_macros::simd_test; + +// Saturating negation: for unsigned integers always returns zero, +// for signed integers negates normally except MIN maps to MAX. + +#[simd_test] +fn saturating_neg_i8x16(simd: S) { + let data: [i8; 16] = [ + i8::MIN, + i8::MAX, + i8::MIN + 1, + 0, + 1, + -1, + 2, + -2, + 3, + -3, + 4, + -4, + 5, + -5, + 6, + -6, + ]; + let a = i8x16::from_slice(simd, &data); + assert_eq!(*a.saturating_neg(), data.map(i8::saturating_neg)); +} + +#[simd_test] +fn saturating_neg_i8x32(simd: S) { + let data: [i8; 32] = [ + i8::MIN, + i8::MAX, + i8::MIN + 1, + 0, + 1, + -1, + 2, + -2, + 3, + -3, + 4, + -4, + 5, + -5, + 6, + -6, + 7, + -7, + 8, + -8, + 9, + -9, + 10, + -10, + 11, + -11, + 12, + -12, + 13, + -13, + 14, + -14, + ]; + let a = i8x32::from_slice(simd, &data); + assert_eq!(*a.saturating_neg(), data.map(i8::saturating_neg)); +} + +#[simd_test] +fn saturating_neg_i8x64(simd: S) { + let data: [i8; 64] = [ + i8::MIN, + i8::MAX, + i8::MIN + 1, + 0, + 1, + -1, + 2, + -2, + 3, + -3, + 4, + -4, + 5, + -5, + 6, + -6, + 7, + -7, + 8, + -8, + 9, + -9, + 10, + -10, + 11, + -11, + 12, + -12, + 13, + -13, + 14, + -14, + i8::MIN, + i8::MAX, + i8::MIN + 1, + 0, + 1, + -1, + 2, + -2, + 3, + -3, + 4, + -4, + 5, + -5, + 6, + -6, + 7, + -7, + 8, + -8, + 9, + -9, + 10, + -10, + 11, + -11, + 12, + -12, + 13, + -13, + 14, + -14, + ]; + let a = i8x64::from_slice(simd, &data); + assert_eq!(*a.saturating_neg(), data.map(i8::saturating_neg)); +} + +#[simd_test] +fn saturating_neg_i16x8(simd: S) { + let data: [i16; 8] = [i16::MIN, i16::MAX, i16::MIN + 1, 0, 1, -1, 2, -2]; + let a = i16x8::from_slice(simd, &data); + assert_eq!(*a.saturating_neg(), data.map(i16::saturating_neg)); +} + +#[simd_test] +fn saturating_neg_i16x16(simd: S) { + let data: [i16; 16] = [ + i16::MIN, + i16::MAX, + i16::MIN + 1, + 0, + 1, + -1, + 2, + -2, + 3, + -3, + 4, + -4, + 5, + -5, + 6, + -6, + ]; + let a = i16x16::from_slice(simd, &data); + assert_eq!(*a.saturating_neg(), data.map(i16::saturating_neg)); +} + +#[simd_test] +fn saturating_neg_i16x32(simd: S) { + let data: [i16; 32] = [ + i16::MIN, + i16::MAX, + i16::MIN + 1, + 0, + 1, + -1, + 2, + -2, + 3, + -3, + 4, + -4, + 5, + -5, + 6, + -6, + 7, + -7, + 8, + -8, + 9, + -9, + 10, + -10, + 11, + -11, + 12, + -12, + 13, + -13, + 14, + -14, + ]; + let a = i16x32::from_slice(simd, &data); + assert_eq!(*a.saturating_neg(), data.map(i16::saturating_neg)); +} + +#[simd_test] +fn saturating_neg_i32x4(simd: S) { + let data: [i32; 4] = [i32::MIN, i32::MAX, i32::MIN + 1, 0]; + let a = i32x4::from_slice(simd, &data); + assert_eq!(*a.saturating_neg(), data.map(i32::saturating_neg)); +} + +#[simd_test] +fn saturating_neg_i32x8(simd: S) { + let data: [i32; 8] = [i32::MIN, i32::MAX, i32::MIN + 1, 0, 1, -1, 2, -2]; + let a = i32x8::from_slice(simd, &data); + assert_eq!(*a.saturating_neg(), data.map(i32::saturating_neg)); +} + +#[simd_test] +fn saturating_neg_i32x16(simd: S) { + let data: [i32; 16] = [ + i32::MIN, + i32::MAX, + i32::MIN + 1, + 0, + 1, + -1, + 2, + -2, + 3, + -3, + 4, + -4, + 5, + -5, + 6, + -6, + ]; + let a = i32x16::from_slice(simd, &data); + assert_eq!(*a.saturating_neg(), data.map(i32::saturating_neg)); +} + +#[simd_test] +fn saturating_neg_i64x2(simd: S) { + let data: [i64; 2] = [i64::MIN, i64::MAX]; + let a = i64x2::from_slice(simd, &data); + assert_eq!(*a.saturating_neg(), data.map(i64::saturating_neg)); + + let data: [i64; 2] = [i64::MIN + 1, 0]; + let a = i64x2::from_slice(simd, &data); + assert_eq!(*a.saturating_neg(), data.map(i64::saturating_neg)); +} + +#[simd_test] +fn saturating_neg_i64x4(simd: S) { + let data: [i64; 4] = [i64::MIN, i64::MAX, i64::MIN + 1, 0]; + let a = i64x4::from_slice(simd, &data); + assert_eq!(*a.saturating_neg(), data.map(i64::saturating_neg)); +} + +#[simd_test] +fn saturating_neg_i64x8(simd: S) { + let data: [i64; 8] = [i64::MIN, i64::MAX, i64::MIN + 1, 0, 1, -1, 2, -2]; + let a = i64x8::from_slice(simd, &data); + assert_eq!(*a.saturating_neg(), data.map(i64::saturating_neg)); +} + +#[simd_test] +fn saturating_neg_u8x16(simd: S) { + let data: [u8; 16] = std::array::from_fn(|i| i as _); + let a = u8x16::from_slice(simd, &data); + assert_eq!(*a.saturating_neg(), [0; 16]); +} + +#[simd_test] +fn saturating_neg_u8x32(simd: S) { + let data: [u8; 32] = std::array::from_fn(|i| i as _); + let a = u8x32::from_slice(simd, &data); + assert_eq!(*a.saturating_neg(), [0; 32]); +} + +#[simd_test] +fn saturating_neg_u8x64(simd: S) { + let data: [u8; 64] = std::array::from_fn(|i| i as _); + let a = u8x64::from_slice(simd, &data); + assert_eq!(*a.saturating_neg(), [0; 64]); +} + +#[simd_test] +fn saturating_neg_u16x8(simd: S) { + let data: [u16; 8] = std::array::from_fn(|i| i as _); + let a = u16x8::from_slice(simd, &data); + assert_eq!(*a.saturating_neg(), [0; 8]); +} + +#[simd_test] +fn saturating_neg_u16x16(simd: S) { + let data: [u16; 16] = std::array::from_fn(|i| i as _); + let a = u16x16::from_slice(simd, &data); + assert_eq!(*a.saturating_neg(), [0; 16]); +} + +#[simd_test] +fn saturating_neg_u16x32(simd: S) { + let data: [u16; 32] = std::array::from_fn(|i| i as _); + let a = u16x32::from_slice(simd, &data); + assert_eq!(*a.saturating_neg(), [0; 32]); +} + +#[simd_test] +fn saturating_neg_u32x4(simd: S) { + let data: [u32; 4] = std::array::from_fn(|i| i as _); + let a = u32x4::from_slice(simd, &data); + assert_eq!(*a.saturating_neg(), [0; 4]); +} + +#[simd_test] +fn saturating_neg_u32x8(simd: S) { + let data: [u32; 8] = std::array::from_fn(|i| i as _); + let a = u32x8::from_slice(simd, &data); + assert_eq!(*a.saturating_neg(), [0; 8]); +} + +#[simd_test] +fn saturating_neg_u32x16(simd: S) { + let data: [u32; 16] = std::array::from_fn(|i| i as _); + let a = u32x16::from_slice(simd, &data); + assert_eq!(*a.saturating_neg(), [0; 16]); +} + +#[simd_test] +fn saturating_neg_u64x2(simd: S) { + let data: [u64; 2] = std::array::from_fn(|i| i as _); + let a = u64x2::from_slice(simd, &data); + assert_eq!(*a.saturating_neg(), [0; 2]); +} + +#[simd_test] +fn saturating_neg_u64x4(simd: S) { + let data: [u64; 4] = std::array::from_fn(|i| i as _); + let a = u64x4::from_slice(simd, &data); + assert_eq!(*a.saturating_neg(), [0; 4]); +} + +#[simd_test] +fn saturating_neg_u64x8(simd: S) { + let data: [u64; 8] = std::array::from_fn(|i| i as _); + let a = u64x8::from_slice(simd, &data); + assert_eq!(*a.saturating_neg(), [0; 8]); +} From 88b93d76d6a79ce032f9ae75fa98e7bd0e35839d Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Sat, 28 Nov 2026 20:35:33 +0100 Subject: [PATCH 2/4] Add `SimdInt::saturating_abs` --- fearless_simd/src/generated/avx2.rs | 210 +++------- fearless_simd/src/generated/avx512.rs | 300 +++------------ fearless_simd/src/generated/fallback.rs | 136 ++++--- fearless_simd/src/generated/neon.rs | 92 +++-- fearless_simd/src/generated/simd_trait.rs | 180 +++++++-- fearless_simd/src/generated/simd_types.rs | 96 +++++ fearless_simd/src/generated/sse2.rs | 106 ++---- fearless_simd/src/generated/sse4_2.rs | 105 ++--- fearless_simd/src/generated/wasm.rs | 44 +-- fearless_simd_gen/src/arch/fallback.rs | 2 + fearless_simd_gen/src/level.rs | 3 +- fearless_simd_gen/src/mk_fallback.rs | 30 -- fearless_simd_gen/src/mk_neon.rs | 30 +- fearless_simd_gen/src/mk_simd_trait.rs | 14 +- fearless_simd_gen/src/mk_wasm.rs | 44 +-- fearless_simd_gen/src/mk_x86.rs | 123 +++--- fearless_simd_gen/src/ops.rs | 7 + fearless_simd_gen/src/types.rs | 4 + fearless_simd_tests/tests/harness/ops/mod.rs | 1 + .../tests/harness/ops/saturating_abs.rs | 359 ++++++++++++++++++ 20 files changed, 1027 insertions(+), 859 deletions(-) create mode 100644 fearless_simd_tests/tests/harness/ops/saturating_abs.rs diff --git a/fearless_simd/src/generated/avx2.rs b/fearless_simd/src/generated/avx2.rs index 5af8706b..ec2711f9 100644 --- a/fearless_simd/src/generated/avx2.rs +++ b/fearless_simd/src/generated/avx2.rs @@ -1022,15 +1022,11 @@ impl Simd for Avx2 { } #[inline(always)] fn saturating_neg_i8x16(self, a: i8x16) -> i8x16 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx2, a: i8x16) -> i8x16 { - let a = a.into(); - let bounded = _mm_max_epi8(a, _mm_set1_epi8(i8::MIN + 1)); - _mm_sub_epi8(_mm_setzero_si128(), bounded).simd_into(token) - } - ); - kernel(self, a) + self.neg_i8x16(self.max_i8x16(a, self.splat_i8x16(const { i8::MIN + 1 }))) + } + #[inline(always)] + fn saturating_abs_i8x16(self, a: i8x16) -> i8x16 { + self.abs_i8x16(self.max_i8x16(a, self.splat_i8x16(const { i8::MIN + 1 }))) } #[inline(always)] fn mul_i8x16(self, a: i8x16, b: i8x16) -> i8x16 { @@ -1847,17 +1843,6 @@ impl Simd for Avx2 { kernel(self, a, b) } #[inline(always)] - fn saturating_neg_u8x16(self, a: u8x16) -> u8x16 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx2, a: u8x16) -> u8x16 { - let _ = a; - _mm_setzero_si128().simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u8x16(self, a: u8x16, b: u8x16) -> u8x16 { crate::kernel!( #[inline(always)] @@ -2770,15 +2755,11 @@ impl Simd for Avx2 { } #[inline(always)] fn saturating_neg_i16x8(self, a: i16x8) -> i16x8 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx2, a: i16x8) -> i16x8 { - let a = a.into(); - let bounded = _mm_max_epi16(a, _mm_set1_epi16(i16::MIN + 1)); - _mm_sub_epi16(_mm_setzero_si128(), bounded).simd_into(token) - } - ); - kernel(self, a) + self.neg_i16x8(self.max_i16x8(a, self.splat_i16x8(const { i16::MIN + 1 }))) + } + #[inline(always)] + fn saturating_abs_i16x8(self, a: i16x8) -> i16x8 { + self.abs_i16x8(self.max_i16x8(a, self.splat_i16x8(const { i16::MIN + 1 }))) } #[inline(always)] fn mul_i16x8(self, a: i16x8, b: i16x8) -> i16x8 { @@ -3457,17 +3438,6 @@ impl Simd for Avx2 { kernel(self, a, b) } #[inline(always)] - fn saturating_neg_u16x8(self, a: u16x8) -> u16x8 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx2, a: u16x8) -> u16x8 { - let _ = a; - _mm_setzero_si128().simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u16x8(self, a: u16x8, b: u16x8) -> u16x8 { crate::kernel!( #[inline(always)] @@ -4419,15 +4389,11 @@ impl Simd for Avx2 { } #[inline(always)] fn saturating_neg_i32x4(self, a: i32x4) -> i32x4 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx2, a: i32x4) -> i32x4 { - let a = a.into(); - let bounded = _mm_max_epi32(a, _mm_set1_epi32(i32::MIN + 1)); - _mm_sub_epi32(_mm_setzero_si128(), bounded).simd_into(token) - } - ); - kernel(self, a) + self.neg_i32x4(self.max_i32x4(a, self.splat_i32x4(const { i32::MIN + 1 }))) + } + #[inline(always)] + fn saturating_abs_i32x4(self, a: i32x4) -> i32x4 { + self.abs_i32x4(self.max_i32x4(a, self.splat_i32x4(const { i32::MIN + 1 }))) } #[inline(always)] fn mul_i32x4(self, a: i32x4, b: i32x4) -> i32x4 { @@ -5073,17 +5039,6 @@ impl Simd for Avx2 { kernel(self, a, b) } #[inline(always)] - fn saturating_neg_u32x4(self, a: u32x4) -> u32x4 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx2, a: u32x4) -> u32x4 { - let _ = a; - _mm_setzero_si128().simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u32x4(self, a: u32x4, b: u32x4) -> u32x4 { crate::kernel!( #[inline(always)] @@ -6710,16 +6665,15 @@ impl Simd for Avx2 { } #[inline(always)] fn saturating_neg_i64x2(self, a: i64x2) -> i64x2 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx2, a: i64x2) -> i64x2 { - let a = a.into(); - let neg_a = _mm_sub_epi64(_mm_setzero_si128(), a); - let is_min = _mm_cmpeq_epi64(neg_a, _mm_set1_epi64x(i64::MIN)); - _mm_xor_si128(neg_a, is_min).simd_into(token) - } - ); - kernel(self, a) + let t = self.neg_i64x2(a); + let mask = self.simd_eq_i64x2(t, self.splat_i64x2(i64::MIN)); + self.xor_i64x2(t, self.to_vector_mask64x2(mask)) + } + #[inline(always)] + fn saturating_abs_i64x2(self, a: i64x2) -> i64x2 { + let t = self.abs_i64x2(a); + let mask = self.simd_eq_i64x2(t, self.splat_i64x2(i64::MIN)); + self.xor_i64x2(t, self.to_vector_mask64x2(mask)) } #[inline(always)] fn mul_i64x2(self, a: i64x2, b: i64x2) -> i64x2 { @@ -7325,17 +7279,6 @@ impl Simd for Avx2 { kernel(self, a, b) } #[inline(always)] - fn saturating_neg_u64x2(self, a: u64x2) -> u64x2 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx2, a: u64x2) -> u64x2 { - let _ = a; - _mm_setzero_si128().simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u64x2(self, a: u64x2, b: u64x2) -> u64x2 { crate::kernel!( #[inline(always)] @@ -8869,15 +8812,11 @@ impl Simd for Avx2 { } #[inline(always)] fn saturating_neg_i8x32(self, a: i8x32) -> i8x32 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx2, a: i8x32) -> i8x32 { - let a = a.into(); - let bounded = _mm256_max_epi8(a, _mm256_set1_epi8(i8::MIN + 1)); - _mm256_sub_epi8(_mm256_setzero_si256(), bounded).simd_into(token) - } - ); - kernel(self, a) + self.neg_i8x32(self.max_i8x32(a, self.splat_i8x32(const { i8::MIN + 1 }))) + } + #[inline(always)] + fn saturating_abs_i8x32(self, a: i8x32) -> i8x32 { + self.abs_i8x32(self.max_i8x32(a, self.splat_i8x32(const { i8::MIN + 1 }))) } #[inline(always)] fn mul_i8x32(self, a: i8x32, b: i8x32) -> i8x32 { @@ -9802,17 +9741,6 @@ impl Simd for Avx2 { kernel(self, a, b) } #[inline(always)] - fn saturating_neg_u8x32(self, a: u8x32) -> u8x32 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx2, a: u8x32) -> u8x32 { - let _ = a; - _mm256_setzero_si256().simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u8x32(self, a: u8x32, b: u8x32) -> u8x32 { crate::kernel!( #[inline(always)] @@ -10705,15 +10633,11 @@ impl Simd for Avx2 { } #[inline(always)] fn saturating_neg_i16x16(self, a: i16x16) -> i16x16 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx2, a: i16x16) -> i16x16 { - let a = a.into(); - let bounded = _mm256_max_epi16(a, _mm256_set1_epi16(i16::MIN + 1)); - _mm256_sub_epi16(_mm256_setzero_si256(), bounded).simd_into(token) - } - ); - kernel(self, a) + self.neg_i16x16(self.max_i16x16(a, self.splat_i16x16(const { i16::MIN + 1 }))) + } + #[inline(always)] + fn saturating_abs_i16x16(self, a: i16x16) -> i16x16 { + self.abs_i16x16(self.max_i16x16(a, self.splat_i16x16(const { i16::MIN + 1 }))) } #[inline(always)] fn mul_i16x16(self, a: i16x16, b: i16x16) -> i16x16 { @@ -11364,17 +11288,6 @@ impl Simd for Avx2 { kernel(self, a, b) } #[inline(always)] - fn saturating_neg_u16x16(self, a: u16x16) -> u16x16 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx2, a: u16x16) -> u16x16 { - let _ = a; - _mm256_setzero_si256().simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u16x16(self, a: u16x16, b: u16x16) -> u16x16 { crate::kernel!( #[inline(always)] @@ -12349,15 +12262,11 @@ impl Simd for Avx2 { } #[inline(always)] fn saturating_neg_i32x8(self, a: i32x8) -> i32x8 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx2, a: i32x8) -> i32x8 { - let a = a.into(); - let bounded = _mm256_max_epi32(a, _mm256_set1_epi32(i32::MIN + 1)); - _mm256_sub_epi32(_mm256_setzero_si256(), bounded).simd_into(token) - } - ); - kernel(self, a) + self.neg_i32x8(self.max_i32x8(a, self.splat_i32x8(const { i32::MIN + 1 }))) + } + #[inline(always)] + fn saturating_abs_i32x8(self, a: i32x8) -> i32x8 { + self.abs_i32x8(self.max_i32x8(a, self.splat_i32x8(const { i32::MIN + 1 }))) } #[inline(always)] fn mul_i32x8(self, a: i32x8, b: i32x8) -> i32x8 { @@ -13004,17 +12913,6 @@ impl Simd for Avx2 { kernel(self, a, b) } #[inline(always)] - fn saturating_neg_u32x8(self, a: u32x8) -> u32x8 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx2, a: u32x8) -> u32x8 { - let _ = a; - _mm256_setzero_si256().simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u32x8(self, a: u32x8, b: u32x8) -> u32x8 { crate::kernel!( #[inline(always)] @@ -14639,16 +14537,15 @@ impl Simd for Avx2 { } #[inline(always)] fn saturating_neg_i64x4(self, a: i64x4) -> i64x4 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx2, a: i64x4) -> i64x4 { - let a = a.into(); - let neg_a = _mm256_sub_epi64(_mm256_setzero_si256(), a); - let is_min = _mm256_cmpeq_epi64(neg_a, _mm256_set1_epi64x(i64::MIN)); - _mm256_xor_si256(neg_a, is_min).simd_into(token) - } - ); - kernel(self, a) + let t = self.neg_i64x4(a); + let mask = self.simd_eq_i64x4(t, self.splat_i64x4(i64::MIN)); + self.xor_i64x4(t, self.to_vector_mask64x4(mask)) + } + #[inline(always)] + fn saturating_abs_i64x4(self, a: i64x4) -> i64x4 { + let t = self.abs_i64x4(a); + let mask = self.simd_eq_i64x4(t, self.splat_i64x4(i64::MIN)); + self.xor_i64x4(t, self.to_vector_mask64x4(mask)) } #[inline(always)] fn mul_i64x4(self, a: i64x4, b: i64x4) -> i64x4 { @@ -15274,17 +15171,6 @@ impl Simd for Avx2 { kernel(self, a, b) } #[inline(always)] - fn saturating_neg_u64x4(self, a: u64x4) -> u64x4 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx2, a: u64x4) -> u64x4 { - let _ = a; - _mm256_setzero_si256().simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u64x4(self, a: u64x4, b: u64x4) -> u64x4 { crate::kernel!( #[inline(always)] diff --git a/fearless_simd/src/generated/avx512.rs b/fearless_simd/src/generated/avx512.rs index d8207813..8b52d0c9 100644 --- a/fearless_simd/src/generated/avx512.rs +++ b/fearless_simd/src/generated/avx512.rs @@ -1337,15 +1337,11 @@ impl Simd for Avx512 { } #[inline(always)] fn saturating_neg_i8x16(self, a: i8x16) -> i8x16 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx512, a: i8x16) -> i8x16 { - let a = a.into(); - let bounded = _mm_max_epi8(a, _mm_set1_epi8(i8::MIN + 1)); - _mm_sub_epi8(_mm_setzero_si128(), bounded).simd_into(token) - } - ); - kernel(self, a) + self.neg_i8x16(self.max_i8x16(a, self.splat_i8x16(const { i8::MIN + 1 }))) + } + #[inline(always)] + fn saturating_abs_i8x16(self, a: i8x16) -> i8x16 { + self.abs_i8x16(self.max_i8x16(a, self.splat_i8x16(const { i8::MIN + 1 }))) } #[inline(always)] fn mul_i8x16(self, a: i8x16, b: i8x16) -> i8x16 { @@ -2152,17 +2148,6 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] - fn saturating_neg_u8x16(self, a: u8x16) -> u8x16 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx512, a: u8x16) -> u8x16 { - let _ = a; - _mm_setzero_si128().simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u8x16(self, a: u8x16, b: u8x16) -> u8x16 { crate::kernel!( #[inline(always)] @@ -3041,15 +3026,11 @@ impl Simd for Avx512 { } #[inline(always)] fn saturating_neg_i16x8(self, a: i16x8) -> i16x8 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx512, a: i16x8) -> i16x8 { - let a = a.into(); - let bounded = _mm_max_epi16(a, _mm_set1_epi16(i16::MIN + 1)); - _mm_sub_epi16(_mm_setzero_si128(), bounded).simd_into(token) - } - ); - kernel(self, a) + self.neg_i16x8(self.max_i16x8(a, self.splat_i16x8(const { i16::MIN + 1 }))) + } + #[inline(always)] + fn saturating_abs_i16x8(self, a: i16x8) -> i16x8 { + self.abs_i16x8(self.max_i16x8(a, self.splat_i16x8(const { i16::MIN + 1 }))) } #[inline(always)] fn mul_i16x8(self, a: i16x8, b: i16x8) -> i16x8 { @@ -3743,17 +3724,6 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] - fn saturating_neg_u16x8(self, a: u16x8) -> u16x8 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx512, a: u16x8) -> u16x8 { - let _ = a; - _mm_setzero_si128().simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u16x8(self, a: u16x8, b: u16x8) -> u16x8 { crate::kernel!( #[inline(always)] @@ -4641,15 +4611,11 @@ impl Simd for Avx512 { } #[inline(always)] fn saturating_neg_i32x4(self, a: i32x4) -> i32x4 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx512, a: i32x4) -> i32x4 { - let a = a.into(); - let bounded = _mm_max_epi32(a, _mm_set1_epi32(i32::MIN + 1)); - _mm_sub_epi32(_mm_setzero_si128(), bounded).simd_into(token) - } - ); - kernel(self, a) + self.neg_i32x4(self.max_i32x4(a, self.splat_i32x4(const { i32::MIN + 1 }))) + } + #[inline(always)] + fn saturating_abs_i32x4(self, a: i32x4) -> i32x4 { + self.abs_i32x4(self.max_i32x4(a, self.splat_i32x4(const { i32::MIN + 1 }))) } #[inline(always)] fn mul_i32x4(self, a: i32x4, b: i32x4) -> i32x4 { @@ -5303,17 +5269,6 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] - fn saturating_neg_u32x4(self, a: u32x4) -> u32x4 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx512, a: u32x4) -> u32x4 { - let _ = a; - _mm_setzero_si128().simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u32x4(self, a: u32x4, b: u32x4) -> u32x4 { crate::kernel!( #[inline(always)] @@ -6926,15 +6881,11 @@ impl Simd for Avx512 { } #[inline(always)] fn saturating_neg_i64x2(self, a: i64x2) -> i64x2 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx512, a: i64x2) -> i64x2 { - let a = a.into(); - let bounded = _mm_max_epi64(a, _mm_set1_epi64x(i64::MIN + 1)); - _mm_sub_epi64(_mm_setzero_si128(), bounded).simd_into(token) - } - ); - kernel(self, a) + self.neg_i64x2(self.max_i64x2(a, self.splat_i64x2(const { i64::MIN + 1 }))) + } + #[inline(always)] + fn saturating_abs_i64x2(self, a: i64x2) -> i64x2 { + self.abs_i64x2(self.max_i64x2(a, self.splat_i64x2(const { i64::MIN + 1 }))) } #[inline(always)] fn mul_i64x2(self, a: i64x2, b: i64x2) -> i64x2 { @@ -7516,17 +7467,6 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] - fn saturating_neg_u64x2(self, a: u64x2) -> u64x2 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx512, a: u64x2) -> u64x2 { - let _ = a; - _mm_setzero_si128().simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u64x2(self, a: u64x2, b: u64x2) -> u64x2 { crate::kernel!( #[inline(always)] @@ -9066,15 +9006,11 @@ impl Simd for Avx512 { } #[inline(always)] fn saturating_neg_i8x32(self, a: i8x32) -> i8x32 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx512, a: i8x32) -> i8x32 { - let a = a.into(); - let bounded = _mm256_max_epi8(a, _mm256_set1_epi8(i8::MIN + 1)); - _mm256_sub_epi8(_mm256_setzero_si256(), bounded).simd_into(token) - } - ); - kernel(self, a) + self.neg_i8x32(self.max_i8x32(a, self.splat_i8x32(const { i8::MIN + 1 }))) + } + #[inline(always)] + fn saturating_abs_i8x32(self, a: i8x32) -> i8x32 { + self.abs_i8x32(self.max_i8x32(a, self.splat_i8x32(const { i8::MIN + 1 }))) } #[inline(always)] fn mul_i8x32(self, a: i8x32, b: i8x32) -> i8x32 { @@ -9859,17 +9795,6 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] - fn saturating_neg_u8x32(self, a: u8x32) -> u8x32 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx512, a: u8x32) -> u8x32 { - let _ = a; - _mm256_setzero_si256().simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u8x32(self, a: u8x32, b: u8x32) -> u8x32 { crate::kernel!( #[inline(always)] @@ -10749,15 +10674,11 @@ impl Simd for Avx512 { } #[inline(always)] fn saturating_neg_i16x16(self, a: i16x16) -> i16x16 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx512, a: i16x16) -> i16x16 { - let a = a.into(); - let bounded = _mm256_max_epi16(a, _mm256_set1_epi16(i16::MIN + 1)); - _mm256_sub_epi16(_mm256_setzero_si256(), bounded).simd_into(token) - } - ); - kernel(self, a) + self.neg_i16x16(self.max_i16x16(a, self.splat_i16x16(const { i16::MIN + 1 }))) + } + #[inline(always)] + fn saturating_abs_i16x16(self, a: i16x16) -> i16x16 { + self.abs_i16x16(self.max_i16x16(a, self.splat_i16x16(const { i16::MIN + 1 }))) } #[inline(always)] fn mul_i16x16(self, a: i16x16, b: i16x16) -> i16x16 { @@ -11444,17 +11365,6 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] - fn saturating_neg_u16x16(self, a: u16x16) -> u16x16 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx512, a: u16x16) -> u16x16 { - let _ = a; - _mm256_setzero_si256().simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u16x16(self, a: u16x16, b: u16x16) -> u16x16 { crate::kernel!( #[inline(always)] @@ -12348,15 +12258,11 @@ impl Simd for Avx512 { } #[inline(always)] fn saturating_neg_i32x8(self, a: i32x8) -> i32x8 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx512, a: i32x8) -> i32x8 { - let a = a.into(); - let bounded = _mm256_max_epi32(a, _mm256_set1_epi32(i32::MIN + 1)); - _mm256_sub_epi32(_mm256_setzero_si256(), bounded).simd_into(token) - } - ); - kernel(self, a) + self.neg_i32x8(self.max_i32x8(a, self.splat_i32x8(const { i32::MIN + 1 }))) + } + #[inline(always)] + fn saturating_abs_i32x8(self, a: i32x8) -> i32x8 { + self.abs_i32x8(self.max_i32x8(a, self.splat_i32x8(const { i32::MIN + 1 }))) } #[inline(always)] fn mul_i32x8(self, a: i32x8, b: i32x8) -> i32x8 { @@ -12996,17 +12902,6 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] - fn saturating_neg_u32x8(self, a: u32x8) -> u32x8 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx512, a: u32x8) -> u32x8 { - let _ = a; - _mm256_setzero_si256().simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u32x8(self, a: u32x8, b: u32x8) -> u32x8 { crate::kernel!( #[inline(always)] @@ -14585,15 +14480,11 @@ impl Simd for Avx512 { } #[inline(always)] fn saturating_neg_i64x4(self, a: i64x4) -> i64x4 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx512, a: i64x4) -> i64x4 { - let a = a.into(); - let bounded = _mm256_max_epi64(a, _mm256_set1_epi64x(i64::MIN + 1)); - _mm256_sub_epi64(_mm256_setzero_si256(), bounded).simd_into(token) - } - ); - kernel(self, a) + self.neg_i64x4(self.max_i64x4(a, self.splat_i64x4(const { i64::MIN + 1 }))) + } + #[inline(always)] + fn saturating_abs_i64x4(self, a: i64x4) -> i64x4 { + self.abs_i64x4(self.max_i64x4(a, self.splat_i64x4(const { i64::MIN + 1 }))) } #[inline(always)] fn mul_i64x4(self, a: i64x4, b: i64x4) -> i64x4 { @@ -15169,17 +15060,6 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] - fn saturating_neg_u64x4(self, a: u64x4) -> u64x4 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx512, a: u64x4) -> u64x4 { - let _ = a; - _mm256_setzero_si256().simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u64x4(self, a: u64x4, b: u64x4) -> u64x4 { crate::kernel!( #[inline(always)] @@ -16708,15 +16588,11 @@ impl Simd for Avx512 { } #[inline(always)] fn saturating_neg_i8x64(self, a: i8x64) -> i8x64 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx512, a: i8x64) -> i8x64 { - let a = a.into(); - let bounded = _mm512_max_epi8(a, _mm512_set1_epi8(i8::MIN + 1)); - _mm512_sub_epi8(_mm512_setzero_si512(), bounded).simd_into(token) - } - ); - kernel(self, a) + self.neg_i8x64(self.max_i8x64(a, self.splat_i8x64(const { i8::MIN + 1 }))) + } + #[inline(always)] + fn saturating_abs_i8x64(self, a: i8x64) -> i8x64 { + self.abs_i8x64(self.max_i8x64(a, self.splat_i8x64(const { i8::MIN + 1 }))) } #[inline(always)] fn mul_i8x64(self, a: i8x64, b: i8x64) -> i8x64 { @@ -17490,17 +17366,6 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] - fn saturating_neg_u8x64(self, a: u8x64) -> u8x64 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx512, a: u8x64) -> u8x64 { - let _ = a; - _mm512_setzero_si512().simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u8x64(self, a: u8x64, b: u8x64) -> u8x64 { crate::kernel!( #[inline(always)] @@ -18362,15 +18227,11 @@ impl Simd for Avx512 { } #[inline(always)] fn saturating_neg_i16x32(self, a: i16x32) -> i16x32 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx512, a: i16x32) -> i16x32 { - let a = a.into(); - let bounded = _mm512_max_epi16(a, _mm512_set1_epi16(i16::MIN + 1)); - _mm512_sub_epi16(_mm512_setzero_si512(), bounded).simd_into(token) - } - ); - kernel(self, a) + self.neg_i16x32(self.max_i16x32(a, self.splat_i16x32(const { i16::MIN + 1 }))) + } + #[inline(always)] + fn saturating_abs_i16x32(self, a: i16x32) -> i16x32 { + self.abs_i16x32(self.max_i16x32(a, self.splat_i16x32(const { i16::MIN + 1 }))) } #[inline(always)] fn mul_i16x32(self, a: i16x32, b: i16x32) -> i16x32 { @@ -19061,17 +18922,6 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] - fn saturating_neg_u16x32(self, a: u16x32) -> u16x32 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx512, a: u16x32) -> u16x32 { - let _ = a; - _mm512_setzero_si512().simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u16x32(self, a: u16x32, b: u16x32) -> u16x32 { crate::kernel!( #[inline(always)] @@ -19986,15 +19836,11 @@ impl Simd for Avx512 { } #[inline(always)] fn saturating_neg_i32x16(self, a: i32x16) -> i32x16 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx512, a: i32x16) -> i32x16 { - let a = a.into(); - let bounded = _mm512_max_epi32(a, _mm512_set1_epi32(i32::MIN + 1)); - _mm512_sub_epi32(_mm512_setzero_si512(), bounded).simd_into(token) - } - ); - kernel(self, a) + self.neg_i32x16(self.max_i32x16(a, self.splat_i32x16(const { i32::MIN + 1 }))) + } + #[inline(always)] + fn saturating_abs_i32x16(self, a: i32x16) -> i32x16 { + self.abs_i32x16(self.max_i32x16(a, self.splat_i32x16(const { i32::MIN + 1 }))) } #[inline(always)] fn mul_i32x16(self, a: i32x16, b: i32x16) -> i32x16 { @@ -20657,17 +20503,6 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] - fn saturating_neg_u32x16(self, a: u32x16) -> u32x16 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx512, a: u32x16) -> u32x16 { - let _ = a; - _mm512_setzero_si512().simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u32x16(self, a: u32x16, b: u32x16) -> u32x16 { crate::kernel!( #[inline(always)] @@ -22269,15 +22104,11 @@ impl Simd for Avx512 { } #[inline(always)] fn saturating_neg_i64x8(self, a: i64x8) -> i64x8 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx512, a: i64x8) -> i64x8 { - let a = a.into(); - let bounded = _mm512_max_epi64(a, _mm512_set1_epi64(i64::MIN + 1)); - _mm512_sub_epi64(_mm512_setzero_si512(), bounded).simd_into(token) - } - ); - kernel(self, a) + self.neg_i64x8(self.max_i64x8(a, self.splat_i64x8(const { i64::MIN + 1 }))) + } + #[inline(always)] + fn saturating_abs_i64x8(self, a: i64x8) -> i64x8 { + self.abs_i64x8(self.max_i64x8(a, self.splat_i64x8(const { i64::MIN + 1 }))) } #[inline(always)] fn mul_i64x8(self, a: i64x8, b: i64x8) -> i64x8 { @@ -22863,17 +22694,6 @@ impl Simd for Avx512 { kernel(self, a, b) } #[inline(always)] - fn saturating_neg_u64x8(self, a: u64x8) -> u64x8 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Avx512, a: u64x8) -> u64x8 { - let _ = a; - _mm512_setzero_si512().simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u64x8(self, a: u64x8, b: u64x8) -> u64x8 { crate::kernel!( #[inline(always)] diff --git a/fearless_simd/src/generated/fallback.rs b/fearless_simd/src/generated/fallback.rs index 02a538ab..c99fe71d 100644 --- a/fearless_simd/src/generated/fallback.rs +++ b/fearless_simd/src/generated/fallback.rs @@ -910,22 +910,44 @@ impl Simd for Fallback { #[inline(always)] fn saturating_neg_i8x16(self, a: i8x16) -> i8x16 { [ - a[0usize].saturating_neg(), - a[1usize].saturating_neg(), - a[2usize].saturating_neg(), - a[3usize].saturating_neg(), - a[4usize].saturating_neg(), - a[5usize].saturating_neg(), - a[6usize].saturating_neg(), - a[7usize].saturating_neg(), - a[8usize].saturating_neg(), - a[9usize].saturating_neg(), - a[10usize].saturating_neg(), - a[11usize].saturating_neg(), - a[12usize].saturating_neg(), - a[13usize].saturating_neg(), - a[14usize].saturating_neg(), - a[15usize].saturating_neg(), + i8::saturating_neg(a[0usize]), + i8::saturating_neg(a[1usize]), + i8::saturating_neg(a[2usize]), + i8::saturating_neg(a[3usize]), + i8::saturating_neg(a[4usize]), + i8::saturating_neg(a[5usize]), + i8::saturating_neg(a[6usize]), + i8::saturating_neg(a[7usize]), + i8::saturating_neg(a[8usize]), + i8::saturating_neg(a[9usize]), + i8::saturating_neg(a[10usize]), + i8::saturating_neg(a[11usize]), + i8::saturating_neg(a[12usize]), + i8::saturating_neg(a[13usize]), + i8::saturating_neg(a[14usize]), + i8::saturating_neg(a[15usize]), + ] + .simd_into(self) + } + #[inline(always)] + fn saturating_abs_i8x16(self, a: i8x16) -> i8x16 { + [ + i8::saturating_abs(a[0usize]), + i8::saturating_abs(a[1usize]), + i8::saturating_abs(a[2usize]), + i8::saturating_abs(a[3usize]), + i8::saturating_abs(a[4usize]), + i8::saturating_abs(a[5usize]), + i8::saturating_abs(a[6usize]), + i8::saturating_abs(a[7usize]), + i8::saturating_abs(a[8usize]), + i8::saturating_abs(a[9usize]), + i8::saturating_abs(a[10usize]), + i8::saturating_abs(a[11usize]), + i8::saturating_abs(a[12usize]), + i8::saturating_abs(a[13usize]), + i8::saturating_abs(a[14usize]), + i8::saturating_abs(a[15usize]), ] .simd_into(self) } @@ -2093,11 +2115,6 @@ impl Simd for Fallback { .simd_into(self) } #[inline(always)] - fn saturating_neg_u8x16(self, a: u8x16) -> u8x16 { - let _ = a; - [0; 16usize].simd_into(self) - } - #[inline(always)] fn mul_u8x16(self, a: u8x16, b: u8x16) -> u8x16 { [ u8::wrapping_mul(a[0usize], b[0usize]), @@ -3497,14 +3514,28 @@ impl Simd for Fallback { #[inline(always)] fn saturating_neg_i16x8(self, a: i16x8) -> i16x8 { [ - a[0usize].saturating_neg(), - a[1usize].saturating_neg(), - a[2usize].saturating_neg(), - a[3usize].saturating_neg(), - a[4usize].saturating_neg(), - a[5usize].saturating_neg(), - a[6usize].saturating_neg(), - a[7usize].saturating_neg(), + i16::saturating_neg(a[0usize]), + i16::saturating_neg(a[1usize]), + i16::saturating_neg(a[2usize]), + i16::saturating_neg(a[3usize]), + i16::saturating_neg(a[4usize]), + i16::saturating_neg(a[5usize]), + i16::saturating_neg(a[6usize]), + i16::saturating_neg(a[7usize]), + ] + .simd_into(self) + } + #[inline(always)] + fn saturating_abs_i16x8(self, a: i16x8) -> i16x8 { + [ + i16::saturating_abs(a[0usize]), + i16::saturating_abs(a[1usize]), + i16::saturating_abs(a[2usize]), + i16::saturating_abs(a[3usize]), + i16::saturating_abs(a[4usize]), + i16::saturating_abs(a[5usize]), + i16::saturating_abs(a[6usize]), + i16::saturating_abs(a[7usize]), ] .simd_into(self) } @@ -4253,11 +4284,6 @@ impl Simd for Fallback { .simd_into(self) } #[inline(always)] - fn saturating_neg_u16x8(self, a: u16x8) -> u16x8 { - let _ = a; - [0; 8usize].simd_into(self) - } - #[inline(always)] fn mul_u16x8(self, a: u16x8, b: u16x8) -> u16x8 { [ u16::wrapping_mul(a[0usize], b[0usize]), @@ -5278,10 +5304,20 @@ impl Simd for Fallback { #[inline(always)] fn saturating_neg_i32x4(self, a: i32x4) -> i32x4 { [ - a[0usize].saturating_neg(), - a[1usize].saturating_neg(), - a[2usize].saturating_neg(), - a[3usize].saturating_neg(), + i32::saturating_neg(a[0usize]), + i32::saturating_neg(a[1usize]), + i32::saturating_neg(a[2usize]), + i32::saturating_neg(a[3usize]), + ] + .simd_into(self) + } + #[inline(always)] + fn saturating_abs_i32x4(self, a: i32x4) -> i32x4 { + [ + i32::saturating_abs(a[0usize]), + i32::saturating_abs(a[1usize]), + i32::saturating_abs(a[2usize]), + i32::saturating_abs(a[3usize]), ] .simd_into(self) } @@ -5798,11 +5834,6 @@ impl Simd for Fallback { .simd_into(self) } #[inline(always)] - fn saturating_neg_u32x4(self, a: u32x4) -> u32x4 { - let _ = a; - [0; 4usize].simd_into(self) - } - #[inline(always)] fn mul_u32x4(self, a: u32x4, b: u32x4) -> u32x4 { [ u32::wrapping_mul(a[0usize], b[0usize]), @@ -6912,7 +6943,19 @@ impl Simd for Fallback { } #[inline(always)] fn saturating_neg_i64x2(self, a: i64x2) -> i64x2 { - [a[0usize].saturating_neg(), a[1usize].saturating_neg()].simd_into(self) + [ + i64::saturating_neg(a[0usize]), + i64::saturating_neg(a[1usize]), + ] + .simd_into(self) + } + #[inline(always)] + fn saturating_abs_i64x2(self, a: i64x2) -> i64x2 { + [ + i64::saturating_abs(a[0usize]), + i64::saturating_abs(a[1usize]), + ] + .simd_into(self) } #[inline(always)] fn mul_i64x2(self, a: i64x2, b: i64x2) -> i64x2 { @@ -7282,11 +7325,6 @@ impl Simd for Fallback { .simd_into(self) } #[inline(always)] - fn saturating_neg_u64x2(self, a: u64x2) -> u64x2 { - let _ = a; - [0; 2usize].simd_into(self) - } - #[inline(always)] fn mul_u64x2(self, a: u64x2, b: u64x2) -> u64x2 { [ u64::wrapping_mul(a[0usize], b[0usize]), diff --git a/fearless_simd/src/generated/neon.rs b/fearless_simd/src/generated/neon.rs index cc0d41ad..9026e7d1 100644 --- a/fearless_simd/src/generated/neon.rs +++ b/fearless_simd/src/generated/neon.rs @@ -902,7 +902,17 @@ impl Simd for Neon { crate::kernel!( #[inline(always)] fn kernel(token: Neon, a: i8x16) -> i8x16 { - vqnegvq_s8(a.into()).simd_into(token) + vqnegq_s8(a.into()).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] + fn saturating_abs_i8x16(self, a: i8x16) -> i8x16 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Neon, a: i8x16) -> i8x16 { + vqabsq_s8(a.into()).simd_into(token) } ); kernel(self, a) @@ -1561,17 +1571,6 @@ impl Simd for Neon { kernel(self, a, b) } #[inline(always)] - fn saturating_neg_u8x16(self, a: u8x16) -> u8x16 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Neon, a: u8x16) -> u8x16 { - let _ = a; - vdupq_n_u8(0).simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u8x16(self, a: u8x16, b: u8x16) -> u8x16 { crate::kernel!( #[inline(always)] @@ -2376,7 +2375,17 @@ impl Simd for Neon { crate::kernel!( #[inline(always)] fn kernel(token: Neon, a: i16x8) -> i16x8 { - vqnegvq_s16(a.into()).simd_into(token) + vqnegq_s16(a.into()).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] + fn saturating_abs_i16x8(self, a: i16x8) -> i16x8 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Neon, a: i16x8) -> i16x8 { + vqabsq_s16(a.into()).simd_into(token) } ); kernel(self, a) @@ -2969,17 +2978,6 @@ impl Simd for Neon { kernel(self, a, b) } #[inline(always)] - fn saturating_neg_u16x8(self, a: u16x8) -> u16x8 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Neon, a: u16x8) -> u16x8 { - let _ = a; - vdupq_n_u16(0).simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u16x8(self, a: u16x8, b: u16x8) -> u16x8 { crate::kernel!( #[inline(always)] @@ -3821,7 +3819,17 @@ impl Simd for Neon { crate::kernel!( #[inline(always)] fn kernel(token: Neon, a: i32x4) -> i32x4 { - vqnegvq_s32(a.into()).simd_into(token) + vqnegq_s32(a.into()).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] + fn saturating_abs_i32x4(self, a: i32x4) -> i32x4 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Neon, a: i32x4) -> i32x4 { + vqabsq_s32(a.into()).simd_into(token) } ); kernel(self, a) @@ -4423,17 +4431,6 @@ impl Simd for Neon { kernel(self, a, b) } #[inline(always)] - fn saturating_neg_u32x4(self, a: u32x4) -> u32x4 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Neon, a: u32x4) -> u32x4 { - let _ = a; - vdupq_n_u32(0).simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u32x4(self, a: u32x4, b: u32x4) -> u32x4 { crate::kernel!( #[inline(always)] @@ -5905,7 +5902,17 @@ impl Simd for Neon { crate::kernel!( #[inline(always)] fn kernel(token: Neon, a: i64x2) -> i64x2 { - vqnegvq_s64(a.into()).simd_into(token) + vqnegq_s64(a.into()).simd_into(token) + } + ); + kernel(self, a) + } + #[inline(always)] + fn saturating_abs_i64x2(self, a: i64x2) -> i64x2 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Neon, a: i64x2) -> i64x2 { + vqabsq_s64(a.into()).simd_into(token) } ); kernel(self, a) @@ -6426,17 +6433,6 @@ impl Simd for Neon { kernel(self, a, b) } #[inline(always)] - fn saturating_neg_u64x2(self, a: u64x2) -> u64x2 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Neon, a: u64x2) -> u64x2 { - let _ = a; - vdupq_n_u64(0).simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u64x2(self, a: u64x2, b: u64x2) -> u64x2 { [ u64::wrapping_mul(a[0usize], b[0usize]), diff --git a/fearless_simd/src/generated/simd_trait.rs b/fearless_simd/src/generated/simd_trait.rs index 60cd4188..869ab81b 100644 --- a/fearless_simd/src/generated/simd_trait.rs +++ b/fearless_simd/src/generated/simd_trait.rs @@ -559,6 +559,8 @@ pub trait Simd: fn saturating_sub_i8x16(self, a: i8x16, b: i8x16) -> i8x16; #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] fn saturating_neg_i8x16(self, a: i8x16) -> i8x16; + #[doc = "Compute the absolute value of each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return the same value. For signed integers, this returns the same as `self.abs()`, except when applied to the minimum value. For example, `saturating_abs(-128i8)` returns `127i8`."] + fn saturating_abs_i8x16(self, a: i8x16) -> i8x16; #[doc = "Multiply two vectors element-wise, wrapping on overflow."] fn mul_i8x16(self, a: i8x16, b: i8x16) -> i8x16; #[doc = "Compute the bitwise AND of two vectors."] @@ -725,7 +727,16 @@ pub trait Simd: #[doc = "Subtract two vectors element-wise, saturating on overflow.\n\n\"Saturating\" means that if the result is not representable, the closest representable value (either `Element::MAX` or `Element::MIN`) is returned.\n\nOn x86 it is implemented in hardware only for 8-bit and 16-bit elements. For 32-bit and 64-bit vectors this operation is slower than wrapping subtraction on x86."] fn saturating_sub_u8x16(self, a: u8x16, b: u8x16) -> u8x16; #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] - fn saturating_neg_u8x16(self, a: u8x16) -> u8x16; + #[inline(always)] + fn saturating_neg_u8x16(self, a: u8x16) -> u8x16 { + let _ = a; + self.splat_u8x16(0) + } + #[doc = "Compute the absolute value of each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return the same value. For signed integers, this returns the same as `self.abs()`, except when applied to the minimum value. For example, `saturating_abs(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_abs_u8x16(self, a: u8x16) -> u8x16 { + a + } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] fn mul_u8x16(self, a: u8x16, b: u8x16) -> u8x16; #[doc = "Compute the bitwise AND of two vectors."] @@ -969,6 +980,8 @@ pub trait Simd: fn saturating_sub_i16x8(self, a: i16x8, b: i16x8) -> i16x8; #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] fn saturating_neg_i16x8(self, a: i16x8) -> i16x8; + #[doc = "Compute the absolute value of each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return the same value. For signed integers, this returns the same as `self.abs()`, except when applied to the minimum value. For example, `saturating_abs(-128i8)` returns `127i8`."] + fn saturating_abs_i16x8(self, a: i16x8) -> i16x8; #[doc = "Multiply two vectors element-wise, wrapping on overflow."] fn mul_i16x8(self, a: i16x8, b: i16x8) -> i16x8; #[doc = "Compute the bitwise AND of two vectors."] @@ -1164,7 +1177,16 @@ pub trait Simd: #[doc = "Subtract two vectors element-wise, saturating on overflow.\n\n\"Saturating\" means that if the result is not representable, the closest representable value (either `Element::MAX` or `Element::MIN`) is returned.\n\nOn x86 it is implemented in hardware only for 8-bit and 16-bit elements. For 32-bit and 64-bit vectors this operation is slower than wrapping subtraction on x86."] fn saturating_sub_u16x8(self, a: u16x8, b: u16x8) -> u16x8; #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] - fn saturating_neg_u16x8(self, a: u16x8) -> u16x8; + #[inline(always)] + fn saturating_neg_u16x8(self, a: u16x8) -> u16x8 { + let _ = a; + self.splat_u16x8(0) + } + #[doc = "Compute the absolute value of each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return the same value. For signed integers, this returns the same as `self.abs()`, except when applied to the minimum value. For example, `saturating_abs(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_abs_u16x8(self, a: u16x8) -> u16x8 { + a + } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] fn mul_u16x8(self, a: u16x8, b: u16x8) -> u16x8; #[doc = "Compute the bitwise AND of two vectors."] @@ -1416,6 +1438,8 @@ pub trait Simd: fn saturating_sub_i32x4(self, a: i32x4, b: i32x4) -> i32x4; #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] fn saturating_neg_i32x4(self, a: i32x4) -> i32x4; + #[doc = "Compute the absolute value of each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return the same value. For signed integers, this returns the same as `self.abs()`, except when applied to the minimum value. For example, `saturating_abs(-128i8)` returns `127i8`."] + fn saturating_abs_i32x4(self, a: i32x4) -> i32x4; #[doc = "Multiply two vectors element-wise, wrapping on overflow."] fn mul_i32x4(self, a: i32x4, b: i32x4) -> i32x4; #[doc = "Compute the bitwise AND of two vectors."] @@ -1613,7 +1637,16 @@ pub trait Simd: #[doc = "Subtract two vectors element-wise, saturating on overflow.\n\n\"Saturating\" means that if the result is not representable, the closest representable value (either `Element::MAX` or `Element::MIN`) is returned.\n\nOn x86 it is implemented in hardware only for 8-bit and 16-bit elements. For 32-bit and 64-bit vectors this operation is slower than wrapping subtraction on x86."] fn saturating_sub_u32x4(self, a: u32x4, b: u32x4) -> u32x4; #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] - fn saturating_neg_u32x4(self, a: u32x4) -> u32x4; + #[inline(always)] + fn saturating_neg_u32x4(self, a: u32x4) -> u32x4 { + let _ = a; + self.splat_u32x4(0) + } + #[doc = "Compute the absolute value of each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return the same value. For signed integers, this returns the same as `self.abs()`, except when applied to the minimum value. For example, `saturating_abs(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_abs_u32x4(self, a: u32x4) -> u32x4 { + a + } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] fn mul_u32x4(self, a: u32x4, b: u32x4) -> u32x4; #[doc = "Compute the bitwise AND of two vectors."] @@ -2082,6 +2115,8 @@ pub trait Simd: fn saturating_sub_i64x2(self, a: i64x2, b: i64x2) -> i64x2; #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] fn saturating_neg_i64x2(self, a: i64x2) -> i64x2; + #[doc = "Compute the absolute value of each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return the same value. For signed integers, this returns the same as `self.abs()`, except when applied to the minimum value. For example, `saturating_abs(-128i8)` returns `127i8`."] + fn saturating_abs_i64x2(self, a: i64x2) -> i64x2; #[doc = "Multiply two vectors element-wise, wrapping on overflow."] fn mul_i64x2(self, a: i64x2, b: i64x2) -> i64x2; #[doc = "Compute the bitwise AND of two vectors."] @@ -2269,7 +2304,16 @@ pub trait Simd: #[doc = "Subtract two vectors element-wise, saturating on overflow.\n\n\"Saturating\" means that if the result is not representable, the closest representable value (either `Element::MAX` or `Element::MIN`) is returned.\n\nOn x86 it is implemented in hardware only for 8-bit and 16-bit elements. For 32-bit and 64-bit vectors this operation is slower than wrapping subtraction on x86."] fn saturating_sub_u64x2(self, a: u64x2, b: u64x2) -> u64x2; #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] - fn saturating_neg_u64x2(self, a: u64x2) -> u64x2; + #[inline(always)] + fn saturating_neg_u64x2(self, a: u64x2) -> u64x2 { + let _ = a; + self.splat_u64x2(0) + } + #[doc = "Compute the absolute value of each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return the same value. For signed integers, this returns the same as `self.abs()`, except when applied to the minimum value. For example, `saturating_abs(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_abs_u64x2(self, a: u64x2) -> u64x2 { + a + } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] fn mul_u64x2(self, a: u64x2, b: u64x2) -> u64x2; #[doc = "Compute the bitwise AND of two vectors."] @@ -3054,6 +3098,12 @@ pub trait Simd: let (a0, a1) = self.split_i8x32(a); self.combine_i8x16(self.saturating_neg_i8x16(a0), self.saturating_neg_i8x16(a1)) } + #[doc = "Compute the absolute value of each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return the same value. For signed integers, this returns the same as `self.abs()`, except when applied to the minimum value. For example, `saturating_abs(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_abs_i8x32(self, a: i8x32) -> i8x32 { + let (a0, a1) = self.split_i8x32(a); + self.combine_i8x16(self.saturating_abs_i8x16(a0), self.saturating_abs_i8x16(a1)) + } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] fn mul_i8x32(self, a: i8x32, b: i8x32) -> i8x32 { @@ -3461,8 +3511,13 @@ pub trait Simd: #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] #[inline(always)] fn saturating_neg_u8x32(self, a: u8x32) -> u8x32 { - let (a0, a1) = self.split_u8x32(a); - self.combine_u8x16(self.saturating_neg_u8x16(a0), self.saturating_neg_u8x16(a1)) + let _ = a; + self.splat_u8x32(0) + } + #[doc = "Compute the absolute value of each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return the same value. For signed integers, this returns the same as `self.abs()`, except when applied to the minimum value. For example, `saturating_abs(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_abs_u8x32(self, a: u8x32) -> u8x32 { + a } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] @@ -4025,6 +4080,12 @@ pub trait Simd: let (a0, a1) = self.split_i16x16(a); self.combine_i16x8(self.saturating_neg_i16x8(a0), self.saturating_neg_i16x8(a1)) } + #[doc = "Compute the absolute value of each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return the same value. For signed integers, this returns the same as `self.abs()`, except when applied to the minimum value. For example, `saturating_abs(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_abs_i16x16(self, a: i16x16) -> i16x16 { + let (a0, a1) = self.split_i16x16(a); + self.combine_i16x8(self.saturating_abs_i16x8(a0), self.saturating_abs_i16x8(a1)) + } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] fn mul_i16x16(self, a: i16x16, b: i16x16) -> i16x16 { @@ -4478,8 +4539,13 @@ pub trait Simd: #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] #[inline(always)] fn saturating_neg_u16x16(self, a: u16x16) -> u16x16 { - let (a0, a1) = self.split_u16x16(a); - self.combine_u16x8(self.saturating_neg_u16x8(a0), self.saturating_neg_u16x8(a1)) + let _ = a; + self.splat_u16x16(0) + } + #[doc = "Compute the absolute value of each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return the same value. For signed integers, this returns the same as `self.abs()`, except when applied to the minimum value. For example, `saturating_abs(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_abs_u16x16(self, a: u16x16) -> u16x16 { + a } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] @@ -5072,6 +5138,12 @@ pub trait Simd: let (a0, a1) = self.split_i32x8(a); self.combine_i32x4(self.saturating_neg_i32x4(a0), self.saturating_neg_i32x4(a1)) } + #[doc = "Compute the absolute value of each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return the same value. For signed integers, this returns the same as `self.abs()`, except when applied to the minimum value. For example, `saturating_abs(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_abs_i32x8(self, a: i32x8) -> i32x8 { + let (a0, a1) = self.split_i32x8(a); + self.combine_i32x4(self.saturating_abs_i32x4(a0), self.saturating_abs_i32x4(a1)) + } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] fn mul_i32x8(self, a: i32x8, b: i32x8) -> i32x8 { @@ -5527,8 +5599,13 @@ pub trait Simd: #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] #[inline(always)] fn saturating_neg_u32x8(self, a: u32x8) -> u32x8 { - let (a0, a1) = self.split_u32x8(a); - self.combine_u32x4(self.saturating_neg_u32x4(a0), self.saturating_neg_u32x4(a1)) + let _ = a; + self.splat_u32x8(0) + } + #[doc = "Compute the absolute value of each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return the same value. For signed integers, this returns the same as `self.abs()`, except when applied to the minimum value. For example, `saturating_abs(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_abs_u32x8(self, a: u32x8) -> u32x8 { + a } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] @@ -6626,6 +6703,12 @@ pub trait Simd: let (a0, a1) = self.split_i64x4(a); self.combine_i64x2(self.saturating_neg_i64x2(a0), self.saturating_neg_i64x2(a1)) } + #[doc = "Compute the absolute value of each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return the same value. For signed integers, this returns the same as `self.abs()`, except when applied to the minimum value. For example, `saturating_abs(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_abs_i64x4(self, a: i64x4) -> i64x4 { + let (a0, a1) = self.split_i64x4(a); + self.combine_i64x2(self.saturating_abs_i64x2(a0), self.saturating_abs_i64x2(a1)) + } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] fn mul_i64x4(self, a: i64x4, b: i64x4) -> i64x4 { @@ -7030,8 +7113,13 @@ pub trait Simd: #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] #[inline(always)] fn saturating_neg_u64x4(self, a: u64x4) -> u64x4 { - let (a0, a1) = self.split_u64x4(a); - self.combine_u64x2(self.saturating_neg_u64x2(a0), self.saturating_neg_u64x2(a1)) + let _ = a; + self.splat_u64x4(0) + } + #[doc = "Compute the absolute value of each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return the same value. For signed integers, this returns the same as `self.abs()`, except when applied to the minimum value. For example, `saturating_abs(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_abs_u64x4(self, a: u64x4) -> u64x4 { + a } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] @@ -8060,6 +8148,12 @@ pub trait Simd: let (a0, a1) = self.split_i8x64(a); self.combine_i8x32(self.saturating_neg_i8x32(a0), self.saturating_neg_i8x32(a1)) } + #[doc = "Compute the absolute value of each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return the same value. For signed integers, this returns the same as `self.abs()`, except when applied to the minimum value. For example, `saturating_abs(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_abs_i8x64(self, a: i8x64) -> i8x64 { + let (a0, a1) = self.split_i8x64(a); + self.combine_i8x32(self.saturating_abs_i8x32(a0), self.saturating_abs_i8x32(a1)) + } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] fn mul_i8x64(self, a: i8x64, b: i8x64) -> i8x64 { @@ -8465,8 +8559,13 @@ pub trait Simd: #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] #[inline(always)] fn saturating_neg_u8x64(self, a: u8x64) -> u8x64 { - let (a0, a1) = self.split_u8x64(a); - self.combine_u8x32(self.saturating_neg_u8x32(a0), self.saturating_neg_u8x32(a1)) + let _ = a; + self.splat_u8x64(0) + } + #[doc = "Compute the absolute value of each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return the same value. For signed integers, this returns the same as `self.abs()`, except when applied to the minimum value. For example, `saturating_abs(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_abs_u8x64(self, a: u8x64) -> u8x64 { + a } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] @@ -9028,6 +9127,15 @@ pub trait Simd: self.saturating_neg_i16x16(a1), ) } + #[doc = "Compute the absolute value of each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return the same value. For signed integers, this returns the same as `self.abs()`, except when applied to the minimum value. For example, `saturating_abs(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_abs_i16x32(self, a: i16x32) -> i16x32 { + let (a0, a1) = self.split_i16x32(a); + self.combine_i16x16( + self.saturating_abs_i16x16(a0), + self.saturating_abs_i16x16(a1), + ) + } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] fn mul_i16x32(self, a: i16x32, b: i16x32) -> i16x32 { @@ -9485,11 +9593,13 @@ pub trait Simd: #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] #[inline(always)] fn saturating_neg_u16x32(self, a: u16x32) -> u16x32 { - let (a0, a1) = self.split_u16x32(a); - self.combine_u16x16( - self.saturating_neg_u16x16(a0), - self.saturating_neg_u16x16(a1), - ) + let _ = a; + self.splat_u16x32(0) + } + #[doc = "Compute the absolute value of each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return the same value. For signed integers, this returns the same as `self.abs()`, except when applied to the minimum value. For example, `saturating_abs(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_abs_u16x32(self, a: u16x32) -> u16x32 { + a } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] @@ -10091,6 +10201,12 @@ pub trait Simd: let (a0, a1) = self.split_i32x16(a); self.combine_i32x8(self.saturating_neg_i32x8(a0), self.saturating_neg_i32x8(a1)) } + #[doc = "Compute the absolute value of each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return the same value. For signed integers, this returns the same as `self.abs()`, except when applied to the minimum value. For example, `saturating_abs(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_abs_i32x16(self, a: i32x16) -> i32x16 { + let (a0, a1) = self.split_i32x16(a); + self.combine_i32x8(self.saturating_abs_i32x8(a0), self.saturating_abs_i32x8(a1)) + } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] fn mul_i32x16(self, a: i32x16, b: i32x16) -> i32x16 { @@ -10548,8 +10664,13 @@ pub trait Simd: #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] #[inline(always)] fn saturating_neg_u32x16(self, a: u32x16) -> u32x16 { - let (a0, a1) = self.split_u32x16(a); - self.combine_u32x8(self.saturating_neg_u32x8(a0), self.saturating_neg_u32x8(a1)) + let _ = a; + self.splat_u32x16(0) + } + #[doc = "Compute the absolute value of each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return the same value. For signed integers, this returns the same as `self.abs()`, except when applied to the minimum value. For example, `saturating_abs(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_abs_u32x16(self, a: u32x16) -> u32x16 { + a } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] @@ -11641,6 +11762,12 @@ pub trait Simd: let (a0, a1) = self.split_i64x8(a); self.combine_i64x4(self.saturating_neg_i64x4(a0), self.saturating_neg_i64x4(a1)) } + #[doc = "Compute the absolute value of each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return the same value. For signed integers, this returns the same as `self.abs()`, except when applied to the minimum value. For example, `saturating_abs(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_abs_i64x8(self, a: i64x8) -> i64x8 { + let (a0, a1) = self.split_i64x8(a); + self.combine_i64x4(self.saturating_abs_i64x4(a0), self.saturating_abs_i64x4(a1)) + } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] fn mul_i64x8(self, a: i64x8, b: i64x8) -> i64x8 { @@ -12043,8 +12170,13 @@ pub trait Simd: #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] #[inline(always)] fn saturating_neg_u64x8(self, a: u64x8) -> u64x8 { - let (a0, a1) = self.split_u64x8(a); - self.combine_u64x4(self.saturating_neg_u64x4(a0), self.saturating_neg_u64x4(a1)) + let _ = a; + self.splat_u64x8(0) + } + #[doc = "Compute the absolute value of each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return the same value. For signed integers, this returns the same as `self.abs()`, except when applied to the minimum value. For example, `saturating_abs(-128i8)` returns `127i8`."] + #[inline(always)] + fn saturating_abs_u64x8(self, a: u64x8) -> u64x8 { + a } #[doc = "Multiply two vectors element-wise, wrapping on overflow."] #[inline(always)] @@ -13401,6 +13533,8 @@ pub trait SimdInt: fn saturating_sub(self, rhs: impl SimdInto) -> Self; #[doc = "Negate each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`."] fn saturating_neg(self) -> Self; + #[doc = "Compute the absolute value of each element of the vector, saturating on overflow.\n\nFor unsigned integers, this will always return the same value. For signed integers, this returns the same as `self.abs()`, except when applied to the minimum value. For example, `saturating_abs(-128i8)` returns `127i8`."] + fn saturating_abs(self) -> Self; } #[doc = r" Functionality implemented by SIMD masks."] #[doc = r""] diff --git a/fearless_simd/src/generated/simd_types.rs b/fearless_simd/src/generated/simd_types.rs index f3e7c498..7b6703cc 100644 --- a/fearless_simd/src/generated/simd_types.rs +++ b/fearless_simd/src/generated/simd_types.rs @@ -784,6 +784,10 @@ impl crate::SimdInt for i8x16 { fn saturating_neg(self) -> Self { self.simd.saturating_neg_i8x16(self) } + #[inline(always)] + fn saturating_abs(self) -> Self { + self.simd.saturating_abs_i8x16(self) + } } impl SimdWiden for i8x16 { type Widened = i16x8; @@ -1177,6 +1181,10 @@ impl crate::SimdInt for u8x16 { fn saturating_neg(self) -> Self { self.simd.saturating_neg_u8x16(self) } + #[inline(always)] + fn saturating_abs(self) -> Self { + self.simd.saturating_abs_u8x16(self) + } } impl SimdWiden for u8x16 { type Widened = u16x8; @@ -1705,6 +1713,10 @@ impl crate::SimdInt for i16x8 { fn saturating_neg(self) -> Self { self.simd.saturating_neg_i16x8(self) } + #[inline(always)] + fn saturating_abs(self) -> Self { + self.simd.saturating_abs_i16x8(self) + } } impl SimdWiden for i16x8 { type Widened = i32x4; @@ -2105,6 +2117,10 @@ impl crate::SimdInt for u16x8 { fn saturating_neg(self) -> Self { self.simd.saturating_neg_u16x8(self) } + #[inline(always)] + fn saturating_abs(self) -> Self { + self.simd.saturating_abs_u16x8(self) + } } impl SimdWiden for u16x8 { type Widened = u32x4; @@ -2643,6 +2659,10 @@ impl crate::SimdInt for i32x4 { fn saturating_neg(self) -> Self { self.simd.saturating_neg_i32x4(self) } + #[inline(always)] + fn saturating_abs(self) -> Self { + self.simd.saturating_abs_i32x4(self) + } } impl SimdCvtTruncate> for i32x4 { #[doc = "Convert each floating-point element to a signed 32-bit integer, truncating towards zero.\n\nOut-of-range values or NaN will produce implementation-defined results."] @@ -3043,6 +3063,10 @@ impl crate::SimdInt for u32x4 { fn saturating_neg(self) -> Self { self.simd.saturating_neg_u32x4(self) } + #[inline(always)] + fn saturating_abs(self) -> Self { + self.simd.saturating_abs_u32x4(self) + } } impl SimdCvtTruncate> for u32x4 { #[doc = "Convert each floating-point element to an unsigned 32-bit integer, truncating towards zero.\n\nOut-of-range values or NaN will produce implementation-defined results.\n\nOn x86 platforms below AVX-512, this operation will still be slower than converting to `i32`, because there is no native instruction for converting to `u32`.\nIf you know your values fit within range of an `i32`, you should convert to an `i32` and cast to your desired datatype afterwards."] @@ -4023,6 +4047,10 @@ impl crate::SimdInt for i64x2 { fn saturating_neg(self) -> Self { self.simd.saturating_neg_i64x2(self) } + #[inline(always)] + fn saturating_abs(self) -> Self { + self.simd.saturating_abs_i64x2(self) + } } impl SimdCvtTruncate> for i64x2 { #[doc = "Convert each floating-point element to a signed 64-bit integer, truncating towards zero.\n\nOut-of-range values or NaN will produce implementation-defined results."] @@ -4397,6 +4425,10 @@ impl crate::SimdInt for u64x2 { fn saturating_neg(self) -> Self { self.simd.saturating_neg_u64x2(self) } + #[inline(always)] + fn saturating_abs(self) -> Self { + self.simd.saturating_abs_u64x2(self) + } } impl SimdCvtTruncate> for u64x2 { #[doc = "Convert each floating-point element to an unsigned 64-bit integer, truncating towards zero.\n\nOut-of-range values or NaN will produce implementation-defined results."] @@ -5372,6 +5404,10 @@ impl crate::SimdInt for i8x32 { fn saturating_neg(self) -> Self { self.simd.saturating_neg_i8x32(self) } + #[inline(always)] + fn saturating_abs(self) -> Self { + self.simd.saturating_abs_i8x32(self) + } } impl SimdWiden for i8x32 { type Widened = i16x16; @@ -5776,6 +5812,10 @@ impl crate::SimdInt for u8x32 { fn saturating_neg(self) -> Self { self.simd.saturating_neg_u8x32(self) } + #[inline(always)] + fn saturating_abs(self) -> Self { + self.simd.saturating_abs_u8x32(self) + } } impl SimdWiden for u8x32 { type Widened = u16x16; @@ -6301,6 +6341,10 @@ impl crate::SimdInt for i16x16 { fn saturating_neg(self) -> Self { self.simd.saturating_neg_i16x16(self) } + #[inline(always)] + fn saturating_abs(self) -> Self { + self.simd.saturating_abs_i16x16(self) + } } impl SimdWiden for i16x16 { type Widened = i32x8; @@ -6707,6 +6751,10 @@ impl crate::SimdInt for u16x16 { fn saturating_neg(self) -> Self { self.simd.saturating_neg_u16x16(self) } + #[inline(always)] + fn saturating_abs(self) -> Self { + self.simd.saturating_abs_u16x16(self) + } } impl SimdWiden for u16x16 { type Widened = u32x8; @@ -7254,6 +7302,10 @@ impl crate::SimdInt for i32x8 { fn saturating_neg(self) -> Self { self.simd.saturating_neg_i32x8(self) } + #[inline(always)] + fn saturating_abs(self) -> Self { + self.simd.saturating_abs_i32x8(self) + } } impl SimdCvtTruncate> for i32x8 { #[doc = "Convert each floating-point element to a signed 32-bit integer, truncating towards zero.\n\nOut-of-range values or NaN will produce implementation-defined results."] @@ -7661,6 +7713,10 @@ impl crate::SimdInt for u32x8 { fn saturating_neg(self) -> Self { self.simd.saturating_neg_u32x8(self) } + #[inline(always)] + fn saturating_abs(self) -> Self { + self.simd.saturating_abs_u32x8(self) + } } impl SimdCvtTruncate> for u32x8 { #[doc = "Convert each floating-point element to an unsigned 32-bit integer, truncating towards zero.\n\nOut-of-range values or NaN will produce implementation-defined results.\n\nOn x86 platforms below AVX-512, this operation will still be slower than converting to `i32`, because there is no native instruction for converting to `u32`.\nIf you know your values fit within range of an `i32`, you should convert to an `i32` and cast to your desired datatype afterwards."] @@ -8638,6 +8694,10 @@ impl crate::SimdInt for i64x4 { fn saturating_neg(self) -> Self { self.simd.saturating_neg_i64x4(self) } + #[inline(always)] + fn saturating_abs(self) -> Self { + self.simd.saturating_abs_i64x4(self) + } } impl SimdCvtTruncate> for i64x4 { #[doc = "Convert each floating-point element to a signed 64-bit integer, truncating towards zero.\n\nOut-of-range values or NaN will produce implementation-defined results."] @@ -9007,6 +9067,10 @@ impl crate::SimdInt for u64x4 { fn saturating_neg(self) -> Self { self.simd.saturating_neg_u64x4(self) } + #[inline(always)] + fn saturating_abs(self) -> Self { + self.simd.saturating_abs_u64x4(self) + } } impl SimdCvtTruncate> for u64x4 { #[doc = "Convert each floating-point element to an unsigned 64-bit integer, truncating towards zero.\n\nOut-of-range values or NaN will produce implementation-defined results."] @@ -10020,6 +10084,10 @@ impl crate::SimdInt for i8x64 { fn saturating_neg(self) -> Self { self.simd.saturating_neg_i8x64(self) } + #[inline(always)] + fn saturating_abs(self) -> Self { + self.simd.saturating_abs_i8x64(self) + } } impl SimdWiden for i8x64 { type Widened = i16x32; @@ -10450,6 +10518,10 @@ impl crate::SimdInt for u8x64 { fn saturating_neg(self) -> Self { self.simd.saturating_neg_u8x64(self) } + #[inline(always)] + fn saturating_abs(self) -> Self { + self.simd.saturating_abs_u8x64(self) + } } impl SimdWiden for u8x64 { type Widened = u16x32; @@ -10978,6 +11050,10 @@ impl crate::SimdInt for i16x32 { fn saturating_neg(self) -> Self { self.simd.saturating_neg_i16x32(self) } + #[inline(always)] + fn saturating_abs(self) -> Self { + self.simd.saturating_abs_i16x32(self) + } } impl SimdWiden for i16x32 { type Widened = i32x16; @@ -11394,6 +11470,10 @@ impl crate::SimdInt for u16x32 { fn saturating_neg(self) -> Self { self.simd.saturating_neg_u16x32(self) } + #[inline(always)] + fn saturating_abs(self) -> Self { + self.simd.saturating_abs_u16x32(self) + } } impl SimdWiden for u16x32 { type Widened = u32x16; @@ -11937,6 +12017,10 @@ impl crate::SimdInt for i32x16 { fn saturating_neg(self) -> Self { self.simd.saturating_neg_i32x16(self) } + #[inline(always)] + fn saturating_abs(self) -> Self { + self.simd.saturating_abs_i32x16(self) + } } impl SimdCvtTruncate> for i32x16 { #[doc = "Convert each floating-point element to a signed 32-bit integer, truncating towards zero.\n\nOut-of-range values or NaN will produce implementation-defined results."] @@ -12349,6 +12433,10 @@ impl crate::SimdInt for u32x16 { fn saturating_neg(self) -> Self { self.simd.saturating_neg_u32x16(self) } + #[inline(always)] + fn saturating_abs(self) -> Self { + self.simd.saturating_abs_u32x16(self) + } } impl SimdCvtTruncate> for u32x16 { #[doc = "Convert each floating-point element to an unsigned 32-bit integer, truncating towards zero.\n\nOut-of-range values or NaN will produce implementation-defined results.\n\nOn x86 platforms below AVX-512, this operation will still be slower than converting to `i32`, because there is no native instruction for converting to `u32`.\nIf you know your values fit within range of an `i32`, you should convert to an `i32` and cast to your desired datatype afterwards."] @@ -13340,6 +13428,10 @@ impl crate::SimdInt for i64x8 { fn saturating_neg(self) -> Self { self.simd.saturating_neg_i64x8(self) } + #[inline(always)] + fn saturating_abs(self) -> Self { + self.simd.saturating_abs_i64x8(self) + } } impl SimdCvtTruncate> for i64x8 { #[doc = "Convert each floating-point element to a signed 64-bit integer, truncating towards zero.\n\nOut-of-range values or NaN will produce implementation-defined results."] @@ -13715,6 +13807,10 @@ impl crate::SimdInt for u64x8 { fn saturating_neg(self) -> Self { self.simd.saturating_neg_u64x8(self) } + #[inline(always)] + fn saturating_abs(self) -> Self { + self.simd.saturating_abs_u64x8(self) + } } impl SimdCvtTruncate> for u64x8 { #[doc = "Convert each floating-point element to an unsigned 64-bit integer, truncating towards zero.\n\nOut-of-range values or NaN will produce implementation-defined results."] diff --git a/fearless_simd/src/generated/sse2.rs b/fearless_simd/src/generated/sse2.rs index 5ee8a6f6..a7ee5e80 100644 --- a/fearless_simd/src/generated/sse2.rs +++ b/fearless_simd/src/generated/sse2.rs @@ -1077,16 +1077,15 @@ impl Simd for Sse2 { } #[inline(always)] fn saturating_neg_i8x16(self, a: i8x16) -> i8x16 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Sse2, a: i8x16) -> i8x16 { - let a = a.into(); - let neg_a = _mm_sub_epi8(_mm_setzero_si128(), a); - let is_min = _mm_cmpeq_epi8(neg_a, _mm_set1_epi8(i8::MIN)); - _mm_xor_si128(neg_a, is_min).simd_into(token) - } - ); - kernel(self, a) + let t = self.neg_i8x16(a); + let mask = self.simd_eq_i8x16(t, self.splat_i8x16(i8::MIN)); + self.xor_i8x16(t, self.to_vector_mask8x16(mask)) + } + #[inline(always)] + fn saturating_abs_i8x16(self, a: i8x16) -> i8x16 { + let t = self.abs_i8x16(a); + let mask = self.simd_eq_i8x16(t, self.splat_i8x16(i8::MIN)); + self.xor_i8x16(t, self.to_vector_mask8x16(mask)) } #[inline(always)] fn mul_i8x16(self, a: i8x16, b: i8x16) -> i8x16 { @@ -2043,17 +2042,6 @@ impl Simd for Sse2 { kernel(self, a, b) } #[inline(always)] - fn saturating_neg_u8x16(self, a: u8x16) -> u8x16 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Sse2, a: u8x16) -> u8x16 { - let _ = a; - _mm_setzero_si128().simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u8x16(self, a: u8x16, b: u8x16) -> u8x16 { crate::kernel!( #[inline(always)] @@ -3040,15 +3028,11 @@ impl Simd for Sse2 { } #[inline(always)] fn saturating_neg_i16x8(self, a: i16x8) -> i16x8 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Sse2, a: i16x8) -> i16x8 { - let a = a.into(); - let bounded = _mm_max_epi16(a, _mm_set1_epi16(i16::MIN + 1)); - _mm_sub_epi16(_mm_setzero_si128(), bounded).simd_into(token) - } - ); - kernel(self, a) + self.neg_i16x8(self.max_i16x8(a, self.splat_i16x8(const { i16::MIN + 1 }))) + } + #[inline(always)] + fn saturating_abs_i16x8(self, a: i16x8) -> i16x8 { + self.abs_i16x8(self.max_i16x8(a, self.splat_i16x8(const { i16::MIN + 1 }))) } #[inline(always)] fn mul_i16x8(self, a: i16x8, b: i16x8) -> i16x8 { @@ -3712,17 +3696,6 @@ impl Simd for Sse2 { kernel(self, a, b) } #[inline(always)] - fn saturating_neg_u16x8(self, a: u16x8) -> u16x8 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Sse2, a: u16x8) -> u16x8 { - let _ = a; - _mm_setzero_si128().simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u16x8(self, a: u16x8, b: u16x8) -> u16x8 { crate::kernel!( #[inline(always)] @@ -4733,16 +4706,15 @@ impl Simd for Sse2 { } #[inline(always)] fn saturating_neg_i32x4(self, a: i32x4) -> i32x4 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Sse2, a: i32x4) -> i32x4 { - let a = a.into(); - let neg_a = _mm_sub_epi32(_mm_setzero_si128(), a); - let is_min = _mm_cmpeq_epi32(neg_a, _mm_set1_epi32(i32::MIN)); - _mm_xor_si128(neg_a, is_min).simd_into(token) - } - ); - kernel(self, a) + let t = self.neg_i32x4(a); + let mask = self.simd_eq_i32x4(t, self.splat_i32x4(i32::MIN)); + self.xor_i32x4(t, self.to_vector_mask32x4(mask)) + } + #[inline(always)] + fn saturating_abs_i32x4(self, a: i32x4) -> i32x4 { + let t = self.abs_i32x4(a); + let mask = self.simd_eq_i32x4(t, self.splat_i32x4(i32::MIN)); + self.xor_i32x4(t, self.to_vector_mask32x4(mask)) } #[inline(always)] fn mul_i32x4(self, a: i32x4, b: i32x4) -> i32x4 { @@ -5416,17 +5388,6 @@ impl Simd for Sse2 { .simd_into(self) } #[inline(always)] - fn saturating_neg_u32x4(self, a: u32x4) -> u32x4 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Sse2, a: u32x4) -> u32x4 { - let _ = a; - _mm_setzero_si128().simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u32x4(self, a: u32x4, b: u32x4) -> u32x4 { [ u32::wrapping_mul(a[0usize], b[0usize]), @@ -7022,7 +6983,15 @@ impl Simd for Sse2 { } #[inline(always)] fn saturating_neg_i64x2(self, a: i64x2) -> i64x2 { - [a[0usize].saturating_neg(), a[1usize].saturating_neg()].simd_into(self) + let t = self.neg_i64x2(a); + let mask = self.simd_eq_i64x2(t, self.splat_i64x2(i64::MIN)); + self.xor_i64x2(t, self.to_vector_mask64x2(mask)) + } + #[inline(always)] + fn saturating_abs_i64x2(self, a: i64x2) -> i64x2 { + let t = self.abs_i64x2(a); + let mask = self.simd_eq_i64x2(t, self.splat_i64x2(i64::MIN)); + self.xor_i64x2(t, self.to_vector_mask64x2(mask)) } #[inline(always)] fn mul_i64x2(self, a: i64x2, b: i64x2) -> i64x2 { @@ -7544,17 +7513,6 @@ impl Simd for Sse2 { .simd_into(self) } #[inline(always)] - fn saturating_neg_u64x2(self, a: u64x2) -> u64x2 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Sse2, a: u64x2) -> u64x2 { - let _ = a; - _mm_setzero_si128().simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u64x2(self, a: u64x2, b: u64x2) -> u64x2 { crate::kernel!( #[inline(always)] diff --git a/fearless_simd/src/generated/sse4_2.rs b/fearless_simd/src/generated/sse4_2.rs index 1381d14f..7afa7c28 100644 --- a/fearless_simd/src/generated/sse4_2.rs +++ b/fearless_simd/src/generated/sse4_2.rs @@ -1016,15 +1016,11 @@ impl Simd for Sse4_2 { } #[inline(always)] fn saturating_neg_i8x16(self, a: i8x16) -> i8x16 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Sse4_2, a: i8x16) -> i8x16 { - let a = a.into(); - let bounded = _mm_max_epi8(a, _mm_set1_epi8(i8::MIN + 1)); - _mm_sub_epi8(_mm_setzero_si128(), bounded).simd_into(token) - } - ); - kernel(self, a) + self.neg_i8x16(self.max_i8x16(a, self.splat_i8x16(const { i8::MIN + 1 }))) + } + #[inline(always)] + fn saturating_abs_i8x16(self, a: i8x16) -> i8x16 { + self.abs_i8x16(self.max_i8x16(a, self.splat_i8x16(const { i8::MIN + 1 }))) } #[inline(always)] fn mul_i8x16(self, a: i8x16, b: i8x16) -> i8x16 { @@ -1841,17 +1837,6 @@ impl Simd for Sse4_2 { kernel(self, a, b) } #[inline(always)] - fn saturating_neg_u8x16(self, a: u8x16) -> u8x16 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Sse4_2, a: u8x16) -> u8x16 { - let _ = a; - _mm_setzero_si128().simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u8x16(self, a: u8x16, b: u8x16) -> u8x16 { crate::kernel!( #[inline(always)] @@ -2753,15 +2738,11 @@ impl Simd for Sse4_2 { } #[inline(always)] fn saturating_neg_i16x8(self, a: i16x8) -> i16x8 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Sse4_2, a: i16x8) -> i16x8 { - let a = a.into(); - let bounded = _mm_max_epi16(a, _mm_set1_epi16(i16::MIN + 1)); - _mm_sub_epi16(_mm_setzero_si128(), bounded).simd_into(token) - } - ); - kernel(self, a) + self.neg_i16x8(self.max_i16x8(a, self.splat_i16x8(const { i16::MIN + 1 }))) + } + #[inline(always)] + fn saturating_abs_i16x8(self, a: i16x8) -> i16x8 { + self.abs_i16x8(self.max_i16x8(a, self.splat_i16x8(const { i16::MIN + 1 }))) } #[inline(always)] fn mul_i16x8(self, a: i16x8, b: i16x8) -> i16x8 { @@ -3418,17 +3399,6 @@ impl Simd for Sse4_2 { kernel(self, a, b) } #[inline(always)] - fn saturating_neg_u16x8(self, a: u16x8) -> u16x8 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Sse4_2, a: u16x8) -> u16x8 { - let _ = a; - _mm_setzero_si128().simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u16x8(self, a: u16x8, b: u16x8) -> u16x8 { crate::kernel!( #[inline(always)] @@ -4382,15 +4352,11 @@ impl Simd for Sse4_2 { } #[inline(always)] fn saturating_neg_i32x4(self, a: i32x4) -> i32x4 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Sse4_2, a: i32x4) -> i32x4 { - let a = a.into(); - let bounded = _mm_max_epi32(a, _mm_set1_epi32(i32::MIN + 1)); - _mm_sub_epi32(_mm_setzero_si128(), bounded).simd_into(token) - } - ); - kernel(self, a) + self.neg_i32x4(self.max_i32x4(a, self.splat_i32x4(const { i32::MIN + 1 }))) + } + #[inline(always)] + fn saturating_abs_i32x4(self, a: i32x4) -> i32x4 { + self.abs_i32x4(self.max_i32x4(a, self.splat_i32x4(const { i32::MIN + 1 }))) } #[inline(always)] fn mul_i32x4(self, a: i32x4, b: i32x4) -> i32x4 { @@ -5037,17 +5003,6 @@ impl Simd for Sse4_2 { kernel(self, a, b) } #[inline(always)] - fn saturating_neg_u32x4(self, a: u32x4) -> u32x4 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Sse4_2, a: u32x4) -> u32x4 { - let _ = a; - _mm_setzero_si128().simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u32x4(self, a: u32x4, b: u32x4) -> u32x4 { crate::kernel!( #[inline(always)] @@ -6600,16 +6555,15 @@ impl Simd for Sse4_2 { } #[inline(always)] fn saturating_neg_i64x2(self, a: i64x2) -> i64x2 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Sse4_2, a: i64x2) -> i64x2 { - let a = a.into(); - let neg_a = _mm_sub_epi64(_mm_setzero_si128(), a); - let is_min = _mm_cmpeq_epi64(neg_a, _mm_set1_epi64x(i64::MIN)); - _mm_xor_si128(neg_a, is_min).simd_into(token) - } - ); - kernel(self, a) + let t = self.neg_i64x2(a); + let mask = self.simd_eq_i64x2(t, self.splat_i64x2(i64::MIN)); + self.xor_i64x2(t, self.to_vector_mask64x2(mask)) + } + #[inline(always)] + fn saturating_abs_i64x2(self, a: i64x2) -> i64x2 { + let t = self.abs_i64x2(a); + let mask = self.simd_eq_i64x2(t, self.splat_i64x2(i64::MIN)); + self.xor_i64x2(t, self.to_vector_mask64x2(mask)) } #[inline(always)] fn mul_i64x2(self, a: i64x2, b: i64x2) -> i64x2 { @@ -7213,17 +7167,6 @@ impl Simd for Sse4_2 { kernel(self, a, b) } #[inline(always)] - fn saturating_neg_u64x2(self, a: u64x2) -> u64x2 { - crate::kernel!( - #[inline(always)] - fn kernel(token: Sse4_2, a: u64x2) -> u64x2 { - let _ = a; - _mm_setzero_si128().simd_into(token) - } - ); - kernel(self, a) - } - #[inline(always)] fn mul_u64x2(self, a: u64x2, b: u64x2) -> u64x2 { crate::kernel!( #[inline(always)] diff --git a/fearless_simd/src/generated/wasm.rs b/fearless_simd/src/generated/wasm.rs index 7cddeae8..d2fa6044 100644 --- a/fearless_simd/src/generated/wasm.rs +++ b/fearless_simd/src/generated/wasm.rs @@ -670,7 +670,11 @@ impl Simd for WasmSimd128 { } #[inline(always)] fn saturating_neg_i8x16(self, a: i8x16) -> i8x16 { - self.neg_i8x16(self.max_i8x16(a, self.splat(i8::MIN + 1))) + i8x16_neg(i8x16_max(a.into(), i8x16_splat(i8::MIN + 1))).simd_into(self) + } + #[inline(always)] + fn saturating_abs_i8x16(self, a: i8x16) -> i8x16 { + i8x16_abs(i8x16_max(a.into(), i8x16_splat(i8::MIN + 1))).simd_into(self) } #[inline(always)] fn mul_i8x16(self, a: i8x16, b: i8x16) -> i8x16 { @@ -1241,11 +1245,6 @@ impl Simd for WasmSimd128 { u8x16_sub_sat(a.into(), b.into()).simd_into(self) } #[inline(always)] - fn saturating_neg_u8x16(self, a: u8x16) -> u8x16 { - let _ = a; - u32x4_splat(0).simd_into(self) - } - #[inline(always)] fn mul_u8x16(self, a: u8x16, b: u8x16) -> u8x16 { let low = u16x8_extmul_low_u8x16(a.into(), b.into()); let high = u16x8_extmul_high_u8x16(a.into(), b.into()); @@ -1864,7 +1863,11 @@ impl Simd for WasmSimd128 { } #[inline(always)] fn saturating_neg_i16x8(self, a: i16x8) -> i16x8 { - self.neg_i16x8(self.max_i16x8(a, self.splat(i16::MIN + 1))) + i16x8_neg(i16x8_max(a.into(), i16x8_splat(i16::MIN + 1))).simd_into(self) + } + #[inline(always)] + fn saturating_abs_i16x8(self, a: i16x8) -> i16x8 { + i16x8_abs(i16x8_max(a.into(), i16x8_splat(i16::MIN + 1))).simd_into(self) } #[inline(always)] fn mul_i16x8(self, a: i16x8, b: i16x8) -> i16x8 { @@ -2247,11 +2250,6 @@ impl Simd for WasmSimd128 { u16x8_sub_sat(a.into(), b.into()).simd_into(self) } #[inline(always)] - fn saturating_neg_u16x8(self, a: u16x8) -> u16x8 { - let _ = a; - u32x4_splat(0).simd_into(self) - } - #[inline(always)] fn mul_u16x8(self, a: u16x8, b: u16x8) -> u16x8 { u16x8_mul(a.into(), b.into()).simd_into(self) } @@ -2804,7 +2802,11 @@ impl Simd for WasmSimd128 { } #[inline(always)] fn saturating_neg_i32x4(self, a: i32x4) -> i32x4 { - self.neg_i32x4(self.max_i32x4(a, self.splat(i32::MIN + 1))) + i32x4_neg(i32x4_max(a.into(), i32x4_splat(i32::MIN + 1))).simd_into(self) + } + #[inline(always)] + fn saturating_abs_i32x4(self, a: i32x4) -> i32x4 { + i32x4_abs(i32x4_max(a.into(), i32x4_splat(i32::MIN + 1))).simd_into(self) } #[inline(always)] fn mul_i32x4(self, a: i32x4, b: i32x4) -> i32x4 { @@ -3182,11 +3184,6 @@ impl Simd for WasmSimd128 { u32x4_sub(u32x4_max(a, b), b).simd_into(self) } #[inline(always)] - fn saturating_neg_u32x4(self, a: u32x4) -> u32x4 { - let _ = a; - u32x4_splat(0).simd_into(self) - } - #[inline(always)] fn mul_u32x4(self, a: u32x4, b: u32x4) -> u32x4 { u32x4_mul(a.into(), b.into()).simd_into(self) } @@ -4140,7 +4137,11 @@ impl Simd for WasmSimd128 { } #[inline(always)] fn saturating_neg_i64x2(self, a: i64x2) -> i64x2 { - self.neg_i64x2(self.max_i64x2(a, self.splat(i64::MIN + 1))) + i64x2_neg(i64x2_max(a.into(), i64x2_splat(i64::MIN + 1))).simd_into(self) + } + #[inline(always)] + fn saturating_abs_i64x2(self, a: i64x2) -> i64x2 { + i64x2_abs(i64x2_max(a.into(), i64x2_splat(i64::MIN + 1))).simd_into(self) } #[inline(always)] fn mul_i64x2(self, a: i64x2, b: i64x2) -> i64x2 { @@ -4492,11 +4493,6 @@ impl Simd for WasmSimd128 { v128_andnot(wrapped, saturation_mask).simd_into(self) } #[inline(always)] - fn saturating_neg_u64x2(self, a: u64x2) -> u64x2 { - let _ = a; - u32x4_splat(0).simd_into(self) - } - #[inline(always)] fn mul_u64x2(self, a: u64x2, b: u64x2) -> u64x2 { u64x2_mul(a.into(), b.into()).simd_into(self) } diff --git a/fearless_simd_gen/src/arch/fallback.rs b/fearless_simd_gen/src/arch/fallback.rs index a2a637c9..fc8ea9c8 100644 --- a/fearless_simd_gen/src/arch/fallback.rs +++ b/fearless_simd_gen/src/arch/fallback.rs @@ -14,6 +14,7 @@ pub(crate) fn translate_op(op: &str, is_float: bool) -> Option<&'static str> { "wrapping_abs" } } + "saturating_abs" => "saturating_abs", "copysign" => "copysign", "neg" => { if is_float { @@ -22,6 +23,7 @@ pub(crate) fn translate_op(op: &str, is_float: bool) -> Option<&'static str> { "wrapping_neg" } } + "saturating_neg" => "saturating_neg", "floor" => "floor", "ceil" => "ceil", "round_ties_even" => "round_ties_even", diff --git a/fearless_simd_gen/src/level.rs b/fearless_simd_gen/src/level.rs index bfceab7d..7bc0aa6e 100644 --- a/fearless_simd_gen/src/level.rs +++ b/fearless_simd_gen/src/level.rs @@ -162,7 +162,8 @@ pub(crate) trait Level { continue; } // Unsigned absolute value uses the identity default at every width. - if (op.method == "abs" && vec_ty.scalar == ScalarType::Unsigned) + if (matches!(op.method, "abs" | "saturating_abs" | "saturating_neg") + && vec_ty.scalar == ScalarType::Unsigned) || op.method == "simd_ne" || op.sig.should_route_swizzle_through_bytes(vec_ty) || op.reversed_compare_method().is_some() diff --git a/fearless_simd_gen/src/mk_fallback.rs b/fearless_simd_gen/src/mk_fallback.rs index 6698c6e4..f94a7e7d 100644 --- a/fearless_simd_gen/src/mk_fallback.rs +++ b/fearless_simd_gen/src/mk_fallback.rs @@ -163,36 +163,6 @@ impl Level for Fallback { }; } - if method == "saturating_neg" { - match vec_ty.scalar { - ScalarType::Unsigned => { - let n = vec_ty.len; - return quote! { - #method_sig { - let _ = a; - [0; #n].simd_into(self) - } - }; - } - ScalarType::Int => { - let items = make_list( - (0..vec_ty.len) - .map(|idx| { - let value = lane(quote! { a }, vec_ty, idx); - quote! { #value.saturating_neg() } - }) - .collect::>(), - ); - return quote! { - #method_sig { - #items.simd_into(self) - } - }; - } - _ => unreachable!(), - } - } - let items = make_list( (0..vec_ty.len) .map(|idx| { diff --git a/fearless_simd_gen/src/mk_neon.rs b/fearless_simd_gen/src/mk_neon.rs index 946a5df7..d6441401 100644 --- a/fearless_simd_gen/src/mk_neon.rs +++ b/fearless_simd_gen/src/mk_neon.rs @@ -342,24 +342,18 @@ impl Level for Neon { } if method == "saturating_neg" { - match vec_ty.scalar { - ScalarType::Unsigned => { - return self.kernel_method(op, vec_ty, |token| { - let zero = format_ident!("vdupq_n_u{}", vec_ty.scalar_bits); - quote! { - let _ = a; - #zero(0).simd_into(#token) - } - }); - } - ScalarType::Int => { - let sat_neg = format_ident!("vqneg{}q_s{}", "v", vec_ty.scalar_bits); - return self.kernel_method(op, vec_ty, |token| { - quote! { #sat_neg(a.into()).simd_into(#token) } - }); - } - _ => unreachable!(), - } + assert_eq!(vec_ty.scalar, ScalarType::Int); + let sat_neg = format_ident!("vqnegq_s{}", vec_ty.scalar_bits); + return self.kernel_method(op, vec_ty, |token| { + quote! { #sat_neg(a.into()).simd_into(#token) } + }); + } + if method == "saturating_abs" { + assert_eq!(vec_ty.scalar, ScalarType::Int); + let sat_abs = format_ident!("vqabsq_s{}", vec_ty.scalar_bits); + return self.kernel_method(op, vec_ty, |token| { + quote! { #sat_abs(a.into()).simd_into(#token) } + }); } if matches!(method, "to_degrees" | "to_radians") { diff --git a/fearless_simd_gen/src/mk_simd_trait.rs b/fearless_simd_gen/src/mk_simd_trait.rs index 942f2aa4..0dbda470 100644 --- a/fearless_simd_gen/src/mk_simd_trait.rs +++ b/fearless_simd_gen/src/mk_simd_trait.rs @@ -28,7 +28,9 @@ pub(crate) fn mk_simd_trait() -> TokenStream { let doc_alias = op .doc_alias() .map(|alias| quote! { #[doc(alias = #alias)] }); - if op.method == "abs" && vec_ty.scalar == ScalarType::Unsigned { + if matches!(op.method, "abs" | "saturating_abs") + && vec_ty.scalar == ScalarType::Unsigned + { methods.extend(quote! { #[doc = #doc] #[inline(always)] @@ -36,6 +38,16 @@ pub(crate) fn mk_simd_trait() -> TokenStream { a } }); + } else if op.method == "saturating_neg" && vec_ty.scalar == ScalarType::Unsigned { + let splat_op = generic_op_name("splat", vec_ty); + methods.extend(quote! { + #[doc = #doc] + #[inline(always)] + #method_sig { + let _ = a; + self.#splat_op(0) + } + }); } else if op.method == "simd_ne" { let eq = generic_op_name("simd_eq", vec_ty); let not = generic_op_name("not", &vec_ty.mask_ty()); diff --git a/fearless_simd_gen/src/mk_wasm.rs b/fearless_simd_gen/src/mk_wasm.rs index 4a2e0452..48b19305 100644 --- a/fearless_simd_gen/src/mk_wasm.rs +++ b/fearless_simd_gen/src/mk_wasm.rs @@ -554,35 +554,23 @@ impl Level for WasmSimd128 { }; } - if method == "saturating_neg" { - match vec_ty.scalar { - ScalarType::Unsigned => { - return quote! { - #method_sig { - let _ = a; - u32x4_splat(0).simd_into(self) - } - }; - } - ScalarType::Int => { - // Emulate saturating neg with -max(x, iN::MIN + 1) - let bound = match vec_ty.scalar_bits { - 8 => quote! { i8::MIN + 1 }, - 16 => quote! { i16::MIN + 1 }, - 32 => quote! { i32::MIN + 1 }, - 64 => quote! { i64::MIN + 1 }, - _ => unreachable!(), - }; - let max_op = generic_op_name("max", vec_ty); - let neg_op = generic_op_name("neg", vec_ty); - return quote! { - #method_sig { - self.#neg_op(self.#max_op(a, self.splat(#bound))) - } - }; + if matches!(method, "saturating_neg" | "saturating_abs") { + assert_eq!(vec_ty.scalar, ScalarType::Int); + + // WASM doesn't have saturating neg/abs, so we emulate using + // saturating_neg(x) = wrapping_neg(max(x, MIN+1)) + // saturating_abs(x) = wrapping_abs(max(x, MIN+1)) + + let scalar = vec_ty.scalar_rust(); + let min_p1 = wasm::expr("splat", vec_ty, &[quote! { #scalar::MIN + 1 }]); + let max = wasm::expr("max", vec_ty, &[quote! { a.into() }, min_p1]); + let expr = wasm::expr(&method["saturating_".len()..], vec_ty, &[max]); + + return quote! { + #method_sig { + #expr.simd_into(self) } - _ => unreachable!(), - } + }; } if method == "round" { diff --git a/fearless_simd_gen/src/mk_x86.rs b/fearless_simd_gen/src/mk_x86.rs index cc0d63bc..e6f5fbf9 100644 --- a/fearless_simd_gen/src/mk_x86.rs +++ b/fearless_simd_gen/src/mk_x86.rs @@ -2777,91 +2777,54 @@ impl X86 { } } - if method == "saturating_neg" { - match vec_ty.scalar { - ScalarType::Unsigned => { - // For unsigned integers, saturating_neg always produces zero - return self.kernel_method(op, vec_ty, |token| { - let zero = intrinsic_ident("setzero", coarse_type(vec_ty), vec_ty.n_bits()); - quote! { - let _ = a; - #zero().simd_into(#token) - } - }); - } - ScalarType::Int => { - // x86 doesn't have saturating neg, so we emulate using saturating_neg(x) = -max(x, MIN+1). - // Unfortunately, the max-family of instructions have requirements: - // - SSE2: i16 available - // - SSE4.1, AVX2: i8, i16, i32 available - // - AVX512: i8, i16, i32, i64 available - - let can_use_max = match self { - Self::Sse2 => vec_ty.scalar_bits == 16, - Self::Sse4_2 | Self::Avx2 => matches!(vec_ty.scalar_bits, 8 | 16 | 32), - Self::Avx512 => matches!(vec_ty.scalar_bits, 8 | 16 | 32 | 64), - }; - - if can_use_max { - let min_plus_one = match vec_ty.scalar_bits { - 8 => quote! { i8::MIN + 1 }, - 16 => quote! { i16::MIN + 1 }, - 32 => quote! { i32::MIN + 1 }, - 64 => quote! { i64::MIN + 1 }, - _ => unreachable!(), - }; - let set1 = set1_intrinsic(vec_ty); - let max_intrinsic = simple_intrinsic("max", vec_ty); - let sub = simple_intrinsic("sub", vec_ty); - let zero = intrinsic_ident("setzero", coarse_type(vec_ty), vec_ty.n_bits()); - - return self.kernel_method(op, vec_ty, |token| { - quote! { - let a = a.into(); - let bounded = #max_intrinsic(a, #set1(#min_plus_one)); - #sub(#zero(), bounded).simd_into(#token) - } - }); - } + if matches!(method, "saturating_neg" | "saturating_abs") { + assert_eq!(vec_ty.scalar, ScalarType::Int); + + // x86 doesn't have saturating neg/abs, so we emulate using + // saturating_neg(x) = wrapping_neg(max(x, MIN+1)) + // saturating_abs(x) = wrapping_abs(max(x, MIN+1)) + + let wrapping_op = generic_op_name(&method["saturating_".len()..], vec_ty); + let splat_op = generic_op_name("splat", vec_ty); + let min_value = { + let scalar = vec_ty.scalar_rust(); + quote! { #scalar::MIN } + }; - // When max isn't avaialble, we use the following alternative formulation: - // Input: signed integer i: - // let a = -i (wrapping negation) - // return a ^ cmpeq(a, iN::MIN) - // This works because: - // - If i == iN::MIN, then a == iN::MIN (wrapping) and cmpeq produces -1, - // so a ^ -1 = !a = !iN::MIN = iN::MAX - // - Otherwise, cmpeq produces 0, so a ^ 0 = a - - // For SSE2 i64, cmpeq_epi64 isn't available, so fall back - if *self == Self::Sse2 && vec_ty.scalar_bits == 64 { - return fallback_method(op, vec_ty); + // Unfortunately, the max-family of instructions have requirements: + // - SSE2: i16 available + // - SSE4.1, AVX2: i8, i16, i32 available + // - AVX512: i8, i16, i32, i64 available + let has_native_max = match self { + Self::Sse2 => vec_ty.scalar_bits == 16, + Self::Sse4_2 | Self::Avx2 => matches!(vec_ty.scalar_bits, 8 | 16 | 32), + Self::Avx512 => matches!(vec_ty.scalar_bits, 8 | 16 | 32 | 64), + }; + if has_native_max { + let max_op = generic_op_name("max", vec_ty); + return quote! { + #method_sig { + self.#wrapping_op(self.#max_op(a, self.#splat_op(const { #min_value + 1 }))) } + }; + } - let min_val = match vec_ty.scalar_bits { - 8 => quote! { i8::MIN }, - 16 => quote! { i16::MIN }, - 32 => quote! { i32::MIN }, - 64 => quote! { i64::MIN }, - _ => unreachable!(), - }; - let set1 = set1_intrinsic(vec_ty); - let cmpeq = simple_intrinsic("cmpeq", vec_ty); - let sub = simple_intrinsic("sub", vec_ty); - let xor = intrinsic_ident("xor", coarse_type(vec_ty), vec_ty.n_bits()); - let zero = intrinsic_ident("setzero", coarse_type(vec_ty), vec_ty.n_bits()); + // When native max isn't avaialble, we use the following alternative formulations: + // saturating_neg(x) = { let t = wrapping_neg(x); t ^ eq(t, MIN) } + // saturating_abs(x) = { let t = wrapping_abs(x); t ^ eq(t, MIN) } + // This works because t == MIN iff the operation wraps. So we use xor to bitwise negate t, which maps MIN to MAX. + // This is one (or more) instructions slower, so it's only the fallback. + let eq_op = generic_op_name("simd_eq", vec_ty); + let mask_to_int_op = generic_op_name("to_vector", &vec_ty.mask_ty()); + let xor_op = generic_op_name("xor", vec_ty); - return self.kernel_method(op, vec_ty, |token| { - quote! { - let a = a.into(); - let neg_a = #sub(#zero(), a); - let is_min = #cmpeq(neg_a, #set1(#min_val)); - #xor(neg_a, is_min).simd_into(#token) - } - }); + return quote! { + #method_sig { + let t = self.#wrapping_op(a); + let mask = self.#eq_op(t, self.#splat_op(#min_value)); + self.#xor_op(t, self.#mask_to_int_op(mask)) } - _ => unreachable!(), - } + }; } if method == "reverse" { diff --git a/fearless_simd_gen/src/ops.rs b/fearless_simd_gen/src/ops.rs index 8d7d3b23..7d900cec 100644 --- a/fearless_simd_gen/src/ops.rs +++ b/fearless_simd_gen/src/ops.rs @@ -1378,6 +1378,13 @@ assert_eq!(*i.reverse_bits(), [0xE6_A2_C4_80, 0xF7_B3_D5_91, 0xD0_50_90_10, 0xF0 "Negate each element of the vector, saturating on overflow.\n\n\ For unsigned integers, this will always return zero. For signed integers, this returns the same as `-self`, except when negating the minimum value. For example, `saturating_neg(-128i8)` returns `127i8`.", ), + Op::new( + "saturating_abs", + OpKind::VecTraitMethod, + OpSig::Unary, + "Compute the absolute value of each element of the vector, saturating on overflow.\n\n\ + For unsigned integers, this will always return the same value. For signed integers, this returns the same as `self.abs()`, except when applied to the minimum value. For example, `saturating_abs(-128i8)` returns `127i8`.", + ), Op::new( "mul", OpKind::Overloaded(CoreOpTrait::Mul), diff --git a/fearless_simd_gen/src/types.rs b/fearless_simd_gen/src/types.rs index 31d525a9..aecb71dd 100644 --- a/fearless_simd_gen/src/types.rs +++ b/fearless_simd_gen/src/types.rs @@ -86,6 +86,10 @@ impl VecType { format!("[{}; {}]", self.scalar.rust(self.scalar_bits), self.len) } + pub(crate) fn scalar_rust(&self) -> TokenStream { + self.scalar.rust(self.scalar_bits) + } + /// Returns the name of the `Aligned{128/256/512}` wrapper for this vector type, used to wrap native vector types or /// arrays of them. pub(crate) fn aligned_wrapper(&self) -> TokenStream { diff --git a/fearless_simd_tests/tests/harness/ops/mod.rs b/fearless_simd_tests/tests/harness/ops/mod.rs index 32ed0735..c2673d2e 100644 --- a/fearless_simd_tests/tests/harness/ops/mod.rs +++ b/fearless_simd_tests/tests/harness/ops/mod.rs @@ -80,6 +80,7 @@ mod rotate_elements_left; mod rotate_elements_right; mod round; mod round_ties_even; +mod saturating_abs; mod saturating_add; mod saturating_neg; mod saturating_sub; diff --git a/fearless_simd_tests/tests/harness/ops/saturating_abs.rs b/fearless_simd_tests/tests/harness/ops/saturating_abs.rs new file mode 100644 index 00000000..693b12d5 --- /dev/null +++ b/fearless_simd_tests/tests/harness/ops/saturating_abs.rs @@ -0,0 +1,359 @@ +// Copyright 2026 the Fearless_SIMD Authors +// SPDX-License-Identifier: Apache-2.0 OR MIT + +use fearless_simd::*; +use fearless_simd_dev_macros::simd_test; + +#[simd_test] +fn saturating_abs_i8x16(simd: S) { + let data: [i8; 16] = [ + i8::MIN, + i8::MAX, + i8::MIN + 1, + 0, + 1, + -1, + 2, + -2, + 3, + -3, + 4, + -4, + 5, + -5, + 6, + -6, + ]; + let a = i8x16::from_slice(simd, &data); + assert_eq!(*a.saturating_abs(), data.map(i8::saturating_abs)); +} + +#[simd_test] +fn saturating_abs_i8x32(simd: S) { + let data: [i8; 32] = [ + i8::MIN, + i8::MAX, + i8::MIN + 1, + 0, + 1, + -1, + 2, + -2, + 3, + -3, + 4, + -4, + 5, + -5, + 6, + -6, + 7, + -7, + 8, + -8, + 9, + -9, + 10, + -10, + 11, + -11, + 12, + -12, + 13, + -13, + 14, + -14, + ]; + let a = i8x32::from_slice(simd, &data); + assert_eq!(*a.saturating_abs(), data.map(i8::saturating_abs)); +} + +#[simd_test] +fn saturating_abs_i8x64(simd: S) { + let data: [i8; 64] = [ + i8::MIN, + i8::MAX, + i8::MIN + 1, + 0, + 1, + -1, + 2, + -2, + 3, + -3, + 4, + -4, + 5, + -5, + 6, + -6, + 7, + -7, + 8, + -8, + 9, + -9, + 10, + -10, + 11, + -11, + 12, + -12, + 13, + -13, + 14, + -14, + i8::MIN, + i8::MAX, + i8::MIN + 1, + 0, + 1, + -1, + 2, + -2, + 3, + -3, + 4, + -4, + 5, + -5, + 6, + -6, + 7, + -7, + 8, + -8, + 9, + -9, + 10, + -10, + 11, + -11, + 12, + -12, + 13, + -13, + 14, + -14, + ]; + let a = i8x64::from_slice(simd, &data); + assert_eq!(*a.saturating_abs(), data.map(i8::saturating_abs)); +} + +#[simd_test] +fn saturating_abs_i16x8(simd: S) { + let data: [i16; 8] = [i16::MIN, i16::MAX, i16::MIN + 1, 0, 1, -1, 2, -2]; + let a = i16x8::from_slice(simd, &data); + assert_eq!(*a.saturating_abs(), data.map(i16::saturating_abs)); +} + +#[simd_test] +fn saturating_abs_i16x16(simd: S) { + let data: [i16; 16] = [ + i16::MIN, + i16::MAX, + i16::MIN + 1, + 0, + 1, + -1, + 2, + -2, + 3, + -3, + 4, + -4, + 5, + -5, + 6, + -6, + ]; + let a = i16x16::from_slice(simd, &data); + assert_eq!(*a.saturating_abs(), data.map(i16::saturating_abs)); +} + +#[simd_test] +fn saturating_abs_i16x32(simd: S) { + let data: [i16; 32] = [ + i16::MIN, + i16::MAX, + i16::MIN + 1, + 0, + 1, + -1, + 2, + -2, + 3, + -3, + 4, + -4, + 5, + -5, + 6, + -6, + 7, + -7, + 8, + -8, + 9, + -9, + 10, + -10, + 11, + -11, + 12, + -12, + 13, + -13, + 14, + -14, + ]; + let a = i16x32::from_slice(simd, &data); + assert_eq!(*a.saturating_abs(), data.map(i16::saturating_abs)); +} + +#[simd_test] +fn saturating_abs_i32x4(simd: S) { + let data: [i32; 4] = [i32::MIN, i32::MAX, i32::MIN + 1, 0]; + let a = i32x4::from_slice(simd, &data); + assert_eq!(*a.saturating_abs(), data.map(i32::saturating_abs)); +} + +#[simd_test] +fn saturating_abs_i32x8(simd: S) { + let data: [i32; 8] = [i32::MIN, i32::MAX, i32::MIN + 1, 0, 1, -1, 2, -2]; + let a = i32x8::from_slice(simd, &data); + assert_eq!(*a.saturating_abs(), data.map(i32::saturating_abs)); +} + +#[simd_test] +fn saturating_abs_i32x16(simd: S) { + let data: [i32; 16] = [ + i32::MIN, + i32::MAX, + i32::MIN + 1, + 0, + 1, + -1, + 2, + -2, + 3, + -3, + 4, + -4, + 5, + -5, + 6, + -6, + ]; + let a = i32x16::from_slice(simd, &data); + assert_eq!(*a.saturating_abs(), data.map(i32::saturating_abs)); +} + +#[simd_test] +fn saturating_abs_i64x2(simd: S) { + let data: [i64; 2] = [i64::MIN, i64::MAX]; + let a = i64x2::from_slice(simd, &data); + assert_eq!(*a.saturating_abs(), data.map(i64::saturating_abs)); + + let data: [i64; 2] = [i64::MIN + 1, 0]; + let a = i64x2::from_slice(simd, &data); + assert_eq!(*a.saturating_abs(), data.map(i64::saturating_abs)); +} + +#[simd_test] +fn saturating_abs_i64x4(simd: S) { + let data: [i64; 4] = [i64::MIN, i64::MAX, i64::MIN + 1, 0]; + let a = i64x4::from_slice(simd, &data); + assert_eq!(*a.saturating_abs(), data.map(i64::saturating_abs)); +} + +#[simd_test] +fn saturating_abs_i64x8(simd: S) { + let data: [i64; 8] = [i64::MIN, i64::MAX, i64::MIN + 1, 0, 1, -1, 2, -2]; + let a = i64x8::from_slice(simd, &data); + assert_eq!(*a.saturating_abs(), data.map(i64::saturating_abs)); +} + +#[simd_test] +fn saturating_abs_u8x16(simd: S) { + let data: [u8; 16] = std::array::from_fn(|i| i as _); + let a = u8x16::from_slice(simd, &data); + assert_eq!(*a.saturating_abs(), data); +} + +#[simd_test] +fn saturating_abs_u8x32(simd: S) { + let data: [u8; 32] = std::array::from_fn(|i| i as _); + let a = u8x32::from_slice(simd, &data); + assert_eq!(*a.saturating_abs(), data); +} + +#[simd_test] +fn saturating_abs_u8x64(simd: S) { + let data: [u8; 64] = std::array::from_fn(|i| i as _); + let a = u8x64::from_slice(simd, &data); + assert_eq!(*a.saturating_abs(), data); +} + +#[simd_test] +fn saturating_abs_u16x8(simd: S) { + let data: [u16; 8] = std::array::from_fn(|i| i as _); + let a = u16x8::from_slice(simd, &data); + assert_eq!(*a.saturating_abs(), data); +} + +#[simd_test] +fn saturating_abs_u16x16(simd: S) { + let data: [u16; 16] = std::array::from_fn(|i| i as _); + let a = u16x16::from_slice(simd, &data); + assert_eq!(*a.saturating_abs(), data); +} + +#[simd_test] +fn saturating_abs_u16x32(simd: S) { + let data: [u16; 32] = std::array::from_fn(|i| i as _); + let a = u16x32::from_slice(simd, &data); + assert_eq!(*a.saturating_abs(), data); +} + +#[simd_test] +fn saturating_abs_u32x4(simd: S) { + let data: [u32; 4] = std::array::from_fn(|i| i as _); + let a = u32x4::from_slice(simd, &data); + assert_eq!(*a.saturating_abs(), data); +} + +#[simd_test] +fn saturating_abs_u32x8(simd: S) { + let data: [u32; 8] = std::array::from_fn(|i| i as _); + let a = u32x8::from_slice(simd, &data); + assert_eq!(*a.saturating_abs(), data); +} + +#[simd_test] +fn saturating_abs_u32x16(simd: S) { + let data: [u32; 16] = std::array::from_fn(|i| i as _); + let a = u32x16::from_slice(simd, &data); + assert_eq!(*a.saturating_abs(), data); +} + +#[simd_test] +fn saturating_abs_u64x2(simd: S) { + let data: [u64; 2] = std::array::from_fn(|i| i as _); + let a = u64x2::from_slice(simd, &data); + assert_eq!(*a.saturating_abs(), data); +} + +#[simd_test] +fn saturating_abs_u64x4(simd: S) { + let data: [u64; 4] = std::array::from_fn(|i| i as _); + let a = u64x4::from_slice(simd, &data); + assert_eq!(*a.saturating_abs(), data); +} + +#[simd_test] +fn saturating_abs_u64x8(simd: S) { + let data: [u64; 8] = std::array::from_fn(|i| i as _); + let a = u64x8::from_slice(simd, &data); + assert_eq!(*a.saturating_abs(), data); +} From 0be7f0e80888a96f7714af0f0c15a77a7d5ef1b6 Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Sat, 28 Nov 2026 20:56:48 +0100 Subject: [PATCH 3/4] Typo --- fearless_simd_gen/src/mk_x86.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fearless_simd_gen/src/mk_x86.rs b/fearless_simd_gen/src/mk_x86.rs index e6f5fbf9..37cd787b 100644 --- a/fearless_simd_gen/src/mk_x86.rs +++ b/fearless_simd_gen/src/mk_x86.rs @@ -2809,7 +2809,7 @@ impl X86 { }; } - // When native max isn't avaialble, we use the following alternative formulations: + // When native max isn't available, we use the following alternative formulations: // saturating_neg(x) = { let t = wrapping_neg(x); t ^ eq(t, MIN) } // saturating_abs(x) = { let t = wrapping_abs(x); t ^ eq(t, MIN) } // This works because t == MIN iff the operation wraps. So we use xor to bitwise negate t, which maps MIN to MAX. From 3c4ee9be5622aaa6a562af07338a9a0cd5d9644a Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Sat, 28 Nov 2026 21:00:42 +0100 Subject: [PATCH 4/4] Don't use WASM intrinsics directly WASM... you disappoint me --- fearless_simd/src/generated/wasm.rs | 16 ++++++++-------- fearless_simd_gen/src/mk_wasm.rs | 13 ++++++++----- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/fearless_simd/src/generated/wasm.rs b/fearless_simd/src/generated/wasm.rs index d2fa6044..1e841ec6 100644 --- a/fearless_simd/src/generated/wasm.rs +++ b/fearless_simd/src/generated/wasm.rs @@ -670,11 +670,11 @@ impl Simd for WasmSimd128 { } #[inline(always)] fn saturating_neg_i8x16(self, a: i8x16) -> i8x16 { - i8x16_neg(i8x16_max(a.into(), i8x16_splat(i8::MIN + 1))).simd_into(self) + self.neg_i8x16(self.max_i8x16(a, self.splat_i8x16(const { i8::MIN + 1 }))) } #[inline(always)] fn saturating_abs_i8x16(self, a: i8x16) -> i8x16 { - i8x16_abs(i8x16_max(a.into(), i8x16_splat(i8::MIN + 1))).simd_into(self) + self.abs_i8x16(self.max_i8x16(a, self.splat_i8x16(const { i8::MIN + 1 }))) } #[inline(always)] fn mul_i8x16(self, a: i8x16, b: i8x16) -> i8x16 { @@ -1863,11 +1863,11 @@ impl Simd for WasmSimd128 { } #[inline(always)] fn saturating_neg_i16x8(self, a: i16x8) -> i16x8 { - i16x8_neg(i16x8_max(a.into(), i16x8_splat(i16::MIN + 1))).simd_into(self) + self.neg_i16x8(self.max_i16x8(a, self.splat_i16x8(const { i16::MIN + 1 }))) } #[inline(always)] fn saturating_abs_i16x8(self, a: i16x8) -> i16x8 { - i16x8_abs(i16x8_max(a.into(), i16x8_splat(i16::MIN + 1))).simd_into(self) + self.abs_i16x8(self.max_i16x8(a, self.splat_i16x8(const { i16::MIN + 1 }))) } #[inline(always)] fn mul_i16x8(self, a: i16x8, b: i16x8) -> i16x8 { @@ -2802,11 +2802,11 @@ impl Simd for WasmSimd128 { } #[inline(always)] fn saturating_neg_i32x4(self, a: i32x4) -> i32x4 { - i32x4_neg(i32x4_max(a.into(), i32x4_splat(i32::MIN + 1))).simd_into(self) + self.neg_i32x4(self.max_i32x4(a, self.splat_i32x4(const { i32::MIN + 1 }))) } #[inline(always)] fn saturating_abs_i32x4(self, a: i32x4) -> i32x4 { - i32x4_abs(i32x4_max(a.into(), i32x4_splat(i32::MIN + 1))).simd_into(self) + self.abs_i32x4(self.max_i32x4(a, self.splat_i32x4(const { i32::MIN + 1 }))) } #[inline(always)] fn mul_i32x4(self, a: i32x4, b: i32x4) -> i32x4 { @@ -4137,11 +4137,11 @@ impl Simd for WasmSimd128 { } #[inline(always)] fn saturating_neg_i64x2(self, a: i64x2) -> i64x2 { - i64x2_neg(i64x2_max(a.into(), i64x2_splat(i64::MIN + 1))).simd_into(self) + self.neg_i64x2(self.max_i64x2(a, self.splat_i64x2(const { i64::MIN + 1 }))) } #[inline(always)] fn saturating_abs_i64x2(self, a: i64x2) -> i64x2 { - i64x2_abs(i64x2_max(a.into(), i64x2_splat(i64::MIN + 1))).simd_into(self) + self.abs_i64x2(self.max_i64x2(a, self.splat_i64x2(const { i64::MIN + 1 }))) } #[inline(always)] fn mul_i64x2(self, a: i64x2, b: i64x2) -> i64x2 { diff --git a/fearless_simd_gen/src/mk_wasm.rs b/fearless_simd_gen/src/mk_wasm.rs index 48b19305..b2ef192f 100644 --- a/fearless_simd_gen/src/mk_wasm.rs +++ b/fearless_simd_gen/src/mk_wasm.rs @@ -561,14 +561,17 @@ impl Level for WasmSimd128 { // saturating_neg(x) = wrapping_neg(max(x, MIN+1)) // saturating_abs(x) = wrapping_abs(max(x, MIN+1)) - let scalar = vec_ty.scalar_rust(); - let min_p1 = wasm::expr("splat", vec_ty, &[quote! { #scalar::MIN + 1 }]); - let max = wasm::expr("max", vec_ty, &[quote! { a.into() }, min_p1]); - let expr = wasm::expr(&method["saturating_".len()..], vec_ty, &[max]); + let wrapping_op = generic_op_name(&method["saturating_".len()..], vec_ty); + let max_op = generic_op_name("max", vec_ty); + let splat_op = generic_op_name("splat", vec_ty); + let min_value = { + let scalar = vec_ty.scalar_rust(); + quote! { #scalar::MIN } + }; return quote! { #method_sig { - #expr.simd_into(self) + self.#wrapping_op(self.#max_op(a, self.#splat_op(const { #min_value + 1 }))) } }; }