Skip to content

Commit 547b713

Browse files
committed
Optimize checked_ilog when base is a power of two
if base == 2 ** k, then log(base, n) == log(2, n) / k
1 parent 4056082 commit 547b713

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

library/core/src/num/uint_macros.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1573,7 +1573,14 @@ macro_rules! uint_impl {
15731573
if core::intrinsics::is_val_statically_known(base) {
15741574
if base == 2 {
15751575
return self.checked_ilog2();
1576-
} else if base == 10 {
1576+
}
1577+
// change of base:
1578+
// if base == 2 ** k, then
1579+
// log(base, n) == log(2, n) / k
1580+
if base.is_power_of_two() {
1581+
return Some(try_opt!(self.checked_ilog2()) / base.ilog2());
1582+
}
1583+
if base == 10 {
15771584
return self.checked_ilog10();
15781585
}
15791586
}

0 commit comments

Comments
 (0)