Auto merge of #124294 - tspiteri:ilog-first-iter, r=the8472 · model-checking/verify-rust-std@cd04000 (original) (raw)

Skip to content

Provide feedback

Saved searches

Use saved searches to filter your results more quickly

Sign up

Appearance settings

Commit cd04000

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.

File tree

1 file changed

lines changed

1 file changed

lines changed

Lines changed: 5 additions & 2 deletions

Original file line number Diff line number Diff line change
@@ -1148,9 +1148,12 @@ macro_rules! uint_impl {
1148 1148 pub const fn checked_ilog(self, base: Self) -> Option<u32> {
1149 1149 if self <= 0 |
1150 1150 None
1151 +} else if self < base {
1152 +Some(0)
1151 1153 } 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;
1154 1157
1155 1158 // Optimization for 128 bit wide integers.
1156 1159 if Self::BITS == 128 {