use constants {INT,FRAC}_NBITS instead of methods where possible

This commit is contained in:
Trevor Spiteri 2019-08-17 18:21:04 +02:00
parent 3f7d1f42a5
commit 07ea3ff1ae
4 changed files with 39 additions and 42 deletions

View File

@ -30,9 +30,9 @@ macro_rules! fixed_cmp_fixed {
#[inline]
fn eq(&self, rhs: &$Rhs<FracRhs>) -> bool {
let conv = rhs.to_bits().to_fixed_helper(
<$Rhs<FracRhs>>::frac_nbits() as i32,
Self::frac_nbits(),
Self::int_nbits(),
<$Rhs<FracRhs>>::FRAC_NBITS as i32,
Self::FRAC_NBITS,
Self::INT_NBITS,
);
let rhs_bits = match conv.bits {
Widest::Unsigned(bits) => bits as <Self as Fixed>::Bits,
@ -51,9 +51,9 @@ macro_rules! fixed_cmp_fixed {
_ => {}
}
let conv = rhs.to_bits().to_fixed_helper(
<$Rhs<FracRhs>>::frac_nbits() as i32,
Self::frac_nbits(),
Self::int_nbits(),
<$Rhs<FracRhs>>::FRAC_NBITS as i32,
Self::FRAC_NBITS,
Self::INT_NBITS,
);
if conv.overflow {
return if rhs.to_bits().is_negative() {
@ -77,9 +77,9 @@ macro_rules! fixed_cmp_fixed {
_ => {}
}
let conv = rhs.to_bits().to_fixed_helper(
<$Rhs<FracRhs>>::frac_nbits() as i32,
Self::frac_nbits(),
Self::int_nbits(),
<$Rhs<FracRhs>>::FRAC_NBITS as i32,
Self::FRAC_NBITS,
Self::INT_NBITS,
);
if conv.overflow {
return !rhs.to_bits().is_negative();
@ -187,7 +187,7 @@ macro_rules! fixed_cmp_float {
impl<Frac: $LeEqU> PartialEq<$Float> for $Fix<Frac> {
#[inline]
fn eq(&self, rhs: &$Float) -> bool {
let conv = match rhs.to_float_kind(Self::frac_nbits(), Self::int_nbits()) {
let conv = match rhs.to_float_kind(Self::FRAC_NBITS, Self::INT_NBITS) {
FloatKind::Finite { conv, .. } => conv,
_ => return false,
};
@ -209,18 +209,18 @@ macro_rules! fixed_cmp_float {
impl<Frac: $LeEqU> PartialOrd<$Float> for $Fix<Frac> {
#[inline]
fn partial_cmp(&self, rhs: &$Float) -> Option<Ordering> {
let (rhs_is_neg, conv) =
match rhs.to_float_kind(Self::frac_nbits(), Self::int_nbits()) {
FloatKind::NaN => return None,
FloatKind::Infinite { neg } => {
return if neg {
Some(Ordering::Greater)
} else {
Some(Ordering::Less)
};
}
FloatKind::Finite { neg, conv } => (neg, conv),
};
let (rhs_is_neg, conv) = match rhs.to_float_kind(Self::FRAC_NBITS, Self::INT_NBITS)
{
FloatKind::NaN => return None,
FloatKind::Infinite { neg } => {
return if neg {
Some(Ordering::Greater)
} else {
Some(Ordering::Less)
};
}
FloatKind::Finite { neg, conv } => (neg, conv),
};
match (self.to_bits().is_negative(), rhs_is_neg) {
(false, true) => return Some(Ordering::Greater),
(true, false) => return Some(Ordering::Less),
@ -242,12 +242,12 @@ macro_rules! fixed_cmp_float {
#[inline]
fn lt(&self, rhs: &$Float) -> bool {
let (rhs_is_neg, conv) =
match rhs.to_float_kind(Self::frac_nbits(), Self::int_nbits()) {
FloatKind::NaN => return false,
FloatKind::Infinite { neg } => return !neg,
FloatKind::Finite { neg, conv } => (neg, conv),
};
let (rhs_is_neg, conv) = match rhs.to_float_kind(Self::FRAC_NBITS, Self::INT_NBITS)
{
FloatKind::NaN => return false,
FloatKind::Infinite { neg } => return !neg,
FloatKind::Finite { neg, conv } => (neg, conv),
};
match (self.to_bits().is_negative(), rhs_is_neg) {
(false, true) => return false,
@ -289,13 +289,12 @@ macro_rules! fixed_cmp_float {
#[inline]
fn lt(&self, rhs: &$Fix<Frac>) -> bool {
let (lhs_is_neg, conv) = match self
.to_float_kind(<$Fix<Frac>>::frac_nbits(), <$Fix<Frac>>::int_nbits())
{
FloatKind::NaN => return false,
FloatKind::Infinite { neg } => return neg,
FloatKind::Finite { neg, conv } => (neg, conv),
};
let (lhs_is_neg, conv) =
match self.to_float_kind(<$Fix<Frac>>::FRAC_NBITS, <$Fix<Frac>>::INT_NBITS) {
FloatKind::NaN => return false,
FloatKind::Infinite { neg } => return neg,
FloatKind::Finite { neg, conv } => (neg, conv),
};
match (lhs_is_neg, rhs.to_bits().is_negative()) {
(false, true) => return false,

View File

@ -547,16 +547,14 @@ macro_rules! impl_from_str {
type Err = ParseFixedError;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
$method(s.as_bytes(), 10, Self::int_nbits(), Self::frac_nbits())
.map(Self::from_bits)
$method(s.as_bytes(), 10, Self::INT_NBITS, Self::FRAC_NBITS).map(Self::from_bits)
}
}
impl<Frac: $LeEqU> FromStrRadix for $Fixed<Frac> {
type Err = ParseFixedError;
#[inline]
fn from_str_radix(s: &str, radix: u32) -> Result<Self, Self::Err> {
$method(s.as_bytes(), radix, Self::int_nbits(), Self::frac_nbits())
.map(Self::from_bits)
$method(s.as_bytes(), radix, Self::INT_NBITS, Self::FRAC_NBITS).map(Self::from_bits)
}
}
};

View File

@ -64,7 +64,7 @@ macro_rules! impl_sealed {
dst_int_nbits: u32,
) -> ToFixedHelper {
self.to_bits().to_fixed_helper(
Self::frac_nbits() as i32,
Self::FRAC_NBITS as i32,
dst_frac_nbits,
dst_int_nbits,
)

View File

@ -1399,7 +1399,7 @@ macro_rules! impl_fixed {
}
#[inline]
fn saturating_from_fixed<F: Fixed>(src: F) -> Self {
let conv = src.private_to_fixed_helper(Self::frac_nbits(), Self::int_nbits());
let conv = src.private_to_fixed_helper(Self::FRAC_NBITS, Self::INT_NBITS);
if conv.overflow {
return if src < 0 {
Self::min_value()
@ -1434,7 +1434,7 @@ macro_rules! impl_fixed {
}
#[inline]
fn overflowing_from_fixed<F: Fixed>(src: F) -> (Self, bool) {
let conv = src.private_to_fixed_helper(Self::frac_nbits(), Self::int_nbits());
let conv = src.private_to_fixed_helper(Self::FRAC_NBITS, Self::INT_NBITS);
let mut new_overflow = false;
let bits = if_signed_unsigned!(
$Signedness,