diff --git a/crates/core_simd/src/intrinsics.rs b/crates/core_simd/src/intrinsics.rs index 213ebff3df4..93c97cfed8e 100644 --- a/crates/core_simd/src/intrinsics.rs +++ b/crates/core_simd/src/intrinsics.rs @@ -62,4 +62,10 @@ extern "platform-intrinsic" { pub(crate) fn simd_shuffle16(x: T, y: T, idx: [u32; 16]) -> U; pub(crate) fn simd_shuffle32(x: T, y: T, idx: [u32; 32]) -> U; pub(crate) fn simd_shuffle64(x: T, y: T, idx: [u32; 64]) -> U; + + // {s,u}add.sat + pub(crate) fn simd_saturating_add(x: T, y: T) -> T; + + // {s,u}sub.sat + pub(crate) fn simd_saturating_sub(x: T, y: T) -> T; } diff --git a/crates/core_simd/src/lib.rs b/crates/core_simd/src/lib.rs index 489996ae15e..8ff08223598 100644 --- a/crates/core_simd/src/lib.rs +++ b/crates/core_simd/src/lib.rs @@ -1,6 +1,7 @@ #![no_std] #![allow(incomplete_features)] #![feature(repr_simd, platform_intrinsics, simd_ffi, const_generics)] +#![feature(extended_key_value_attributes)] #![warn(missing_docs)] //! Portable SIMD module. @@ -16,6 +17,8 @@ mod intrinsics; mod ops; mod round; +mod math; + mod lanes_at_most_64; pub use lanes_at_most_64::LanesAtMost64; diff --git a/crates/core_simd/src/math.rs b/crates/core_simd/src/math.rs new file mode 100644 index 00000000000..6fabf35e3da --- /dev/null +++ b/crates/core_simd/src/math.rs @@ -0,0 +1,88 @@ +macro_rules! impl_uint_arith { + ($(($name:ident, $n:ty)),+) => { + $( impl $name where Self: crate::LanesAtMost64 { + + /// Lanewise saturating add. + /// + /// # Examples + /// ``` + /// # use core_simd::*; + #[doc = concat!("# use core::", stringify!($n), "::MAX;")] + #[doc = concat!("let x = ", stringify!($name), "::from_array([2, 1, 0, MAX]);")] + #[doc = concat!("let max = ", stringify!($name), "::splat(MAX);")] + /// let unsat = x + max; + /// let sat = x.saturating_add(max); + /// assert_eq!(x - 1, unsat); + /// assert_eq!(sat, max); + /// ``` + #[inline] + pub fn saturating_add(self, second: Self) -> Self { + unsafe { crate::intrinsics::simd_saturating_add(self, second) } + } + + /// Lanewise saturating subtract. + /// + /// # Examples + /// ``` + /// # use core_simd::*; + #[doc = concat!("# use core::", stringify!($n), "::MAX;")] + #[doc = concat!("let x = ", stringify!($name), "::from_array([2, 1, 0, MAX]);")] + #[doc = concat!("let max = ", stringify!($name), "::splat(MAX);")] + /// let unsat = x - max; + /// let sat = x.saturating_sub(max); + /// assert_eq!(unsat, x + 1); + #[doc = concat!("assert_eq!(sat, ", stringify!($name), "::splat(0));")] + #[inline] + pub fn saturating_sub(self, second: Self) -> Self { + unsafe { crate::intrinsics::simd_saturating_sub(self, second) } + } + })+ + } +} + +macro_rules! impl_int_arith { + ($(($name:ident, $n:ty)),+) => { + $( impl $name where Self: crate::LanesAtMost64 { + + /// Lanewise saturating add. + /// + /// # Examples + /// ``` + /// # use core_simd::*; + #[doc = concat!("# use core::", stringify!($n), "::{MIN, MAX};")] + #[doc = concat!("let x = ", stringify!($name), "::from_array([MIN, 0, 1, MAX]);")] + #[doc = concat!("let max = ", stringify!($name), "::splat(MAX);")] + /// let unsat = x + max; + /// let sat = x.saturating_add(max); + #[doc = concat!("assert_eq!(unsat, ", stringify!($name), "::from_array([-1, MAX, MIN, -2]));")] + #[doc = concat!("assert_eq!(sat, ", stringify!($name), "::from_array([-1, MAX, MAX, MAX]));")] + /// ``` + #[inline] + pub fn saturating_add(self, second: Self) -> Self { + unsafe { crate::intrinsics::simd_saturating_add(self, second) } + } + + /// Lanewise saturating subtract. + /// + /// # Examples + /// ``` + /// # use core_simd::*; + #[doc = concat!("# use core::", stringify!($n), "::{MIN, MAX};")] + #[doc = concat!("let x = ", stringify!($name), "::from_array([MIN, -2, -1, MAX]);")] + #[doc = concat!("let max = ", stringify!($name), "::splat(MAX);")] + /// let unsat = x - max; + /// let sat = x.saturating_sub(max); + #[doc = concat!("assert_eq!(unsat, ", stringify!($name), "::from_array([1, MAX, MIN, 0]));")] + #[doc = concat!("assert_eq!(sat, ", stringify!($name), "::from_array([MIN, MIN, MIN, 0]));")] + #[inline] + pub fn saturating_sub(self, second: Self) -> Self { + unsafe { crate::intrinsics::simd_saturating_sub(self, second) } + } + })+ + } +} + +use crate::vector::*; + +impl_uint_arith! { (SimdU8, u8), (SimdU16, u16), (SimdU32, u32), (SimdU64, u64), (SimdUsize, usize) } +impl_int_arith! { (SimdI8, i8), (SimdI16, i16), (SimdI32, i32), (SimdI64, i64), (SimdIsize, isize) }