Skip to content

Commit 3de51c9

Browse files
committed
checked_ilog: remove duplication by delegating to unsigned integers
1 parent 0f9a4d9 commit 3de51c9

File tree

1 file changed

+3
-22
lines changed

1 file changed

+3
-22
lines changed

library/core/src/num/int_macros.rs

+3-22
Original file line numberDiff line numberDiff line change
@@ -2476,28 +2476,9 @@ macro_rules! int_impl {
24762476
if self <= 0 || base <= 1 {
24772477
None
24782478
} else {
2479-
let mut n = 0;
2480-
let mut r = 1;
2481-
2482-
// Optimization for 128 bit wide integers.
2483-
if Self::BITS == 128 {
2484-
// The following is a correct lower bound for ⌊log(base,self)⌋ because
2485-
//
2486-
// log(base,self) = log(2,self) / log(2,base)
2487-
// ≥ ⌊log(2,self)⌋ / (⌊log(2,base)⌋ + 1)
2488-
//
2489-
// hence
2490-
//
2491-
// ⌊log(base,self)⌋ ≥ ⌊ ⌊log(2,self)⌋ / (⌊log(2,base)⌋ + 1) ⌋ .
2492-
n = self.ilog2() / (base.ilog2() + 1);
2493-
r = base.pow(n);
2494-
}
2495-
2496-
while r <= self / base {
2497-
n += 1;
2498-
r *= base;
2499-
}
2500-
Some(n)
2479+
// Delegate to the unsigned implementation.
2480+
// The condition makes sure that both casts are exact.
2481+
(self as $UnsignedT).checked_ilog(base as $UnsignedT)
25012482
}
25022483
}
25032484

0 commit comments

Comments
 (0)