Skip to content
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

Add log2 and log10 to NonZeroU* #92956

Merged
merged 1 commit into from
Jan 18, 2022
Merged
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
43 changes: 21 additions & 22 deletions library/core/src/num/int_log10.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
mod unchecked {
/// These functions compute the integer logarithm of their type, assuming
/// that someone has already checked that the the value is strictly positive.

// 0 < val <= u8::MAX
#[inline]
pub const fn u8(val: u8) -> u32 {
Expand Down Expand Up @@ -89,6 +91,24 @@ mod unchecked {
log + u64(val as u64)
}

#[cfg(target_pointer_width = "16")]
#[inline]
pub const fn usize(val: usize) -> u32 {
u16(val as _)
}

#[cfg(target_pointer_width = "32")]
#[inline]
pub const fn usize(val: usize) -> u32 {
u32(val as _)
}

#[cfg(target_pointer_width = "64")]
#[inline]
pub const fn usize(val: usize) -> u32 {
u64(val as _)
}

// 0 < val <= i8::MAX
#[inline]
pub const fn i8(val: i8) -> u32 {
Expand Down Expand Up @@ -118,24 +138,3 @@ mod unchecked {
pub const fn i128(val: i128) -> u32 {
u128(val as u128)
}
}

macro_rules! impl_checked {
($T:ident) => {
#[inline]
pub const fn $T(val: $T) -> Option<u32> {
if val > 0 { Some(unchecked::$T(val)) } else { None }
}
};
}

impl_checked! { u8 }
impl_checked! { u16 }
impl_checked! { u32 }
impl_checked! { u64 }
impl_checked! { u128 }
impl_checked! { i8 }
impl_checked! { i16 }
impl_checked! { i32 }
impl_checked! { i64 }
impl_checked! { i128 }
6 changes: 5 additions & 1 deletion library/core/src/num/int_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2362,7 +2362,11 @@ macro_rules! int_impl {
without modifying the original"]
#[inline]
pub const fn checked_log10(self) -> Option<u32> {
int_log10::$ActualT(self as $ActualT)
if self > 0 {
Some(int_log10::$ActualT(self as $ActualT))
} else {
None
}
}

/// Computes the absolute value of `self`.
Expand Down
16 changes: 8 additions & 8 deletions library/core/src/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ const ASCII_CASE_MASK: u8 = 0b0010_0000;

#[lang = "u8"]
impl u8 {
uint_impl! { u8, u8, i8, 8, 255, 2, "0x82", "0xa", "0x12", "0x12", "0x48", "[0x12]",
uint_impl! { u8, u8, i8, NonZeroU8, 8, 255, 2, "0x82", "0xa", "0x12", "0x12", "0x48", "[0x12]",
"[0x12]", "", "" }
widening_impl! { u8, u16, 8, unsigned }

Expand Down Expand Up @@ -813,21 +813,21 @@ impl u8 {

#[lang = "u16"]
impl u16 {
uint_impl! { u16, u16, i16, 16, 65535, 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48",
uint_impl! { u16, u16, i16, NonZeroU16, 16, 65535, 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48",
"[0x34, 0x12]", "[0x12, 0x34]", "", "" }
widening_impl! { u16, u32, 16, unsigned }
}

#[lang = "u32"]
impl u32 {
uint_impl! { u32, u32, i32, 32, 4294967295, 8, "0x10000b3", "0xb301", "0x12345678",
uint_impl! { u32, u32, i32, NonZeroU32, 32, 4294967295, 8, "0x10000b3", "0xb301", "0x12345678",
"0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78]", "", "" }
widening_impl! { u32, u64, 32, unsigned }
}

#[lang = "u64"]
impl u64 {
uint_impl! { u64, u64, i64, 64, 18446744073709551615, 12, "0xaa00000000006e1", "0x6e10aa",
uint_impl! { u64, u64, i64, NonZeroU64, 64, 18446744073709551615, 12, "0xaa00000000006e1", "0x6e10aa",
"0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48",
"[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
"[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
Expand All @@ -837,7 +837,7 @@ impl u64 {

#[lang = "u128"]
impl u128 {
uint_impl! { u128, u128, i128, 128, 340282366920938463463374607431768211455, 16,
uint_impl! { u128, u128, i128, NonZeroU128, 128, 340282366920938463463374607431768211455, 16,
"0x13f40000000000000000000000004f76", "0x4f7613f4", "0x12345678901234567890123456789012",
"0x12907856341290785634129078563412", "0x48091e6a2c48091e6a2c48091e6a2c48",
"[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
Expand All @@ -850,15 +850,15 @@ impl u128 {
#[cfg(target_pointer_width = "16")]
#[lang = "usize"]
impl usize {
uint_impl! { usize, u16, isize, 16, 65535, 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48",
uint_impl! { usize, u16, isize, NonZeroUsize, 16, 65535, 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48",
"[0x34, 0x12]", "[0x12, 0x34]",
usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
widening_impl! { usize, u32, 16, unsigned }
}
#[cfg(target_pointer_width = "32")]
#[lang = "usize"]
impl usize {
uint_impl! { usize, u32, isize, 32, 4294967295, 8, "0x10000b3", "0xb301", "0x12345678",
uint_impl! { usize, u32, isize, NonZeroUsize, 32, 4294967295, 8, "0x10000b3", "0xb301", "0x12345678",
"0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78]",
usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!() }
widening_impl! { usize, u64, 32, unsigned }
Expand All @@ -867,7 +867,7 @@ impl usize {
#[cfg(target_pointer_width = "64")]
#[lang = "usize"]
impl usize {
uint_impl! { usize, u64, isize, 64, 18446744073709551615, 12, "0xaa00000000006e1", "0x6e10aa",
uint_impl! { usize, u64, isize, NonZeroUsize, 64, 18446744073709551615, 12, "0xaa00000000006e1", "0x6e10aa",
"0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48",
"[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
"[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
Expand Down
52 changes: 51 additions & 1 deletion library/core/src/num/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ nonzero_integers_div! {

// A bunch of methods for unsigned nonzero types only.
macro_rules! nonzero_unsigned_operations {
( $( $Ty: ident($Int: ty); )+ ) => {
( $( $Ty: ident($Int: ident); )+ ) => {
$(
impl $Ty {
/// Add an unsigned integer to a non-zero value.
Expand Down Expand Up @@ -442,6 +442,56 @@ macro_rules! nonzero_unsigned_operations {
None
}
}

/// Returns the base 2 logarithm of the number, rounded down.
///
/// This is the same operation as
#[doc = concat!("[`", stringify!($Int), "::log2`],")]
/// except that it has no failure cases to worry about
/// since this value can never be zero.
///
/// # Examples
///
/// ```
/// #![feature(int_log)]
#[doc = concat!("# use std::num::", stringify!($Ty), ";")]
///
#[doc = concat!("assert_eq!(", stringify!($Ty), "::new(7).unwrap().log2(), 2);")]
#[doc = concat!("assert_eq!(", stringify!($Ty), "::new(8).unwrap().log2(), 3);")]
#[doc = concat!("assert_eq!(", stringify!($Ty), "::new(9).unwrap().log2(), 3);")]
/// ```
#[unstable(feature = "int_log", issue = "70887")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn log2(self) -> u32 {
<$Int>::BITS - 1 - self.leading_zeros()
}

/// Returns the base 10 logarithm of the number, rounded down.
///
/// This is the same operation as
#[doc = concat!("[`", stringify!($Int), "::log10`],")]
/// except that it has no failure cases to worry about
/// since this value can never be zero.
///
/// # Examples
///
/// ```
/// #![feature(int_log)]
#[doc = concat!("# use std::num::", stringify!($Ty), ";")]
///
#[doc = concat!("assert_eq!(", stringify!($Ty), "::new(99).unwrap().log10(), 1);")]
#[doc = concat!("assert_eq!(", stringify!($Ty), "::new(100).unwrap().log10(), 2);")]
#[doc = concat!("assert_eq!(", stringify!($Ty), "::new(101).unwrap().log10(), 2);")]
/// ```
#[unstable(feature = "int_log", issue = "70887")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn log10(self) -> u32 {
super::int_log10::$Int(self.0)
}
}
)+
}
Expand Down
17 changes: 10 additions & 7 deletions library/core/src/num/uint_macros.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
macro_rules! uint_impl {
($SelfT:ty, $ActualT:ident, $SignedT:ident, $BITS:expr, $MaxV:expr,
($SelfT:ty, $ActualT:ident, $SignedT:ident, $NonZeroT:ident,
$BITS:expr, $MaxV:expr,
$rot:expr, $rot_op:expr, $rot_result:expr, $swap_op:expr, $swapped:expr,
$reversed:expr, $le_bytes:expr, $be_bytes:expr,
$to_xe_bytes_doc:expr, $from_xe_bytes_doc:expr) => {
Expand Down Expand Up @@ -839,12 +840,10 @@ macro_rules! uint_impl {
without modifying the original"]
#[inline]
pub const fn checked_log2(self) -> Option<u32> {
if self <= 0 {
None
if let Some(x) = <$NonZeroT>::new(self) {
Some(x.log2())
} else {
// SAFETY: We just checked that this number is positive
let log = (Self::BITS - 1) - unsafe { intrinsics::ctlz_nonzero(self) as u32 };
Copy link
Member Author

Choose a reason for hiding this comment

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

One fewer unsafe, as a bonus.

Some(log)
None
}
}

Expand All @@ -863,7 +862,11 @@ macro_rules! uint_impl {
without modifying the original"]
#[inline]
pub const fn checked_log10(self) -> Option<u32> {
int_log10::$ActualT(self as $ActualT)
if let Some(x) = <$NonZeroT>::new(self) {
Some(x.log10())
} else {
None
}
}

/// Checked negation. Computes `-self`, returning `None` unless `self ==
Expand Down