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

View File

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

View File

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

View File

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