From 28885e09ab01e668b604192e2a9b6b3a2ae00f0b Mon Sep 17 00:00:00 2001 From: Trevor Spiteri Date: Sat, 17 Aug 2019 17:55:50 +0200 Subject: [PATCH] make INT_NBITS and FRAC_NBITS constants public --- README.md | 4 +++- RELEASES.md | 1 + src/macros_frac.rs | 54 +++++++++++++++++++++++++++++++++++----------- 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 6e8fb45..4499ffd 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,10 @@ Various conversion methods are available: [`overflowing_mul_int`], [`overflowing_shl`], [`overflowing_shr`] * [`is_positive`], [`is_negative`] + * The associated constants [`INT_NBITS`] and [`FRAC_NBITS`] were added. +[`FRAC_NBITS`]: https://docs.rs/fixed/0.4.2/fixed/struct.FixedI32.html#associatedconstant.FRAC_NBITS +[`INT_NBITS`]: https://docs.rs/fixed/0.4.2/fixed/struct.FixedI32.html#associatedconstant.INT_NBITS [`count_ones`]: https://docs.rs/fixed/0.4.2/fixed/struct.FixedI32.html#method.count_ones [`count_zeros`]: https://docs.rs/fixed/0.4.2/fixed/struct.FixedI32.html#method.count_zeros [`from_bits`]: https://docs.rs/fixed/0.4.2/fixed/struct.FixedI32.html#method.from_bits @@ -86,7 +89,6 @@ Various conversion methods are available: [`wrapping_neg`]: https://docs.rs/fixed/0.4.2/fixed/struct.FixedI32.html#method.wrapping_neg [`wrapping_shl`]: https://docs.rs/fixed/0.4.2/fixed/struct.FixedI32.html#method.wrapping_shl [`wrapping_shr`]: https://docs.rs/fixed/0.4.2/fixed/struct.FixedI32.html#method.wrapping_shr -[`wrapping_sub`]: https://docs.rs/fixed/0.4.2/fixed/struct.FixedI32.html#method.wrapping_sub ### Version 0.4.2 news (2019-08-16) diff --git a/RELEASES.md b/RELEASES.md index 51012d9..4c3d8f9 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -18,6 +18,7 @@ Version 0.4.3 (unreleased) * `overflowing_neg`, `overflowing_add`, `overflowing_sub`, `overflowing_mul_int`, `overflowing_shl`, `overflowing_shr` * `is_positive`, `is_negative` + * The associated constants `INT_NBITS` and `FRAC_NBITS` were added. Version 0.4.2 (2019-08-16) ========================== diff --git a/src/macros_frac.rs b/src/macros_frac.rs index aa7ee9d..6b81b09 100644 --- a/src/macros_frac.rs +++ b/src/macros_frac.rs @@ -20,6 +20,47 @@ macro_rules! fixed_frac { $UInner:ty, $Signedness:tt ) => { impl $Fixed { + comment!( + "The number of integer bits. + +# Examples + +```rust +use fixed::{frac::U6, ", $s_fixed, "}; +type Fix = ", $s_fixed, "; +assert_eq!(Fix::INT_NBITS, ", $s_nbits, " - 6); +``` +"; + pub const INT_NBITS: u32 = mem::size_of::<$Inner>() as u32 * 8 - Self::FRAC_NBITS; + ); + + comment!( + "The number of fractional bits. + +# Examples + +```rust +use fixed::{frac::U6, ", $s_fixed, "}; +type Fix = ", $s_fixed, "; +assert_eq!(Fix::FRAC_NBITS, 6); +``` +"; + pub const FRAC_NBITS: u32 = Frac::U32; + ); + + // some other useful constants for internal use: + + const INT_MASK: $Inner = + !0 << (Self::FRAC_NBITS / 2) << (Self::FRAC_NBITS - Self::FRAC_NBITS / 2); + const FRAC_MASK: $Inner = !Self::INT_MASK; + + // 0 when FRAC_NBITS = 0 + const INT_LSB: $Inner = Self::INT_MASK ^ (Self::INT_MASK << 1); + + // 0 when INT_NBITS = 0 + const FRAC_MSB: $Inner = + Self::FRAC_MASK ^ ((Self::FRAC_MASK as $UInner) >> 1) as $Inner; + comment!( "Returns the number of integer bits. @@ -301,19 +342,6 @@ assert_eq!(Fix::max_value().overflowing_div(quarter), (wrapped, true)); ); fixed_deprecated! { $Fixed($Inner) } - - // some useful constants - const INT_NBITS: u32 = mem::size_of::<$Inner>() as u32 * 8 - Self::FRAC_NBITS; - // split shift in two parts to avoid overflow when INT_NBITS = 0 - const INT_MASK: $Inner = - !0 << (Self::FRAC_NBITS / 2) << (Self::FRAC_NBITS - Self::FRAC_NBITS / 2); - const INT_LSB: $Inner = Self::INT_MASK ^ (Self::INT_MASK << 1); - // 0 when FRAC_NBITS = 0 - const FRAC_NBITS: u32 = Frac::U32; - const FRAC_MASK: $Inner = !Self::INT_MASK; - // 0 when INT_NBITS = 0 - const FRAC_MSB: $Inner = - Self::FRAC_MASK ^ ((Self::FRAC_MASK as $UInner) >> 1) as $Inner; } }; }