Coverage Report

Created: 2026-06-17 13:10

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
472
    static void transfer(const uint8_t* src, const uint8_t* src_end, uint8_t* dst) {
35
472
        const auto flip_case_mask = 'A' ^ 'a';
36
37
472
#if defined(__SSE2__) || defined(__aarch64__)
38
472
        const auto bytes_sse = sizeof(__m128i);
39
472
        const auto src_end_sse = src_end - (src_end - src) % bytes_sse;
40
41
472
        const auto v_not_case_lower_bound = _mm_set1_epi8(not_case_lower_bound - 1);
42
472
        const auto v_not_case_upper_bound = _mm_set1_epi8(not_case_upper_bound + 1);
43
472
        const auto v_flip_case_mask = _mm_set1_epi8(flip_case_mask);
44
45
621
        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
472
#endif
54
55
3.65k
        for (; src < src_end; ++src, ++dst)
56
3.18k
            if (*src >= not_case_lower_bound && *src <= not_case_upper_bound)
57
1.24k
                *dst = *src ^ flip_case_mask;
58
1.93k
            else
59
1.93k
                *dst = *src;
60
472
    }
_ZN5doris4simd14LowerUpperImplILc65ELc90EE8transferEPKhS4_Ph
Line
Count
Source
34
380
    static void transfer(const uint8_t* src, const uint8_t* src_end, uint8_t* dst) {
35
380
        const auto flip_case_mask = 'A' ^ 'a';
36
37
380
#if defined(__SSE2__) || defined(__aarch64__)
38
380
        const auto bytes_sse = sizeof(__m128i);
39
380
        const auto src_end_sse = src_end - (src_end - src) % bytes_sse;
40
41
380
        const auto v_not_case_lower_bound = _mm_set1_epi8(not_case_lower_bound - 1);
42
380
        const auto v_not_case_upper_bound = _mm_set1_epi8(not_case_upper_bound + 1);
43
380
        const auto v_flip_case_mask = _mm_set1_epi8(flip_case_mask);
44
45
476
        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
380
#endif
54
55
2.94k
        for (; src < src_end; ++src, ++dst)
56
2.56k
            if (*src >= not_case_lower_bound && *src <= not_case_upper_bound)
57
811
                *dst = *src ^ flip_case_mask;
58
1.74k
            else
59
1.74k
                *dst = *src;
60
380
    }
_ZN5doris4simd14LowerUpperImplILc97ELc122EE8transferEPKhS4_Ph
Line
Count
Source
34
92
    static void transfer(const uint8_t* src, const uint8_t* src_end, uint8_t* dst) {
35
92
        const auto flip_case_mask = 'A' ^ 'a';
36
37
92
#if defined(__SSE2__) || defined(__aarch64__)
38
92
        const auto bytes_sse = sizeof(__m128i);
39
92
        const auto src_end_sse = src_end - (src_end - src) % bytes_sse;
40
41
92
        const auto v_not_case_lower_bound = _mm_set1_epi8(not_case_lower_bound - 1);
42
92
        const auto v_not_case_upper_bound = _mm_set1_epi8(not_case_upper_bound + 1);
43
92
        const auto v_flip_case_mask = _mm_set1_epi8(flip_case_mask);
44
45
145
        for (; src < src_end_sse; src += bytes_sse, dst += bytes_sse) {
46
53
            const auto chars = _mm_loadu_si128(reinterpret_cast<const __m128i*>(src));
47
53
            const auto is_not_case = _mm_and_si128(_mm_cmpgt_epi8(chars, v_not_case_lower_bound),
48
53
                                                   _mm_cmplt_epi8(chars, v_not_case_upper_bound));
49
53
            const auto xor_mask = _mm_and_si128(v_flip_case_mask, is_not_case);
50
53
            const auto cased_chars = _mm_xor_si128(chars, xor_mask);
51
53
            _mm_storeu_si128(reinterpret_cast<__m128i*>(dst), cased_chars);
52
53
        }
53
92
#endif
54
55
716
        for (; src < src_end; ++src, ++dst)
56
624
            if (*src >= not_case_lower_bound && *src <= not_case_upper_bound)
57
435
                *dst = *src ^ flip_case_mask;
58
189
            else
59
189
                *dst = *src;
60
92
    }
61
};
62
} // namespace doris::simd