Skip to content
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

Use bitwise XOR in to_ascii_uppercase #95831

Merged
merged 1 commit into from
Apr 9, 2022
Merged

Conversation

redzic
Copy link
Contributor

@redzic redzic commented Apr 9, 2022

This saves an instruction compared to the previous approach, which
was to unset the fifth bit with bitwise OR.

Comparison of generated assembly on x86: https://godbolt.org/z/GdfvdGs39

This can also affect autovectorization, saving SIMD instructions as well: https://godbolt.org/z/cnPcz75T9

Not sure if u8::to_ascii_lowercase should also be changed, since using bitwise OR for that function does not require an extra bitwise negate since the code is setting a bit rather than unsetting a bit. char::to_ascii_uppercase already uses XOR, so no change seems to be required there.

This saves an instruction compared to the previous approach, which
was to unset the fifth bit with bitwise OR.
@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @m-ou-se (or someone else) soon.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 9, 2022
@workingjubilee
Copy link
Member

The Godbolt checks out, and MCA says it will be less cycles. Nice catch!
@bors r+ rollup=always

@bors
Copy link
Contributor

bors commented Apr 9, 2022

📌 Commit 1e6365d has been approved by workingjubilee

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 9, 2022
@m-ou-se m-ou-se assigned workingjubilee and unassigned m-ou-se Apr 9, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Apr 9, 2022
Use bitwise XOR in to_ascii_uppercase

This saves an instruction compared to the previous approach, which
was to unset the fifth bit with bitwise OR.

Comparison of generated assembly on x86: https://godbolt.org/z/GdfvdGs39

This can also affect autovectorization, saving SIMD instructions as well: https://godbolt.org/z/cnPcz75T9

Not sure if `u8::to_ascii_lowercase` should also be changed, since using bitwise OR for that function does not require an extra bitwise negate since the code is setting a bit rather than unsetting a bit. `char::to_ascii_uppercase` already uses XOR, so no change seems to be required there.
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 9, 2022
Rollup of 7 pull requests

Successful merges:

 - rust-lang#94794 (Clarify indexing into Strings)
 - rust-lang#95361 (Make non-power-of-two alignments a validity error in `Layout`)
 - rust-lang#95369 (Fix `x test src/librustdoc` with `download-rustc` enabled )
 - rust-lang#95805 (Left overs of rust-lang#95761)
 - rust-lang#95808 (expand: Remove `ParseSess::missing_fragment_specifiers`)
 - rust-lang#95817 (hide another #[allow] directive from a docs example)
 - rust-lang#95831 (Use bitwise XOR in to_ascii_uppercase)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 7726265 into rust-lang:master Apr 9, 2022
@rustbot rustbot added this to the 1.62.0 milestone Apr 9, 2022
@redzic redzic deleted the xor-uppercase branch April 9, 2022 22:54
@gilescope
Copy link
Contributor

Nice. If you're in the area @redzic have a look at

pub const fn to_ascii_uppercase(&self) -> char {
- I wonder if we can avoid the if? I had a quick try in godbolt but couldn't see anything that improved on what we had but you might fair better. I did find a few things that did seem worth doing in this area: #96141

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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants