Coverage Report

Created: 2026-09-02 21:59

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
// This header used to lean on the bswap_16/32/64 macros that protobuf's
26
// stubs/port.h happened to leak into most TUs; use the compiler builtins
27
// directly so it stands on its own natural include closure (redefining the
28
// macros here would clash with protobuf's unguarded definitions).
29
50.1k
inline uint16_t gbswap_16(uint16_t host_int) {
30
50.1k
    return __builtin_bswap16(host_int);
31
50.1k
}
32
33
1.45M
inline uint32_t gbswap_32(uint32_t host_int) {
34
1.45M
    return __builtin_bswap32(host_int);
35
1.45M
}
36
37
9.31M
inline uint64_t gbswap_64(uint64_t host_int) {
38
9.31M
#if defined(__GNUC__) && defined(__x86_64__) && !defined(__APPLE__)
39
    // Adapted from /usr/include/byteswap.h.  Not available on Mac.
40
9.31M
    if (__builtin_constant_p(host_int)) {
41
0
        return __bswap_constant_64(host_int);
42
9.31M
    } else {
43
9.31M
        uint64_t result;
44
9.31M
        __asm__("bswap %0" : "=r"(result) : "0"(host_int));
45
9.31M
        return result;
46
9.31M
    }
47
#else
48
    return __builtin_bswap64(host_int);
49
#endif
50
9.31M
}
51
52
4.03M
inline unsigned __int128 gbswap_128(unsigned __int128 host_int) {
53
4.03M
    return static_cast<unsigned __int128>(gbswap_64(static_cast<uint64_t>(host_int >> 64))) |
54
4.03M
           (static_cast<unsigned __int128>(gbswap_64(static_cast<uint64_t>(host_int))) << 64);
55
4.03M
}
56
57
49.6k
inline wide::UInt256 gbswap_256(wide::UInt256 host_int) {
58
49.6k
    wide::UInt256 result {gbswap_64(host_int.items[3]), gbswap_64(host_int.items[2]),
59
49.6k
                          gbswap_64(host_int.items[1]), gbswap_64(host_int.items[0])};
60
49.6k
    return result;
61
49.6k
}
62
63
// Swap bytes of a 24-bit value.
64
49.0k
inline uint32_t bswap_24(uint32_t x) {
65
49.0k
    return uint32_t((x & 0x0000ffULL) << 16) | ((x & 0x00ff00ULL)) | ((x & 0xff0000ULL) >> 16);
66
49.0k
}
67
68
// use std::byteswap after doris enable cpp23
69
template <typename T>
70
6.79M
T byte_swap(T x) {
71
6.79M
    if constexpr (sizeof(T) == sizeof(wide::Int256)) {
72
49.6k
        return gbswap_256(x);
73
4.03M
    } else if constexpr (sizeof(T) == sizeof(__int128)) {
74
4.03M
        return gbswap_128(x);
75
4.03M
    } else if constexpr (sizeof(T) == sizeof(int64_t)) {
76
1.03M
        return gbswap_64(x);
77
1.45M
    } else if constexpr (sizeof(T) == sizeof(int32_t)) {
78
1.45M
        return gbswap_32(x);
79
1.45M
    } else if constexpr (sizeof(T) == sizeof(doris::uint24_t)) {
80
49.0k
        return bswap_24(x);
81
50.1k
    } else if constexpr (sizeof(T) == sizeof(int16_t)) {
82
50.1k
        return gbswap_16(x);
83
113k
    } else {
84
113k
        static_assert(sizeof(T) == 1, "Unsupported type size for byte_swap");
85
113k
        return x; // No byte swap needed for unsupported types
86
113k
    }
87
6.79M
}
_ZN5doris9byte_swapItEET_S1_
Line
Count
Source
70
50.1k
T byte_swap(T x) {
71
    if constexpr (sizeof(T) == sizeof(wide::Int256)) {
72
        return gbswap_256(x);
73
    } else if constexpr (sizeof(T) == sizeof(__int128)) {
74
        return gbswap_128(x);
75
    } else if constexpr (sizeof(T) == sizeof(int64_t)) {
76
        return gbswap_64(x);
77
    } else if constexpr (sizeof(T) == sizeof(int32_t)) {
78
        return gbswap_32(x);
79
    } else if constexpr (sizeof(T) == sizeof(doris::uint24_t)) {
80
        return bswap_24(x);
81
50.1k
    } else if constexpr (sizeof(T) == sizeof(int16_t)) {
82
50.1k
        return gbswap_16(x);
83
    } else {
84
        static_assert(sizeof(T) == 1, "Unsupported type size for byte_swap");
85
        return x; // No byte swap needed for unsupported types
86
    }
87
50.1k
}
_ZN5doris9byte_swapIjEET_S1_
Line
Count
Source
70
1.45M
T byte_swap(T x) {
71
    if constexpr (sizeof(T) == sizeof(wide::Int256)) {
72
        return gbswap_256(x);
73
    } else if constexpr (sizeof(T) == sizeof(__int128)) {
74
        return gbswap_128(x);
75
    } else if constexpr (sizeof(T) == sizeof(int64_t)) {
76
        return gbswap_64(x);
77
1.45M
    } else if constexpr (sizeof(T) == sizeof(int32_t)) {
78
1.45M
        return gbswap_32(x);
79
    } else if constexpr (sizeof(T) == sizeof(doris::uint24_t)) {
80
        return bswap_24(x);
81
    } else if constexpr (sizeof(T) == sizeof(int16_t)) {
82
        return gbswap_16(x);
83
    } else {
84
        static_assert(sizeof(T) == 1, "Unsupported type size for byte_swap");
85
        return x; // No byte swap needed for unsupported types
86
    }
87
1.45M
}
_ZN5doris9byte_swapImEET_S1_
Line
Count
Source
70
505k
T byte_swap(T x) {
71
    if constexpr (sizeof(T) == sizeof(wide::Int256)) {
72
        return gbswap_256(x);
73
    } else if constexpr (sizeof(T) == sizeof(__int128)) {
74
        return gbswap_128(x);
75
505k
    } else if constexpr (sizeof(T) == sizeof(int64_t)) {
76
505k
        return gbswap_64(x);
77
    } else if constexpr (sizeof(T) == sizeof(int32_t)) {
78
        return gbswap_32(x);
79
    } else if constexpr (sizeof(T) == sizeof(doris::uint24_t)) {
80
        return bswap_24(x);
81
    } else if constexpr (sizeof(T) == sizeof(int16_t)) {
82
        return gbswap_16(x);
83
    } else {
84
        static_assert(sizeof(T) == 1, "Unsupported type size for byte_swap");
85
        return x; // No byte swap needed for unsupported types
86
    }
87
505k
}
_ZN5doris9byte_swapINS_8uint24_tEEET_S2_
Line
Count
Source
70
49.0k
T byte_swap(T x) {
71
    if constexpr (sizeof(T) == sizeof(wide::Int256)) {
72
        return gbswap_256(x);
73
    } else if constexpr (sizeof(T) == sizeof(__int128)) {
74
        return gbswap_128(x);
75
    } else if constexpr (sizeof(T) == sizeof(int64_t)) {
76
        return gbswap_64(x);
77
    } else if constexpr (sizeof(T) == sizeof(int32_t)) {
78
        return gbswap_32(x);
79
49.0k
    } else if constexpr (sizeof(T) == sizeof(doris::uint24_t)) {
80
49.0k
        return bswap_24(x);
81
    } else if constexpr (sizeof(T) == sizeof(int16_t)) {
82
        return gbswap_16(x);
83
    } else {
84
        static_assert(sizeof(T) == 1, "Unsupported type size for byte_swap");
85
        return x; // No byte swap needed for unsupported types
86
    }
87
49.0k
}
_ZN5doris9byte_swapIoEET_S1_
Line
Count
Source
70
167k
T byte_swap(T x) {
71
    if constexpr (sizeof(T) == sizeof(wide::Int256)) {
72
        return gbswap_256(x);
73
167k
    } else if constexpr (sizeof(T) == sizeof(__int128)) {
74
167k
        return gbswap_128(x);
75
    } else if constexpr (sizeof(T) == sizeof(int64_t)) {
76
        return gbswap_64(x);
77
    } else if constexpr (sizeof(T) == sizeof(int32_t)) {
78
        return gbswap_32(x);
79
    } else if constexpr (sizeof(T) == sizeof(doris::uint24_t)) {
80
        return bswap_24(x);
81
    } else if constexpr (sizeof(T) == sizeof(int16_t)) {
82
        return gbswap_16(x);
83
    } else {
84
        static_assert(sizeof(T) == 1, "Unsupported type size for byte_swap");
85
        return x; // No byte swap needed for unsupported types
86
    }
87
167k
}
_ZN5doris9byte_swapIN4wide7integerILm256EjEEEET_S4_
Line
Count
Source
70
49.6k
T byte_swap(T x) {
71
49.6k
    if constexpr (sizeof(T) == sizeof(wide::Int256)) {
72
49.6k
        return gbswap_256(x);
73
    } else if constexpr (sizeof(T) == sizeof(__int128)) {
74
        return gbswap_128(x);
75
    } else if constexpr (sizeof(T) == sizeof(int64_t)) {
76
        return gbswap_64(x);
77
    } else if constexpr (sizeof(T) == sizeof(int32_t)) {
78
        return gbswap_32(x);
79
    } else if constexpr (sizeof(T) == sizeof(doris::uint24_t)) {
80
        return bswap_24(x);
81
    } else if constexpr (sizeof(T) == sizeof(int16_t)) {
82
        return gbswap_16(x);
83
    } else {
84
        static_assert(sizeof(T) == 1, "Unsupported type size for byte_swap");
85
        return x; // No byte swap needed for unsupported types
86
    }
87
49.6k
}
_ZN5doris9byte_swapIlEET_S1_
Line
Count
Source
70
532k
T byte_swap(T x) {
71
    if constexpr (sizeof(T) == sizeof(wide::Int256)) {
72
        return gbswap_256(x);
73
    } else if constexpr (sizeof(T) == sizeof(__int128)) {
74
        return gbswap_128(x);
75
532k
    } else if constexpr (sizeof(T) == sizeof(int64_t)) {
76
532k
        return gbswap_64(x);
77
    } else if constexpr (sizeof(T) == sizeof(int32_t)) {
78
        return gbswap_32(x);
79
    } else if constexpr (sizeof(T) == sizeof(doris::uint24_t)) {
80
        return bswap_24(x);
81
    } else if constexpr (sizeof(T) == sizeof(int16_t)) {
82
        return gbswap_16(x);
83
    } else {
84
        static_assert(sizeof(T) == 1, "Unsupported type size for byte_swap");
85
        return x; // No byte swap needed for unsupported types
86
    }
87
532k
}
_ZN5doris9byte_swapInEET_S1_
Line
Count
Source
70
3.87M
T byte_swap(T x) {
71
    if constexpr (sizeof(T) == sizeof(wide::Int256)) {
72
        return gbswap_256(x);
73
3.87M
    } else if constexpr (sizeof(T) == sizeof(__int128)) {
74
3.87M
        return gbswap_128(x);
75
    } else if constexpr (sizeof(T) == sizeof(int64_t)) {
76
        return gbswap_64(x);
77
    } else if constexpr (sizeof(T) == sizeof(int32_t)) {
78
        return gbswap_32(x);
79
    } else if constexpr (sizeof(T) == sizeof(doris::uint24_t)) {
80
        return bswap_24(x);
81
    } else if constexpr (sizeof(T) == sizeof(int16_t)) {
82
        return gbswap_16(x);
83
    } else {
84
        static_assert(sizeof(T) == 1, "Unsupported type size for byte_swap");
85
        return x; // No byte swap needed for unsupported types
86
    }
87
3.87M
}
Unexecuted instantiation: _ZN5doris9byte_swapIN4wide7integerILm256EiEEEET_S4_
Unexecuted instantiation: _ZN5doris9byte_swapIiEET_S1_
_ZN5doris9byte_swapIhEET_S1_
Line
Count
Source
70
113k
T byte_swap(T x) {
71
    if constexpr (sizeof(T) == sizeof(wide::Int256)) {
72
        return gbswap_256(x);
73
    } else if constexpr (sizeof(T) == sizeof(__int128)) {
74
        return gbswap_128(x);
75
    } else if constexpr (sizeof(T) == sizeof(int64_t)) {
76
        return gbswap_64(x);
77
    } else if constexpr (sizeof(T) == sizeof(int32_t)) {
78
        return gbswap_32(x);
79
    } else if constexpr (sizeof(T) == sizeof(doris::uint24_t)) {
80
        return bswap_24(x);
81
    } else if constexpr (sizeof(T) == sizeof(int16_t)) {
82
        return gbswap_16(x);
83
113k
    } else {
84
113k
        static_assert(sizeof(T) == 1, "Unsupported type size for byte_swap");
85
113k
        return x; // No byte swap needed for unsupported types
86
113k
    }
87
113k
}
88
89
template <std::endian target, typename T>
90
14.6M
T to_endian(T value) {
91
14.6M
    if constexpr (std::endian::native == target) {
92
7.86M
        return value; // No swap needed
93
7.86M
    } else {
94
6.79M
        static_assert(std::endian::native == std::endian::big ||
95
6.79M
                              std::endian::native == std::endian::little,
96
6.79M
                      "Unsupported endianness");
97
6.79M
        return byte_swap(value);
98
6.79M
    }
99
14.6M
}
_ZN5doris9to_endianILSt6endian1234EtEET0_S2_
Line
Count
Source
90
48.5k
T to_endian(T value) {
91
48.5k
    if constexpr (std::endian::native == target) {
92
48.5k
        return value; // No swap needed
93
    } else {
94
        static_assert(std::endian::native == std::endian::big ||
95
                              std::endian::native == std::endian::little,
96
                      "Unsupported endianness");
97
        return byte_swap(value);
98
    }
99
48.5k
}
_ZN5doris9to_endianILSt6endian1234EjEET0_S2_
Line
Count
Source
90
7.63M
T to_endian(T value) {
91
7.63M
    if constexpr (std::endian::native == target) {
92
7.63M
        return value; // No swap needed
93
    } else {
94
        static_assert(std::endian::native == std::endian::big ||
95
                              std::endian::native == std::endian::little,
96
                      "Unsupported endianness");
97
        return byte_swap(value);
98
    }
99
7.63M
}
_ZN5doris9to_endianILSt6endian1234EmEET0_S2_
Line
Count
Source
90
129k
T to_endian(T value) {
91
129k
    if constexpr (std::endian::native == target) {
92
129k
        return value; // No swap needed
93
    } else {
94
        static_assert(std::endian::native == std::endian::big ||
95
                              std::endian::native == std::endian::little,
96
                      "Unsupported endianness");
97
        return byte_swap(value);
98
    }
99
129k
}
_ZN5doris9to_endianILSt6endian4321EtEET0_S2_
Line
Count
Source
90
50.1k
T to_endian(T value) {
91
    if constexpr (std::endian::native == target) {
92
        return value; // No swap needed
93
50.1k
    } else {
94
50.1k
        static_assert(std::endian::native == std::endian::big ||
95
50.1k
                              std::endian::native == std::endian::little,
96
50.1k
                      "Unsupported endianness");
97
50.1k
        return byte_swap(value);
98
50.1k
    }
99
50.1k
}
_ZN5doris9to_endianILSt6endian4321EjEET0_S2_
Line
Count
Source
90
1.45M
T to_endian(T value) {
91
    if constexpr (std::endian::native == target) {
92
        return value; // No swap needed
93
1.45M
    } else {
94
1.45M
        static_assert(std::endian::native == std::endian::big ||
95
1.45M
                              std::endian::native == std::endian::little,
96
1.45M
                      "Unsupported endianness");
97
1.45M
        return byte_swap(value);
98
1.45M
    }
99
1.45M
}
_ZN5doris9to_endianILSt6endian4321EmEET0_S2_
Line
Count
Source
90
505k
T to_endian(T value) {
91
    if constexpr (std::endian::native == target) {
92
        return value; // No swap needed
93
505k
    } else {
94
505k
        static_assert(std::endian::native == std::endian::big ||
95
505k
                              std::endian::native == std::endian::little,
96
505k
                      "Unsupported endianness");
97
505k
        return byte_swap(value);
98
505k
    }
99
505k
}
_ZN5doris9to_endianILSt6endian1234EoEET0_S2_
Line
Count
Source
90
48.8k
T to_endian(T value) {
91
48.8k
    if constexpr (std::endian::native == target) {
92
48.8k
        return value; // No swap needed
93
    } else {
94
        static_assert(std::endian::native == std::endian::big ||
95
                              std::endian::native == std::endian::little,
96
                      "Unsupported endianness");
97
        return byte_swap(value);
98
    }
99
48.8k
}
_ZN5doris9to_endianILSt6endian4321ENS_8uint24_tEEET0_S3_
Line
Count
Source
90
49.0k
T to_endian(T value) {
91
    if constexpr (std::endian::native == target) {
92
        return value; // No swap needed
93
49.0k
    } else {
94
49.0k
        static_assert(std::endian::native == std::endian::big ||
95
49.0k
                              std::endian::native == std::endian::little,
96
49.0k
                      "Unsupported endianness");
97
49.0k
        return byte_swap(value);
98
49.0k
    }
99
49.0k
}
_ZN5doris9to_endianILSt6endian4321EoEET0_S2_
Line
Count
Source
90
167k
T to_endian(T value) {
91
    if constexpr (std::endian::native == target) {
92
        return value; // No swap needed
93
167k
    } else {
94
167k
        static_assert(std::endian::native == std::endian::big ||
95
167k
                              std::endian::native == std::endian::little,
96
167k
                      "Unsupported endianness");
97
167k
        return byte_swap(value);
98
167k
    }
99
167k
}
_ZN5doris9to_endianILSt6endian4321EN4wide7integerILm256EjEEEET0_S5_
Line
Count
Source
90
49.6k
T to_endian(T value) {
91
    if constexpr (std::endian::native == target) {
92
        return value; // No swap needed
93
49.6k
    } else {
94
49.6k
        static_assert(std::endian::native == std::endian::big ||
95
49.6k
                              std::endian::native == std::endian::little,
96
49.6k
                      "Unsupported endianness");
97
49.6k
        return byte_swap(value);
98
49.6k
    }
99
49.6k
}
_ZN5doris9to_endianILSt6endian4321ElEET0_S2_
Line
Count
Source
90
532k
T to_endian(T value) {
91
    if constexpr (std::endian::native == target) {
92
        return value; // No swap needed
93
532k
    } else {
94
532k
        static_assert(std::endian::native == std::endian::big ||
95
532k
                              std::endian::native == std::endian::little,
96
532k
                      "Unsupported endianness");
97
532k
        return byte_swap(value);
98
532k
    }
99
532k
}
_ZN5doris9to_endianILSt6endian4321EnEET0_S2_
Line
Count
Source
90
3.87M
T to_endian(T value) {
91
    if constexpr (std::endian::native == target) {
92
        return value; // No swap needed
93
3.87M
    } else {
94
3.87M
        static_assert(std::endian::native == std::endian::big ||
95
3.87M
                              std::endian::native == std::endian::little,
96
3.87M
                      "Unsupported endianness");
97
3.87M
        return byte_swap(value);
98
3.87M
    }
99
3.87M
}
Unexecuted instantiation: _ZN5doris9to_endianILSt6endian4321EN4wide7integerILm256EiEEEET0_S5_
Unexecuted instantiation: _ZN5doris9to_endianILSt6endian4321EiEET0_S2_
_ZN5doris9to_endianILSt6endian4321EhEET0_S2_
Line
Count
Source
90
113k
T to_endian(T value) {
91
    if constexpr (std::endian::native == target) {
92
        return value; // No swap needed
93
113k
    } else {
94
113k
        static_assert(std::endian::native == std::endian::big ||
95
113k
                              std::endian::native == std::endian::little,
96
113k
                      "Unsupported endianness");
97
113k
        return byte_swap(value);
98
113k
    }
99
113k
}
100
101
// Utilities to convert numbers between the current hosts's native byte
102
// order and little-endian byte order
103
//
104
// Load/Store methods are alignment safe
105
class LittleEndian {
106
public:
107
    // Functions to do unaligned loads and stores in little-endian order.
108
0
    static uint16_t Load16(const void* p) {
109
0
        return to_endian<std::endian::little>(unaligned_load<uint16_t>(p));
110
0
    }
111
112
0
    static void Store16(void* p, uint16_t v) {
113
0
        unaligned_store<uint16_t>(p, to_endian<std::endian::little>(v));
114
0
    }
115
116
5.66M
    static uint32_t Load32(const void* p) {
117
5.66M
        return to_endian<std::endian::little>(unaligned_load<uint32_t>(p));
118
5.66M
    }
119
120
2.06k
    static void Store32(void* p, uint32_t v) {
121
2.06k
        unaligned_store<uint32_t>(p, to_endian<std::endian::little>(v));
122
2.06k
    }
123
124
4.20k
    static uint64_t Load64(const void* p) {
125
4.20k
        return to_endian<std::endian::little>(unaligned_load<uint64_t>(p));
126
4.20k
    }
127
128
1.87k
    static void Store64(void* p, uint64_t v) {
129
1.87k
        unaligned_store<uint64_t>(p, to_endian<std::endian::little>(v));
130
1.87k
    }
131
};
132
133
// Utilities to convert numbers between the current hosts's native byte
134
// order and big-endian byte order (same as network byte order)
135
//
136
// Load/Store methods are alignment safe
137
class BigEndian {
138
public:
139
    // Functions to do unaligned loads and stores in little-endian order.
140
0
    static uint16_t Load16(const void* p) {
141
0
        return to_endian<std::endian::big>(unaligned_load<uint16_t>(p));
142
0
    }
143
144
0
    static void Store16(void* p, uint16_t v) {
145
0
        unaligned_store<uint16_t>(p, to_endian<std::endian::big>(v));
146
0
    }
147
148
49
    static uint32_t Load32(const void* p) {
149
49
        return to_endian<std::endian::big>(unaligned_load<uint32_t>(p));
150
49
    }
151
152
37
    static void Store32(void* p, uint32_t v) {
153
37
        unaligned_store<uint32_t>(p, to_endian<std::endian::big>(v));
154
37
    }
155
156
0
    static uint64_t Load64(const void* p) {
157
0
        return to_endian<std::endian::big>(unaligned_load<uint64_t>(p));
158
0
    }
159
160
0
    static void Store64(void* p, uint64_t v) {
161
0
        unaligned_store<uint64_t>(p, to_endian<std::endian::big>(v));
162
0
    }
163
}; // BigEndian
164
} // namespace doris