1
1
macro_rules! impl_uint_arith {
2
- ( $( ( $name: ident, $n: ty ) ) ,+) => {
2
+ ( $( ( $name: ident, $n: ident ) ) ,+) => {
3
3
$( impl <const LANES : usize > $name<LANES > where Self : crate :: LanesAtMost32 {
4
4
5
5
/// Lanewise saturating add.
@@ -41,7 +41,7 @@ macro_rules! impl_uint_arith {
41
41
}
42
42
43
43
macro_rules! impl_int_arith {
44
- ( $( ( $name: ident, $n: ty ) ) ,+) => {
44
+ ( $( ( $name: ident, $n: ident ) ) ,+) => {
45
45
$( impl <const LANES : usize > $name<LANES > where Self : crate :: LanesAtMost32 {
46
46
47
47
/// Lanewise saturating add.
@@ -79,16 +79,34 @@ macro_rules! impl_int_arith {
79
79
unsafe { crate :: intrinsics:: simd_saturating_sub( self , second) }
80
80
}
81
81
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
+
82
99
/// Lanewise saturating absolute value, implemented in Rust.
83
100
/// As abs(), except the MIN value becomes MAX instead of itself.
84
101
///
85
102
/// # Examples
103
+ /// ```
86
104
/// # use core_simd::*;
87
105
#[ 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])) ;" ) ]
92
110
#[ doc = concat!( "assert_eq!(sat, " , stringify!( $name) , "::from_array([MAX, 2, 0, 3]));" ) ]
93
111
/// ```
94
112
#[ inline]
@@ -103,12 +121,13 @@ macro_rules! impl_int_arith {
103
121
/// As neg(), except the MIN value becomes MAX instead of itself.
104
122
///
105
123
/// # Examples
124
+ /// ```
106
125
/// # use core_simd::*;
107
126
#[ 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]);" ) ]
109
128
/// let unsat = -x;
110
129
/// 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])) ;" ) ]
112
131
#[ doc = concat!( "assert_eq!(sat, " , stringify!( $name) , "::from_array([MAX, 2, -3, MIN + 1]));" ) ]
113
132
/// ```
114
133
#[ inline]
0 commit comments