about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2022-03-12 18:32:28 -0500
committerGitHub <noreply@github.com>2022-03-12 15:32:28 -0800
commit49043f4434b02d0a8a68c87d7672cc2e944a3deb (patch)
treebddaac1807f8631cf9fee265f63eada58522ecef
parentb6ee5293f43a6e9ec1c0d977a4037462bd4d02ca (diff)
rust-lang/portable-simd#262: also implement clamp for integer vectors
* add test from issue rust-lang/portable-simd#253
-rw-r--r--crates/core_simd/src/comparisons.rs44
-rw-r--r--crates/core_simd/tests/i16_ops.rs12
-rw-r--r--crates/core_simd/tests/ops_macros.rs12
3 files changed, 55 insertions, 13 deletions
diff --git a/crates/core_simd/src/comparisons.rs b/crates/core_simd/src/comparisons.rs
index 88270a9b7e9..7b0d0a6864b 100644
--- a/crates/core_simd/src/comparisons.rs
+++ b/crates/core_simd/src/comparisons.rs
@@ -67,36 +67,54 @@ where
     }
 }
 
-macro_rules! impl_min_max_vector {
+macro_rules! impl_ord_methods_vector {
     { $type:ty } => {
         impl<const LANES: usize> Simd<$type, LANES>
         where
             LaneCount<LANES>: SupportedLaneCount,
         {
-            /// Returns the lane-wise minimum with other
+            /// Returns the lane-wise minimum with `other`.
             #[must_use = "method returns a new vector and does not mutate the original value"]
             #[inline]
             pub fn min(self, other: Self) -> Self {
                 self.lanes_gt(other).select(other, self)
             }
 
-            /// Returns the lane-wise maximum with other
+            /// Returns the lane-wise maximum with `other`.
             #[must_use = "method returns a new vector and does not mutate the original value"]
             #[inline]
             pub fn max(self, other: Self) -> Self {
                 self.lanes_lt(other).select(other, self)
             }
+
+            /// Restrict each lane to a certain interval.
+            ///
+            /// For each lane, returns `max` if `self` is greater than `max`, and `min` if `self` is
+            /// less than `min`. Otherwise returns `self`.
+            ///
+            /// # Panics
+            ///
+            /// Panics if `min > max` on any lane.
+            #[must_use = "method returns a new vector and does not mutate the original value"]
+            #[inline]
+            pub fn clamp(self, min: Self, max: Self) -> Self {
+                assert!(
+                    min.lanes_le(max).all(),
+                    "each lane in `min` must be less than or equal to the corresponding lane in `max`",
+                );
+                self.max(min).min(max)
+            }
         }
     }
 }
 
-impl_min_max_vector!(i8);
-impl_min_max_vector!(i16);
-impl_min_max_vector!(i32);
-impl_min_max_vector!(i64);
-impl_min_max_vector!(isize);
-impl_min_max_vector!(u8);
-impl_min_max_vector!(u16);
-impl_min_max_vector!(u32);
-impl_min_max_vector!(u64);
-impl_min_max_vector!(usize);
+impl_ord_methods_vector!(i8);
+impl_ord_methods_vector!(i16);
+impl_ord_methods_vector!(i32);
+impl_ord_methods_vector!(i64);
+impl_ord_methods_vector!(isize);
+impl_ord_methods_vector!(u8);
+impl_ord_methods_vector!(u16);
+impl_ord_methods_vector!(u32);
+impl_ord_methods_vector!(u64);
+impl_ord_methods_vector!(usize);
diff --git a/crates/core_simd/tests/i16_ops.rs b/crates/core_simd/tests/i16_ops.rs
index cd6cadc2d5e..171e5b472fa 100644
--- a/crates/core_simd/tests/i16_ops.rs
+++ b/crates/core_simd/tests/i16_ops.rs
@@ -18,3 +18,15 @@ fn min_is_not_lexicographic() {
     let b = i16x2::from_array([12, -4]);
     assert_eq!(a.min(b), i16x2::from_array([10, -4]));
 }
+
+#[test]
+fn clamp_is_not_lexicographic() {
+    let a = i16x2::splat(10);
+    let lo = i16x2::from_array([-12, -4]);
+    let up = i16x2::from_array([-4, 12]);
+    assert_eq!(a.clamp(lo, up), i16x2::from_array([-4, 10]));
+
+    let x = i16x2::from_array([1, 10]);
+    let y = x.clamp(i16x2::splat(0), i16x2::splat(9));
+    assert_eq!(y, i16x2::from_array([1, 9]));
+}
diff --git a/crates/core_simd/tests/ops_macros.rs b/crates/core_simd/tests/ops_macros.rs
index 96da8c1b8dc..bea02750ef2 100644
--- a/crates/core_simd/tests/ops_macros.rs
+++ b/crates/core_simd/tests/ops_macros.rs
@@ -239,6 +239,18 @@ macro_rules! impl_signed_tests {
                     let b = Vector::<LANES>::splat(0);
                     assert_eq!(a.max(b), a);
                 }
+
+                fn clamp<const LANES: usize>() {
+                    let min = Vector::<LANES>::splat(Scalar::MIN);
+                    let max = Vector::<LANES>::splat(Scalar::MAX);
+                    let zero = Vector::<LANES>::splat(0);
+                    let one = Vector::<LANES>::splat(1);
+                    let negone = Vector::<LANES>::splat(-1);
+                    assert_eq!(zero.clamp(min, max), zero);
+                    assert_eq!(zero.clamp(min, one), zero);
+                    assert_eq!(zero.clamp(one, max), one);
+                    assert_eq!(zero.clamp(min, negone), negone);
+                }
             }
 
             test_helpers::test_lanes_panic! {