Skip to content

Commit

Permalink
Branchless abs
Browse files Browse the repository at this point in the history
  • Loading branch information
workingjubilee committed Apr 25, 2021
1 parent e8b6bca commit 91134e6
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions crates/core_simd/src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 91134e6

Please sign in to comment.