Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions fearless_simd/src/generated/avx2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3160,6 +3160,28 @@ impl Simd for Avx2 {
kernel(self, vectors, dest);
}
#[inline(always)]
fn mul_q15_i16x8(self, a: i16x8<Self>, b: i16x8<Self>) -> i16x8<Self> {
crate::kernel!(
#[inline(always)]
fn kernel(token: Avx2, a: i16x8<Avx2>, b: i16x8<Avx2>) -> i16x8<Avx2> {
_mm_mulhrs_epi16(a.into(), b.into()).simd_into(token)
}
);
kernel(self, a, b)
}
#[inline(always)]
fn mul_q15_precise_i16x8(self, a: i16x8<Self>, b: i16x8<Self>) -> i16x8<Self> {
crate::kernel!(
#[inline(always)]
fn kernel(token: Avx2, a: i16x8<Avx2>, b: i16x8<Avx2>) -> i16x8<Avx2> {
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<Self>) -> (i32x4<Self>, i32x4<Self>) {
crate::kernel!(
#[inline(always)]
Expand Down Expand Up @@ -10915,6 +10937,31 @@ impl Simd for Avx2 {
kernel(self, a)
}
#[inline(always)]
fn mul_q15_i16x16(self, a: i16x16<Self>, b: i16x16<Self>) -> i16x16<Self> {
crate::kernel!(
#[inline(always)]
fn kernel(token: Avx2, a: i16x16<Avx2>, b: i16x16<Avx2>) -> i16x16<Avx2> {
_mm256_mulhrs_epi16(a.into(), b.into()).simd_into(token)
}
);
kernel(self, a, b)
}
#[inline(always)]
fn mul_q15_precise_i16x16(self, a: i16x16<Self>, b: i16x16<Self>) -> i16x16<Self> {
crate::kernel!(
#[inline(always)]
fn kernel(token: Avx2, a: i16x16<Avx2>, b: i16x16<Avx2>) -> i16x16<Avx2> {
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<Self>) -> (i32x8<Self>, i32x8<Self>) {
crate::kernel!(
#[inline(always)]
Expand Down
73 changes: 73 additions & 0 deletions fearless_simd/src/generated/avx512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3419,6 +3419,28 @@ impl Simd for Avx512 {
kernel(self, vectors, dest);
}
#[inline(always)]
fn mul_q15_i16x8(self, a: i16x8<Self>, b: i16x8<Self>) -> i16x8<Self> {
crate::kernel!(
#[inline(always)]
fn kernel(token: Avx512, a: i16x8<Avx512>, b: i16x8<Avx512>) -> i16x8<Avx512> {
_mm_mulhrs_epi16(a.into(), b.into()).simd_into(token)
}
);
kernel(self, a, b)
}
#[inline(always)]
fn mul_q15_precise_i16x8(self, a: i16x8<Self>, b: i16x8<Self>) -> i16x8<Self> {
crate::kernel!(
#[inline(always)]
fn kernel(token: Avx512, a: i16x8<Avx512>, b: i16x8<Avx512>) -> i16x8<Avx512> {
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<Self>) -> (i32x4<Self>, i32x4<Self>) {
crate::kernel!(
#[inline(always)]
Expand Down Expand Up @@ -10969,6 +10991,31 @@ impl Simd for Avx512 {
kernel(self, a)
}
#[inline(always)]
fn mul_q15_i16x16(self, a: i16x16<Self>, b: i16x16<Self>) -> i16x16<Self> {
crate::kernel!(
#[inline(always)]
fn kernel(token: Avx512, a: i16x16<Avx512>, b: i16x16<Avx512>) -> i16x16<Avx512> {
_mm256_mulhrs_epi16(a.into(), b.into()).simd_into(token)
}
);
kernel(self, a, b)
}
#[inline(always)]
fn mul_q15_precise_i16x16(self, a: i16x16<Self>, b: i16x16<Self>) -> i16x16<Self> {
crate::kernel!(
#[inline(always)]
fn kernel(token: Avx512, a: i16x16<Avx512>, b: i16x16<Avx512>) -> i16x16<Avx512> {
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<Self>) -> (i32x8<Self>, i32x8<Self>) {
crate::kernel!(
#[inline(always)]
Expand Down Expand Up @@ -18498,6 +18545,32 @@ impl Simd for Avx512 {
kernel(self, a)
}
#[inline(always)]
fn mul_q15_i16x32(self, a: i16x32<Self>, b: i16x32<Self>) -> i16x32<Self> {
crate::kernel!(
#[inline(always)]
fn kernel(token: Avx512, a: i16x32<Avx512>, b: i16x32<Avx512>) -> i16x32<Avx512> {
_mm512_mulhrs_epi16(a.into(), b.into()).simd_into(token)
}
);
kernel(self, a, b)
}
#[inline(always)]
fn mul_q15_precise_i16x32(self, a: i16x32<Self>, b: i16x32<Self>) -> i16x32<Self> {
crate::kernel!(
#[inline(always)]
fn kernel(token: Avx512, a: i16x32<Avx512>, b: i16x32<Avx512>) -> i16x32<Avx512> {
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<Self>) -> (i32x16<Self>, i32x16<Self>) {
crate::kernel!(
#[inline(always)]
Expand Down
29 changes: 29 additions & 0 deletions fearless_simd/src/generated/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -3939,6 +3940,34 @@ impl Simd for Fallback {
];
}
#[inline(always)]
fn mul_q15_i16x8(self, a: i16x8<Self>, b: i16x8<Self>) -> i16x8<Self> {
[
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<Self>, b: i16x8<Self>) -> i16x8<Self> {
[
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<Self>) -> (i32x4<Self>, i32x4<Self>) {
(
[
Expand Down
20 changes: 20 additions & 0 deletions fearless_simd/src/generated/neon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2655,6 +2655,26 @@ impl Simd for Neon {
}
}
#[inline(always)]
fn mul_q15_i16x8(self, a: i16x8<Self>, b: i16x8<Self>) -> i16x8<Self> {
crate::kernel!(
#[inline(always)]
fn kernel(token: Neon, a: i16x8<Neon>, b: i16x8<Neon>) -> i16x8<Neon> {
vqrdmulhq_s16(a.into(), b.into()).simd_into(token)
}
);
kernel(self, a, b)
}
#[inline(always)]
fn mul_q15_precise_i16x8(self, a: i16x8<Self>, b: i16x8<Self>) -> i16x8<Self> {
crate::kernel!(
#[inline(always)]
fn kernel(token: Neon, a: i16x8<Neon>, b: i16x8<Neon>) -> i16x8<Neon> {
vqrdmulhq_s16(a.into(), b.into()).simd_into(token)
}
);
kernel(self, a, b)
}
#[inline(always)]
fn widen_i16x8(self, a: i16x8<Self>) -> (i32x4<Self>, i32x4<Self>) {
crate::kernel!(
#[inline(always)]
Expand Down
38 changes: 38 additions & 0 deletions fearless_simd/src/generated/simd_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,10 @@ pub trait Simd:
vectors: [i16x8<Self>; 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<Self>, b: i16x8<Self>) -> i16x8<Self>;
#[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<Self>, b: i16x8<Self>) -> i16x8<Self>;
#[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<Self>) -> (i32x4<Self>, i32x4<Self>);
#[doc = "Multiply corresponding integer lanes exactly into twice-wide lanes. The first result contains the lower lanes and the second contains the upper lanes."]
Expand Down Expand Up @@ -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<Self>, b: i16x16<Self>) -> i16x16<Self> {
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<Self>, b: i16x16<Self>) -> i16x16<Self> {
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<Self>) -> (i32x8<Self>, i32x8<Self>) {
Expand Down Expand Up @@ -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<Self>, b: i16x32<Self>) -> i16x32<Self> {
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<Self>, b: i16x32<Self>) -> i16x32<Self> {
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<Self>) -> (i32x16<Self>, i32x16<Self>) {
Expand Down
30 changes: 30 additions & 0 deletions fearless_simd/src/generated/sse2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3407,6 +3407,36 @@ impl Simd for Sse2 {
];
}
#[inline(always)]
fn mul_q15_i16x8(self, a: i16x8<Self>, b: i16x8<Self>) -> i16x8<Self> {
crate::kernel!(
#[inline(always)]
fn kernel(token: Sse2, a: i16x8<Sse2>, b: i16x8<Sse2>) -> i16x8<Sse2> {
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<Self>, b: i16x8<Self>) -> i16x8<Self> {
crate::kernel!(
#[inline(always)]
fn kernel(token: Sse2, a: i16x8<Sse2>, b: i16x8<Sse2>) -> i16x8<Sse2> {
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<Self>) -> (i32x4<Self>, i32x4<Self>) {
crate::kernel!(
#[inline(always)]
Expand Down
22 changes: 22 additions & 0 deletions fearless_simd/src/generated/sse4_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3144,6 +3144,28 @@ impl Simd for Sse4_2 {
kernel(self, vectors, dest);
}
#[inline(always)]
fn mul_q15_i16x8(self, a: i16x8<Self>, b: i16x8<Self>) -> i16x8<Self> {
crate::kernel!(
#[inline(always)]
fn kernel(token: Sse4_2, a: i16x8<Sse4_2>, b: i16x8<Sse4_2>) -> i16x8<Sse4_2> {
_mm_mulhrs_epi16(a.into(), b.into()).simd_into(token)
}
);
kernel(self, a, b)
}
#[inline(always)]
fn mul_q15_precise_i16x8(self, a: i16x8<Self>, b: i16x8<Self>) -> i16x8<Self> {
crate::kernel!(
#[inline(always)]
fn kernel(token: Sse4_2, a: i16x8<Sse4_2>, b: i16x8<Sse4_2>) -> i16x8<Sse4_2> {
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<Self>) -> (i32x4<Self>, i32x4<Self>) {
crate::kernel!(
#[inline(always)]
Expand Down
8 changes: 8 additions & 0 deletions fearless_simd/src/generated/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2081,6 +2081,14 @@ impl Simd for WasmSimd128 {
crate::transmute::checked_transmute_store::<v128, [i16; 8usize]>(out3, &mut chunks[3]);
}
#[inline(always)]
fn mul_q15_i16x8(self, a: i16x8<Self>, b: i16x8<Self>) -> i16x8<Self> {
i16x8_q15mulr_sat(a.into(), b.into()).simd_into(self)
}
#[inline(always)]
fn mul_q15_precise_i16x8(self, a: i16x8<Self>, b: i16x8<Self>) -> i16x8<Self> {
i16x8_q15mulr_sat(a.into(), b.into()).simd_into(self)
}
#[inline(always)]
fn widen_i16x8(self, a: i16x8<Self>) -> (i32x4<Self>, i32x4<Self>) {
(
i32x4_extend_low_i16x8(a.into()).simd_into(self),
Expand Down
13 changes: 13 additions & 0 deletions fearless_simd/src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ pub(crate) trait ScalarExt: Copy + PartialOrd {

impl<T: Copy + PartialOrd> 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;
Expand Down
1 change: 1 addition & 0 deletions fearless_simd_gen/src/arch/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading
Loading