Skip to content

Commit

Permalink
Rollup merge of #100332 - eholk:stabilize-ilog, r=scottmcm
Browse files Browse the repository at this point in the history
Rename integer log* methods to ilog*

This reflects the concensus from the libs team as reported at #70887 (comment).

Joint work with `@yoshuawuyts.`
  • Loading branch information
matthiaskrgr authored Aug 10, 2022
2 parents f9d6cc5 + 0fd4db3 commit 0cbecda
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 134 deletions.
6 changes: 3 additions & 3 deletions library/core/benches/num/int_log/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ macro_rules! int_log_bench {
for n in 0..(<$t>::BITS / 8) {
for i in 1..=(100 as $t) {
let x = black_box(i << (n * 8));
black_box(x.log10());
black_box(x.ilog10());
}
}
});
Expand All @@ -27,7 +27,7 @@ macro_rules! int_log_bench {
.collect();
bench.iter(|| {
for x in &numbers {
black_box(black_box(x).log10());
black_box(black_box(x).ilog10());
}
});
}
Expand All @@ -44,7 +44,7 @@ macro_rules! int_log_bench {
.collect();
bench.iter(|| {
for x in &numbers {
black_box(black_box(x).log10());
black_box(black_box(x).ilog10());
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/num/bignum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ macro_rules! define_bignum {
// Find the most significant non-zero digit.
let msd = digits.iter().rposition(|&x| x != 0);
match msd {
Some(msd) => msd * digitbits + digits[msd].log2() as usize + 1,
Some(msd) => msd * digitbits + digits[msd].ilog2() as usize + 1,
// There are no non-zero digits, i.e., the number is zero.
_ => 0,
}
Expand Down
38 changes: 19 additions & 19 deletions library/core/src/num/int_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2204,7 +2204,7 @@ macro_rules! int_impl {
/// rounded down.
///
/// This method might not be optimized owing to implementation details;
/// `log2` can produce results more efficiently for base 2, and `log10`
/// `ilog2` can produce results more efficiently for base 2, and `ilog10`
/// can produce results more efficiently for base 10.
///
/// # Panics
Expand All @@ -2217,7 +2217,7 @@ macro_rules! int_impl {
///
/// ```
/// #![feature(int_log)]
#[doc = concat!("assert_eq!(5", stringify!($SelfT), ".log(5), 1);")]
#[doc = concat!("assert_eq!(5", stringify!($SelfT), ".ilog(5), 1);")]
/// ```
#[unstable(feature = "int_log", issue = "70887")]
#[must_use = "this returns the result of the operation, \
Expand All @@ -2226,8 +2226,8 @@ macro_rules! int_impl {
#[track_caller]
#[rustc_inherit_overflow_checks]
#[allow(arithmetic_overflow)]
pub const fn log(self, base: Self) -> u32 {
match self.checked_log(base) {
pub const fn ilog(self, base: Self) -> u32 {
match self.checked_ilog(base) {
Some(n) => n,
None => {
// In debug builds, trigger a panic on None.
Expand All @@ -2250,7 +2250,7 @@ macro_rules! int_impl {
///
/// ```
/// #![feature(int_log)]
#[doc = concat!("assert_eq!(2", stringify!($SelfT), ".log2(), 1);")]
#[doc = concat!("assert_eq!(2", stringify!($SelfT), ".ilog2(), 1);")]
/// ```
#[unstable(feature = "int_log", issue = "70887")]
#[must_use = "this returns the result of the operation, \
Expand All @@ -2259,8 +2259,8 @@ macro_rules! int_impl {
#[track_caller]
#[rustc_inherit_overflow_checks]
#[allow(arithmetic_overflow)]
pub const fn log2(self) -> u32 {
match self.checked_log2() {
pub const fn ilog2(self) -> u32 {
match self.checked_ilog2() {
Some(n) => n,
None => {
// In debug builds, trigger a panic on None.
Expand All @@ -2283,7 +2283,7 @@ macro_rules! int_impl {
///
/// ```
/// #![feature(int_log)]
#[doc = concat!("assert_eq!(10", stringify!($SelfT), ".log10(), 1);")]
#[doc = concat!("assert_eq!(10", stringify!($SelfT), ".ilog10(), 1);")]
/// ```
#[unstable(feature = "int_log", issue = "70887")]
#[must_use = "this returns the result of the operation, \
Expand All @@ -2292,8 +2292,8 @@ macro_rules! int_impl {
#[track_caller]
#[rustc_inherit_overflow_checks]
#[allow(arithmetic_overflow)]
pub const fn log10(self) -> u32 {
match self.checked_log10() {
pub const fn ilog10(self) -> u32 {
match self.checked_ilog10() {
Some(n) => n,
None => {
// In debug builds, trigger a panic on None.
Expand All @@ -2311,20 +2311,20 @@ macro_rules! int_impl {
/// Returns `None` if the number is negative or zero, or if the base is not at least 2.
///
/// This method might not be optimized owing to implementation details;
/// `checked_log2` can produce results more efficiently for base 2, and
/// `checked_log10` can produce results more efficiently for base 10.
/// `checked_ilog2` can produce results more efficiently for base 2, and
/// `checked_ilog10` can produce results more efficiently for base 10.
///
/// # Examples
///
/// ```
/// #![feature(int_log)]
#[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_log(5), Some(1));")]
#[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_ilog(5), Some(1));")]
/// ```
#[unstable(feature = "int_log", issue = "70887")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_log(self, base: Self) -> Option<u32> {
pub const fn checked_ilog(self, base: Self) -> Option<u32> {
if self <= 0 || base <= 1 {
None
} else {
Expand All @@ -2333,7 +2333,7 @@ macro_rules! int_impl {

// Optimization for 128 bit wide integers.
if Self::BITS == 128 {
let b = Self::log2(self) / (Self::log2(base) + 1);
let b = Self::ilog2(self) / (Self::ilog2(base) + 1);
n += b;
r /= base.pow(b as u32);
}
Expand All @@ -2354,13 +2354,13 @@ macro_rules! int_impl {
///
/// ```
/// #![feature(int_log)]
#[doc = concat!("assert_eq!(2", stringify!($SelfT), ".checked_log2(), Some(1));")]
#[doc = concat!("assert_eq!(2", stringify!($SelfT), ".checked_ilog2(), Some(1));")]
/// ```
#[unstable(feature = "int_log", issue = "70887")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_log2(self) -> Option<u32> {
pub const fn checked_ilog2(self) -> Option<u32> {
if self <= 0 {
None
} else {
Expand All @@ -2378,13 +2378,13 @@ macro_rules! int_impl {
///
/// ```
/// #![feature(int_log)]
#[doc = concat!("assert_eq!(10", stringify!($SelfT), ".checked_log10(), Some(1));")]
#[doc = concat!("assert_eq!(10", stringify!($SelfT), ".checked_ilog10(), Some(1));")]
/// ```
#[unstable(feature = "int_log", issue = "70887")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_log10(self) -> Option<u32> {
pub const fn checked_ilog10(self) -> Option<u32> {
if self > 0 {
Some(int_log10::$ActualT(self as $ActualT))
} else {
Expand Down
20 changes: 10 additions & 10 deletions library/core/src/num/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ macro_rules! nonzero_unsigned_operations {
/// Returns the base 2 logarithm of the number, rounded down.
///
/// This is the same operation as
#[doc = concat!("[`", stringify!($Int), "::log2`],")]
#[doc = concat!("[`", stringify!($Int), "::ilog2`],")]
/// except that it has no failure cases to worry about
/// since this value can never be zero.
///
Expand All @@ -460,22 +460,22 @@ macro_rules! nonzero_unsigned_operations {
/// #![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);")]
#[doc = concat!("assert_eq!(", stringify!($Ty), "::new(7).unwrap().ilog2(), 2);")]
#[doc = concat!("assert_eq!(", stringify!($Ty), "::new(8).unwrap().ilog2(), 3);")]
#[doc = concat!("assert_eq!(", stringify!($Ty), "::new(9).unwrap().ilog2(), 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 {
pub const fn ilog2(self) -> u32 {
Self::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`],")]
#[doc = concat!("[`", stringify!($Int), "::ilog10`],")]
/// except that it has no failure cases to worry about
/// since this value can never be zero.
///
Expand All @@ -485,15 +485,15 @@ macro_rules! nonzero_unsigned_operations {
/// #![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);")]
#[doc = concat!("assert_eq!(", stringify!($Ty), "::new(99).unwrap().ilog10(), 1);")]
#[doc = concat!("assert_eq!(", stringify!($Ty), "::new(100).unwrap().ilog10(), 2);")]
#[doc = concat!("assert_eq!(", stringify!($Ty), "::new(101).unwrap().ilog10(), 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 {
pub const fn ilog10(self) -> u32 {
super::int_log10::$Int(self.0)
}
}
Expand Down
40 changes: 20 additions & 20 deletions library/core/src/num/uint_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ macro_rules! uint_impl {
///
/// ```
/// #![feature(int_log)]
#[doc = concat!("assert_eq!(5", stringify!($SelfT), ".log(5), 1);")]
#[doc = concat!("assert_eq!(5", stringify!($SelfT), ".ilog(5), 1);")]
/// ```
#[unstable(feature = "int_log", issue = "70887")]
#[must_use = "this returns the result of the operation, \
Expand All @@ -709,8 +709,8 @@ macro_rules! uint_impl {
#[track_caller]
#[rustc_inherit_overflow_checks]
#[allow(arithmetic_overflow)]
pub const fn log(self, base: Self) -> u32 {
match self.checked_log(base) {
pub const fn ilog(self, base: Self) -> u32 {
match self.checked_ilog(base) {
Some(n) => n,
None => {
// In debug builds, trigger a panic on None.
Expand All @@ -733,7 +733,7 @@ macro_rules! uint_impl {
///
/// ```
/// #![feature(int_log)]
#[doc = concat!("assert_eq!(2", stringify!($SelfT), ".log2(), 1);")]
#[doc = concat!("assert_eq!(2", stringify!($SelfT), ".ilog2(), 1);")]
/// ```
#[unstable(feature = "int_log", issue = "70887")]
#[must_use = "this returns the result of the operation, \
Expand All @@ -742,8 +742,8 @@ macro_rules! uint_impl {
#[track_caller]
#[rustc_inherit_overflow_checks]
#[allow(arithmetic_overflow)]
pub const fn log2(self) -> u32 {
match self.checked_log2() {
pub const fn ilog2(self) -> u32 {
match self.checked_ilog2() {
Some(n) => n,
None => {
// In debug builds, trigger a panic on None.
Expand All @@ -766,7 +766,7 @@ macro_rules! uint_impl {
///
/// ```
/// #![feature(int_log)]
#[doc = concat!("assert_eq!(10", stringify!($SelfT), ".log10(), 1);")]
#[doc = concat!("assert_eq!(10", stringify!($SelfT), ".ilog10(), 1);")]
/// ```
#[unstable(feature = "int_log", issue = "70887")]
#[must_use = "this returns the result of the operation, \
Expand All @@ -775,8 +775,8 @@ macro_rules! uint_impl {
#[track_caller]
#[rustc_inherit_overflow_checks]
#[allow(arithmetic_overflow)]
pub const fn log10(self) -> u32 {
match self.checked_log10() {
pub const fn ilog10(self) -> u32 {
match self.checked_ilog10() {
Some(n) => n,
None => {
// In debug builds, trigger a panic on None.
Expand All @@ -794,20 +794,20 @@ macro_rules! uint_impl {
/// Returns `None` if the number is zero, or if the base is not at least 2.
///
/// This method might not be optimized owing to implementation details;
/// `checked_log2` can produce results more efficiently for base 2, and
/// `checked_log10` can produce results more efficiently for base 10.
/// `checked_ilog2` can produce results more efficiently for base 2, and
/// `checked_ilog10` can produce results more efficiently for base 10.
///
/// # Examples
///
/// ```
/// #![feature(int_log)]
#[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_log(5), Some(1));")]
#[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_ilog(5), Some(1));")]
/// ```
#[unstable(feature = "int_log", issue = "70887")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_log(self, base: Self) -> Option<u32> {
pub const fn checked_ilog(self, base: Self) -> Option<u32> {
if self <= 0 || base <= 1 {
None
} else {
Expand All @@ -816,7 +816,7 @@ macro_rules! uint_impl {

// Optimization for 128 bit wide integers.
if Self::BITS == 128 {
let b = Self::log2(self) / (Self::log2(base) + 1);
let b = Self::ilog2(self) / (Self::ilog2(base) + 1);
n += b;
r /= base.pow(b as u32);
}
Expand All @@ -837,15 +837,15 @@ macro_rules! uint_impl {
///
/// ```
/// #![feature(int_log)]
#[doc = concat!("assert_eq!(2", stringify!($SelfT), ".checked_log2(), Some(1));")]
#[doc = concat!("assert_eq!(2", stringify!($SelfT), ".checked_ilog2(), Some(1));")]
/// ```
#[unstable(feature = "int_log", issue = "70887")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_log2(self) -> Option<u32> {
pub const fn checked_ilog2(self) -> Option<u32> {
if let Some(x) = <$NonZeroT>::new(self) {
Some(x.log2())
Some(x.ilog2())
} else {
None
}
Expand All @@ -859,15 +859,15 @@ macro_rules! uint_impl {
///
/// ```
/// #![feature(int_log)]
#[doc = concat!("assert_eq!(10", stringify!($SelfT), ".checked_log10(), Some(1));")]
#[doc = concat!("assert_eq!(10", stringify!($SelfT), ".checked_ilog10(), Some(1));")]
/// ```
#[unstable(feature = "int_log", issue = "70887")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn checked_log10(self) -> Option<u32> {
pub const fn checked_ilog10(self) -> Option<u32> {
if let Some(x) = <$NonZeroT>::new(self) {
Some(x.log10())
Some(x.ilog10())
} else {
None
}
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1135,7 +1135,7 @@ impl fmt::Debug for Duration {
// 2. The postfix: can be "µs" so we have to count UTF8 characters.
let mut actual_w = prefix.len() + postfix.chars().count();
// 3. The integer part:
if let Some(log) = integer_part.checked_log10() {
if let Some(log) = integer_part.checked_ilog10() {
// integer_part is > 0, so has length log10(x)+1
actual_w += 1 + log as usize;
} else {
Expand Down
Loading

0 comments on commit 0cbecda

Please sign in to comment.