Merge pull request #361 from rust-lang/wrapping-negation

Add wrapping negation for unsigned integers
This commit is contained in:
Caleb Zulawski 2023-09-10 16:59:16 -04:00 committed by GitHub
commit f2ac32f21f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 @@ macro_rules! impl_trait {
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 @@ macro_rules! impl_trait {
}
}
impl_trait! { u8, u16, u32, u64, usize }
impl_trait! { u8 (i8), u16 (i16), u32 (i32), u64 (i64), usize (isize) }

View File

@ -361,6 +361,16 @@ macro_rules! impl_unsigned_tests {
}
}
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);