diff --git a/library/core/src/num/uint_macros.rs b/library/core/src/num/uint_macros.rs index 10d9498d15e45..acf4ca9a287c2 100644 --- a/library/core/src/num/uint_macros.rs +++ b/library/core/src/num/uint_macros.rs @@ -1491,6 +1491,20 @@ macro_rules! uint_impl { without modifying the original"] #[inline] pub const fn checked_ilog(self, base: Self) -> Option { + // Inform compiler of optimizations when the base is known at + // compile time and there's a cheaper method available. + // + // Note: Like all optimizations, this is not guaranteed to be + // applied by the compiler. If you want those specific bases, + // use `.checked_ilog2()` or `.checked_ilog10()` directly. + if core::intrinsics::is_val_statically_known(base) { + if base == 2 { + return self.checked_ilog2(); + } else if base == 10 { + return self.checked_ilog10(); + } + } + if self <= 0 || base <= 1 { None } else if self < base {