add leading_ones and trailing_ones

This commit is contained in:
Trevor Spiteri 2020-05-10 01:09:41 +02:00
parent 41d57b242f
commit 8227ae5dd7
5 changed files with 93 additions and 2 deletions

View File

@ -82,16 +82,24 @@ The conversions supported cover the following cases.
* The [`LosslessTryFrom`][ltf-0-5-7] and
[`LosslessTryInto`][lti-0-5-7] traits were added.
* The following methods were added to all fixed-point types, to the
[`Fixed`][tf-0-5-7] trait, and to the [`Wrapping`][wr-0-5-7]
wrapper:
* [`leading_ones`][f-lo-0-5-7], [`trailing_ones`][f-to-0-5-7]
* The [`PHI`][phi-0-5-7] and [`FRAC_1_PHI`][f1phi-0-5-7] constants
were added to the [`consts`][cons-0-5-7] module and as
[associated constants][f-phi-0-5-7] for fixed-point types.
[cons-0-5-7]: https://tspiteri.gitlab.io/fixed/dev/fixed/consts/index.html
[f-lo-0-5-7]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.leading_ones
[f-phi-0-5-7]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#associatedconstant.PHI
[f-to-0-5-7]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.trailing_ones
[f1phi-0-5-7]: https://tspiteri.gitlab.io/fixed/dev/fixed/consts/constant.FRAC_1_PHI.html
[ltf-0-5-7]: https://tspiteri.gitlab.io/fixed/dev/fixed/traits/trait.LosslessTryFrom.html
[lti-0-5-7]: https://tspiteri.gitlab.io/fixed/dev/fixed/traits/trait.LosslessTryInto.html
[phi-0-5-7]: https://tspiteri.gitlab.io/fixed/dev/fixed/consts/constant.PHI.html
[tf-0-5-7]: https://tspiteri.gitlab.io/fixed/dev/fixed/traits/trait.Fixed.html
[wr-0-5-7]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.Wrapping.html
### Version 0.5.6 news (2020-05-01)
@ -101,7 +109,7 @@ dependency updates and the removal of deprecated items.
Other news in this release:
* The following method were added to signed fixed-point types and to
* The following methods were added to signed fixed-point types and to
the [`FixedSigned`][tfs-0-5-6] trait:
* [`checked_signum`][f-csig-0-5-6],
[`saturating_signum`][f-ssig-0-5-6],

View File

@ -9,13 +9,16 @@ Version 0.5.7 (unreleased)
==========================
* The `LosslessTryFrom` and `LosslessTryInto` traits were added.
* The following methods were added to all fixed-point types, to the
`Fixed` trait, and to the `Wrapping` wrapper:
* `leading_ones`, `trailing_ones`
* The `PHI` and `FRAC_1_PHI` constants were added to the `consts`
module and as associated constants for fixed-point types.
Version 0.5.6 (2020-05-01)
==========================
* The following method were added to signed fixed-point types and to
* The following methods were added to signed fixed-point types and to
the `FixedSigned` trait:
* `checked_signum`, `saturating_signum`, `wrapping_signum`,
`overflowing_signum`

View File

@ -250,6 +250,7 @@ assert_eq!(f.count_ones(), 3);
self.to_bits().count_ones()
}
}
comment! {
"Returns the number of zeros in the binary
representation.
@ -268,6 +269,27 @@ assert_eq!(f.count_zeros(), 3);
self.to_bits().count_zeros()
}
}
comment! {
"Returns the number of leading ones in the binary
representation.
# Examples
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
let all_ones = !Fix::from_bits(0);
let f = all_ones - Fix::from_bits(0b10_0000);
assert_eq!(f.leading_ones(), ", $s_nbits, " - 6);
```
";
#[inline]
pub const fn leading_ones(self) -> u32 {
(!self.to_bits()).leading_zeros()
}
}
comment! {
"Returns the number of leading zeros in the binary
representation.
@ -286,6 +308,26 @@ assert_eq!(f.leading_zeros(), ", $s_nbits, " - 6);
self.to_bits().leading_zeros()
}
}
comment! {
"Returns the number of trailing ones in the binary
representation.
# Examples
```rust
use fixed::{types::extra::U4, ", $s_fixed, "};
type Fix = ", $s_fixed, "<U4>;
let f = Fix::from_bits(0b101_1111);
assert_eq!(f.trailing_ones(), 5);
```
";
#[inline]
pub const fn trailing_ones(self) -> u32 {
(!self.to_bits()).trailing_zeros()
}
}
comment! {
"Returns the number of trailing zeros in the binary
representation.
@ -304,6 +346,7 @@ assert_eq!(f.trailing_zeros(), 5);
self.to_bits().trailing_zeros()
}
}
comment! {
"Shifts to the left by `n` bits, wrapping the
truncated bits to the right end.
@ -324,6 +367,7 @@ assert_eq!(Fix::from_bits(bits).rotate_left(3), Fix::from_bits(rot));
Self::from_bits(self.to_bits().rotate_left(n))
}
}
comment! {
"Shifts to the right by `n` bits, wrapping the
truncated bits to the left end.

View File

@ -608,9 +608,15 @@ where
/// Returns the number of zeros in the binary representation.
fn count_zeros(self) -> u32;
/// Returns the number of leading ones in the binary representation.
fn leading_ones(self) -> u32;
/// Returns the number of leading zeros in the binary representation.
fn leading_zeros(self) -> u32;
/// Returns the number of trailing ones in the binary representation.
fn trailing_ones(self) -> u32;
/// Returns the number of trailing zeros in the binary representation.
fn trailing_zeros(self) -> u32;
@ -1989,7 +1995,9 @@ macro_rules! impl_fixed {
trait_delegate! { fn overflowing_round_ties_to_even(self) -> (Self, bool) }
trait_delegate! { fn count_ones(self) -> u32 }
trait_delegate! { fn count_zeros(self) -> u32 }
trait_delegate! { fn leading_ones(self) -> u32 }
trait_delegate! { fn leading_zeros(self) -> u32 }
trait_delegate! { fn trailing_ones(self) -> u32 }
trait_delegate! { fn trailing_zeros(self) -> u32 }
trait_delegate! { fn int_log2(self) -> i32 }
trait_delegate! { fn int_log10(self) -> i32 }

View File

@ -463,6 +463,20 @@ impl<F: Fixed> Wrapping<F> {
self.0.count_zeros()
}
/// Returns the number of leading ones in the binary representation.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let w = Wrapping(I16F16::from_bits(0xFF00_00FF));
/// assert_eq!(w.leading_ones(), w.0.leading_ones());
/// ```
#[inline]
pub fn leading_ones(self) -> u32 {
self.0.leading_ones()
}
/// Returns the number of leading zeros in the binary representation.
///
/// # Examples
@ -477,6 +491,20 @@ impl<F: Fixed> Wrapping<F> {
self.0.leading_zeros()
}
/// Returns the number of trailing ones in the binary representation.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let w = Wrapping(I16F16::from_bits(0xFF00_00FF));
/// assert_eq!(w.trailing_ones(), w.0.trailing_ones());
/// ```
#[inline]
pub fn trailing_ones(self) -> u32 {
self.0.trailing_ones()
}
/// Returns the number of trailing zeros in the binary representation.
///
/// # Examples