From 07ea3ff1aeb878bd6d6f76cba70ff6cb2b9da5bf Mon Sep 17 00:00:00 2001 From: Trevor Spiteri Date: Sat, 17 Aug 2019 18:21:04 +0200 Subject: [PATCH] use constants {INT,FRAC}_NBITS instead of methods where possible --- src/cmp.rs | 69 ++++++++++++++++++++++++------------------------- src/from_str.rs | 6 ++--- src/helpers.rs | 2 +- src/traits.rs | 4 +-- 4 files changed, 39 insertions(+), 42 deletions(-) diff --git a/src/cmp.rs b/src/cmp.rs index 2c4ca80..b995bd8 100644 --- a/src/cmp.rs +++ b/src/cmp.rs @@ -30,9 +30,9 @@ macro_rules! fixed_cmp_fixed { #[inline] fn eq(&self, rhs: &$Rhs) -> bool { let conv = rhs.to_bits().to_fixed_helper( - <$Rhs>::frac_nbits() as i32, - Self::frac_nbits(), - Self::int_nbits(), + <$Rhs>::FRAC_NBITS as i32, + Self::FRAC_NBITS, + Self::INT_NBITS, ); let rhs_bits = match conv.bits { Widest::Unsigned(bits) => bits as ::Bits, @@ -51,9 +51,9 @@ macro_rules! fixed_cmp_fixed { _ => {} } let conv = rhs.to_bits().to_fixed_helper( - <$Rhs>::frac_nbits() as i32, - Self::frac_nbits(), - Self::int_nbits(), + <$Rhs>::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>::frac_nbits() as i32, - Self::frac_nbits(), - Self::int_nbits(), + <$Rhs>::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 PartialEq<$Float> for $Fix { #[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 PartialOrd<$Float> for $Fix { #[inline] fn partial_cmp(&self, rhs: &$Float) -> Option { - 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) -> bool { - let (lhs_is_neg, conv) = match self - .to_float_kind(<$Fix>::frac_nbits(), <$Fix>::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_NBITS, <$Fix>::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, diff --git a/src/from_str.rs b/src/from_str.rs index a104b15..4853bfd 100644 --- a/src/from_str.rs +++ b/src/from_str.rs @@ -547,16 +547,14 @@ macro_rules! impl_from_str { type Err = ParseFixedError; #[inline] fn from_str(s: &str) -> Result { - $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 FromStrRadix for $Fixed { type Err = ParseFixedError; #[inline] fn from_str_radix(s: &str, radix: u32) -> Result { - $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) } } }; diff --git a/src/helpers.rs b/src/helpers.rs index 9706f37..81e7905 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -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, ) diff --git a/src/traits.rs b/src/traits.rs index a64a662..0fa68b0 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -1399,7 +1399,7 @@ macro_rules! impl_fixed { } #[inline] fn saturating_from_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(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,