From 4a5d2b419698fcf7a5e3f68b51e9cdff8f872a26 Mon Sep 17 00:00:00 2001 From: Federico Stra Date: Fri, 15 Sep 2023 17:36:54 +0200 Subject: [PATCH] checked_ilog: improve performance Addresses #115874 --- library/core/src/num/int_macros.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/library/core/src/num/int_macros.rs b/library/core/src/num/int_macros.rs index 1f43520e1b30a..aed6a34788d86 100644 --- a/library/core/src/num/int_macros.rs +++ b/library/core/src/num/int_macros.rs @@ -2477,18 +2477,18 @@ macro_rules! int_impl { None } else { let mut n = 0; - let mut r = self; + let mut r = 1; // Optimization for 128 bit wide integers. if Self::BITS == 128 { let b = Self::ilog2(self) / (Self::ilog2(base) + 1); n += b; - r /= base.pow(b as u32); + r *= base.pow(b); } - while r >= base { - r /= base; + while r <= self / base { n += 1; + r *= base; } Some(n) }