make INT_NBITS and FRAC_NBITS constants public

This commit is contained in:
Trevor Spiteri 2019-08-17 17:55:50 +02:00
parent 27f3da832f
commit 28885e09ab
3 changed files with 45 additions and 14 deletions

View File

@ -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)

View File

@ -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)
==========================

View File

@ -20,6 +20,47 @@ macro_rules! fixed_frac {
$UInner:ty, $Signedness:tt
) => {
impl<Frac: $LeEqU> $Fixed<Frac> {
comment!(
"The number of integer bits.
# Examples
```rust
use fixed::{frac::U6, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U6>;
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, "<U6>;
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;
}
};
}