Skip to content

Implement feature int_lowest_highest_one for integer and NonZero types #145381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions library/core/src/num/int_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,48 @@ macro_rules! int_impl {
self & self.wrapping_neg()
}

/// Returns the index of the highest bit set to one in `self`, or `None`
/// if `self` is `0`.
///
/// # Examples
///
/// ```
/// #![feature(int_lowest_highest_one)]
///
#[doc = concat!("assert_eq!(0x0_", stringify!($SelfT), ".highest_one(), None);")]
#[doc = concat!("assert_eq!(0x1_", stringify!($SelfT), ".highest_one(), Some(0));")]
#[doc = concat!("assert_eq!(0x10_", stringify!($SelfT), ".highest_one(), Some(4));")]
#[doc = concat!("assert_eq!(0x1f_", stringify!($SelfT), ".highest_one(), Some(4));")]
/// ```
#[unstable(feature = "int_lowest_highest_one", issue = "145203")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn highest_one(self) -> Option<u32> {
(self as $UnsignedT).highest_one()
}

/// Returns the index of the lowest bit set to one in `self`, or `None`
/// if `self` is `0`.
///
/// # Examples
///
/// ```
/// #![feature(int_lowest_highest_one)]
///
#[doc = concat!("assert_eq!(0x0_", stringify!($SelfT), ".lowest_one(), None);")]
#[doc = concat!("assert_eq!(0x1_", stringify!($SelfT), ".lowest_one(), Some(0));")]
#[doc = concat!("assert_eq!(0x10_", stringify!($SelfT), ".lowest_one(), Some(4));")]
#[doc = concat!("assert_eq!(0x1f_", stringify!($SelfT), ".lowest_one(), Some(0));")]
/// ```
#[unstable(feature = "int_lowest_highest_one", issue = "145203")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn lowest_one(self) -> Option<u32> {
(self as $UnsignedT).lowest_one()
}

/// Returns the bit pattern of `self` reinterpreted as an unsigned integer of the same size.
///
/// This produces the same result as an `as` cast, but ensures that the bit-width remains
Expand Down
50 changes: 50 additions & 0 deletions library/core/src/num/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,56 @@ macro_rules! nonzero_integer {
unsafe { NonZero::new_unchecked(n) }
}

/// Returns the index of the highest bit set to one in `self`, or `None`
/// if `self` is `0`.
Comment on lines +684 to +685
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Returns the index of the highest bit set to one in `self`, or `None`
/// if `self` is `0`.
/// Returns the index of the highest bit set to one in `self`.

not needed for NonZero

///
/// # Examples
///
/// ```
/// #![feature(int_lowest_highest_one)]
///
/// # use core::num::NonZero;
/// # fn main() { test().unwrap(); }
/// # fn test() -> Option<()> {
#[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0x1)?.highest_one(), 0);")]
#[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0x10)?.highest_one(), 4);")]
#[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0x1f)?.highest_one(), 4);")]
/// # Some(())
/// # }
/// ```
#[unstable(feature = "int_lowest_highest_one", issue = "145203")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn highest_one(self) -> u32 {
Self::BITS - 1 - self.leading_zeros()
}

/// Returns the index of the lowest bit set to one in `self`, or `None`
/// if `self` is `0`.
///
/// # Examples
///
/// ```
/// #![feature(int_lowest_highest_one)]
///
/// # use core::num::NonZero;
/// # fn main() { test().unwrap(); }
/// # fn test() -> Option<()> {
#[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0x1)?.lowest_one(), 0);")]
#[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0x10)?.lowest_one(), 4);")]
#[doc = concat!("assert_eq!(NonZero::<", stringify!($Int), ">::new(0x1f)?.lowest_one(), 0);")]
/// # Some(())
/// # }
/// ```
#[unstable(feature = "int_lowest_highest_one", issue = "145203")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn lowest_one(self) -> u32 {
self.trailing_zeros()
}

/// Returns the number of ones in the binary representation of `self`.
///
/// # Examples
Expand Down
48 changes: 48 additions & 0 deletions library/core/src/num/uint_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,54 @@ macro_rules! uint_impl {
self & self.wrapping_neg()
}

