From 9c349b2ddb91884e12e39271dd0c48ee1a164d22 Mon Sep 17 00:00:00 2001 From: Laurenz Stampfl Date: Tue, 17 Nov 2026 20:02:28 +0100 Subject: [PATCH] Add `mul_q15` and `mul_q15_precise` for 16-bit signed integer vectors --- fearless_simd/src/generated/avx2.rs | 47 +++++++ fearless_simd/src/generated/avx512.rs | 73 ++++++++++ fearless_simd/src/generated/fallback.rs | 29 ++++ fearless_simd/src/generated/neon.rs | 20 +++ fearless_simd/src/generated/simd_trait.rs | 38 +++++ fearless_simd/src/generated/sse2.rs | 30 ++++ fearless_simd/src/generated/sse4_2.rs | 22 +++ fearless_simd/src/generated/wasm.rs | 8 ++ fearless_simd/src/math.rs | 13 ++ fearless_simd_gen/src/arch/fallback.rs | 1 + fearless_simd_gen/src/arch/neon.rs | 1 + fearless_simd_gen/src/arch/wasm.rs | 1 + fearless_simd_gen/src/mk_fallback.rs | 3 + fearless_simd_gen/src/mk_x86.rs | 54 +++++++ fearless_simd_gen/src/ops.rs | 20 +++ fearless_simd_tests/tests/harness/ops/mod.rs | 1 + .../tests/harness/ops/mul_q15.rs | 133 ++++++++++++++++++ 17 files changed, 494 insertions(+) create mode 100644 fearless_simd_tests/tests/harness/ops/mul_q15.rs diff --git a/fearless_simd/src/generated/avx2.rs b/fearless_simd/src/generated/avx2.rs index 4a0bd7970..75416c241 100644 --- a/fearless_simd/src/generated/avx2.rs +++ b/fearless_simd/src/generated/avx2.rs @@ -3160,6 +3160,28 @@ impl Simd for Avx2 { kernel(self, vectors, dest); } #[inline(always)] + fn mul_q15_i16x8(self, a: i16x8, b: i16x8) -> i16x8 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: i16x8, b: i16x8) -> i16x8 { + _mm_mulhrs_epi16(a.into(), b.into()).simd_into(token) + } + ); + kernel(self, a, b) + } + #[inline(always)] + fn mul_q15_precise_i16x8(self, a: i16x8, b: i16x8) -> i16x8 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: i16x8, b: i16x8) -> i16x8 { + let product = _mm_mulhrs_epi16(a.into(), b.into()); + _mm_xor_si128(product, _mm_cmpeq_epi16(product, _mm_set1_epi16(i16::MIN))) + .simd_into(token) + } + ); + kernel(self, a, b) + } + #[inline(always)] fn widen_i16x8(self, a: i16x8) -> (i32x4, i32x4) { crate::kernel!( #[inline(always)] @@ -10915,6 +10937,31 @@ impl Simd for Avx2 { kernel(self, a) } #[inline(always)] + fn mul_q15_i16x16(self, a: i16x16, b: i16x16) -> i16x16 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: i16x16, b: i16x16) -> i16x16 { + _mm256_mulhrs_epi16(a.into(), b.into()).simd_into(token) + } + ); + kernel(self, a, b) + } + #[inline(always)] + fn mul_q15_precise_i16x16(self, a: i16x16, b: i16x16) -> i16x16 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx2, a: i16x16, b: i16x16) -> i16x16 { + let product = _mm256_mulhrs_epi16(a.into(), b.into()); + _mm256_xor_si256( + product, + _mm256_cmpeq_epi16(product, _mm256_set1_epi16(i16::MIN)), + ) + .simd_into(token) + } + ); + kernel(self, a, b) + } + #[inline(always)] fn widen_i16x16(self, a: i16x16) -> (i32x8, i32x8) { crate::kernel!( #[inline(always)] diff --git a/fearless_simd/src/generated/avx512.rs b/fearless_simd/src/generated/avx512.rs index 5a880cf3d..f7a6ad53a 100644 --- a/fearless_simd/src/generated/avx512.rs +++ b/fearless_simd/src/generated/avx512.rs @@ -3419,6 +3419,28 @@ impl Simd for Avx512 { kernel(self, vectors, dest); } #[inline(always)] + fn mul_q15_i16x8(self, a: i16x8, b: i16x8) -> i16x8 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: i16x8, b: i16x8) -> i16x8 { + _mm_mulhrs_epi16(a.into(), b.into()).simd_into(token) + } + ); + kernel(self, a, b) + } + #[inline(always)] + fn mul_q15_precise_i16x8(self, a: i16x8, b: i16x8) -> i16x8 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: i16x8, b: i16x8) -> i16x8 { + let product = _mm_mulhrs_epi16(a.into(), b.into()); + _mm_xor_si128(product, _mm_cmpeq_epi16(product, _mm_set1_epi16(i16::MIN))) + .simd_into(token) + } + ); + kernel(self, a, b) + } + #[inline(always)] fn widen_i16x8(self, a: i16x8) -> (i32x4, i32x4) { crate::kernel!( #[inline(always)] @@ -10969,6 +10991,31 @@ impl Simd for Avx512 { kernel(self, a) } #[inline(always)] + fn mul_q15_i16x16(self, a: i16x16, b: i16x16) -> i16x16 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: i16x16, b: i16x16) -> i16x16 { + _mm256_mulhrs_epi16(a.into(), b.into()).simd_into(token) + } + ); + kernel(self, a, b) + } + #[inline(always)] + fn mul_q15_precise_i16x16(self, a: i16x16, b: i16x16) -> i16x16 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: i16x16, b: i16x16) -> i16x16 { + let product = _mm256_mulhrs_epi16(a.into(), b.into()); + _mm256_xor_si256( + product, + _mm256_cmpeq_epi16(product, _mm256_set1_epi16(i16::MIN)), + ) + .simd_into(token) + } + ); + kernel(self, a, b) + } + #[inline(always)] fn widen_i16x16(self, a: i16x16) -> (i32x8, i32x8) { crate::kernel!( #[inline(always)] @@ -18498,6 +18545,32 @@ impl Simd for Avx512 { kernel(self, a) } #[inline(always)] + fn mul_q15_i16x32(self, a: i16x32, b: i16x32) -> i16x32 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: i16x32, b: i16x32) -> i16x32 { + _mm512_mulhrs_epi16(a.into(), b.into()).simd_into(token) + } + ); + kernel(self, a, b) + } + #[inline(always)] + fn mul_q15_precise_i16x32(self, a: i16x32, b: i16x32) -> i16x32 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Avx512, a: i16x32, b: i16x32) -> i16x32 { + let product = _mm512_mulhrs_epi16(a.into(), b.into()); + _mm512_mask_mov_epi16( + product, + _mm512_cmpeq_epi16_mask(product, _mm512_set1_epi16(i16::MIN)), + _mm512_set1_epi16(i16::MAX), + ) + .simd_into(token) + } + ); + kernel(self, a, b) + } + #[inline(always)] fn widen_i16x32(self, a: i16x32) -> (i32x16, i32x16) { crate::kernel!( #[inline(always)] diff --git a/fearless_simd/src/generated/fallback.rs b/fearless_simd/src/generated/fallback.rs index 13a0ae8ad..b17785449 100644 --- a/fearless_simd/src/generated/fallback.rs +++ b/fearless_simd/src/generated/fallback.rs @@ -9,6 +9,7 @@ reason = "Generated backends use different subsets of these helpers" )] use crate::math::FloatExt as _; +use crate::math::I16Ext as _; use crate::math::ScalarExt as _; use crate::{Level, arch_types::ArchTypes, prelude::*, seal::Seal}; use crate::{ @@ -3939,6 +3940,34 @@ impl Simd for Fallback { ]; } #[inline(always)] + fn mul_q15_i16x8(self, a: i16x8, b: i16x8) -> i16x8 { + [ + i16::mul_q15(a[0usize], b[0usize]), + i16::mul_q15(a[1usize], b[1usize]), + i16::mul_q15(a[2usize], b[2usize]), + i16::mul_q15(a[3usize], b[3usize]), + i16::mul_q15(a[4usize], b[4usize]), + i16::mul_q15(a[5usize], b[5usize]), + i16::mul_q15(a[6usize], b[6usize]), + i16::mul_q15(a[7usize], b[7usize]), + ] + .simd_into(self) + } + #[inline(always)] + fn mul_q15_precise_i16x8(self, a: i16x8, b: i16x8) -> i16x8 { + [ + i16::mul_q15(a[0usize], b[0usize]), + i16::mul_q15(a[1usize], b[1usize]), + i16::mul_q15(a[2usize], b[2usize]), + i16::mul_q15(a[3usize], b[3usize]), + i16::mul_q15(a[4usize], b[4usize]), + i16::mul_q15(a[5usize], b[5usize]), + i16::mul_q15(a[6usize], b[6usize]), + i16::mul_q15(a[7usize], b[7usize]), + ] + .simd_into(self) + } + #[inline(always)] fn widen_i16x8(self, a: i16x8) -> (i32x4, i32x4) { ( [ diff --git a/fearless_simd/src/generated/neon.rs b/fearless_simd/src/generated/neon.rs index 4b8fcf037..4cd4981ad 100644 --- a/fearless_simd/src/generated/neon.rs +++ b/fearless_simd/src/generated/neon.rs @@ -2655,6 +2655,26 @@ impl Simd for Neon { } } #[inline(always)] + fn mul_q15_i16x8(self, a: i16x8, b: i16x8) -> i16x8 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Neon, a: i16x8, b: i16x8) -> i16x8 { + vqrdmulhq_s16(a.into(), b.into()).simd_into(token) + } + ); + kernel(self, a, b) + } + #[inline(always)] + fn mul_q15_precise_i16x8(self, a: i16x8, b: i16x8) -> i16x8 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Neon, a: i16x8, b: i16x8) -> i16x8 { + vqrdmulhq_s16(a.into(), b.into()).simd_into(token) + } + ); + kernel(self, a, b) + } + #[inline(always)] fn widen_i16x8(self, a: i16x8) -> (i32x4, i32x4) { crate::kernel!( #[inline(always)] diff --git a/fearless_simd/src/generated/simd_trait.rs b/fearless_simd/src/generated/simd_trait.rs index 25a5f52b2..452b4f247 100644 --- a/fearless_simd/src/generated/simd_trait.rs +++ b/fearless_simd/src/generated/simd_trait.rs @@ -1026,6 +1026,10 @@ pub trait Simd: vectors: [i16x8; 4usize], dest: &mut [i16; 32usize], ) -> (); + #[doc = "Multiply two vectors of Q15 fixed-point numbers element-wise, rounding to nearest.\n\nA Q15 number `x` represents `x / 32768`. Lane `i` of the result is `(a[i] * b[i] + (1 << 14)) >> 15`, evaluated without intermediate overflow. The result of `i16::MIN * i16::MIN` is implementation-defined; see `mul_q15_precise` for a version that saturates it to `i16::MAX`."] + fn mul_q15_i16x8(self, a: i16x8, b: i16x8) -> i16x8; + #[doc = "Multiply two vectors of Q15 fixed-point numbers element-wise, rounding to nearest and saturating.\n\nThis operation is the same as `mul_q15`, except that `i16::MIN * i16::MIN` saturates to `i16::MAX`. It can be slower than `mul_q15`."] + fn mul_q15_precise_i16x8(self, a: i16x8, b: i16x8) -> i16x8; #[doc = "Widen every lane into two same-width vectors.\n\nThe first result contains the widened lower lanes and the second contains the widened upper lanes."] fn widen_i16x8(self, a: i16x8) -> (i32x4, i32x4); #[doc = "Multiply corresponding integer lanes exactly into twice-wide lanes. The first result contains the lower lanes and the second contains the upper lanes."] @@ -4189,6 +4193,23 @@ pub trait Simd: let (a0, a1) = self.split_i16x16(a); self.combine_i16x8(self.neg_i16x8(a0), self.neg_i16x8(a1)) } + #[doc = "Multiply two vectors of Q15 fixed-point numbers element-wise, rounding to nearest.\n\nA Q15 number `x` represents `x / 32768`. Lane `i` of the result is `(a[i] * b[i] + (1 << 14)) >> 15`, evaluated without intermediate overflow. The result of `i16::MIN * i16::MIN` is implementation-defined; see `mul_q15_precise` for a version that saturates it to `i16::MAX`."] + #[inline(always)] + fn mul_q15_i16x16(self, a: i16x16, b: i16x16) -> i16x16 { + let (a0, a1) = self.split_i16x16(a); + let (b0, b1) = self.split_i16x16(b); + self.combine_i16x8(self.mul_q15_i16x8(a0, b0), self.mul_q15_i16x8(a1, b1)) + } + #[doc = "Multiply two vectors of Q15 fixed-point numbers element-wise, rounding to nearest and saturating.\n\nThis operation is the same as `mul_q15`, except that `i16::MIN * i16::MIN` saturates to `i16::MAX`. It can be slower than `mul_q15`."] + #[inline(always)] + fn mul_q15_precise_i16x16(self, a: i16x16, b: i16x16) -> i16x16 { + let (a0, a1) = self.split_i16x16(a); + let (b0, b1) = self.split_i16x16(b); + self.combine_i16x8( + self.mul_q15_precise_i16x8(a0, b0), + self.mul_q15_precise_i16x8(a1, b1), + ) + } #[doc = "Widen every lane into two same-width vectors.\n\nThe first result contains the widened lower lanes and the second contains the widened upper lanes."] #[inline(always)] fn widen_i16x16(self, a: i16x16) -> (i32x8, i32x8) { @@ -9145,6 +9166,23 @@ pub trait Simd: let (a0, a1) = self.split_i16x32(a); self.combine_i16x16(self.neg_i16x16(a0), self.neg_i16x16(a1)) } + #[doc = "Multiply two vectors of Q15 fixed-point numbers element-wise, rounding to nearest.\n\nA Q15 number `x` represents `x / 32768`. Lane `i` of the result is `(a[i] * b[i] + (1 << 14)) >> 15`, evaluated without intermediate overflow. The result of `i16::MIN * i16::MIN` is implementation-defined; see `mul_q15_precise` for a version that saturates it to `i16::MAX`."] + #[inline(always)] + fn mul_q15_i16x32(self, a: i16x32, b: i16x32) -> i16x32 { + let (a0, a1) = self.split_i16x32(a); + let (b0, b1) = self.split_i16x32(b); + self.combine_i16x16(self.mul_q15_i16x16(a0, b0), self.mul_q15_i16x16(a1, b1)) + } + #[doc = "Multiply two vectors of Q15 fixed-point numbers element-wise, rounding to nearest and saturating.\n\nThis operation is the same as `mul_q15`, except that `i16::MIN * i16::MIN` saturates to `i16::MAX`. It can be slower than `mul_q15`."] + #[inline(always)] + fn mul_q15_precise_i16x32(self, a: i16x32, b: i16x32) -> i16x32 { + let (a0, a1) = self.split_i16x32(a); + let (b0, b1) = self.split_i16x32(b); + self.combine_i16x16( + self.mul_q15_precise_i16x16(a0, b0), + self.mul_q15_precise_i16x16(a1, b1), + ) + } #[doc = "Widen every lane into two same-width vectors.\n\nThe first result contains the widened lower lanes and the second contains the widened upper lanes."] #[inline(always)] fn widen_i16x32(self, a: i16x32) -> (i32x16, i32x16) { diff --git a/fearless_simd/src/generated/sse2.rs b/fearless_simd/src/generated/sse2.rs index 13701067a..24fde7dca 100644 --- a/fearless_simd/src/generated/sse2.rs +++ b/fearless_simd/src/generated/sse2.rs @@ -3407,6 +3407,36 @@ impl Simd for Sse2 { ]; } #[inline(always)] + fn mul_q15_i16x8(self, a: i16x8, b: i16x8) -> i16x8 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse2, a: i16x8, b: i16x8) -> i16x8 { + let (a, b) = (a.into(), b.into()); + let (low, high) = (_mm_mullo_epi16(a, b), _mm_mulhi_epi16(a, b)); + let truncated = _mm_or_si128(_mm_slli_epi16::<1>(high), _mm_srli_epi16::<15>(low)); + let round = _mm_and_si128(_mm_srli_epi16::<14>(low), _mm_set1_epi16(1)); + _mm_add_epi16(truncated, round).simd_into(token) + } + ); + kernel(self, a, b) + } + #[inline(always)] + fn mul_q15_precise_i16x8(self, a: i16x8, b: i16x8) -> i16x8 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse2, a: i16x8, b: i16x8) -> i16x8 { + let (a, b) = (a.into(), b.into()); + let (low, high) = (_mm_mullo_epi16(a, b), _mm_mulhi_epi16(a, b)); + let truncated = _mm_or_si128(_mm_slli_epi16::<1>(high), _mm_srli_epi16::<15>(low)); + let round = _mm_and_si128(_mm_srli_epi16::<14>(low), _mm_set1_epi16(1)); + let product = _mm_add_epi16(truncated, round); + _mm_xor_si128(product, _mm_cmpeq_epi16(product, _mm_set1_epi16(i16::MIN))) + .simd_into(token) + } + ); + kernel(self, a, b) + } + #[inline(always)] fn widen_i16x8(self, a: i16x8) -> (i32x4, i32x4) { crate::kernel!( #[inline(always)] diff --git a/fearless_simd/src/generated/sse4_2.rs b/fearless_simd/src/generated/sse4_2.rs index e22afd377..f68a12181 100644 --- a/fearless_simd/src/generated/sse4_2.rs +++ b/fearless_simd/src/generated/sse4_2.rs @@ -3144,6 +3144,28 @@ impl Simd for Sse4_2 { kernel(self, vectors, dest); } #[inline(always)] + fn mul_q15_i16x8(self, a: i16x8, b: i16x8) -> i16x8 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse4_2, a: i16x8, b: i16x8) -> i16x8 { + _mm_mulhrs_epi16(a.into(), b.into()).simd_into(token) + } + ); + kernel(self, a, b) + } + #[inline(always)] + fn mul_q15_precise_i16x8(self, a: i16x8, b: i16x8) -> i16x8 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Sse4_2, a: i16x8, b: i16x8) -> i16x8 { + let product = _mm_mulhrs_epi16(a.into(), b.into()); + _mm_xor_si128(product, _mm_cmpeq_epi16(product, _mm_set1_epi16(i16::MIN))) + .simd_into(token) + } + ); + kernel(self, a, b) + } + #[inline(always)] fn widen_i16x8(self, a: i16x8) -> (i32x4, i32x4) { crate::kernel!( #[inline(always)] diff --git a/fearless_simd/src/generated/wasm.rs b/fearless_simd/src/generated/wasm.rs index e843e4168..c29349c90 100644 --- a/fearless_simd/src/generated/wasm.rs +++ b/fearless_simd/src/generated/wasm.rs @@ -2081,6 +2081,14 @@ impl Simd for WasmSimd128 { crate::transmute::checked_transmute_store::(out3, &mut chunks[3]); } #[inline(always)] + fn mul_q15_i16x8(self, a: i16x8, b: i16x8) -> i16x8 { + i16x8_q15mulr_sat(a.into(), b.into()).simd_into(self) + } + #[inline(always)] + fn mul_q15_precise_i16x8(self, a: i16x8, b: i16x8) -> i16x8 { + i16x8_q15mulr_sat(a.into(), b.into()).simd_into(self) + } + #[inline(always)] fn widen_i16x8(self, a: i16x8) -> (i32x4, i32x4) { ( i32x4_extend_low_i16x8(a.into()).simd_into(self), diff --git a/fearless_simd/src/math.rs b/fearless_simd/src/math.rs index 655939ac1..dabb0971a 100644 --- a/fearless_simd/src/math.rs +++ b/fearless_simd/src/math.rs @@ -28,6 +28,19 @@ pub(crate) trait ScalarExt: Copy + PartialOrd { impl ScalarExt for T {} +pub(crate) trait I16Ext { + fn mul_q15(self, rhs: Self) -> Self; +} + +impl I16Ext for i16 { + #[inline(always)] + fn mul_q15(self, rhs: Self) -> Self { + let product = (i32::from(self) * i32::from(rhs) + (1 << 14)) >> 15; + // Only `i16::MIN * i16::MIN` is out of range, and only above it. + Self::try_from(product).unwrap_or(Self::MAX) + } +} + #[cfg(all(feature = "libm", not(feature = "std")))] pub(crate) trait FloatExt { fn floor(self) -> Self; diff --git a/fearless_simd_gen/src/arch/fallback.rs b/fearless_simd_gen/src/arch/fallback.rs index a2a637c90..38e8ef8fd 100644 --- a/fearless_simd_gen/src/arch/fallback.rs +++ b/fearless_simd_gen/src/arch/fallback.rs @@ -40,6 +40,7 @@ pub(crate) fn translate_op(op: &str, is_float: bool) -> Option<&'static str> { } "saturating_add" => "saturating_add", "saturating_sub" => "saturating_sub", + "mul_q15" | "mul_q15_precise" => "mul_q15", "sub" => { if is_float { "sub" diff --git a/fearless_simd_gen/src/arch/neon.rs b/fearless_simd_gen/src/arch/neon.rs index 0aee13331..f9773116f 100644 --- a/fearless_simd_gen/src/arch/neon.rs +++ b/fearless_simd_gen/src/arch/neon.rs @@ -18,6 +18,7 @@ fn translate_op(op: &str) -> Option<&'static str> { "add" => "vadd", "saturating_add" => "vqadd", "saturating_sub" => "vqsub", + "mul_q15" | "mul_q15_precise" => "vqrdmulh", "sub" => "vsub", "mul" => "vmul", "div" => "vdiv", diff --git a/fearless_simd_gen/src/arch/wasm.rs b/fearless_simd_gen/src/arch/wasm.rs index eb9281745..516b247aa 100644 --- a/fearless_simd_gen/src/arch/wasm.rs +++ b/fearless_simd_gen/src/arch/wasm.rs @@ -17,6 +17,7 @@ fn translate_op(op: &str) -> Option<&'static str> { "add" => "add", "saturating_add" => "add_sat", "saturating_sub" => "sub_sat", + "mul_q15" | "mul_q15_precise" => "q15mulr_sat", // TODO: Is wrapping sub same on WASM? "sub" => "sub", "mul" => "mul", diff --git a/fearless_simd_gen/src/mk_fallback.rs b/fearless_simd_gen/src/mk_fallback.rs index 88ab89a60..e88efd00c 100644 --- a/fearless_simd_gen/src/mk_fallback.rs +++ b/fearless_simd_gen/src/mk_fallback.rs @@ -92,6 +92,7 @@ impl Level for Fallback { quote! { use core::ops::*; use crate::math::ScalarExt as _; + use crate::math::I16Ext as _; #float_ext } @@ -874,6 +875,8 @@ fn rhs_reference(method: &str) -> bool { | "wrapping_add" | "saturating_add" | "saturating_sub" + | "mul_q15" + | "mul_q15_precise" | "wrapping_shl" | "wrapping_shr" ) diff --git a/fearless_simd_gen/src/mk_x86.rs b/fearless_simd_gen/src/mk_x86.rs index 5fb6eda2a..14e0479a8 100644 --- a/fearless_simd_gen/src/mk_x86.rs +++ b/fearless_simd_gen/src/mk_x86.rs @@ -3995,10 +3995,64 @@ impl X86 { } } + fn handle_mul_q15(&self, op: Op, vec_ty: &VecType, saturate: bool) -> TokenStream { + let bits = vec_ty.n_bits(); + let set1 = intrinsic_ident("set1", "epi16", bits); + let (setup, product) = if *self == Self::Sse2 { + // SSE2 lacks `pmulhrsw` (SSSE3), so the product is rebuilt from its two halves. + let mullo = intrinsic_ident("mullo", "epi16", bits); + let mulhi = intrinsic_ident("mulhi", "epi16", bits); + let slli = intrinsic_ident("slli", "epi16", bits); + let srli = intrinsic_ident("srli", "epi16", bits); + let add = intrinsic_ident("add", "epi16", bits); + let and = intrinsic_ident("and", coarse_type(vec_ty), bits); + let or = intrinsic_ident("or", coarse_type(vec_ty), bits); + ( + quote! { + let (a, b) = (a.into(), b.into()); + let (low, high) = (#mullo(a, b), #mulhi(a, b)); + let truncated = #or(#slli::<1>(high), #srli::<15>(low)); + let round = #and(#srli::<14>(low), #set1(1)); + }, + quote! { #add(truncated, round) }, + ) + } else { + let mulhrs = intrinsic_ident("mulhrs", "epi16", bits); + (quote! {}, quote! { #mulhrs(a.into(), b.into()) }) + }; + + self.kernel_method(op, vec_ty, |token| { + if !saturate { + return quote! { + #setup + #product.simd_into(#token) + }; + } + + // Both wrap `i16::MIN * i16::MIN` to `i16::MIN`, which no other product rounds to. + let saturated = if bits == 512 { + let cmpeq = intrinsic_ident("cmpeq", "epi16_mask", bits); + let mask_mov = intrinsic_ident("mask_mov", "epi16", bits); + quote! { #mask_mov(product, #cmpeq(product, #set1(i16::MIN)), #set1(i16::MAX)) } + } else { + let cmpeq = intrinsic_ident("cmpeq", "epi16", bits); + let xor = intrinsic_ident("xor", coarse_type(vec_ty), bits); + quote! { #xor(product, #cmpeq(product, #set1(i16::MIN))) } + }; + quote! { + #setup + let product = #product; + #saturated.simd_into(#token) + } + }) + } + pub(crate) fn handle_binary(&self, op: Op, method: &str, vec_ty: &VecType) -> TokenStream { match method { "saturating_add" => self.handle_saturating_add_sub(op, SaturatingOp::Add, vec_ty), "saturating_sub" => self.handle_saturating_add_sub(op, SaturatingOp::Sub, vec_ty), + "mul_q15" => self.handle_mul_q15(op, vec_ty, false), + "mul_q15_precise" => self.handle_mul_q15(op, vec_ty, true), "and" | "or" | "xor" if *self == Self::Avx512 && vec_ty.scalar == ScalarType::Mask => { let method_sig = op.simd_trait_method_sig(vec_ty); let lane_mask = avx512_mask_lane_bits(vec_ty); diff --git a/fearless_simd_gen/src/ops.rs b/fearless_simd_gen/src/ops.rs index 413c4c549..5280906ff 100644 --- a/fearless_simd_gen/src/ops.rs +++ b/fearless_simd_gen/src/ops.rs @@ -1895,6 +1895,26 @@ pub(crate) fn ops_for_type(ty: &VecType) -> Vec { )); } + if ty.scalar == ScalarType::Int && ty.scalar_bits == 16 { + ops.push(Op::new( + "mul_q15", + OpKind::AssociatedOnly, + OpSig::Binary, + "Multiply two vectors of Q15 fixed-point numbers element-wise, rounding to nearest.\n\n\ + A Q15 number `x` represents `x / 32768`. Lane `i` of the result is `(a[i] * b[i] + (1 << 14)) >> 15`, \ + evaluated without intermediate overflow. The result of `i16::MIN * i16::MIN` is implementation-defined; \ + see `mul_q15_precise` for a version that saturates it to `i16::MAX`.", + )); + ops.push(Op::new( + "mul_q15_precise", + OpKind::AssociatedOnly, + OpSig::Binary, + "Multiply two vectors of Q15 fixed-point numbers element-wise, rounding to nearest and saturating.\n\n\ + This operation is the same as `mul_q15`, except that `i16::MIN * i16::MIN` saturates to `i16::MAX`. \ + It can be slower than `mul_q15`.", + )); + } + if let Some(target_ty) = ty.widened() { let doc = if ty.scalar == ScalarType::Mask { "Widen each boolean lane into two masks of the same logical width, preserving its value.\n\nThe first result contains the lower lanes and the second contains the upper lanes. Masks constructed from integer lanes other than 0 or -1 have unspecified behavior." diff --git a/fearless_simd_tests/tests/harness/ops/mod.rs b/fearless_simd_tests/tests/harness/ops/mod.rs index 3278095f9..917b41c37 100644 --- a/fearless_simd_tests/tests/harness/ops/mod.rs +++ b/fearless_simd_tests/tests/harness/ops/mod.rs @@ -58,6 +58,7 @@ mod min_precise; mod mul; mod mul_add; mod mul_add_precise; +mod mul_q15; mod mul_sub; mod mul_sub_precise; mod narrow; diff --git a/fearless_simd_tests/tests/harness/ops/mul_q15.rs b/fearless_simd_tests/tests/harness/ops/mul_q15.rs new file mode 100644 index 000000000..a85a8c6ee --- /dev/null +++ b/fearless_simd_tests/tests/harness/ops/mul_q15.rs @@ -0,0 +1,133 @@ +// Copyright 2026 the Fearless_SIMD Authors +// SPDX-License-Identifier: Apache-2.0 OR MIT + +use fearless_simd::*; +use fearless_simd_dev_macros::simd_test; + +fn mul_q15(a: i16, b: i16) -> i16 { + ((i32::from(a) * i32::from(b) + (1 << 14)) >> 15).min(i32::from(i16::MAX)) as i16 +} + +fn check(a: [i16; N], b: [i16; N], fast: [i16; N], precise: [i16; N]) { + for i in 0..N { + let expected = mul_q15(a[i], b[i]); + assert_eq!(precise[i], expected, "mul_q15_precise({}, {})", a[i], b[i]); + + if (a[i], b[i]) != (i16::MIN, i16::MIN) { + assert_eq!(fast[i], expected, "mul_q15({}, {})", a[i], b[i]); + } + } +} + +/// Pairs of the extremes, of ties that round upwards from both signs, and of ordinary values. +const LHS: [i16; 32] = [ + i16::MIN, + i16::MIN, + i16::MAX, + i16::MAX, + i16::MIN, + 0, + 1, + -1, + 3, + -3, + 2, + -2, + 16384, + -16384, + 100, + -100, + 12345, + -12345, + 1, + -1, + 7, + 8191, + -8192, + 20000, + -20000, + 30000, + 255, + 16320, + -32767, + 32767, + 4, + -5, +]; +const RHS: [i16; 32] = [ + i16::MIN, + i16::MAX, + i16::MAX, + i16::MIN, + 1, + i16::MIN, + 16384, + 16384, + 16384, + 16384, + 8192, + 8192, + 16384, + 16384, + 16384, + 16384, + 23456, + 23456, + 16383, + 16383, + -7, + 4, + 4, + -30000, + -30000, + 30000, + 32767, + 4681, + -32767, + -1, + i16::MIN, + i16::MIN, +]; + +const SPLAT_RHS: [i16; 8] = [i16::MIN, i16::MAX, 16384, -16384, 1, -1, 12345, 0]; + +fn cases() -> impl Iterator { + let pairs = LHS.as_chunks::().0.iter().zip(RHS.as_chunks::().0); + let every_lhs = SPLAT_RHS.into_iter().flat_map(|b| { + (i16::MIN..=i16::MAX) + .step_by(N) + .map(move |start| (core::array::from_fn(|i| start + i as i16), [b; N])) + }); + + pairs.map(|(a, b)| (*a, *b)).chain(every_lhs) +} + +#[simd_test] +fn mul_q15_i16x8(simd: S) { + for (a, b) in cases::<8>() { + let (lhs, rhs) = (i16x8::from_slice(simd, &a), i16x8::from_slice(simd, &b)); + let fast = *simd.mul_q15_i16x8(lhs, rhs); + let precise = *simd.mul_q15_precise_i16x8(lhs, rhs); + check(a, b, fast, precise); + } +} + +#[simd_test] +fn mul_q15_i16x16(simd: S) { + for (a, b) in cases::<16>() { + let (lhs, rhs) = (i16x16::from_slice(simd, &a), i16x16::from_slice(simd, &b)); + let fast = *simd.mul_q15_i16x16(lhs, rhs); + let precise = *simd.mul_q15_precise_i16x16(lhs, rhs); + check(a, b, fast, precise); + } +} + +#[simd_test] +fn mul_q15_i16x32(simd: S) { + for (a, b) in cases::<32>() { + let (lhs, rhs) = (i16x32::from_slice(simd, &a), i16x32::from_slice(simd, &b)); + let fast = *simd.mul_q15_i16x32(lhs, rhs); + let precise = *simd.mul_q15_precise_i16x32(lhs, rhs); + check(a, b, fast, precise); + } +}