Coverage Report

Created: 2026-09-28 17:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/core/uint24.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 <cstdint>
21
#include <cstring>
22
#include <iostream>
23
#include <string>
24
25
namespace doris {
26
27
// 24bit int type, used to store date type in storage
28
struct uint24_t {
29
public:
30
    uint24_t() = default;
31
32
202k
    uint24_t(const uint32_t& value) {
33
202k
        data[0] = static_cast<uint8_t>(value);
34
202k
        data[1] = static_cast<uint8_t>(value >> 8);
35
202k
        data[2] = static_cast<uint8_t>(value >> 16);
36
202k
    }
37
38
0
    uint24_t& operator=(const uint32_t value) {
39
0
        data[0] = static_cast<uint8_t>(value);
40
0
        data[1] = static_cast<uint8_t>(value >> 8);
41
0
        data[2] = static_cast<uint8_t>(value >> 16);
42
0
        return *this;
43
0
    }
44
45
0
    uint24_t& operator=(const unsigned __int128& value) {
46
0
        data[0] = static_cast<uint8_t>(value);
47
0
        data[1] = static_cast<uint8_t>(value >> 8);
48
0
        data[2] = static_cast<uint8_t>(value >> 16);
49
0
        return *this;
50
0
    }
51
52
954
    uint24_t& operator=(const uint64_t value) {
53
954
        data[0] = static_cast<uint8_t>(value);
54
954
        data[1] = static_cast<uint8_t>(value >> 8);
55
954
        data[2] = static_cast<uint8_t>(value >> 16);
56
954
        return *this;
57
954
    }
58
59
0
    uint24_t& operator+=(const uint24_t& value) {
60
0
        *this = static_cast<int>(*this) + static_cast<int>(value);
61
0
        return *this;
62
0
    }
63
64
0
    uint24_t& operator>>=(const int bits) {
65
0
        *this = static_cast<unsigned int>(*this) >> bits;
66
0
        return *this;
67
0
    }
68
69
0
    uint24_t& operator|=(const uint24_t& value) {
70
0
        *this = static_cast<int>(*this) | static_cast<int>(value);
71
0
        return *this;
72
0
    }
73
74
106k
    operator uint32_t() const {
75
106k
        uint32_t value = static_cast<uint8_t>(data[0]);
76
106k
        value += (static_cast<uint32_t>(static_cast<uint8_t>(data[1]))) << 8;
77
106k
        value += (static_cast<uint32_t>(static_cast<uint8_t>(data[2]))) << 16;
78
106k
        return value;
79
106k
    }
80
81
6.14k
    uint24_t& operator=(const int value) {
82
6.14k
        data[0] = static_cast<uint8_t>(value);
83
6.14k
        data[1] = static_cast<uint8_t>(value >> 8);
84
6.14k
        data[2] = static_cast<uint8_t>(value >> 16);
85
6.14k
        return *this;
86
6.14k
    }
87
88
0
    uint24_t& operator=(const int64_t value) {
89
0
        data[0] = static_cast<uint8_t>(value);
90
0
        data[1] = static_cast<uint8_t>(value >> 8);
91
0
        data[2] = static_cast<uint8_t>(value >> 16);
92
0
        return *this;
93
0
    }
94
95
118
    bool operator==(const uint24_t& value) const { return cmp(value) == 0; }
96
97
0
    bool operator!=(const uint24_t& value) const { return cmp(value) != 0; }
98
99
197k
    bool operator<(const uint24_t& value) const { return cmp(value) < 0; }
100
101
12
    bool operator<=(const uint24_t& value) const { return cmp(value) <= 0; }
102
103
130
    bool operator>(const uint24_t& value) const { return cmp(value) > 0; }
104
105
12
    bool operator>=(const uint24_t& value) const { return cmp(value) >= 0; }
106
107
197k
    int32_t cmp(const uint24_t& other) const {
108
197k
        if (data[2] > other.data[2]) {
109
322
            return 1;
110
196k
        } else if (data[2] < other.data[2]) {
111
470
            return -1;
112
470
        }
113
114
196k
        if (data[1] > other.data[1]) {
115
22.3k
            return 1;
116
174k
        } else if (data[1] < other.data[1]) {
117
21.8k
            return -1;
118
21.8k
        }
119
120
152k
        if (data[0] > other.data[0]) {
121
75.0k
            return 1;
122
77.2k
        } else if (data[0] < other.data[0]) {
123
76.5k
            return -1;
124
76.5k
        }
125
126
704
        return 0;
127
152k
    }
128
129
    // Defined in uint24.cpp: the FMT_COMPILE format machinery is expensive to
130
    // instantiate and this header is included nearly everywhere.
131
    std::string to_string() const;
132
133
0
    const uint8_t* get_data() const { return data; }
134
135
private:
136
    uint8_t data[3];
137
} __attribute__((packed));
138
139
static_assert(std::is_trivial_v<uint24_t>, "uint24_t should be a POD type");
140
141
0
inline std::ostream& operator<<(std::ostream& os, const uint24_t& val) {
142
0
    os << val.to_string();
143
0
    return os;
144
0
}
145
146
} // namespace doris