replace {int,frac}_nbits() by {INT,FRAC}_NBITS

This commit is contained in:
Trevor Spiteri 2020-04-11 18:50:26 +02:00
parent e1febebb31
commit 47287ab98f
5 changed files with 75 additions and 59 deletions

View File

@ -79,12 +79,15 @@ The conversions supported cover the following cases.
* The associated constants [`MIN`] and [`MAX`] were added to all
fixed-point types, to the [`Fixed`] trait, and to the [`Wrapping`]
wrapper.
* The associated constants [`INT_NBITS`] and [`FRAC_NBITS`] were
added to the [`Fixed`] trait, and to the [`Wrapping`] wrapper.
* The methods [`int_log2`] and [`checked_int_log2`] were added to
all fixed-point types and to the [`Fixed`] trait.
* The method [`int_log2`][wril] was added to the [`Wrapping`]
wrapper.
* The following methods were deprecated:
* [`min_value`], [`max_value`]
* [`int_nbits`][`int_nbits()`], [`frac_nbits`][`frac_nbits()`]
### Version 0.5.4 news (2020-02-21)
@ -119,7 +122,9 @@ The conversions supported cover the following cases.
* [`Wrapping`] now supports serialization. (Thanks: Shane Pearman)
[`FRAC_NBITS`]: https://docs.rs/fixed/0.5.4/fixed/traits/trait.Fixed.html#associatedconstant.FRAC_NBITS
[`Fixed`]: https://docs.rs/fixed/0.5.4/fixed/traits/trait.Fixed.html
[`INT_NBITS`]: https://docs.rs/fixed/0.5.4/fixed/traits/trait.Fixed.html#associatedconstant.INT_NBITS
[`MAX`]: https://docs.rs/fixed/0.5.4/fixed/struct.FixedI32.html#associatedconstant.MAX
[`MIN`]: https://docs.rs/fixed/0.5.4/fixed/struct.FixedI32.html#associatedconstant.MIN
[`RemAssign`]: https://doc.rust-lang.org/nightly/core/ops/trait.RemAssign.html
@ -130,7 +135,9 @@ The conversions supported cover the following cases.
[`checked_rem_euclid`]: https://docs.rs/fixed/0.5.4/fixed/struct.FixedI32.html#method.checked_rem_euclid
[`checked_rem`]: https://docs.rs/fixed/0.5.4/fixed/struct.FixedI32.html#method.checked_rem
[`div_euclid`]: https://docs.rs/fixed/0.5.4/fixed/struct.FixedI32.html#method.div_euclid
[`frac_nbits()`]: https://docs.rs/fixed/0.5.4/fixed/struct.FixedI32.html#method.frac_nbits
[`int_log2`]: https://docs.rs/fixed/0.5.4/fixed/struct.FixedI32.html#method.int_log2
[`int_nbits()`]: https://docs.rs/fixed/0.5.4/fixed/struct.FixedI32.html#method.int_nbits
[`max_value`]: https://docs.rs/fixed/0.5.4/fixed/struct.FixedI32.html#method.max_value
[`min_value`]: https://docs.rs/fixed/0.5.4/fixed/struct.FixedI32.html#method.min_value
[`overflowing_div_euclid`]: https://docs.rs/fixed/0.5.4/fixed/struct.FixedI32.html#method.overflowing_div_euclid

View File

@ -11,11 +11,14 @@ Version 0.5.5 (unreleased)
* The associated constants `MIN` and `MAX` were added to all
fixed-point types, to the `Fixed` trait, and to the `Wrapping`
wrapper.
* The associated constants `INT_NBITS` and `FRAC_NBITS` were added
to the `Fixed` trait, and to the `Wrapping` wrapper.
* The methods `int_log2` and `checked_int_log2` were added to all
fixed-point types and to the `Fixed` trait.
* The method `int_log2` was added to the `Wrapping` wrapper.
* The following methods were deprecated:
* `min_value`, `max_value`
* `int_nbits`, `frac_nbits`
Version 0.5.4 (2020-02-21)
==========================

View File

