make INT_NBITS and FRAC_NBITS constants public
This commit is contained in:
parent
27f3da832f
commit
28885e09ab
|
@ -62,7 +62,10 @@ Various conversion methods are available:
|
||||||
[`overflowing_mul_int`], [`overflowing_shl`],
|
[`overflowing_mul_int`], [`overflowing_shl`],
|
||||||
[`overflowing_shr`]
|
[`overflowing_shr`]
|
||||||
* [`is_positive`], [`is_negative`]
|
* [`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_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
|
[`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
|
[`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_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_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_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)
|
### Version 0.4.2 news (2019-08-16)
|
||||||
|
|
||||||
|
|
|
@ -18,6 +18,7 @@ Version 0.4.3 (unreleased)
|
||||||
* `overflowing_neg`, `overflowing_add`, `overflowing_sub`,
|
* `overflowing_neg`, `overflowing_add`, `overflowing_sub`,
|
||||||
`overflowing_mul_int`, `overflowing_shl`, `overflowing_shr`
|
`overflowing_mul_int`, `overflowing_shl`, `overflowing_shr`
|
||||||
* `is_positive`, `is_negative`
|
* `is_positive`, `is_negative`
|
||||||
|
* The associated constants `INT_NBITS` and `FRAC_NBITS` were added.
|
||||||
|
|
||||||
Version 0.4.2 (2019-08-16)
|
Version 0.4.2 (2019-08-16)
|
||||||
==========================
|
==========================
|
||||||
|
|
|
@ -20,6 +20,47 @@ macro_rules! fixed_frac {
|
||||||
$UInner:ty, $Signedness:tt
|
$UInner:ty, $Signedness:tt
|
||||||
) => {
|
) => {
|
||||||
impl<Frac: $LeEqU> $Fixed<Frac> {
|
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!(
|
comment!(
|
||||||
"Returns the number of integer bits.
|
"Returns the number of integer bits.
|
||||||
|
|
||||||
|
@ -301,19 +342,6 @@ assert_eq!(Fix::max_value().overflowing_div(quarter), (wrapped, true));
|
||||||
);
|
);
|
||||||
|
|
||||||
fixed_deprecated! { $Fixed($Inner) }
|
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;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue