From a6d9a9cddade9139616a980c8092431492b84f3c Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Mon, 23 Oct 2023 17:02:31 -0700 Subject: Performance improvements for `absl::AsciiStrToLower` and `absl::AsciiStrToUpper`. PiperOrigin-RevId: 575969640 Change-Id: If6ddc0a71debfe571c2739ec91fc99594bc36f88 --- absl/strings/ascii.cc | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'absl/strings/ascii.cc') diff --git a/absl/strings/ascii.cc b/absl/strings/ascii.cc index 8c6b1e05..1e6566e9 100644 --- a/absl/strings/ascii.cc +++ b/absl/strings/ascii.cc @@ -159,6 +159,20 @@ ABSL_DLL const char kToUpper[256] = { }; // clang-format on +// Returns whether `c` is in the a-z/A-Z range (w.r.t. `ToUpper`). +// Implemented by: +// 1. Pushing the a-z/A-Z range to [SCHAR_MIN, SCHAR_MIN + 26). +// 2. Comparing to SCHAR_MIN + 26. +template +constexpr bool AsciiInAZRange(unsigned char c) { + constexpr unsigned char sub = (ToUpper ? 'a' : 'A') - SCHAR_MIN; + constexpr signed char threshold = SCHAR_MIN + 26; // 26 = alphabet size. + // Using unsigned arithmetic as overflows/underflows are well defined. + unsigned char u = c - sub; + // Using signed cmp, as SIMD unsigned cmp isn't available in many platforms. + return static_cast(u) < threshold; +} + template constexpr void AsciiStrCaseFold(char* p, char* end) { // The upper- and lowercase versions of ASCII characters differ by only 1 bit. @@ -168,15 +182,9 @@ constexpr void AsciiStrCaseFold(char* p, char* end) { // have the same single bit difference. constexpr unsigned char kAsciiCaseBitFlip = 'a' ^ 'A'; - constexpr char ch_a = ToUpper ? 'a' : 'A'; - constexpr char ch_z = ToUpper ? 'z' : 'Z'; for (; p < end; ++p) { unsigned char v = static_cast(*p); - // We use & instead of && to ensure this always stays branchless - // We use static_cast to suppress -Wbitwise-instead-of-logical - bool is_in_range = static_cast(static_cast(ch_a <= v) & - static_cast(v <= ch_z)); - v ^= is_in_range ? kAsciiCaseBitFlip : 0; + v ^= AsciiInAZRange(v) ? kAsciiCaseBitFlip : 0; *p = static_cast(v); } } -- cgit v1.2.3