Add wrapping negation

This commit is contained in:
Caleb Zulawski 2023-07-27 16:26:22 -04:00
parent 7c7dbe0c50
commit 5c97c0db24
2 changed files with 24 additions and 2 deletions

View File

@ -16,6 +16,12 @@ pub trait SimdUint: Copy + Sealed {
#[must_use]
fn cast<T: SimdCast>(self) -> Self::Cast<T>;
/// Wrapping negation.
///
/// Like [`u32::wrapping_neg`], all applications of this function will wrap, with the exception
/// of `-0`.
fn wrapping_neg(self) -> Self;
/// Lanewise saturating add.
///
/// # Examples
@ -74,7 +80,7 @@ pub trait SimdUint: Copy + Sealed {
}
macro_rules! impl_trait {
{ $($ty:ty),* } => {
{ $($ty:ident ($signed:ident)),* } => {
$(
impl<const LANES: usize> Sealed for Simd<$ty, LANES>
where
@ -95,6 +101,12 @@ fn cast<T: SimdCast>(self) -> Self::Cast<T> {
unsafe { intrinsics::simd_as(self) }
}
#[inline]
fn wrapping_neg(self) -> Self {
use crate::simd::SimdInt;
(-self.cast::<$signed>()).cast()
}
#[inline]
fn saturating_add(self, second: Self) -> Self {
// Safety: `self` is a vector
@ -153,4 +165,4 @@ fn reduce_xor(self) -> Self::Scalar {
}
}
impl_trait! { u8, u16, u32, u64, usize }
impl_trait! { u8 (i8), u16 (i16), u32 (i32), u64 (i64), usize (isize) }

View File

@ -327,6 +327,16 @@ fn rem_zero_panic<const LANES: usize>() {
}
}
test_helpers::test_lanes! {
fn wrapping_neg<const LANES: usize>() {
test_helpers::test_unary_elementwise(
&Vector::<LANES>::wrapping_neg,
&Scalar::wrapping_neg,
&|_| true,
);
}
}
impl_binary_op_test!(Scalar, Add::add, AddAssign::add_assign, Scalar::wrapping_add);
impl_binary_op_test!(Scalar, Sub::sub, SubAssign::sub_assign, Scalar::wrapping_sub);
impl_binary_op_test!(Scalar, Mul::mul, MulAssign::mul_assign, Scalar::wrapping_mul);