Coverage Report

Created: 2026-03-15 15:32

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
329
    static void transfer(const uint8_t* src, const uint8_t* src_end, uint8_t* dst) {
35
329
        const auto flip_case_mask = 'A' ^ 'a';
36
37
329
#if defined(__SSE2__) || defined(__aarch64__)
38
329
        const auto bytes_sse = sizeof(__m128i);
39
329
        const auto src_end_sse = src_end - (src_end - src) % bytes_sse;
40
41
329
        const auto v_not_case_lower_bound = _mm_set1_epi8(not_case_lower_bound - 1);
42
329
        const auto v_not_case_upper_bound = _mm_set1_epi8(not_case_upper_bound + 1);
43
329
        const auto v_flip_case_mask = _mm_set1_epi8(flip_case_mask);
44
45
478
        for (; src < src_end_sse; src += bytes_sse, dst += bytes_sse) {
46
149
            const auto chars = _mm_loadu_si128(reinterpret_cast<const __m128i*>(src));
47
149
            const auto is_not_case = _mm_and_si128(_mm_cmpgt_epi8(chars, v_not_case_lower_bound),
48
149
                                                   _mm_cmplt_epi8(chars, v_not_case_upper_bound));
49
149
            const auto xor_mask = _mm_and_si128(v_flip_case_mask, is_not_case);
50
149
            const auto cased_chars = _mm_xor_si128(chars, xor_mask);
51
149
            _mm_storeu_si128(reinterpret_cast<__m128i*>(dst), cased_chars);
52
149
        }
53
329
#endif
54
55
2.43k
        for (; src < src_end; ++src, ++dst)
56
2.10k
            if (*src >= not_case_lower_bound && *src <= not_case_upper_bound)
57
519
                *dst = *src ^ flip_case_mask;
58
1.58k
            else
59
1.58k
                *dst = *src;
60
329
    }
_ZN5doris4simd14LowerUpperImplILc65ELc90EE8transferEPKhS4_Ph
Line
Count
Source
34
244
    static void transfer(const uint8_t* src, const uint8_t* src_end, uint8_t* dst) {
35
244
        const auto flip_case_mask = 'A' ^ 'a';
36
37
244
#if defined(__SSE2__) || defined(__aarch64__)
38
244
        const auto bytes_sse = sizeof(__m128i);
39
244
        const auto src_end_sse = src_end - (src_end - src) % bytes_sse;
40
41
244
        const auto v_not_case_lower_bound = _mm_set1_epi8(not_case_lower_bound - 1);
42
244
        const auto v_not_case_upper_bound = _mm_set1_epi8(not_case_upper_bound + 1);
43
244
        const auto v_flip_case_mask = _mm_set1_epi8(flip_case_mask);
44
45
341
        for (; src < src_end_sse; src += bytes_sse, dst += bytes_sse) {
46
97
            const auto chars = _mm_loadu_si128(reinterpret_cast<const __m128i*>(src));
47
97
            const auto is_not_case = _mm_and_si128(_mm_cmpgt_epi8(chars, v_not_case_lower_bound),
48
97
                                                   _mm_cmplt_epi8(chars, v_not_case_upper_bound));
49
97
            const auto xor_mask = _mm_and_si128(v_flip_case_mask, is_not_case);
50
97
            const auto cased_chars = _mm_xor_si128(chars, xor_mask);
51
97
            _mm_storeu_si128(reinterpret_cast<__m128i*>(dst), cased_chars);
52
97
        }
53
244
#endif
54
55
1.79k
        for (; src < src_end; ++src, ++dst)
56
1.55k
            if (*src >= not_case_lower_bound && *src <= not_case_upper_bound)
57
107
                *dst = *src ^ flip_case_mask;
58
1.44k
            else
59
1.44k
                *dst = *src;
60
244
    }
_ZN5doris4simd14LowerUpperImplILc97ELc122EE8transferEPKhS4_Ph
Line
Count
Source
34
85
    static void transfer(const uint8_t* src, const uint8_t* src_end, uint8_t* dst) {
35
85
        const auto flip_case_mask = 'A' ^ 'a';
36
37
85
#if defined(__SSE2__) || defined(__aarch64__)
38
85
        const auto bytes_sse = sizeof(__m128i);
39
85
        const auto src_end_sse = src_end - (src_end - src) % bytes_sse;
40
41
85
        const auto v_not_case_lower_bound = _mm_set1_epi8(not_case_lower_bound - 1);
42
85
        const auto v_not_case_upper_bound = _mm_set1_epi8(not_case_upper_bound + 1);
43
85
        const auto v_flip_case_mask = _mm_set1_epi8(flip_case_mask);
44
45
137
        for (; src < src_end_sse; src += bytes_sse, dst += bytes_sse) {
46
52
            const auto chars = _mm_loadu_si128(reinterpret_cast<const __m128i*>(src));
47
52
            const auto is_not_case = _mm_and_si128(_mm_cmpgt_epi8(chars, v_not_case_lower_bound),
48
52
                                                   _mm_cmplt_epi8(chars, v_not_case_upper_bound));
49
52
            const auto xor_mask = _mm_and_si128(v_flip_case_mask, is_not_case);
50
52
            const auto cased_chars = _mm_xor_si128(chars, xor_mask);
51
52
            _mm_storeu_si128(reinterpret_cast<__m128i*>(dst), cased_chars);
52
52
        }
53
85
#endif
54
55
641
        for (; src < src_end; ++src, ++dst)
56
556
            if (*src >= not_case_lower_bound && *src <= not_case_upper_bound)
57
412
                *dst = *src ^ flip_case_mask;
58
144
            else
59
144
                *dst = *src;
60
85
    }
61
};
62
} // namespace doris::simd