@ -63,40 +63,6 @@ assert_eq!(Fix::FRAC_NBITS, 6);
const FRAC_MSB: $Inner =
Self::FRAC_MASK ^ ((Self::FRAC_MASK as $UInner) >> 1) as $Inner;
comment! {
"Returns the number of integer bits.
# Examples
```rust
use fixed::{types::extra::U6, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U6>;
assert_eq!(Fix::int_nbits(), ", $s_nbits, " - 6);
```
";
#[inline]
pub fn int_nbits() -> u32 {
Self::INT_NBITS
}
}
comment! {
"Returns the number of fractional bits.
# Examples
```rust
use fixed::{types::extra::U6, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U6>;
assert_eq!(Fix::frac_nbits(), 6);
```
";
#[inline]
pub fn frac_nbits() -> u32 {
Self::FRAC_NBITS
}
}
fixed_from_to! { $Fixed[$s_fixed]($Inner[$s_inner], $s_nbits), $Signedness }
fixed_round! { $Fixed[$s_fixed]($s_nbits), $Signedness }
@ -1046,13 +1012,27 @@ assert_eq!(Fix::from_num(-7.5).overflowing_rem_euclid_int(20), (Fix::from_num(-3
}
}
/// Returns the number of integer bits.
#[inline]
#[deprecated(since = "0.5.5", note = "use `INT_NBITS` instead")]
pub fn int_nbits() -> u32 {
Self::INT_NBITS
}
/// Returns the number of fractional bits.
#[inline]
#[deprecated(since = "0.5.5", note = "use `FRAC_NBITS` instead")]
pub fn frac_nbits() -> u32 {
Self::FRAC_NBITS
}
/// Remainder for division by an integer.
///
/// # Panics
///
/// Panics if the divisor is zero.
#[deprecated(since = "0.5.3", note = "cannot overflow, use `%` or `Rem::rem` instead")]
#[inline]
#[deprecated(since = "0.5.3", note = "cannot overflow, use `%` or `Rem::rem` instead")]
pub fn wrapping_rem_int(self, rhs: $Inner) -> $Fixed<Frac> {
self % rhs
}
@ -1062,8 +1042,8 @@ assert_eq!(Fix::from_num(-7.5).overflowing_rem_euclid_int(20), (Fix::from_num(-3
/// # Panics
///
/// Panics if the divisor is zero.
#[deprecated(since = "0.5.3", note = "cannot overflow, use `%` or `Rem::rem` instead")]
#[inline]
#[deprecated(since = "0.5.3", note = "cannot overflow, use `%` or `Rem::rem` instead")]
pub fn overflowing_rem_int(self, rhs: $Inner) -> ($Fixed<Frac>, bool) {
(self % rhs, false)
}

View File

@ -252,11 +252,11 @@ where
/// The largest value that can be represented.
const MAX: Self;
/// Returns the number of integer bits.
fn int_nbits() -> u32;
/// The number of integer bits.
const INT_NBITS: u32;
/// Returns the number of fractional bits.
fn frac_nbits() -> u32;
/// The number of fractional bits.
const FRAC_NBITS: u32;
/// Creates a fixed-point number that has a bitwise representation
/// identical to the given integer.
@ -986,22 +986,39 @@ where
fn overflowing_shr(self, rhs: u32) -> (Self, bool);
/// Returns the smallest value that can be represented.
#[inline]
#[deprecated(since = "0.5.5", note = "replaced by `MIN`")]
fn min_value() -> Self {
Self::MIN
}
/// Returns the largest value that can be represented.
#[inline]
#[deprecated(since = "0.5.5", note = "replaced by `MAX`")]
fn max_value() -> Self {
Self::MAX
}
/// Returns the number of integer bits.
#[inline]
#[deprecated(since = "0.5.5", note = "replaced by `INT_NBITS`")]
fn int_nbits() -> u32 {
Self::INT_NBITS
}
/// Returns the number of fractional bits.
#[inline]
#[deprecated(since = "0.5.5", note = "replaced by `FRAC_NBITS`")]
fn frac_nbits() -> u32 {
Self::FRAC_NBITS
}
/// Remainder for division by an integer.
///
/// # Panics
///
/// Panics if the divisor is zero.
#[inline]
#[deprecated(
since = "0.5.3",
note = "cannot overflow, use `%` or `Rem::rem` instead"
@ -1015,6 +1032,7 @@ where
/// # Panics
///
/// Panics if the divisor is zero.
#[inline]
#[deprecated(
since = "0.5.3",
note = "cannot overflow, use `%` or `Rem::rem` instead"
@ -1555,7 +1573,7 @@ macro_rules! impl_float {
#[inline]
fn from_fixed<F: Fixed>(src: F) -> Self {
let helper = src.private_to_float_helper();
FloatHelper::from_to_float_helper(helper, F::frac_nbits(), F::int_nbits())
FloatHelper::from_to_float_helper(helper, F::FRAC_NBITS, F::INT_NBITS)
}
/// Converts a fixed-point number to a floating-point
@ -1638,7 +1656,7 @@ it panics; if wrapping is required use [`wrapping_to_fixed`] instead.
/// [`None`]: https://doc.rust-lang.org/nightly/core/option/enum.Option.html#variant.None
#[inline]
fn checked_to_fixed<F: Fixed>(self) -> Option<F> {
let kind = self.to_float_kind(F::frac_nbits(), F::int_nbits());
let kind = self.to_float_kind(F::FRAC_NBITS, F::INT_NBITS);
match kind {
FloatKind::Finite { .. } => {
let helper = FromFloatHelper { kind };
@ -1665,7 +1683,7 @@ Panics if `self` is [NaN].
";
#[inline]
fn saturating_to_fixed<F: Fixed>(self) -> F {
let kind = self.to_float_kind(F::frac_nbits(), F::int_nbits());
let kind = self.to_float_kind(F::FRAC_NBITS, F::INT_NBITS);
let helper = FromFloatHelper { kind };
F::private_saturating_from_float_helper(helper)
}
@ -1709,7 +1727,7 @@ Panics if `self` is not [finite].
";
#[inline]
fn overflowing_to_fixed<F: Fixed>(self) -> (F, bool) {
let kind = self.to_float_kind(F::frac_nbits(), F::int_nbits());
let kind = self.to_float_kind(F::FRAC_NBITS, F::INT_NBITS);
let helper = FromFloatHelper { kind };
F::private_overflowing_from_float_helper(helper)
}
@ -1762,8 +1780,8 @@ macro_rules! impl_fixed {
type Frac = Frac;
const MIN: Self = Self::MIN;
const MAX: Self = Self::MAX;
trait_delegate! { fn int_nbits() -> u32 }
trait_delegate! { fn frac_nbits() -> u32 }
const INT_NBITS: u32 = Self::INT_NBITS;
const FRAC_NBITS: u32 = Self::FRAC_NBITS;
trait_delegate! { fn from_bits(bits: Self::Bits) -> Self }
trait_delegate! { fn to_bits(self) -> Self::Bits }
trait_delegate! { fn from_be_bytes(bits: Self::Bytes) -> Self }

View File

@ -71,31 +71,25 @@ impl<F: Fixed> Wrapping<F> {
/// ```
pub const MAX: Wrapping<F> = Wrapping(F::MAX);
/// Returns the number of integer bits.
/// The number of integer bits.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// assert_eq!(Wrapping::<I16F16>::int_nbits(), I16F16::int_nbits());
/// assert_eq!(Wrapping::<I16F16>::INT_NBITS, I16F16::INT_NBITS);
/// ```
#[inline]
pub fn int_nbits() -> u32 {
F::int_nbits()
}
pub const INT_NBITS: u32 = F::INT_NBITS;
/// Returns the number of fractional bits.
/// The number of fractional bits.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// assert_eq!(Wrapping::<I16F16>::frac_nbits(), I16F16::frac_nbits());
/// assert_eq!(Wrapping::<I16F16>::FRAC_NBITS, I16F16::FRAC_NBITS);
/// ```
#[inline]
pub fn frac_nbits() -> u32 {
F::frac_nbits()
}
pub const FRAC_NBITS: u32 = F::FRAC_NBITS;
/// Creates a fixed-point number that has a bitwise representation
/// identical to the given integer.
@ -617,8 +611,8 @@ impl<F: Fixed> Wrapping<F> {
}
/// Returns the smallest value that can be represented.
#[deprecated(since = "0.5.5", note = "replaced by `MIN`")]
#[inline]
#[deprecated(since = "0.5.5", note = "replaced by `MIN`")]
pub fn min_value() -> Wrapping<F> {
Self::MIN
}
@ -629,6 +623,20 @@ impl<F: Fixed> Wrapping<F> {
pub fn max_value() -> Wrapping<F> {
Self::MAX
}
/// Returns the number of integer bits.
#[inline]
#[deprecated(since = "0.5.5", note = "replaced by `INT_NBITS`")]
pub fn int_nbits() -> u32 {
Self::INT_NBITS
}
/// Returns the number of fractional bits.
#[inline]
#[deprecated(since = "0.5.5", note = "replaced by `FRAC_NBITS`")]
pub fn frac_nbits() -> u32 {
Self::FRAC_NBITS
}
}
impl<F: FixedSigned> Wrapping<F> {