From 91134e614ef7ae4e758cc894eceaae6054ec5631 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Tue, 13 Apr 2021 21:15:20 -0700 Subject: [PATCH] Branchless abs --- crates/core_simd/src/math.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/crates/core_simd/src/math.rs b/crates/core_simd/src/math.rs index baf92ee097bd..50e8f2c10c9b 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.