Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit e8b6bca

Browse files
Finish fixing up abs docs
1 parent 1f4e902 commit e8b6bca

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

crates/core_simd/src/math.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,37 @@ macro_rules! impl_int_arith {
7979
unsafe { crate::intrinsics::simd_saturating_sub(self, second) }
8080
}
8181

82+
/// Lanewise absolute value, implemented in Rust.
83+
/// Every lane becomes its absolute value.
84+
///
85+
/// # Examples
86+
/// ```
87+
/// # use core_simd::*;
88+
#[doc = concat!("# use core::", stringify!($n), "::{MIN, MAX};")]
89+
#[doc = concat!("let xs = ", stringify!($name), "::from_array([MIN, MIN +1, -5, 0]);")]
90+
#[doc = concat!("assert_eq!(xs.abs(), ", stringify!($name), "::from_array([MIN, MAX, 5, 0]));")]
91+
/// ```
92+
#[inline]
93+
pub fn abs(self) -> Self {
94+
let mut xs = self.to_array();
95+
for (i, x) in xs.clone().iter().enumerate() {
96+
xs[i] = x.wrapping_abs()
97+
}
98+
$name::from_array(xs)
99+
}
100+
82101
/// Lanewise saturating absolute value, implemented in Rust.
83102
/// As abs(), except the MIN value becomes MAX instead of itself.
84103
///
85104
/// # Examples
86105
/// ```
87106
/// # use core_simd::*;
88107
#[doc = concat!("# use core::", stringify!($n), "::{MIN, MAX};")]
89-
#[doc = concat!("let x = ", stringify!($name), "::from_array([MIN, -2, 0, 3]);")]
90-
/// let abs = x.saturating_abs();
91-
#[doc = concat!("assert_eq!(abs, ", stringify!($name), "::from_array([MAX, 2, 0, 3]));")]
108+
#[doc = concat!("let xs = ", stringify!($name), "::from_array([MIN, -2, 0, 3]);")]
109+
/// let unsat = xs.abs();
110+
/// let sat = xs.saturating_abs();
111+
#[doc = concat!("assert_eq!(unsat, ", stringify!($name), "::from_array([MIN, 2, 0, 3]));")]
112+
#[doc = concat!("assert_eq!(sat, ", stringify!($name), "::from_array([MAX, 2, 0, 3]));")]
92113
/// ```
93114
#[inline]
94115
pub fn saturating_abs(self) -> Self {

0 commit comments

Comments
 (0)