11macro_rules! impl_uint_arith {
2- ( $( ( $name: ident, $n: ty ) ) ,+) => {
2+ ( $( ( $name: ident, $n: ident ) ) ,+) => {
33 $( impl <const LANES : usize > $name<LANES > where Self : crate :: LanesAtMost32 {
44
55 /// Lanewise saturating add.
@@ -41,7 +41,7 @@ macro_rules! impl_uint_arith {
4141}
4242
4343macro_rules! impl_int_arith {
44- ( $( ( $name: ident, $n: ty ) ) ,+) => {
44+ ( $( ( $name: ident, $n: ident ) ) ,+) => {
4545 $( impl <const LANES : usize > $name<LANES > where Self : crate :: LanesAtMost32 {
4646
4747 /// Lanewise saturating add.
@@ -79,16 +79,34 @@ 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+ const SHR : $n = <$n>:: BITS as $n - 1 ;
95+ let m = self >> SHR ;
96+ ( self ^m) - m
97+ }
98+
8299 /// Lanewise saturating absolute value, implemented in Rust.
83100 /// As abs(), except the MIN value becomes MAX instead of itself.
84101 ///
85102 /// # Examples
103+ /// ```
86104 /// # use core_simd::*;
87105 #[ doc = concat!( "# use core::" , stringify!( $n) , "::{MIN, MAX};" ) ]
88- #[ doc = concat!( "let x = " , stringify!( $name) , "::splat ([MIN, -2, 0, 3]);" ) ]
89- /// let unsat = x .abs();
90- /// let sat = x .saturating_abs();
91- #[ doc = concat!( "assert_eq!(unsat, " , stringify!( $name) , "::from_array([MIN, 2, 0, 3]);" ) ]
106+ #[ doc = concat!( "let xs = " , stringify!( $name) , "::from_array ([MIN, -2, 0, 3]);" ) ]
107+ /// let unsat = xs .abs();
108+ /// let sat = xs .saturating_abs();
109+ #[ doc = concat!( "assert_eq!(unsat, " , stringify!( $name) , "::from_array([MIN, 2, 0, 3])) ;" ) ]
92110 #[ doc = concat!( "assert_eq!(sat, " , stringify!( $name) , "::from_array([MAX, 2, 0, 3]));" ) ]
93111 /// ```
94112 #[ inline]
@@ -103,12 +121,13 @@ macro_rules! impl_int_arith {
103121 /// As neg(), except the MIN value becomes MAX instead of itself.
104122 ///
105123 /// # Examples
124+ /// ```
106125 /// # use core_simd::*;
107126 #[ doc = concat!( "# use core::" , stringify!( $n) , "::{MIN, MAX};" ) ]
108- #[ doc = concat!( "let x = " , stringify!( $name) , "::splat ([MIN, -2, 3, MAX]);" ) ]
127+ #[ doc = concat!( "let x = " , stringify!( $name) , "::from_array ([MIN, -2, 3, MAX]);" ) ]
109128 /// let unsat = -x;
110129 /// let sat = x.saturating_neg();
111- #[ doc = concat!( "assert_eq!(unsat, " , stringify!( $name) , "::from_array([MIN, 2, -3, MIN + 1]);" ) ]
130+ #[ doc = concat!( "assert_eq!(unsat, " , stringify!( $name) , "::from_array([MIN, 2, -3, MIN + 1])) ;" ) ]
112131 #[ doc = concat!( "assert_eq!(sat, " , stringify!( $name) , "::from_array([MAX, 2, -3, MIN + 1]));" ) ]
113132 /// ```
114133 #[ inline]
0 commit comments