Coverage Report

Created: 2025-07-31 18:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/util/bit_util.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
// This file is copied from
18
// https://github.com/apache/impala/blob/branch-2.9.0/be/src/util/bit-util.h
19
// and modified by Doris
20
21
#pragma once
22
23
#include <type_traits>
24
25
#include "vec/core/extended_types.h"
26
#ifndef __APPLE__
27
#include <endian.h>
28
#endif
29
30
#include "common/compiler_util.h" // IWYU pragma: keep
31
#include "util/cpu_info.h"
32
#include "util/sse_util.hpp"
33
34
namespace doris {
35
36
// Utility class to do standard bit tricks
37
// TODO: is this in boost or something else like that?
38
class BitUtil {
39
public:
40
    // Returns the ceil of value/divisor
41
7
    static inline int64_t ceil(int64_t value, int64_t divisor) {
42
7
        return value / divisor + (value % divisor != 0);
43
7
    }
44
45
    // Returns 'value' rounded up to the nearest multiple of 'factor'
46
36
    static inline int64_t round_up(int64_t value, int64_t factor) {
47
36
        return (value + (factor - 1)) / factor * factor;
48
36
    }
49
50
    // Returns the smallest power of two that contains v. Taken from
51
    // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
52
    // TODO: Pick a better name, as it is not clear what happens when the input is
53
    // already a power of two.
54
108
    static inline int64_t next_power_of_two(int64_t v) {
55
108
        --v;
56
108
        v |= v >> 1;
57
108
        v |= v >> 2;
58
108
        v |= v >> 4;
59
108
        v |= v >> 8;
60
108
        v |= v >> 16;
61
108
        v |= v >> 32;
62
108
        ++v;
63
108
        return v;
64
108
    }
65
66
    // Non hw accelerated pop count.
67
    // TODO: we don't use this in any perf sensitive code paths currently.  There
68
    // might be a much faster way to implement this.
69
0
    static inline int popcount_no_hw(uint64_t x) {
70
0
        int count = 0;
71
0
72
0
        for (; x != 0; ++count) {
73
0
            x &= x - 1;
74
0
        }
75
0
76
0
        return count;
77
0
    }
78
79
    // Returns the number of set bits in x
80
0
    static inline int popcount(uint64_t x) {
81
0
        if (LIKELY(CpuInfo::is_supported(CpuInfo::POPCNT))) {
82
0
            return __builtin_popcountl(x);
83
0
        } else {
84
0
            return popcount_no_hw(x);
85
0
        }
86
0
    }
87
88
    // Returns the 'num_bits' least-significant bits of 'v'.
89
0
    static inline uint64_t trailing_bits(uint64_t v, int num_bits) {
90
0
        if (UNLIKELY(num_bits == 0)) {
91
0
            return 0;
92
0
        }
93
0
94
0
        if (UNLIKELY(num_bits >= 64)) {
95
0
            return v;
96
0
        }
97
0
98
0
        int n = 64 - num_bits;
99
0
        return (v << n) >> n;
100
0
    }
101
102
    template <typename T>
103
1
    static std::string IntToByteBuffer(T input) {
104
1
        std::string buffer;
105
1
        T value = input;
106
2
        for (int i = 0; i < sizeof(value); ++i) {
107
            // Applies a mask for a byte range on the input.
108
2
            signed char value_to_save = value & 0XFF;
109
2
            buffer.push_back(value_to_save);
110
            // Remove the just processed part from the input so that we can exit early if there
111
            // is nothing left to process.
112
2
            value >>= 8;
113
2
            if (value == 0 && value_to_save >= 0) {
114
1
                break;
115
1
            }
116
1
            if (value == -1 && value_to_save < 0) {
117
0
                break;
118
0
            }
119
1
        }
120
1
        std::reverse(buffer.begin(), buffer.end());
121
1
        return buffer;
122
1
    }
_ZN5doris7BitUtil15IntToByteBufferIiEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEET_
Line
Count
Source
103
1
    static std::string IntToByteBuffer(T input) {
104
1
        std::string buffer;
105
1
        T value = input;
106
2
        for (int i = 0; i < sizeof(value); ++i) {
107
            // Applies a mask for a byte range on the input.
108
2
            signed char value_to_save = value & 0XFF;
109
2
            buffer.push_back(value_to_save);
110
            // Remove the just processed part from the input so that we can exit early if there
111
            // is nothing left to process.
112
2
            value >>= 8;
113
2
            if (value == 0 && value_to_save >= 0) {
114
1
                break;
115
1
            }
116
1
            if (value == -1 && value_to_save < 0) {
117
0
                break;
118
0
            }
119
1
        }
120
1
        std::reverse(buffer.begin(), buffer.end());
121
1
        return buffer;
122
1
    }
Unexecuted instantiation: _ZN5doris7BitUtil15IntToByteBufferInEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEET_
Unexecuted instantiation: _ZN5doris7BitUtil15IntToByteBufferIlEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEET_
Unexecuted instantiation: _ZN5doris7BitUtil15IntToByteBufferIN4wide7integerILm256EiEEEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEET_
123
124
    // Returns ceil(log2(x)).
125
    // TODO: this could be faster if we use __builtin_clz.  Fix this if this ever shows up
126
    // in a hot path.
127
117
    static inline int log2(uint64_t x) {
128
117
        DCHECK_GT(x, 0);
129
130
117
        if (x == 1) {
131
0
            return 0;
132
0
        }
133
134
        // Compute result = ceil(log2(x))
135
        //                = floor(log2(x - 1)) + 1, for x > 1
136
        // by finding the position of the most significant bit (1-indexed) of x - 1
137
        // (floor(log2(n)) = MSB(n) (0-indexed))
138
117
        --x;
139
117
        int result = 1;
140
141
124
        while (x >>= 1) {
142
7
            ++result;
143
7
        }
144
145
117
        return result;
146
117
    }
147
148
    // Returns the rounded up to 64 multiple. Used for conversions of bits to i64.
149
0
    static inline uint32_t round_up_numi64(uint32_t bits) { return (bits + 63) >> 6; }
150
151
    // Returns the rounded up to 32 multiple. Used for conversions of bits to i32.
152
0
    constexpr static inline uint32_t round_up_numi32(uint32_t bits) { return (bits + 31) >> 5; }
153
154
    /// Returns the smallest power of two that contains v. If v is a power of two, v is
155
    /// returned. Taken from
156
    /// http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
157
8
    static inline int64_t RoundUpToPowerOfTwo(int64_t v) {
158
8
        --v;
159
8
        v |= v >> 1;
160
8
        v |= v >> 2;
161
8
        v |= v >> 4;
162
8
        v |= v >> 8;
163
8
        v |= v >> 16;
164
8
        v |= v >> 32;
165
8
        ++v;
166
8
        return v;
167
8
    }
168
169
    // Wrap the gutil/ version for convenience.
170
0
    static inline int Log2FloorNonZero64(uint64_t n) { return 63 ^ __builtin_clzll(n); }
171
172
    // Wrap the gutil/ version for convenience.
173
0
    static inline int Log2Floor64(uint64_t n) { return n == 0 ? -1 : 63 ^ __builtin_clzll(n); }
174
175
0
    static inline int Log2Ceiling64(uint64_t n) {
176
0
        int floor = Log2Floor64(n);
177
0
        // Check if zero or a power of two. This pattern is recognised by gcc and optimised
178
0
        // into branch-free code.
179
0
        if (0 == (n & (n - 1))) {
180
0
            return floor;
181
0
        } else {
182
0
            return floor + 1;
183
0
        }
184
0
    }
185
186
0
    static inline int Log2CeilingNonZero64(uint64_t n) {
187
0
        int floor = Log2FloorNonZero64(n);
188
0
        // Check if zero or a power of two. This pattern is recognised by gcc and optimised
189
0
        // into branch-free code.
190
0
        if (0 == (n & (n - 1))) {
191
0
            return floor;
192
0
        } else {
193
0
            return floor + 1;
194
0
        }
195
0
    }
196
197
    // Returns the rounded up to 64 multiple. Used for conversions of bits to i64.
198
0
    static inline uint32_t round_up_numi_64(uint32_t bits) { return (bits + 63) >> 6; }
199
200
0
    constexpr static inline int64_t Ceil(int64_t value, int64_t divisor) {
201
0
        return value / divisor + (value % divisor != 0);
202
0
    }
203
204
0
    constexpr static inline bool IsPowerOf2(int64_t value) { return (value & (value - 1)) == 0; }
205
206
0
    constexpr static inline int64_t RoundDown(int64_t value, int64_t factor) {
207
0
        return (value / factor) * factor;
208
0
    }
209
210
    /// Specialized round up and down functions for frequently used factors,
211
    /// like 8 (bits->bytes), 32 (bits->i32), and 64 (bits->i64)
212
    /// Returns the rounded up number of bytes that fit the number of bits.
213
71
    constexpr static inline uint32_t RoundUpNumBytes(uint32_t bits) { return (bits + 7) >> 3; }
214
215
    template <typename T>
216
28
    static inline T RoundDownToPowerOf2(T value, T factor) {
217
28
        static_assert(std::is_integral<T>::value, "T must be an integral type");
218
28
        DCHECK((factor > 0) && ((factor & (factor - 1)) == 0));
219
28
        return value & ~(factor - 1);
220
28
    }
221
222
    // Returns the ceil of value/divisor
223
206k
    static inline int Ceil(int value, int divisor) {
224
206k
        return value / divisor + (value % divisor != 0);
225
206k
    }
226
227
    // Returns the 'num_bits' least-significant bits of 'v'.
228
630k
    static inline uint64_t TrailingBits(uint64_t v, int num_bits) {
229
630k
        if (num_bits == 0) [[unlikely]] {
230
24.6k
            return 0;
231
24.6k
        }
232
606k
        if (num_bits >= 64) [[unlikely]] {
233
200k
            return v;
234
200k
        }
235
405k
        int n = 64 - num_bits;
236
405k
        return (v << n) >> n;
237
606k
    }
238
239
200k
    static inline uint64_t ShiftLeftZeroOnOverflow(uint64_t v, int num_bits) {
240
200k
        if (num_bits >= 64) [[unlikely]] {
241
6.42k
            return 0;
242
6.42k
        }
243
194k
        return v << num_bits;
244
200k
    }
245
246
200k
    static inline uint64_t ShiftRightZeroOnOverflow(uint64_t v, int num_bits) {
247
200k
        if (num_bits >= 64) [[unlikely]] {
248
6.42k
            return 0;
249
6.42k
        }
250
194k
        return v >> num_bits;
251
200k
    }
252
};
253
254
} // namespace doris