Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 91134e6

Browse files
Branchless abs
1 parent e8b6bca commit 91134e6

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

crates/core_simd/src/math.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,9 @@ macro_rules! impl_int_arith {
9191
/// ```
9292
#[inline]
9393
pub fn abs(self) -> Self {
94-
let mut xs = self.to_array();
95-
for (i, x) in xs.clone().iter().enumerate() {
96-
xs[i] = x.wrapping_abs()
97-
}
98-
$name::from_array(xs)
94+
const SHR: $n = <$n>::BITS as $n - 1;
95+
let m = self >> SHR;
96+
(self^m) - m
9997
}
10098

10199
/// Lanewise saturating absolute value, implemented in Rust.

0 commit comments

Comments
 (0)