remove if_cond_else const hack

This commit is contained in:
Trevor Spiteri 2020-09-02 10:22:44 +02:00
parent 5a4d1a6732
commit 7522557e1d
1 changed files with 20 additions and 29 deletions

View File

@ -877,7 +877,7 @@ assert_eq!(Fix::from_num(5).saturating_neg(), Fix::from_num(0));",
$Signedness,
{
let (val, overflow) = self.overflowing_neg();
val.if_cond_else(!overflow, Self::MAX)
if !overflow { val } else { Self::MAX }
},
Self::from_bits(0),
}
@ -899,14 +899,15 @@ assert_eq!(Fix::MAX.saturating_add(Fix::from_num(1)), Fix::MAX);
#[inline]
pub const fn saturating_add(self, rhs: $Fixed<Frac>) -> $Fixed<Frac> {
let (val, overflow) = self.overflowing_add(rhs);
val.if_cond_else(
!overflow,
if !overflow {
val
} else {
if_signed_unsigned! {
$Signedness,
Self::MIN.if_cond_else(self.to_bits() < 0, Self::MAX),
if self.to_bits() < 0 { Self::MIN } else { Self::MAX },
Self::MAX,
},
)
}
}
}
}
@ -932,17 +933,15 @@ assert_eq!(Fix::from_num(0).saturating_sub(Fix::from_num(1)), Fix::from_num(0));
#[inline]
pub const fn saturating_sub(self, rhs: $Fixed<Frac>) -> $Fixed<Frac> {
let (val, overflow) = self.overflowing_sub(rhs);
val.if_cond_else(
!overflow,
if !overflow {
val
} else {
if_signed_unsigned! {
$Signedness,
Self::MIN.if_cond_else(
self.to_bits() < rhs.to_bits(),
Self::MAX,
),
if self.to_bits() < rhs.to_bits() { Self::MIN } else { Self::MAX },
Self::MIN,
},
)
}
}
}
}
@ -961,17 +960,15 @@ assert_eq!(Fix::MAX.saturating_mul_int(2), Fix::MAX);
#[inline]
pub const fn saturating_mul_int(self, rhs: $Inner) -> $Fixed<Frac> {
let (val, overflow) = self.overflowing_mul_int(rhs);
val.if_cond_else(
!overflow,
if !overflow {
val
} else {
if_signed_unsigned! {
$Signedness,
Self::MIN.if_cond_else(
(self.to_bits() < 0) != (rhs < 0),
Self::MAX,
),
if (self.to_bits() < 0) != (rhs < 0) { Self::MIN } else { Self::MAX },
Self::MAX,
},
)
}
}
}
}
@ -994,7 +991,7 @@ assert_eq!(Fix::MIN.saturating_abs(), Fix::MAX);
#[inline]
pub const fn saturating_abs(self) -> $Fixed<Frac> {
let (val, overflow) = self.overflowing_abs();
val.if_cond_else(!overflow, Self::MAX)
if !overflow { val } else { Self::MAX }
}
}
}
@ -1485,12 +1482,6 @@ assert_eq!(Fix::MIN.overflowing_abs(), (Fix::MIN, true));
}
}
}
#[inline]
const fn if_cond_else(self, cond: bool, otherwise: Self) -> Self {
let not_mask = (cond as $Inner).wrapping_sub(1);
Self::from_bits((self.to_bits() & !not_mask) | (otherwise.to_bits() & not_mask))
}
}
};
}