Skip to content

Commit dfbba07

Browse files
authored
Rollup merge of #145776 - ChaiTRex:ilog_specialization, r=joboet
Optimize `.ilog({2,10})` to `.ilog{2,10}()` 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()`
2 parents b5c19e8 + 2c21b88 commit dfbba07

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

library/core/src/num/uint_macros.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,6 +1491,20 @@ 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+
//
1497+
// Note: Like all optimizations, this is not guaranteed to be
1498+
// applied by the compiler. If you want those specific bases,
1499+
// use `.checked_ilog2()` or `.checked_ilog10()` directly.
1500+
if core::intrinsics::is_val_statically_known(base) {
1501+
if base == 2 {
1502+
return self.checked_ilog2();
1503+
} else if base == 10 {
1504+
return self.checked_ilog10();
1505+
}
1506+
}
1507+
14941508
if self <= 0 || base <= 1 {
14951509
None
14961510
} else if self < base {

0 commit comments

Comments
 (0)