Coverage Report

Created: 2026-05-09 05:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/simd/lower_upper_impl.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <stdint.h>
21
22
#include <iostream>
23
#include <string>
24
25
#include "util/sse_util.hpp"
26
27
// the code refer: https://clickhouse.tech/codebrowser/html_report//ClickHouse/src/Functions/LowerUpperImpl.h.html
28
// Doris only handle one character at a time, this function use SIMD to more characters at a time
29
namespace doris::simd {
30
31
template <char not_case_lower_bound, char not_case_upper_bound>
32
class LowerUpperImpl {
33
public:
34
478
    static void transfer(const uint8_t* src, const uint8_t* src_end, uint8_t* dst) {
35
478
        const auto flip_case_mask = 'A' ^ 'a';
36
37
478
#if defined(__SSE2__) || defined(__aarch64__)
38
478
        const auto bytes_sse = sizeof(__m128i);
39
478
        const auto src_end_sse = src_end - (src_end - src) % bytes_sse;
40
41
478
        const auto v_not_case_lower_bound = _mm_set1_epi8(not_case_lower_bound - 1);
42
478
        const auto v_not_case_upper_bound = _mm_set1_epi8(not_case_upper_bound + 1);
43
478
        const auto v_flip_case_mask = _mm_set1_epi8(flip_case_mask);
44
45
622
        for (; src < src_end_sse; src += bytes_sse, dst += bytes_sse) {
46
144
            const auto chars = _mm_loadu_si128(reinterpret_cast<const __m128i*>(src));
47
144
            const auto is_not_case = _mm_and_si128(_mm_cmpgt_epi8(chars, v_not_case_lower_bound),
48
144
                                                   _mm_cmplt_epi8(chars, v_not_case_upper_bound));
49
144
            const auto xor_mask = _mm_and_si128(v_flip_case_mask, is_not_case);
50
144
            const auto cased_chars = _mm_xor_si128(chars, xor_mask);
51
144
            _mm_storeu_si128(reinterpret_cast<__m128i*>(dst), cased_chars);
52
144
        }
53
478
#endif
54
55
3.66k
        for (; src < src_end; ++src, ++dst)
56
3.18k
            if (*src >= not_case_lower_bound && *src <= not_case_upper_bound)
57
1.23k
                *dst = *src ^ flip_case_mask;
58
1.94k
            else
59
1.94k
                *dst = *src;
60
478
    }
_ZN5doris4simd14LowerUpperImplILc65ELc90EE8transferEPKhS4_Ph
Line
Count
Source
34
390
    static void transfer(const uint8_t* src, const uint8_t* src_end, uint8_t* dst) {
35
390
        const auto flip_case_mask = 'A' ^ 'a';
36
37
390
#if defined(__SSE2__) || defined(__aarch64__)
38
390
        const auto bytes_sse = sizeof(__m128i);
39
390
        const auto src_end_sse = src_end - (src_end - src) % bytes_sse;
40
41
390
        const auto v_not_case_lower_bound = _mm_set1_epi8(not_case_lower_bound - 1);
42
390
        const auto v_not_case_upper_bound = _mm_set1_epi8(not_case_upper_bound + 1);
43
390
        const auto v_flip_case_mask = _mm_set1_epi8(flip_case_mask);
44
45
486
        for (; src < src_end_sse; src += bytes_sse, dst += bytes_sse) {
46
96
            const auto chars = _mm_loadu_si128(reinterpret_cast<const __m128i*>(src));
47
96
            const auto is_not_case = _mm_and_si128(_mm_cmpgt_epi8(chars, v_not_case_lower_bound),
48
96
                                                   _mm_cmplt_epi8(chars, v_not_case_upper_bound));
49
96
            const auto xor_mask = _mm_and_si128(v_flip_case_mask, is_not_case);
50
96
            const auto cased_chars = _mm_xor_si128(chars, xor_mask);
51
96
            _mm_storeu_si128(reinterpret_cast<__m128i*>(dst), cased_chars);
52
96
        }
53
390
#endif
54
55
2.99k
        for (; src < src_end; ++src, ++dst)
56
2.60k
            if (*src >= not_case_lower_bound && *src <= not_case_upper_bound)
57
851
                *dst = *src ^ flip_case_mask;
58
1.75k
            else
59
1.75k
                *dst = *src;
60
390
    }
_ZN5doris4simd14LowerUpperImplILc97ELc122EE8transferEPKhS4_Ph
Line
Count
Source
34
88
    static void transfer(const uint8_t* src, const uint8_t* src_end, uint8_t* dst) {
35
88
        const auto flip_case_mask = 'A' ^ 'a';
36
37
88
#if defined(__SSE2__) || defined(__aarch64__)
38
88
        const auto bytes_sse = sizeof(__m128i);
39
88
        const auto src_end_sse = src_end - (src_end - src) % bytes_sse;
40
41
88
        const auto v_not_case_lower_bound = _mm_set1_epi8(not_case_lower_bound - 1);
42
88
        const auto v_not_case_upper_bound = _mm_set1_epi8(not_case_upper_bound + 1);
43
88
        const auto v_flip_case_mask = _mm_set1_epi8(flip_case_mask);
44
45
136
        for (; src < src_end_sse; src += bytes_sse, dst += bytes_sse) {
46
48
            const auto chars = _mm_loadu_si128(reinterpret_cast<const __m128i*>(src));
47
48
            const auto is_not_case = _mm_and_si128(_mm_cmpgt_epi8(chars, v_not_case_lower_bound),
48
48
                                                   _mm_cmplt_epi8(chars, v_not_case_upper_bound));
49
48
            const auto xor_mask = _mm_and_si128(v_flip_case_mask, is_not_case);
50
48
            const auto cased_chars = _mm_xor_si128(chars, xor_mask);
51
48
            _mm_storeu_si128(reinterpret_cast<__m128i*>(dst), cased_chars);
52
48
        }
53
88
#endif
54
55
662
        for (; src < src_end; ++src, ++dst)
56
574
            if (*src >= not_case_lower_bound && *src <= not_case_upper_bound)
57
385
                *dst = *src ^ flip_case_mask;
58
189
            else
59
189
                *dst = *src;
60
88
    }
61
};
62
} // namespace doris::simd