From e8b6bca694098e4865d602ef438458ea52335e6a Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Tue, 13 Apr 2021 20:19:53 -0700 Subject: [PATCH] Finish fixing up abs docs --- crates/core_simd/src/math.rs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/crates/core_simd/src/math.rs b/crates/core_simd/src/math.rs index e987ec4e9c9..baf92ee097b 100644 --- a/crates/core_simd/src/math.rs +++ b/crates/core_simd/src/math.rs @@ -79,6 +79,25 @@ macro_rules! impl_int_arith { unsafe { crate::intrinsics::simd_saturating_sub(self, second) } } + /// Lanewise absolute value, implemented in Rust. + /// Every lane becomes its absolute value. + /// + /// # Examples + /// ``` + /// # use core_simd::*; + #[doc = concat!("# use core::", stringify!($n), "::{MIN, MAX};")] + #[doc = concat!("let xs = ", stringify!($name), "::from_array([MIN, MIN +1, -5, 0]);")] + #[doc = concat!("assert_eq!(xs.abs(), ", stringify!($name), "::from_array([MIN, MAX, 5, 0]));")] + /// ``` + #[inline] + pub fn abs(self) -> Self { + let mut xs = self.to_array(); + for (i, x) in xs.clone().iter().enumerate() { + xs[i] = x.wrapping_abs() + } + $name::from_array(xs) + } + /// Lanewise saturating absolute value, implemented in Rust. /// As abs(), except the MIN value becomes MAX instead of itself. /// @@ -86,9 +105,11 @@ macro_rules! impl_int_arith { /// ``` /// # use core_simd::*; #[doc = concat!("# use core::", stringify!($n), "::{MIN, MAX};")] - #[doc = concat!("let x = ", stringify!($name), "::from_array([MIN, -2, 0, 3]);")] - /// let abs = x.saturating_abs(); - #[doc = concat!("assert_eq!(abs, ", stringify!($name), "::from_array([MAX, 2, 0, 3]));")] + #[doc = concat!("let xs = ", stringify!($name), "::from_array([MIN, -2, 0, 3]);")] + /// let unsat = xs.abs(); + /// let sat = xs.saturating_abs(); + #[doc = concat!("assert_eq!(unsat, ", stringify!($name), "::from_array([MIN, 2, 0, 3]));")] + #[doc = concat!("assert_eq!(sat, ", stringify!($name), "::from_array([MAX, 2, 0, 3]));")] /// ``` #[inline] pub fn saturating_abs(self) -> Self {