Rollup merge of #145381 - Gnurou:int_lowest_highest_one, r=jhpratt

Implement feature `int_lowest_highest_one` for integer and NonZero types

Tracking issue: rust-lang/rust#145203

Implement the accepted ACP rust-lang/rust#145203 for methods that find the index of the least significant (lowest) and most significant (highest) set bit in an integer for signed, unsigned, and NonZero types.

Also add unit tests for all these types.
This commit is contained in:
Jacob Pratt 2025-08-20 00:45:56 -04:00 committed by GitHub
commit 5a0c9386a2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 321 additions and 0 deletions

View file

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

View file

@ -681,6 +681,54 @@ macro_rules! nonzero_integer {
unsafe { NonZero::new_unchecked(n) }
}
/// Returns the index of the highest bit set to one in `self`.
///
/// # 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`.
///
/// # 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

View file

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

View file

@ -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(is_ascii_octdigit)]

View file

@ -462,3 +462,111 @@ fn test_nonzero_fmt() {
assert_eq!(i, nz);
}
#[test]
fn test_nonzero_highest_one() {
macro_rules! nonzero_int_impl {
($($T:ty),+) => {
$(
{
for i in 0..<$T>::BITS {
// Set single bit.
assert_eq!(NonZero::<$T>::new(1 << 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(-1 << i).unwrap().highest_one(),
<$T>::BITS - 1,
);
}
}
)+
};
}
macro_rules! nonzero_uint_impl {
($($T:ty),+) => {
$(
{
for i in 0..<$T>::BITS {
// Set single bit.
assert_eq!(NonZero::<$T>::new(1 << 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),+) => {
$(
{
for i in 0..<$T>::BITS {
// Set single bit.
assert_eq!(NonZero::<$T>::new(1 << 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(-1 << i).unwrap().lowest_one(),
i,
);
}
}
)+
};
}
macro_rules! nonzero_uint_impl {
($($T:ty),+) => {
$(
{
for i in 0..<$T>::BITS {
// Set single bit.
assert_eq!(NonZero::<$T>::new(1 << 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);
}

View file

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

View file

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