Skip to content

Commit cd04000

Browse files
committed
Auto merge of rust-lang#124294 - tspiteri:ilog-first-iter, r=the8472
Unroll first iteration of checked_ilog loop This follows the optimization of rust-lang#115913. As shown in rust-lang#115913 (comment), the performance was improved in all important cases, but some regressions were introduced for the benchmarks `u32_log_random_small`, `u8_log_random` and `u8_log_random_small`. Basically, rust-lang#115913 changed the implementation from one division per iteration to one multiplication per iteration plus one division. When there are zero iterations, this is a regression from zero divisions to one division. This PR avoids this by avoiding the division if we need zero iterations by returning `Some(0)` early. It also reduces the number of multiplications by one in all other cases.
2 parents 7f0b19d + ad38f9b commit cd04000

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

core/src/num/uint_macros.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1148,9 +1148,12 @@ macro_rules! uint_impl {
11481148
pub const fn checked_ilog(self, base: Self) -> Option<u32> {
11491149
if self <= 0 || base <= 1 {
11501150
None
1151+
} else if self < base {
1152+
Some(0)
11511153
} else {
1152-
let mut n = 0;
1153-
let mut r = 1;
1154+
// Since base >= self, n >= 1
1155+
let mut n = 1;
1156+
let mut r = base;
11541157

11551158
// Optimization for 128 bit wide integers.
11561159
if Self::BITS == 128 {

0 commit comments

Comments
 (0)