-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Conversation
This saves an instruction compared to the previous approach, which was to unset the fifth bit with bitwise OR.
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. |
The Godbolt checks out, and MCA says it will be less cycles. Nice catch! |
📌 Commit 1e6365d has been approved by |
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.
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
Nice. If you're in the area @redzic have a look at rust/library/core/src/char/methods.rs Line 1129 in edba282
|
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.