Add wrapping negation
This commit is contained in:
parent
7c7dbe0c50
commit
5c97c0db24
@ -16,6 +16,12 @@ pub trait SimdUint: Copy + Sealed {
|
|||||||
#[must_use]
|
#[must_use]
|
||||||
fn cast<T: SimdCast>(self) -> Self::Cast<T>;
|
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.
|
/// Lanewise saturating add.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
@ -74,7 +80,7 @@ pub trait SimdUint: Copy + Sealed {
|
|||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! impl_trait {
|
macro_rules! impl_trait {
|
||||||
{ $($ty:ty),* } => {
|
{ $($ty:ident ($signed:ident)),* } => {
|
||||||
$(
|
$(
|
||||||
impl<const LANES: usize> Sealed for Simd<$ty, LANES>
|
impl<const LANES: usize> Sealed for Simd<$ty, LANES>
|
||||||
where
|
where
|
||||||
@ -95,6 +101,12 @@ fn cast<T: SimdCast>(self) -> Self::Cast<T> {
|
|||||||
unsafe { intrinsics::simd_as(self) }
|
unsafe { intrinsics::simd_as(self) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn wrapping_neg(self) -> Self {
|
||||||
|
use crate::simd::SimdInt;
|
||||||
|
(-self.cast::<$signed>()).cast()
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn saturating_add(self, second: Self) -> Self {
|
fn saturating_add(self, second: Self) -> Self {
|
||||||
// Safety: `self` is a vector
|
// 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) }
|
||||||
|
@ -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, 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, Sub::sub, SubAssign::sub_assign, Scalar::wrapping_sub);
|
||||||
impl_binary_op_test!(Scalar, Mul::mul, MulAssign::mul_assign, Scalar::wrapping_mul);
|
impl_binary_op_test!(Scalar, Mul::mul, MulAssign::mul_assign, Scalar::wrapping_mul);
|
||||||
|
Loading…
Reference in New Issue
Block a user