Skip to content

Commit

Permalink
checked_ilog: remove duplication by delegating to unsigned integers
Browse files Browse the repository at this point in the history
  • Loading branch information
FedericoStra committed Sep 22, 2023
1 parent 0f9a4d9 commit 3de51c9
Showing 1 changed file with 3 additions and 22 deletions.
25 changes: 3 additions & 22 deletions library/core/src/num/int_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2476,28 +2476,9 @@ macro_rules! int_impl {
if self <= 0 || base <= 1 {
None
} else {
let mut n = 0;
let mut r = 1;

// Optimization for 128 bit wide integers.
if Self::BITS == 128 {
// The following is a correct lower bound for ⌊log(base,self)⌋ because
//
// log(base,self) = log(2,self) / log(2,base)
// ≥ ⌊log(2,self)⌋ / (⌊log(2,base)⌋ + 1)
//
// hence
//
// ⌊log(base,self)⌋ ≥ ⌊ ⌊log(2,self)⌋ / (⌊log(2,base)⌋ + 1) ⌋ .
n = self.ilog2() / (base.ilog2() + 1);
r = base.pow(n);
}

while r <= self / base {
n += 1;
r *= base;
}
Some(n)
// Delegate to the unsigned implementation.
// The condition makes sure that both casts are exact.
(self as $UnsignedT).checked_ilog(base as $UnsignedT)
}
}

Expand Down

0 comments on commit 3de51c9

Please sign in to comment.