Coverage Report

Created: 2026-04-15 18:59

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
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
126
        for (; src < src_end_sse; src += bytes_sse, dst += bytes_sse) {
46
38
            const auto chars = _mm_loadu_si128(reinterpret_cast<const __m128i*>(src));
47
38
            const auto is_not_case = _mm_and_si128(_mm_cmpgt_epi8(chars, v_not_case_lower_bound),
48
38
                                                   _mm_cmplt_epi8(chars, v_not_case_upper_bound));
49
38
            const auto xor_mask = _mm_and_si128(v_flip_case_mask, is_not_case);
50
38
            const auto cased_chars = _mm_xor_si128(chars, xor_mask);
51
38
            _mm_storeu_si128(reinterpret_cast<__m128i*>(dst), cased_chars);
52
38
        }
53
88
#endif
54
55
708
        for (; src < src_end; ++src, ++dst)
56
620
            if (*src >= not_case_lower_bound && *src <= not_case_upper_bound)
57
304
                *dst = *src ^ flip_case_mask;
58
316
            else
59
316
                *dst = *src;
60
88
    }
_ZN5doris4simd14LowerUpperImplILc65ELc90EE8transferEPKhS4_Ph
Line
Count
Source
34
42
    static void transfer(const uint8_t* src, const uint8_t* src_end, uint8_t* dst) {
35
42
        const auto flip_case_mask = 'A' ^ 'a';
36
37
42
#if defined(__SSE2__) || defined(__aarch64__)
38
42
        const auto bytes_sse = sizeof(__m128i);
39
42
        const auto src_end_sse = src_end - (src_end - src) % bytes_sse;
40
41
42
        const auto v_not_case_lower_bound = _mm_set1_epi8(not_case_lower_bound - 1);
42
42
        const auto v_not_case_upper_bound = _mm_set1_epi8(not_case_upper_bound + 1);
43
42
        const auto v_flip_case_mask = _mm_set1_epi8(flip_case_mask);
44
45
46
        for (; src < src_end_sse; src += bytes_sse, dst += bytes_sse) {
46
4
            const auto chars = _mm_loadu_si128(reinterpret_cast<const __m128i*>(src));
47
4
            const auto is_not_case = _mm_and_si128(_mm_cmpgt_epi8(chars, v_not_case_lower_bound),
48
4
                                                   _mm_cmplt_epi8(chars, v_not_case_upper_bound));
49
4
            const auto xor_mask = _mm_and_si128(v_flip_case_mask, is_not_case);
50
4
            const auto cased_chars = _mm_xor_si128(chars, xor_mask);
51
4
            _mm_storeu_si128(reinterpret_cast<__m128i*>(dst), cased_chars);
52
4
        }
53
42
#endif
54
55
388
        for (; src < src_end; ++src, ++dst)
56
346
            if (*src >= not_case_lower_bound && *src <= not_case_upper_bound)
57
200
                *dst = *src ^ flip_case_mask;
58
146
            else
59
146
                *dst = *src;
60
42
    }
_ZN5doris4simd14LowerUpperImplILc97ELc122EE8transferEPKhS4_Ph
Line
Count
Source
34
46
    static void transfer(const uint8_t* src, const uint8_t* src_end, uint8_t* dst) {
35
46
        const auto flip_case_mask = 'A' ^ 'a';
36
37
46
#if defined(__SSE2__) || defined(__aarch64__)
38
46
        const auto bytes_sse = sizeof(__m128i);
39
46
        const auto src_end_sse = src_end - (src_end - src) % bytes_sse;
40
41
46
        const auto v_not_case_lower_bound = _mm_set1_epi8(not_case_lower_bound - 1);
42
46
        const auto v_not_case_upper_bound = _mm_set1_epi8(not_case_upper_bound + 1);
43
46
        const auto v_flip_case_mask = _mm_set1_epi8(flip_case_mask);
44
45
80
        for (; src < src_end_sse; src += bytes_sse, dst += bytes_sse) {
46
34
            const auto chars = _mm_loadu_si128(reinterpret_cast<const __m128i*>(src));
47
34
            const auto is_not_case = _mm_and_si128(_mm_cmpgt_epi8(chars, v_not_case_lower_bound),
48
34
                                                   _mm_cmplt_epi8(chars, v_not_case_upper_bound));
49
34
            const auto xor_mask = _mm_and_si128(v_flip_case_mask, is_not_case);
50
34
            const auto cased_chars = _mm_xor_si128(chars, xor_mask);
51
34
            _mm_storeu_si128(reinterpret_cast<__m128i*>(dst), cased_chars);
52
34
        }
53
46
#endif
54
55
320
        for (; src < src_end; ++src, ++dst)
56
274
            if (*src >= not_case_lower_bound && *src <= not_case_upper_bound)
57
104
                *dst = *src ^ flip_case_mask;
58
170
            else
59
170
                *dst = *src;
60
46
    }
61
};
62
} // namespace doris::simd