-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Labels
C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchCategory: An issue highlighting optimization opportunities or PRs implementing suchI-slowIssue: Problems and improvements with respect to performance of generated code.Issue: Problems and improvements with respect to performance of generated code.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Description
Currently checked_ilog
is implemented with iterated divisions:
rust/library/core/src/num/int_macros.rs
Lines 2475 to 2495 in e7f9f48
pub const fn checked_ilog(self, base: Self) -> Option<u32> { | |
if self <= 0 || base <= 1 { | |
None | |
} else { | |
let mut n = 0; | |
let mut r = self; | |
// Optimization for 128 bit wide integers. | |
if Self::BITS == 128 { | |
let b = Self::ilog2(self) / (Self::ilog2(base) + 1); | |
n += b; | |
r /= base.pow(b as u32); | |
} | |
while r >= base { | |
r /= base; | |
n += 1; | |
} | |
Some(n) | |
} | |
} |
It's pretty straightforward to convert it to iterated multiplications:
fn checked_int_log(self, base:Self) -> Option<u32> {
if self <= 0 || base <= 1 {
None
} else {
let mut n = 0;
let mut r = 1;
// Optimization for 128 bit wide integers.
if Self::BITS == 128 {
let b = Self::ilog2(self) / (Self::ilog2(base) + 1);
n += b;
r *= base.pow(b);
}
while r <= self / base {
n += 1;
r *= base;
}
Some(n)
}
}
and this results in some decent speedups (1.5x - 7x).
Metadata
Metadata
Assignees
Labels
C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchCategory: An issue highlighting optimization opportunities or PRs implementing suchI-slowIssue: Problems and improvements with respect to performance of generated code.Issue: Problems and improvements with respect to performance of generated code.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.