Skip to content

Commit 859a95c

Browse files
committed
Optimize .ilog({2,10}) to .ilog{2,10}()
Inform compiler of optimizations when the base is known at compile time and there's a cheaper method available: * `{integer}.checked_ilog(2)` -> `{integer}.checked_ilog2()` * `{integer}.checked_ilog(10)` -> `{integer}.checked_ilog10()` * `{integer}.ilog(2)` -> `{integer}.ilog2()` * `{integer}.ilog(10)` -> `{integer}.ilog10()`
1 parent 6ba0ce4 commit 859a95c

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

library/core/src/num/uint_macros.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,6 +1491,16 @@ macro_rules! uint_impl {
14911491
without modifying the original"]
14921492
#[inline]
14931493
pub const fn checked_ilog(self, base: Self) -> Option<u32> {
1494+
// Inform compiler of optimizations when the base is known at
1495+
// compile time and there's a cheaper method available.
1496+
if core::intrinsics::is_val_statically_known(base) {
1497+
if base == 2 {
1498+
return self.checked_ilog2();
1499+
} else if base == 10 {
1500+
return self.checked_ilog10();
1501+
}
1502+
}
1503+
14941504
if self <= 0 || base <= 1 {
14951505
None
14961506
} else if self < base {

0 commit comments

Comments
 (0)