checked_ilog: remove duplication by delegating to unsigned integers

This commit is contained in:
Federico Stra 2023-09-22 16:02:13 +02:00
parent 0f9a4d9ab4
commit 3de51c95a1

View File

@ -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)
}
}