Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add abs_diff function to SimdInt and SimdUint traits #429

Merged
merged 1 commit into from
Aug 25, 2024
Merged
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
27 changes: 25 additions & 2 deletions crates/core_simd/src/simd/num/int.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::sealed::Sealed;
use crate::simd::{
cmp::SimdPartialOrd, num::SimdUint, LaneCount, Mask, Simd, SimdCast, SimdElement,
cmp::SimdOrd, cmp::SimdPartialOrd, num::SimdUint, LaneCount, Mask, Simd, SimdCast, SimdElement,
SupportedLaneCount,
};

Expand Down Expand Up @@ -70,11 +70,27 @@ pub trait SimdInt: Copy + Sealed {
/// # #[cfg(not(feature = "as_crate"))] use core::simd;
/// # use simd::prelude::*;
/// use core::i32::{MIN, MAX};
/// let xs = Simd::from_array([MIN, MIN +1, -5, 0]);
/// let xs = Simd::from_array([MIN, MIN + 1, -5, 0]);
/// assert_eq!(xs.abs(), Simd::from_array([MIN, MAX, 5, 0]));
/// ```
fn abs(self) -> Self;

/// Lanewise absolute difference.
/// Every element becomes the absolute difference of `self` and `second`.
///
/// # Examples
/// ```
/// # #![feature(portable_simd)]
/// # #[cfg(feature = "as_crate")] use core_simd::simd;
/// # #[cfg(not(feature = "as_crate"))] use core::simd;
/// # use simd::prelude::*;
/// use core::i32::{MIN, MAX};
/// let a = Simd::from_array([MIN, MAX, 100, -100]);
/// let b = Simd::from_array([MAX, MIN, -80, -120]);
/// assert_eq!(a.abs_diff(b), Simd::from_array([u32::MAX, u32::MAX, 180, 20]));
/// ```
fn abs_diff(self, second: Self) -> Self::Unsigned;

/// Lanewise saturating absolute value, implemented in Rust.
/// As abs(), except the MIN value becomes MAX instead of itself.
///
Expand Down Expand Up @@ -259,6 +275,13 @@ macro_rules! impl_trait {
(self^m) - m
}

#[inline]
fn abs_diff(self, second: Self) -> Self::Unsigned {
let max = self.simd_max(second);
let min = self.simd_min(second);
(max - min).cast()
}

#[inline]
fn saturating_abs(self) -> Self {
// arith shift for -1 or 0 mask based on sign bit, giving 2s complement
Expand Down
25 changes: 24 additions & 1 deletion crates/core_simd/src/simd/num/uint.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::sealed::Sealed;
use crate::simd::{LaneCount, Simd, SimdCast, SimdElement, SupportedLaneCount};
use crate::simd::{cmp::SimdOrd, LaneCount, Simd, SimdCast, SimdElement, SupportedLaneCount};

/// Operations on SIMD vectors of unsigned integers.
pub trait SimdUint: Copy + Sealed {
Expand Down Expand Up @@ -57,6 +57,22 @@ pub trait SimdUint: Copy + Sealed {
/// assert_eq!(sat, Simd::splat(0));
fn saturating_sub(self, second: Self) -> Self;

/// Lanewise absolute difference.
/// Every element becomes the absolute difference of `self` and `second`.
///
/// # Examples
/// ```
/// # #![feature(portable_simd)]
/// # #[cfg(feature = "as_crate")] use core_simd::simd;
/// # #[cfg(not(feature = "as_crate"))] use core::simd;
/// # use simd::prelude::*;
/// use core::u32::MAX;
/// let a = Simd::from_array([0, MAX, 100, 20]);
/// let b = Simd::from_array([MAX, 0, 80, 200]);
/// assert_eq!(a.abs_diff(b), Simd::from_array([MAX, MAX, 20, 180]));
/// ```
fn abs_diff(self, second: Self) -> Self;

/// Returns the sum of the elements of the vector, with wrapping addition.
fn reduce_sum(self) -> Self::Scalar;

Expand Down Expand Up @@ -138,6 +154,13 @@ macro_rules! impl_trait {
unsafe { core::intrinsics::simd::simd_saturating_sub(self, second) }
}

#[inline]
fn abs_diff(self, second: Self) -> Self {
let max = self.simd_max(second);
let min = self.simd_min(second);
max - min
}

#[inline]
fn reduce_sum(self) -> Self::Scalar {
// Safety: `self` is an integer vector
Expand Down
16 changes: 16 additions & 0 deletions crates/core_simd/tests/ops_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,14 @@ macro_rules! impl_signed_tests {
assert_eq!(a % b, Vector::<LANES>::splat(0));
}

fn abs_diff<const LANES: usize>() {
test_helpers::test_binary_elementwise(
&Vector::<LANES>::abs_diff,
&Scalar::abs_diff,
&|_, _| true,
)
}

fn simd_min<const LANES: usize>() {
use core_simd::simd::cmp::SimdOrd;
let a = Vector::<LANES>::splat(Scalar::MIN);
Expand Down Expand Up @@ -419,6 +427,14 @@ macro_rules! impl_unsigned_tests {
&|_| true,
);
}

fn abs_diff<const LANES: usize>() {
test_helpers::test_binary_elementwise(
&Vector::<LANES>::abs_diff,
&Scalar::abs_diff,
&|_, _| true,
)
}
}

impl_binary_op_test!(Scalar, Add::add, AddAssign::add_assign, Scalar::wrapping_add);
Expand Down
Loading