about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDylan DPC <99973273+Dylan-DPC@users.noreply.github.com>2022-04-09 18:26:30 +0200
committerGitHub <noreply@github.com>2022-04-09 18:26:30 +0200
commit7726265ae0d3d2b37b4254980a72d5d5347fb92d (patch)
treeb7068e427d11a5cbc64f5da386e58e102f671235
parent2464ea251010f6de62ec875b32f3463dfa326cfb (diff)
parent1e6365d0751c0c7b88a30ad5eab0becb6cc87f7e (diff)
downloadrust-7726265ae0d3d2b37b4254980a72d5d5347fb92d.tar.gz
rust-7726265ae0d3d2b37b4254980a72d5d5347fb92d.zip
Rollup merge of #95831 - redzic:xor-uppercase, r=workingjubilee
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.
-rw-r--r--library/core/src/num/mod.rs4
1 files changed, 2 insertions, 2 deletions
diff --git a/library/core/src/num/mod.rs b/library/core/src/num/mod.rs
index a30d2ff0ea6..98c9bf556bb 100644
--- a/library/core/src/num/mod.rs
+++ b/library/core/src/num/mod.rs
@@ -299,8 +299,8 @@ impl u8 {
     #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
     #[inline]
     pub const fn to_ascii_uppercase(&self) -> u8 {
-        // Unset the fifth bit if this is a lowercase letter
-        *self & !((self.is_ascii_lowercase() as u8) * ASCII_CASE_MASK)
+        // Toggle the fifth bit if this is a lowercase letter
+        *self ^ ((self.is_ascii_lowercase() as u8) * ASCII_CASE_MASK)
     }
 
     /// Makes a copy of the value in its ASCII lower case equivalent.