remove if_cond_else hack

This commit is contained in:
Trevor Spiteri 2021-02-01 11:44:12 +01:00
parent c3a92b5c72
commit 4d439c0b09
1 changed files with 31 additions and 34 deletions

View File

@ -1054,8 +1054,10 @@ assert_eq!(Fix::from_num(5).saturating_neg(), Fix::from_num(0));",
if_signed_unsigned! {
$Signedness,
{
let (val, overflow) = self.overflowing_neg();
val.if_cond_else(!overflow, Self::MAX)
match self.overflowing_neg() {
(val, false) => val,
(_, true) => Self::MAX,
}
},
Self::from_bits(0),
}
@ -1076,15 +1078,14 @@ 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_signed_unsigned! {
match self.overflowing_add(rhs) {
(val, false) => val,
(_, true) => if_signed_unsigned! {
$Signedness,
Self::MIN.if_cond_else(self.to_bits() < 0, Self::MAX),
if self.is_negative() { Self::MIN } else { Self::MAX },
Self::MAX,
},
)
}
}
}
@ -1109,18 +1110,18 @@ 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_signed_unsigned! {
match self.overflowing_sub(rhs) {
(val, false) => val,
(_, true) => 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,
},
)
}
}
}
@ -1204,18 +1205,18 @@ 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_signed_unsigned! {
match self.overflowing_mul_int(rhs) {
(val, false) => val,
(_, true) => if_signed_unsigned! {
$Signedness,
Self::MIN.if_cond_else(
(self.to_bits() < 0) != (rhs < 0),
Self::MAX,
),
if self.is_negative() != rhs.is_negative() {
Self::MIN
} else {
Self::MAX
},
Self::MAX,
},
)
}
}
}
@ -1237,8 +1238,10 @@ 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)
match self.overflowing_abs() {
(val, false) => val,
(_, true) => Self::MAX,
}
}
}
}
@ -2202,12 +2205,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))
}
}
};
}