Coverage Report

Created: 2026-08-07 08:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/common/endian.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 "core/extended_types.h"
21
#include "core/uint24.h"
22
#include "util/unaligned.h"
23
24
namespace doris {
25
5.87k
inline uint64_t gbswap_64(uint64_t host_int) {
26
5.87k
#if defined(__GNUC__) && defined(__x86_64__) && !defined(__APPLE__)
27
    // Adapted from /usr/include/byteswap.h.  Not available on Mac.
28
5.87k
    if (__builtin_constant_p(host_int)) {
29
0
        return __bswap_constant_64(host_int);
30
5.87k
    } else {
31
5.87k
        uint64_t result;
32
5.87k
        __asm__("bswap %0" : "=r"(result) : "0"(host_int));
33
5.87k
        return result;
34
5.87k
    }
35
#elif defined(bswap_64)
36
    return bswap_64(host_int);
37
#else
38
    return static_cast<uint64_t>(bswap_32(static_cast<uint32_t>(host_int >> 32))) |
39
           (static_cast<uint64_t>(bswap_32(static_cast<uint32_t>(host_int))) << 32);
40
#endif // bswap_64
41
5.87k
}
42
43
3.90M
inline unsigned __int128 gbswap_128(unsigned __int128 host_int) {
44
3.90M
    return static_cast<unsigned __int128>(bswap_64(static_cast<uint64_t>(host_int >> 64))) |
45
3.90M
           (static_cast<unsigned __int128>(bswap_64(static_cast<uint64_t>(host_int))) << 64);
46
3.90M
}
47
48
1.46k
inline wide::UInt256 gbswap_256(wide::UInt256 host_int) {
49
1.46k
    wide::UInt256 result {gbswap_64(host_int.items[3]), gbswap_64(host_int.items[2]),
50
1.46k
                          gbswap_64(host_int.items[1]), gbswap_64(host_int.items[0])};
51
1.46k
    return result;
52
1.46k
}
53
54
// Swap bytes of a 24-bit value.
55
2.99k
inline uint32_t bswap_24(uint32_t x) {
56
2.99k
    return uint32_t((x & 0x0000ffULL) << 16) | ((x & 0x00ff00ULL)) | ((x & 0xff0000ULL) >> 16);
57
2.99k
}
58
59
// use std::byteswap after doris enable cpp23
60
template <typename T>
61
7.43M
T byte_swap(T x) {
62
7.43M
    if constexpr (sizeof(T) == sizeof(wide::Int256)) {
63
1.46k
        return gbswap_256(x);
64
3.90M
    } else if constexpr (sizeof(T) == sizeof(__int128)) {
65
3.90M
        return gbswap_128(x);
66
3.90M
    } else if constexpr (sizeof(T) == sizeof(int64_t)) {
67
1.14M
        return bswap_64(x);
68
2.37M
    } else if constexpr (sizeof(T) == sizeof(int32_t)) {
69
2.37M
        return bswap_32(x);
70
2.37M
    } else if constexpr (sizeof(T) == sizeof(doris::uint24_t)) {
71
2.99k
        return bswap_24(x);
72
7.54k
    } else if constexpr (sizeof(T) == sizeof(int16_t)) {
73
7.54k
        return bswap_16(x);
74
7.54k
    } else {
75
6.76k
        static_assert(sizeof(T) == 1, "Unsupported type size for byte_swap");
76
6.76k
        return x; // No byte swap needed for unsupported types
77
6.76k
    }
78
7.43M
}
_ZN5doris9byte_swapItEET_S1_
Line
Count
Source
61
7.54k
T byte_swap(T x) {
62
    if constexpr (sizeof(T) == sizeof(wide::Int256)) {
63
        return gbswap_256(x);
64
    } else if constexpr (sizeof(T) == sizeof(__int128)) {
65
        return gbswap_128(x);
66
    } else if constexpr (sizeof(T) == sizeof(int64_t)) {
67
        return bswap_64(x);
68
    } else if constexpr (sizeof(T) == sizeof(int32_t)) {
69
        return bswap_32(x);
70
    } else if constexpr (sizeof(T) == sizeof(doris::uint24_t)) {
71
        return bswap_24(x);
72
7.54k
    } else if constexpr (sizeof(T) == sizeof(int16_t)) {
73
7.54k
        return bswap_16(x);
74
    } else {
75
        static_assert(sizeof(T) == 1, "Unsupported type size for byte_swap");
76
        return x; // No byte swap needed for unsupported types
77
    }
78
7.54k
}
_ZN5doris9byte_swapIjEET_S1_
Line
Count
Source
61
2.37M
T byte_swap(T x) {
62
    if constexpr (sizeof(T) == sizeof(wide::Int256)) {
63
        return gbswap_256(x);
64
    } else if constexpr (sizeof(T) == sizeof(__int128)) {
65
        return gbswap_128(x);
66
    } else if constexpr (sizeof(T) == sizeof(int64_t)) {
67
        return bswap_64(x);
68
2.37M
    } else if constexpr (sizeof(T) == sizeof(int32_t)) {
69
2.37M
        return bswap_32(x);
70
    } else if constexpr (sizeof(T) == sizeof(doris::uint24_t)) {
71
        return bswap_24(x);
72
    } else if constexpr (sizeof(T) == sizeof(int16_t)) {
73
        return bswap_16(x);
74
    } else {
75
        static_assert(sizeof(T) == 1, "Unsupported type size for byte_swap");
76
        return x; // No byte swap needed for unsupported types
77
    }
78
2.37M
}
_ZN5doris9byte_swapImEET_S1_
Line
Count
Source
61
561k
T byte_swap(T x) {
62
    if constexpr (sizeof(T) == sizeof(wide::Int256)) {
63
        return gbswap_256(x);
64
    } else if constexpr (sizeof(T) == sizeof(__int128)) {
65
        return gbswap_128(x);
66
561k
    } else if constexpr (sizeof(T) == sizeof(int64_t)) {
67
561k
        return bswap_64(x);
68
    } else if constexpr (sizeof(T) == sizeof(int32_t)) {
69
        return bswap_32(x);
70
    } else if constexpr (sizeof(T) == sizeof(doris::uint24_t)) {
71
        return bswap_24(x);
72
    } else if constexpr (sizeof(T) == sizeof(int16_t)) {
73
        return bswap_16(x);
74
    } else {
75
        static_assert(sizeof(T) == 1, "Unsupported type size for byte_swap");
76
        return x; // No byte swap needed for unsupported types
77
    }
78
561k
}
_ZN5doris9byte_swapINS_8uint24_tEEET_S2_
Line
Count
Source
61
2.99k
T byte_swap(T x) {
62
    if constexpr (sizeof(T) == sizeof(wide::Int256)) {
63
        return gbswap_256(x);
64
    } else if constexpr (sizeof(T) == sizeof(__int128)) {
65
        return gbswap_128(x);
66
    } else if constexpr (sizeof(T) == sizeof(int64_t)) {
67
        return bswap_64(x);
68
    } else if constexpr (sizeof(T) == sizeof(int32_t)) {
69
        return bswap_32(x);
70
2.99k
    } else if constexpr (sizeof(T) == sizeof(doris::uint24_t)) {
71
2.99k
        return bswap_24(x);
72
    } else if constexpr (sizeof(T) == sizeof(int16_t)) {
73
        return bswap_16(x);
74
    } else {
75
        static_assert(sizeof(T) == 1, "Unsupported type size for byte_swap");
76
        return x; // No byte swap needed for unsupported types
77
    }
78
2.99k
}
_ZN5doris9byte_swapIoEET_S1_
Line
Count
Source
61
29.6k
T byte_swap(T x) {
62
    if constexpr (sizeof(T) == sizeof(wide::Int256)) {
63
        return gbswap_256(x);
64
29.6k
    } else if constexpr (sizeof(T) == sizeof(__int128)) {
65
29.6k
        return gbswap_128(x);
66
    } else if constexpr (sizeof(T) == sizeof(int64_t)) {
67
        return bswap_64(x);
68
    } else if constexpr (sizeof(T) == sizeof(int32_t)) {
69
        return bswap_32(x);
70
    } else if constexpr (sizeof(T) == sizeof(doris::uint24_t)) {
71
        return bswap_24(x);
72
    } else if constexpr (sizeof(T) == sizeof(int16_t)) {
73
        return bswap_16(x);
74
    } else {
75
        static_assert(sizeof(T) == 1, "Unsupported type size for byte_swap");
76
        return x; // No byte swap needed for unsupported types
77
    }
78
29.6k
}
_ZN5doris9byte_swapIhEET_S1_
Line
Count
Source
61
6.76k
T byte_swap(T x) {
62
    if constexpr (sizeof(T) == sizeof(wide::Int256)) {
63
        return gbswap_256(x);
64
    } else if constexpr (sizeof(T) == sizeof(__int128)) {
65
        return gbswap_128(x);
66
    } else if constexpr (sizeof(T) == sizeof(int64_t)) {
67
        return bswap_64(x);
68
    } else if constexpr (sizeof(T) == sizeof(int32_t)) {
69
        return bswap_32(x);
70
    } else if constexpr (sizeof(T) == sizeof(doris::uint24_t)) {
71
        return bswap_24(x);
72
    } else if constexpr (sizeof(T) == sizeof(int16_t)) {
73
        return bswap_16(x);
74
6.76k
    } else {
75
6.76k
        static_assert(sizeof(T) == 1, "Unsupported type size for byte_swap");
76
6.76k
        return x; // No byte swap needed for unsupported types
77
6.76k
    }
78
6.76k
}
_ZN5doris9byte_swapIN4wide7integerILm256EjEEEET_S4_
Line
Count
Source
61
1.46k
T byte_swap(T x) {
62
1.46k
    if constexpr (sizeof(T) == sizeof(wide::Int256)) {
63
1.46k
        return gbswap_256(x);
64
    } else if constexpr (sizeof(T) == sizeof(__int128)) {
65
        return gbswap_128(x);
66
    } else if constexpr (sizeof(T) == sizeof(int64_t)) {
67
        return bswap_64(x);
68
    } else if constexpr (sizeof(T) == sizeof(int32_t)) {
69
        return bswap_32(x);
70
    } else if constexpr (sizeof(T) == sizeof(doris::uint24_t)) {
71
        return bswap_24(x);
72
    } else if constexpr (sizeof(T) == sizeof(int16_t)) {
73
        return bswap_16(x);
74
    } else {
75
        static_assert(sizeof(T) == 1, "Unsupported type size for byte_swap");
76
        return x; // No byte swap needed for unsupported types
77
    }
78
1.46k
}
_ZN5doris9byte_swapIlEET_S1_
Line
Count
Source
61
584k
T byte_swap(T x) {
62
    if constexpr (sizeof(T) == sizeof(wide::Int256)) {
63
        return gbswap_256(x);
64
    } else if constexpr (sizeof(T) == sizeof(__int128)) {
65
        return gbswap_128(x);
66
584k
    } else if constexpr (sizeof(T) == sizeof(int64_t)) {
67
584k
        return bswap_64(x);
68
    } else if constexpr (sizeof(T) == sizeof(int32_t)) {
69
        return bswap_32(x);
70
    } else if constexpr (sizeof(T) == sizeof(doris::uint24_t)) {
71
        return bswap_24(x);
72
    } else if constexpr (sizeof(T) == sizeof(int16_t)) {
73
        return bswap_16(x);
74
    } else {
75
        static_assert(sizeof(T) == 1, "Unsupported type size for byte_swap");
76
        return x; // No byte swap needed for unsupported types
77
    }
78
584k
}
_ZN5doris9byte_swapInEET_S1_
Line
Count
Source
61
3.87M
T byte_swap(T x) {
62
    if constexpr (sizeof(T) == sizeof(wide::Int256)) {
63
        return gbswap_256(x);
64
3.87M
    } else if constexpr (sizeof(T) == sizeof(__int128)) {
65
3.87M
        return gbswap_128(x);
66
    } else if constexpr (sizeof(T) == sizeof(int64_t)) {
67
        return bswap_64(x);
68
    } else if constexpr (sizeof(T) == sizeof(int32_t)) {
69
        return bswap_32(x);
70
    } else if constexpr (sizeof(T) == sizeof(doris::uint24_t)) {
71
        return bswap_24(x);
72
    } else if constexpr (sizeof(T) == sizeof(int16_t)) {
73
        return bswap_16(x);
74
    } else {
75
        static_assert(sizeof(T) == 1, "Unsupported type size for byte_swap");
76
        return x; // No byte swap needed for unsupported types
77
    }
78
3.87M
}
Unexecuted instantiation: _ZN5doris9byte_swapIN4wide7integerILm256EiEEEET_S4_
Unexecuted instantiation: _ZN5doris9byte_swapIiEET_S1_
79
80
template <std::endian target, typename T>
81
91.5M
T to_endian(T value) {
82
91.5M
    if constexpr (std::endian::native == target) {
83
84.1M
        return value; // No swap needed
84
84.1M
    } else {
85
7.43M
        static_assert(std::endian::native == std::endian::big ||
86
7.43M
                              std::endian::native == std::endian::little,
87
7.43M
                      "Unsupported endianness");
88
7.43M
        return byte_swap(value);
89
7.43M
    }
90
91.5M
}
_ZN5doris9to_endianILSt6endian1234EtEET0_S2_
Line
Count
Source
81
48.5k
T to_endian(T value) {
82
48.5k
    if constexpr (std::endian::native == target) {
83
48.5k
        return value; // No swap needed
84
    } else {
85
        static_assert(std::endian::native == std::endian::big ||
86
                              std::endian::native == std::endian::little,
87
                      "Unsupported endianness");
88
        return byte_swap(value);
89
    }
90
48.5k
}
_ZN5doris9to_endianILSt6endian1234EjEET0_S2_
Line
Count
Source
81
83.8M
T to_endian(T value) {
82
83.8M
    if constexpr (std::endian::native == target) {
83
83.8M
        return value; // No swap needed
84
    } else {
85
        static_assert(std::endian::native == std::endian::big ||
86
                              std::endian::native == std::endian::little,
87
                      "Unsupported endianness");
88
        return byte_swap(value);
89
    }
90
83.8M
}
_ZN5doris9to_endianILSt6endian1234EmEET0_S2_
Line
Count
Source
81
175k
T to_endian(T value) {
82
175k
    if constexpr (std::endian::native == target) {
83
175k
        return value; // No swap needed
84
    } else {
85
        static_assert(std::endian::native == std::endian::big ||
86
                              std::endian::native == std::endian::little,
87
                      "Unsupported endianness");
88
        return byte_swap(value);
89
    }
90
175k
}
_ZN5doris9to_endianILSt6endian4321EtEET0_S2_
Line
Count
Source
81
7.54k
T to_endian(T value) {
82
    if constexpr (std::endian::native == target) {
83
        return value; // No swap needed
84
7.54k
    } else {
85
7.54k
        static_assert(std::endian::native == std::endian::big ||
86
7.54k
                              std::endian::native == std::endian::little,
87
7.54k
                      "Unsupported endianness");
88
7.54k
        return byte_swap(value);
89
7.54k
    }
90
7.54k
}
_ZN5doris9to_endianILSt6endian4321EjEET0_S2_
Line
Count
Source
81
2.37M
T to_endian(T value) {
82
    if constexpr (std::endian::native == target) {
83
        return value; // No swap needed
84
2.37M
    } else {
85
2.37M
        static_assert(std::endian::native == std::endian::big ||
86
2.37M
                              std::endian::native == std::endian::little,
87
2.37M
                      "Unsupported endianness");
88
2.37M
        return byte_swap(value);
89
2.37M
    }
90
2.37M
}
_ZN5doris9to_endianILSt6endian4321EmEET0_S2_
Line
Count
Source
81
561k
T to_endian(T value) {
82
    if constexpr (std::endian::native == target) {
83
        return value; // No swap needed
84
561k
    } else {
85
561k
        static_assert(std::endian::native == std::endian::big ||
86
561k
                              std::endian::native == std::endian::little,
87
561k
                      "Unsupported endianness");
88
561k
        return byte_swap(value);
89
561k
    }
90
561k
}
_ZN5doris9to_endianILSt6endian1234EoEET0_S2_
Line
Count
Source
81
48.8k
T to_endian(T value) {
82
48.8k
    if constexpr (std::endian::native == target) {
83
48.8k
        return value; // No swap needed
84
    } else {
85
        static_assert(std::endian::native == std::endian::big ||
86
                              std::endian::native == std::endian::little,
87
                      "Unsupported endianness");
88
        return byte_swap(value);
89
    }
90
48.8k
}
_ZN5doris9to_endianILSt6endian4321ENS_8uint24_tEEET0_S3_
Line
Count
Source
81
2.99k
T to_endian(T value) {
82
    if constexpr (std::endian::native == target) {
83
        return value; // No swap needed
84
2.99k
    } else {
85
2.99k
        static_assert(std::endian::native == std::endian::big ||
86
2.99k
                              std::endian::native == std::endian::little,
87
2.99k
                      "Unsupported endianness");
88
2.99k
        return byte_swap(value);
89
2.99k
    }
90
2.99k
}
_ZN5doris9to_endianILSt6endian4321EoEET0_S2_
Line
Count
Source
81
29.6k
T to_endian(T value) {
82
    if constexpr (std::endian::native == target) {
83
        return value; // No swap needed
84
29.6k
    } else {
85
29.6k
        static_assert(std::endian::native == std::endian::big ||
86
29.6k
                              std::endian::native == std::endian::little,
87
29.6k
                      "Unsupported endianness");
88
29.6k
        return byte_swap(value);
89
29.6k
    }
90
29.6k
}
_ZN5doris9to_endianILSt6endian4321EhEET0_S2_
Line
Count
Source
81
6.76k
T to_endian(T value) {
82
    if constexpr (std::endian::native == target) {
83
        return value; // No swap needed
84
6.76k
    } else {
85
6.76k
        static_assert(std::endian::native == std::endian::big ||
86
6.76k
                              std::endian::native == std::endian::little,
87
6.76k
                      "Unsupported endianness");
88
6.76k
        return byte_swap(value);
89
6.76k
    }
90
6.76k
}
_ZN5doris9to_endianILSt6endian4321EN4wide7integerILm256EjEEEET0_S5_
Line
Count
Source
81
1.46k
T to_endian(T value) {
82
    if constexpr (std::endian::native == target) {
83
        return value; // No swap needed
84
1.46k
    } else {
85
1.46k
        static_assert(std::endian::native == std::endian::big ||
86
1.46k
                              std::endian::native == std::endian::little,
87
1.46k
                      "Unsupported endianness");
88
1.46k
        return byte_swap(value);
89
1.46k
    }
90
1.46k
}
_ZN5doris9to_endianILSt6endian4321ElEET0_S2_
Line
Count
Source
81
584k
T to_endian(T value) {
82
    if constexpr (std::endian::native == target) {
83
        return value; // No swap needed
84
584k
    } else {
85
584k
        static_assert(std::endian::native == std::endian::big ||
86
584k
                              std::endian::native == std::endian::little,
87
584k
                      "Unsupported endianness");
88
584k
        return byte_swap(value);
89
584k
    }
90
584k
}
_ZN5doris9to_endianILSt6endian4321EnEET0_S2_
Line
Count
Source
81
3.87M
T to_endian(T value) {
82
    if constexpr (std::endian::native == target) {
83
        return value; // No swap needed
84
3.87M
    } else {
85
3.87M
        static_assert(std::endian::native == std::endian::big ||
86
3.87M
                              std::endian::native == std::endian::little,
87
3.87M
                      "Unsupported endianness");
88
3.87M
        return byte_swap(value);
89
3.87M
    }
90
3.87M
}
Unexecuted instantiation: _ZN5doris9to_endianILSt6endian4321EN4wide7integerILm256EiEEEET0_S5_
Unexecuted instantiation: _ZN5doris9to_endianILSt6endian4321EiEET0_S2_
91
92
// Utilities to convert numbers between the current hosts's native byte
93
// order and little-endian byte order
94
//
95
// Load/Store methods are alignment safe
96
class LittleEndian {
97
public:
98
    // Functions to do unaligned loads and stores in little-endian order.
99
0
    static uint16_t Load16(const void* p) {
100
0
        return to_endian<std::endian::little>(unaligned_load<uint16_t>(p));
101
0
    }
102
103
0
    static void Store16(void* p, uint16_t v) {
104
0
        unaligned_store<uint16_t>(p, to_endian<std::endian::little>(v));
105
0
    }
106
107
5.66M
    static uint32_t Load32(const void* p) {
108
5.66M
        return to_endian<std::endian::little>(unaligned_load<uint32_t>(p));
109
5.66M
    }
110
111
2.33k
    static void Store32(void* p, uint32_t v) {
112
2.33k
        unaligned_store<uint32_t>(p, to_endian<std::endian::little>(v));
113
2.33k
    }
114
115
4.20k
    static uint64_t Load64(const void* p) {
116
4.20k
        return to_endian<std::endian::little>(unaligned_load<uint64_t>(p));
117
4.20k
    }
118
119
1.93k
    static void Store64(void* p, uint64_t v) {
120
1.93k
        unaligned_store<uint64_t>(p, to_endian<std::endian::little>(v));
121
1.93k
    }
122
};
123
124
// Utilities to convert numbers between the current hosts's native byte
125
// order and big-endian byte order (same as network byte order)
126
//
127
// Load/Store methods are alignment safe
128
class BigEndian {
129
public:
130
    // Functions to do unaligned loads and stores in little-endian order.
131
0
    static uint16_t Load16(const void* p) {
132
0
        return to_endian<std::endian::big>(unaligned_load<uint16_t>(p));
133
0
    }
134
135
0
    static void Store16(void* p, uint16_t v) {
136
0
        unaligned_store<uint16_t>(p, to_endian<std::endian::big>(v));
137
0
    }
138
139
18.4k
    static uint32_t Load32(const void* p) {
140
18.4k
        return to_endian<std::endian::big>(unaligned_load<uint32_t>(p));
141
18.4k
    }
142
143
1.34k
    static void Store32(void* p, uint32_t v) {
144
1.34k
        unaligned_store<uint32_t>(p, to_endian<std::endian::big>(v));
145
1.34k
    }
146
147
0
    static uint64_t Load64(const void* p) {
148
0
        return to_endian<std::endian::big>(unaligned_load<uint64_t>(p));
149
0
    }
150
151
0
    static void Store64(void* p, uint64_t v) {
152
0
        unaligned_store<uint64_t>(p, to_endian<std::endian::big>(v));
153
0
    }
154
}; // BigEndian
155
} // namespace doris