Branchless abs

This commit is contained in:
Jubilee Young 2021-04-13 21:15:20 -07:00
parent e8b6bca694
commit 91134e614e

View file

@ -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.