diff options
| author | Jubilee Young <workingjubilee@gmail.com> | 2021-04-13 21:15:20 -0700 |
|---|---|---|
| committer | Jubilee Young <workingjubilee@gmail.com> | 2021-04-25 16:42:49 -0700 |
| commit | 91134e614ef7ae4e758cc894eceaae6054ec5631 (patch) | |
| tree | 58f1559cf8cb18a2511ff2795930c528954ee906 | |
| parent | e8b6bca694098e4865d602ef438458ea52335e6a (diff) | |
| download | rust-91134e614ef7ae4e758cc894eceaae6054ec5631.tar.gz rust-91134e614ef7ae4e758cc894eceaae6054ec5631.zip | |
Branchless abs
| -rw-r--r-- | crates/core_simd/src/math.rs | 8 |
1 files changed, 3 insertions, 5 deletions
diff --git a/crates/core_simd/src/math.rs b/crates/core_simd/src/math.rs index baf92ee097b..50e8f2c10c9 100644 --- a/crates/core_simd/src/math.rs +++ b/crates/core_simd/src/math.rs @@ -91,11 +91,9 @@ macro_rules! impl_int_arith { /// ``` #[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) + const SHR: $n = <$n>::BITS as $n - 1; + let m = self >> SHR; + (self^m) - m } /// Lanewise saturating absolute value, implemented in Rust. |
