github.com/goshafaq/sonic@v0.0.0-20231026082336-871835fb94c6/native/to_lower.c (about)

     1  #include "native.h"
     2  
     3  /**
     4   * @brief uppercase to lowercase
     5   * @param[in] src original string
     6   * @param[in, out] dst lowercase string
     7   * @param[in] len length of the string
     8  */
     9  void to_lower(char* dst, const char* src, size_t len) {
    10      const __m128i _A = _mm_set1_epi8('A' - 1);
    11      const __m128i Z_ = _mm_set1_epi8('Z' + 1);
    12      const __m128i delta = _mm_set1_epi8('a' - 'A');
    13      char* q = dst;
    14  
    15      while (len >= 16){
    16          __m128i op = _mm_loadu_si128((__m128i*)src);
    17          __m128i gt = _mm_cmpgt_epi8(op, _A);
    18          __m128i lt = _mm_cmplt_epi8(op, Z_);
    19          __m128i mingle = _mm_and_si128(gt, lt);
    20          __m128i add = _mm_and_si128(mingle, delta);
    21          __m128i lower = _mm_add_epi8(op, add);
    22          _mm_storeu_si128((__m128i *)q, lower);
    23          src += 16;
    24          q += 16;
    25          len -= 16;
    26      };
    27  
    28      while(len > 0) {
    29          len--;
    30          bool isUpper = (*src >= 'A' && *src <= 'Z');
    31          *q = isUpper? (*src + 32): *src;
    32          src++;
    33          q++;
    34      }
    35  }