Coverage Report

Created: 2026-10-27 17:29

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