-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Optimize .ilog({2,10})
to .ilog{2,10}()
#145776
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
clarfonthey
reviewed
Aug 23, 2025
8947c19
to
859a95c
Compare
I squashed commits because checking the squashed commit is easier than checking the changes between the old commits. |
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()`
859a95c
to
2c21b88
Compare
Great idea! |
bors
added a commit
that referenced
this pull request
Aug 30, 2025
Rollup of 5 pull requests Successful merges: - #143462 (fix(lib-std-fs): handle `usize` overflow in `read*`) - #144651 (Implementation: `#[feature(nonpoison_condvar)]`) - #145465 (Stabilize `array_repeat` feature) - #145776 (Optimize `.ilog({2,10})` to `.ilog{2,10}()`) - #145969 (Add Duration::from_nanos_u128) r? `@ghost` `@rustbot` modify labels: rollup
rust-timer
added a commit
that referenced
this pull request
Aug 30, 2025
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()`
scottmcm
reviewed
Sep 8, 2025
// Note: Like all optimizations, this is not guaranteed to be | ||
// applied by the compiler. If you want those specific bases, | ||
// use `.checked_ilog2()` or `.checked_ilog10()` directly. | ||
if core::intrinsics::is_val_statically_known(base) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great use for this intrinsic! Nice.
github-actions bot
pushed a commit
to model-checking/verify-rust-std
that referenced
this pull request
Sep 9, 2025
…boet 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()`
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
S-waiting-on-bors
Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
T-libs
Relevant to the library team, which will review and decide on the PR/issue.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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()
Test crate aarch64 assembly diffs/function aliases
Related source code
Test crate source code
Assembly differences generation script (change $asmfile)
Base 2 (compare new vs new to check that assembly is optimized)
i8 checked base 2
i8 base 2
i16 checked base 2
i16 base 2
i32 checked base 2
i32 base 2
i64 checked base 2
i64 base 2
i128 checked base 2
i128 base 2
isize checked base 2
isize base 2
u8 checked base 2
u8 base 2
u16 checked base 2
u16 base 2
u32 checked base 2
u32 base 2
u64 checked base 2
u64 base 2
u128 checked base 2
u128 base 2
usize checked base 2
usize base 2
Base 5 (compare nightly vs new to check that assembly stays the same)
i8 checked base 5
i8 base 5
i16 checked base 5
i16 base 5
i32 checked base 5
i32 base 5
i64 checked base 5
i64 base 5
i128 checked base 5
i128 base 5
isize checked base 5
isize base 5
u8 checked base 5
u8 base 5
u16 checked base 5
u16 base 5
u32 checked base 5
u32 base 5
u64 checked base 5
u64 base 5
u128 checked base 5
u128 base 5
usize checked base 5
usize base 5
Base 10 (compare new vs new to check that assembly is optimized)
i8 checked base 10
i8 base 10
i16 checked base 10
i16 base 10
i32 checked base 10
i32 base 10
i64 checked base 10
i64 base 10
i128 checked base 10
i128 base 10
isize checked base 10
isize base 10
u8 checked base 10
u8 base 10
u16 checked base 10
u16 base 10
u32 checked base 10
u32 base 10
u64 checked base 10
u64 base 10
u128 checked base 10
u128 base 10
usize checked base 10
usize base 10
Unknown base (compare nightly vs new to check that assembly stays the same)
i8 checked base unknown
i8 base unknown
i16 checked base unknown
i16 base unknown
i32 checked base unknown
i32 base unknown
i64 checked base unknown
i64 base unknown
i128 checked base unknown
i128 base unknown
isize checked base unknown
isize base unknown
u8 checked base unknown
u8 base unknown
u16 checked base unknown
u16 base unknown
u32 checked base unknown
u32 base unknown
u64 checked base unknown
u64 base unknown
u128 checked base unknown
u128 base unknown
usize checked base unknown
usize base unknown