/// Returns the index of the highest bit set to one in `self`, or `None`
/// if `self` is `0`.
///
/// # Examples
///
/// ```
/// #![feature(int_lowest_highest_one)]
///
#[doc = concat!("assert_eq!(0x0_", stringify!($SelfT), ".highest_one(), None);")]
#[doc = concat!("assert_eq!(0x1_", stringify!($SelfT), ".highest_one(), Some(0));")]
#[doc = concat!("assert_eq!(0x10_", stringify!($SelfT), ".highest_one(), Some(4));")]
#[doc = concat!("assert_eq!(0x1f_", stringify!($SelfT), ".highest_one(), Some(4));")]
/// ```
#[unstable(feature = "int_lowest_highest_one", issue = "145203")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn highest_one(self) -> Option<u32> {
match NonZero::new(self) {
Some(v) => Some(v.highest_one()),
None => None,
}
}

/// Returns the index of the lowest bit set to one in `self`, or `None`
/// if `self` is `0`.
///
/// # Examples
///
/// ```
/// #![feature(int_lowest_highest_one)]
///
#[doc = concat!("assert_eq!(0x0_", stringify!($SelfT), ".lowest_one(), None);")]
#[doc = concat!("assert_eq!(0x1_", stringify!($SelfT), ".lowest_one(), Some(0));")]
#[doc = concat!("assert_eq!(0x10_", stringify!($SelfT), ".lowest_one(), Some(4));")]
#[doc = concat!("assert_eq!(0x1f_", stringify!($SelfT), ".lowest_one(), Some(0));")]
/// ```
#[unstable(feature = "int_lowest_highest_one", issue = "145203")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn lowest_one(self) -> Option<u32> {
match NonZero::new(self) {
Some(v) => Some(v.lowest_one()),
None => None,
}
}

/// Returns the bit pattern of `self` reinterpreted as a signed integer of the same size.
///
/// This produces the same result as an `as` cast, but ensures that the bit-width remains
Expand Down
1 change: 1 addition & 0 deletions library/coretests/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#![feature(generic_assert_internals)]
#![feature(hasher_prefixfree_extras)]
#![feature(hashmap_internals)]
#![feature(int_lowest_highest_one)]
#![feature(int_roundings)]
#![feature(ip)]
#![feature(ip_from)]
Expand Down
118 changes: 118 additions & 0 deletions library/coretests/tests/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,3 +462,121 @@ fn test_nonzero_fmt() {

assert_eq!(i, nz);
}

#[test]
fn test_nonzero_highest_one() {
macro_rules! nonzero_int_impl {
($($T:ty),+) => {
$(
{
const ONE: $T = 1;
const MINUS_ONE: $T = -1;
Comment on lines +472 to +473
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these constants necessary?


for i in 0..<$T>::BITS {
// Set single bit.
assert_eq!(NonZero::<$T>::new(ONE << i).unwrap().highest_one(), i);
if i > <$T>::BITS {
// Set lowest bits.
assert_eq!(
NonZero::<$T>::new(<$T>::MAX >> i).unwrap().highest_one(),
<$T>::BITS - i - 2,
);
}
// Set highest bits.
assert_eq!(
NonZero::<$T>::new(MINUS_ONE << i).unwrap().highest_one(),
<$T>::BITS - 1,
);
}
}
)+
};
}

macro_rules! nonzero_uint_impl {
($($T:ty),+) => {
$(
{
const ONE: $T = 1;

for i in 0..<$T>::BITS {
// Set single bit.
assert_eq!(NonZero::<$T>::new(ONE << i).unwrap().highest_one(), i);
// Set lowest bits.
assert_eq!(
NonZero::<$T>::new(<$T>::MAX >> i).unwrap().highest_one(),
<$T>::BITS - i - 1,
);
// Set highest bits.
assert_eq!(
NonZero::<$T>::new(<$T>::MAX << i).unwrap().highest_one(),
<$T>::BITS - 1,
);
}
}
)+
};
}

nonzero_int_impl!(i8, i16, i32, i64, i128, isize);
nonzero_uint_impl!(u8, u16, u32, u64, u128, usize);
}

