about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJubilee Young <workingjubilee@gmail.com>2021-03-28 18:44:16 -0700
committerJubilee Young <workingjubilee@gmail.com>2021-03-30 17:42:05 -0700
commit4a6b4c0a2e245e3484196f2533600c29a8b8ae1a (patch)
tree00c2e9770a46baec9084ed51e3cb15920d4d4b7a
parentfa77b196c8f4cca27009c334f09466b80aca2f8a (diff)
downloadrust-4a6b4c0a2e245e3484196f2533600c29a8b8ae1a.tar.gz
rust-4a6b4c0a2e245e3484196f2533600c29a8b8ae1a.zip
Introduce saturating math
-rw-r--r--crates/core_simd/src/intrinsics.rs6
-rw-r--r--crates/core_simd/src/lib.rs3
-rw-r--r--crates/core_simd/src/math.rs88
3 files changed, 97 insertions, 0 deletions
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<T, U>(x: T, y: T, idx: [u32; 16]) -> U;
     pub(crate) fn simd_shuffle32<T, U>(x: T, y: T, idx: [u32; 32]) -> U;
     pub(crate) fn simd_shuffle64<T, U>(x: T, y: T, idx: [u32; 64]) -> U;
+
+    // {s,u}add.sat
+    pub(crate) fn simd_saturating_add<T>(x: T, y: T) -> T;
+
+    // {s,u}sub.sat
+    pub(crate) fn simd_saturating_sub<T>(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<const LANES: usize> $name<LANES> 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<const LANES: usize> $name<LANES> 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) }