#[test]
fn test_nonzero_lowest_one() {
macro_rules! nonzero_int_impl {
($($T:ty),+) => {
$(
{
const ONE: $T = 1;
const MINUS_ONE: $T = -1;

for i in 0..<$T>::BITS {
// Set single bit.
assert_eq!(NonZero::<$T>::new(ONE << i).unwrap().lowest_one(), i);
if i > <$T>::BITS {
// Set lowest bits.
assert_eq!(
NonZero::<$T>::new(<$T>::MAX >> i).unwrap().lowest_one(),
0,
);
}
// Set highest bits.
assert_eq!(
NonZero::<$T>::new(MINUS_ONE << i).unwrap().lowest_one(),
i,
);
}
}
)+
};
}

macro_rules! nonzero_uint_impl {
($($T:ty),+) => {
$(
{
const ONE: $T = 1;

for i in 0..<$T>::BITS {
// Set single bit.
assert_eq!(NonZero::<$T>::new(ONE << i).unwrap().lowest_one(), i);
// Set lowest bits.
assert_eq!(
NonZero::<$T>::new(<$T>::MAX >> i).unwrap().lowest_one(),
0,
);
// Set highest bits.
assert_eq!(
NonZero::<$T>::new(<$T>::MAX << i).unwrap().lowest_one(),
i,
);
}
}
)+
};
}

nonzero_int_impl!(i8, i16, i32, i64, i128, isize);
nonzero_uint_impl!(u8, u16, u32, u64, u128, usize);
}
40 changes: 40 additions & 0 deletions library/coretests/tests/num/int_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,46 @@ macro_rules! int_module {
}
}

#[test]
fn test_highest_one() {
const ZERO: $T = 0;
const ONE: $T = 1;
const MINUS_ONE: $T = -1;

assert_eq!(ZERO.highest_one(), None);

for i in 0..<$T>::BITS {
// Set single bit.
assert_eq!((ONE << i).highest_one(), Some(i));
if i != <$T>::BITS - 1 {
// Set lowest bits.
assert_eq!((<$T>::MAX >> i).highest_one(), Some(<$T>::BITS - i - 2));
}
// Set highest bits.
assert_eq!((MINUS_ONE << i).highest_one(), Some(<$T>::BITS - 1));
}
}

#[test]
fn test_lowest_one() {
const ZERO: $T = 0;
const ONE: $T = 1;
const MINUS_ONE: $T = -1;

assert_eq!(ZERO.lowest_one(), None);

for i in 0..<$T>::BITS {
// Set single bit.
assert_eq!((ONE << i).lowest_one(), Some(i));
if i != <$T>::BITS - 1 {
// Set lowest bits.
assert_eq!((<$T>::MAX >> i).lowest_one(), Some(0));
}
// Set highest bits.
assert_eq!((MINUS_ONE << i).lowest_one(), Some(i));
}
}

#[test]
fn test_from_str() {
fn from_str<T: std::str::FromStr>(t: &str) -> Option<T> {
Expand Down
34 changes: 34 additions & 0 deletions library/coretests/tests/num/uint_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,40 @@ macro_rules! uint_module {
}
}

#[test]
fn test_highest_one() {
const ZERO: $T = 0;
const ONE: $T = 1;

assert_eq!(ZERO.highest_one(), None);

for i in 0..<$T>::BITS {
// Set single bit.
assert_eq!((ONE << i).highest_one(), Some(i));
// Set lowest bits.
assert_eq!((<$T>::MAX >> i).highest_one(), Some(<$T>::BITS - i - 1));
// Set highest bits.
assert_eq!((<$T>::MAX << i).highest_one(), Some(<$T>::BITS - 1));
}
}

#[test]
fn test_lowest_one() {
const ZERO: $T = 0;
const ONE: $T = 1;

assert_eq!(ZERO.lowest_one(), None);

for i in 0..<$T>::BITS {
// Set single bit.
assert_eq!((ONE << i).lowest_one(), Some(i));
// Set lowest bits.
assert_eq!((<$T>::MAX >> i).lowest_one(), Some(0));
// Set highest bits.
assert_eq!((<$T>::MAX << i).lowest_one(), Some(i));
}
}

fn from_str<T: core::str::FromStr>(t: &str) -> Option<T> {
core::str::FromStr::from_str(t).ok()
}
Expand Down
Loading