Coverage Report

Created: 2026-07-09 16:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/bit_stream_utils.inline.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-stream-utils.inline.h
19
// and modified by Doris
20
21
#pragma once
22
23
#include <algorithm>
24
25
#include "glog/logging.h"
26
#include "util/alignment.h"
27
#include "util/bit_packing.inline.h"
28
#include "util/bit_stream_utils.h"
29
#include "util/bit_util.h"
30
31
using doris::BitUtil;
32
33
namespace doris {
34
22.3M
inline void BitWriter::PutValue(uint64_t v, int num_bits) {
35
22.3M
    DCHECK_LE(num_bits, 64);
36
    // Truncate the higher-order bits. This is necessary to
37
    // support signed values.
38
22.3M
    v &= ~0ULL >> (64 - num_bits);
39
40
22.3M
    buffered_values_ |= v << bit_offset_;
41
22.3M
    bit_offset_ += num_bits;
42
43
22.3M
    if (bit_offset_ >= 64) [[unlikely]] {
44
        // Flush buffered_values_ and write out bits of v that did not fit
45
299k
        buffer_->reserve(ALIGN_UP(byte_offset_ + 8, 8));
46
299k
        buffer_->resize(byte_offset_ + 8);
47
299k
        DCHECK_LE(byte_offset_ + 8, buffer_->capacity());
48
299k
        memcpy(buffer_->data() + byte_offset_, &buffered_values_, 8);
49
299k
        buffered_values_ = 0;
50
299k
        byte_offset_ += 8;
51
299k
        bit_offset_ -= 64;
52
299k
        buffered_values_ = BitUtil::ShiftRightZeroOnOverflow(v, (num_bits - bit_offset_));
53
299k
    }
54
22.3M
    DCHECK_LT(bit_offset_, 64);
55
22.3M
}
56
57
70.9M
inline void BitWriter::Flush(bool align) {
58
70.9M
    int num_bytes = BitUtil::Ceil(bit_offset_, 8);
59
70.9M
    buffer_->reserve(ALIGN_UP(byte_offset_ + num_bytes, 8));
60
70.9M
    buffer_->resize(byte_offset_ + num_bytes);
61
70.9M
    DCHECK_LE(byte_offset_ + num_bytes, buffer_->capacity());
62
70.9M
    memcpy(buffer_->data() + byte_offset_, &buffered_values_, num_bytes);
63
64
70.9M
    if (align) {
65
59.8M
        buffered_values_ = 0;
66
59.8M
        byte_offset_ += num_bytes;
67
59.8M
        bit_offset_ = 0;
68
59.8M
    }
69
70.9M
}
70
71
59.8M
inline uint8_t* BitWriter::GetNextBytePtr(int num_bytes) {
72
59.8M
    Flush(/* align */ true);
73
59.8M
    buffer_->reserve(ALIGN_UP(byte_offset_ + num_bytes, 8));
74
59.8M
    buffer_->resize(byte_offset_ + num_bytes);
75
59.8M
    uint8_t* ptr = buffer_->data() + byte_offset_;
76
59.8M
    byte_offset_ += num_bytes;
77
59.8M
    DCHECK_LE(byte_offset_, buffer_->capacity());
78
59.8M
    return ptr;
79
59.8M
}
80
81
template <typename T>
82
58.6M
void BitWriter::PutAligned(T val, int num_bytes) {
83
58.6M
    DCHECK_LE(num_bytes, sizeof(T));
84
58.6M
    uint8_t* ptr = GetNextBytePtr(num_bytes);
85
58.6M
    memcpy(ptr, &val, num_bytes);
86
58.6M
}
_ZN5doris9BitWriter10PutAlignedIhEEvT_i
Line
Count
Source
82
38.4M
void BitWriter::PutAligned(T val, int num_bytes) {
83
    DCHECK_LE(num_bytes, sizeof(T));
84
38.4M
    uint8_t* ptr = GetNextBytePtr(num_bytes);
85
38.4M
    memcpy(ptr, &val, num_bytes);
86
38.4M
}
_ZN5doris9BitWriter10PutAlignedImEEvT_i
Line
Count
Source
82
20.2M
void BitWriter::PutAligned(T val, int num_bytes) {
83
    DCHECK_LE(num_bytes, sizeof(T));
84
20.2M
    uint8_t* ptr = GetNextBytePtr(num_bytes);
85
20.2M
    memcpy(ptr, &val, num_bytes);
86
20.2M
}
87
88
20.2M
inline void BitWriter::PutVlqInt(int32_t v) {
89
38.4M
    while ((v & 0xFFFFFF80) != 0L) {
90
18.1M
        PutAligned<uint8_t>((v & 0x7F) | 0x80, 1);
91
18.1M
        v >>= 7;
92
18.1M
    }
93
20.2M
    PutAligned<uint8_t>(v & 0x7F, 1);
94
20.2M
}
95
96
inline BitReader::BitReader(const uint8_t* buffer, int buffer_len)
97
302k
        : buffer_(buffer),
98
302k
          max_bytes_(buffer_len),
99
302k
          buffered_values_(0),
100
302k
          byte_offset_(0),
101
302k
          bit_offset_(0) {
102
302k
    int num_bytes = std::min(8, max_bytes_);
103
302k
    memcpy(&buffered_values_, buffer_ + byte_offset_, num_bytes);
104
302k
}
105
106
481k
inline void BitReader::BufferValues() {
107
481k
    int bytes_remaining = max_bytes_ - byte_offset_;
108
481k
    if (bytes_remaining >= 8) [[likely]] {
109
479k
        memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
110
479k
    } else {
111
2.05k
        memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining);
112
2.05k
    }
113
481k
}
114
115
template <typename T>
116
21.7M
bool BitReader::GetValue(int num_bits, T* v) {
117
21.7M
    DCHECK_LE(num_bits, 64);
118
21.7M
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
21.7M
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
21.7M
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
21.7M
                        bit_offset_);
126
127
21.7M
    bit_offset_ += num_bits;
128
21.7M
    if (bit_offset_ >= 64) {
129
481k
        byte_offset_ += 8;
130
481k
        bit_offset_ -= 64;
131
481k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
481k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
481k
                                               (num_bits - bit_offset_));
135
481k
    }
136
21.7M
    DCHECK_LE(bit_offset_, 64);
137
21.7M
    return true;
138
21.7M
}
_ZN5doris9BitReader8GetValueIsEEbiPT_
Line
Count
Source
116
80.2k
bool BitReader::GetValue(int num_bits, T* v) {
117
80.2k
    DCHECK_LE(num_bits, 64);
118
80.2k
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
80.2k
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
80.2k
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
80.2k
                        bit_offset_);
126
127
80.2k
    bit_offset_ += num_bits;
128
80.2k
    if (bit_offset_ >= 64) {
129
527
        byte_offset_ += 8;
130
527
        bit_offset_ -= 64;
131
527
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
527
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
527
                                               (num_bits - bit_offset_));
135
527
    }
136
    DCHECK_LE(bit_offset_, 64);
137
80.2k
    return true;
138
80.2k
}
_ZN5doris9BitReader8GetValueImEEbiPT_
Line
Count
Source
116
2.92M
bool BitReader::GetValue(int num_bits, T* v) {
117
2.92M
    DCHECK_LE(num_bits, 64);
118
2.92M
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
2.92M
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
2.92M
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
2.92M
                        bit_offset_);
126
127
2.92M
    bit_offset_ += num_bits;
128
2.92M
    if (bit_offset_ >= 64) {
129
56.2k
        byte_offset_ += 8;
130
56.2k
        bit_offset_ -= 64;
131
56.2k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
56.2k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
56.2k
                                               (num_bits - bit_offset_));
135
56.2k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
2.92M
    return true;
138
2.92M
}
_ZN5doris9BitReader8GetValueIbEEbiPT_
Line
Count
Source
116
1.33M
bool BitReader::GetValue(int num_bits, T* v) {
117
1.33M
    DCHECK_LE(num_bits, 64);
118
1.33M
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
1.33M
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
1.33M
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
1.33M
                        bit_offset_);
126
127
1.33M
    bit_offset_ += num_bits;
128
1.33M
    if (bit_offset_ >= 64) {
129
9.22k
        byte_offset_ += 8;
130
9.22k
        bit_offset_ -= 64;
131
9.22k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
9.22k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
9.22k
                                               (num_bits - bit_offset_));
135
9.22k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
1.33M
    return true;
138
1.33M
}
_ZN5doris9BitReader8GetValueIhEEbiPT_
Line
Count
Source
116
17.0M
bool BitReader::GetValue(int num_bits, T* v) {
117
17.0M
    DCHECK_LE(num_bits, 64);
118
17.0M
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
17.0M
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
17.0M
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
17.0M
                        bit_offset_);
126
127
17.0M
    bit_offset_ += num_bits;
128
17.0M
    if (bit_offset_ >= 64) {
129
238k
        byte_offset_ += 8;
130
238k
        bit_offset_ -= 64;
131
238k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
238k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
238k
                                               (num_bits - bit_offset_));
135
238k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
17.0M
    return true;
138
17.0M
}
_ZN5doris9BitReader8GetValueIiEEbiPT_
Line
Count
Source
116
598
bool BitReader::GetValue(int num_bits, T* v) {
117
598
    DCHECK_LE(num_bits, 64);
118
598
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
598
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
598
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
598
                        bit_offset_);
126
127
598
    bit_offset_ += num_bits;
128
598
    if (bit_offset_ >= 64) {
129
80
        byte_offset_ += 8;
130
80
        bit_offset_ -= 64;
131
80
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
80
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
80
                                               (num_bits - bit_offset_));
135
80
    }
136
    DCHECK_LE(bit_offset_, 64);
137
598
    return true;
138
598
}
_ZN5doris9BitReader8GetValueIlEEbiPT_
Line
Count
Source
116
315k
bool BitReader::GetValue(int num_bits, T* v) {
117
315k
    DCHECK_LE(num_bits, 64);
118
315k
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
315k
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
315k
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
315k
                        bit_offset_);
126
127
315k
    bit_offset_ += num_bits;
128
315k
    if (bit_offset_ >= 64) {
129
177k
        byte_offset_ += 8;
130
177k
        bit_offset_ -= 64;
131
177k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
177k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
177k
                                               (num_bits - bit_offset_));
135
177k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
315k
    return true;
138
315k
}
_ZN5doris9BitReader8GetValueIcEEbiPT_
Line
Count
Source
116
508
bool BitReader::GetValue(int num_bits, T* v) {
117
508
    DCHECK_LE(num_bits, 64);
118
508
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
508
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
508
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
508
                        bit_offset_);
126
127
508
    bit_offset_ += num_bits;
128
508
    if (bit_offset_ >= 64) {
129
59
        byte_offset_ += 8;
130
59
        bit_offset_ -= 64;
131
59
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
59
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
59
                                               (num_bits - bit_offset_));
135
59
    }
136
    DCHECK_LE(bit_offset_, 64);
137
508
    return true;
138
508
}
139
140
1.21M
inline void BitReader::Rewind(int num_bits) {
141
1.21M
    bit_offset_ -= num_bits;
142
1.21M
    if (bit_offset_ >= 0) {
143
1.20M
        return;
144
1.20M
    }
145
19.2k
    while (bit_offset_ < 0) {
146
9.08k
        int seek_back = std::min(byte_offset_, 8);
147
9.08k
        byte_offset_ -= seek_back;
148
9.08k
        bit_offset_ += seek_back * 8;
149
9.08k
    }
150
    // This should only be executed *if* rewinding by 'num_bits'
151
    // make the existing buffered_values_ invalid
152
10.2k
    DCHECK_GE(byte_offset_, 0); // Check for underflow
153
10.2k
    memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
154
10.2k
}
155
156
29
inline bool BitReader::Advance(int64_t num_bits) {
157
29
    int64_t bits_required = bit_offset_ + num_bits;
158
29
    int64_t bytes_required = (bits_required >> 3) + ((bits_required & 7) != 0);
159
29
    if (bytes_required > max_bytes_ - byte_offset_) {
160
0
        return false;
161
0
    }
162
29
    byte_offset_ += static_cast<int>(bits_required >> 3);
163
29
    bit_offset_ = static_cast<int>(bits_required & 7);
164
29
    BufferValues();
165
29
    return true;
166
29
}
167
168
0
inline void BitReader::SeekToBit(unsigned int stream_position) {
169
0
    DCHECK_LE(stream_position, max_bytes_ * 8);
170
0
171
0
    int delta = static_cast<int>(stream_position) - position();
172
0
    if (delta == 0) {
173
0
        return;
174
0
    } else if (delta < 0) {
175
0
        Rewind(position() - stream_position);
176
0
    } else {
177
0
        bit_offset_ += delta;
178
0
        while (bit_offset_ >= 64) {
179
0
            byte_offset_ += 8;
180
0
            bit_offset_ -= 64;
181
0
            if (bit_offset_ < 64) {
182
0
                // This should only be executed if seeking to
183
0
                // 'stream_position' makes the existing buffered_values_
184
0
                // invalid.
185
0
                BufferValues();
186
0
            }
187
0
        }
188
0
    }
189
0
}
190
191
template <typename T>
192
1.05M
bool BitReader::GetAligned(int num_bytes, T* v) {
193
1.05M
    DCHECK_LE(num_bytes, sizeof(T));
194
1.05M
    int bytes_read = BitUtil::Ceil(bit_offset_, 8);
195
1.05M
    if (byte_offset_ + bytes_read + num_bytes > max_bytes_) [[unlikely]] {
196
12.4k
        return false;
197
12.4k
    }
198
199
    // Advance byte_offset to next unread byte and read num_bytes
200
1.03M
    byte_offset_ += bytes_read;
201
1.03M
    memcpy(v, buffer_ + byte_offset_, num_bytes);
202
1.03M
    byte_offset_ += num_bytes;
203
204
    // Reset buffered_values_
205
1.03M
    bit_offset_ = 0;
206
1.03M
    int bytes_remaining = max_bytes_ - byte_offset_;
207
1.03M
    if (bytes_remaining >= 8) [[likely]] {
208
368k
        memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
209
670k
    } else {
210
670k
        memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining);
211
670k
    }
212
1.03M
    return true;
213
1.05M
}
_ZN5doris9BitReader10GetAlignedIhEEbiPT_
Line
Count
Source
192
765k
bool BitReader::GetAligned(int num_bytes, T* v) {
193
765k
    DCHECK_LE(num_bytes, sizeof(T));
194
765k
    int bytes_read = BitUtil::Ceil(bit_offset_, 8);
195
765k
    if (byte_offset_ + bytes_read + num_bytes > max_bytes_) [[unlikely]] {
196
12.4k
        return false;
197
12.4k
    }
198
199
    // Advance byte_offset to next unread byte and read num_bytes
200
753k
    byte_offset_ += bytes_read;
201
753k
    memcpy(v, buffer_ + byte_offset_, num_bytes);
202
753k
    byte_offset_ += num_bytes;
203
204
    // Reset buffered_values_
205
753k
    bit_offset_ = 0;
206
753k
    int bytes_remaining = max_bytes_ - byte_offset_;
207
753k
    if (bytes_remaining >= 8) [[likely]] {
208
294k
        memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
209
458k
    } else {
210
458k
        memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining);
211
458k
    }
212
753k
    return true;
213
765k
}
_ZN5doris9BitReader10GetAlignedIsEEbiPT_
Line
Count
Source
192
45.9k
bool BitReader::GetAligned(int num_bytes, T* v) {
193
45.9k
    DCHECK_LE(num_bytes, sizeof(T));
194
45.9k
    int bytes_read = BitUtil::Ceil(bit_offset_, 8);
195
45.9k
    if (byte_offset_ + bytes_read + num_bytes > max_bytes_) [[unlikely]] {
196
0
        return false;
197
0
    }
198
199
    // Advance byte_offset to next unread byte and read num_bytes
200
45.9k
    byte_offset_ += bytes_read;
201
45.9k
    memcpy(v, buffer_ + byte_offset_, num_bytes);
202
45.9k
    byte_offset_ += num_bytes;
203
204
    // Reset buffered_values_
205
45.9k
    bit_offset_ = 0;
206
45.9k
    int bytes_remaining = max_bytes_ - byte_offset_;
207
45.9k
    if (bytes_remaining >= 8) [[likely]] {
208
12.4k
        memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
209
33.5k
    } else {
210
33.5k
        memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining);
211
33.5k
    }
212
45.9k
    return true;
213
45.9k
}
_ZN5doris9BitReader10GetAlignedIbEEbiPT_
Line
Count
Source
192
239k
bool BitReader::GetAligned(int num_bytes, T* v) {
193
239k
    DCHECK_LE(num_bytes, sizeof(T));
194
239k
    int bytes_read = BitUtil::Ceil(bit_offset_, 8);
195
239k
    if (byte_offset_ + bytes_read + num_bytes > max_bytes_) [[unlikely]] {
196
0
        return false;
197
0
    }
198
199
    // Advance byte_offset to next unread byte and read num_bytes
200
239k
    byte_offset_ += bytes_read;
201
239k
    memcpy(v, buffer_ + byte_offset_, num_bytes);
202
239k
    byte_offset_ += num_bytes;
203
204
    // Reset buffered_values_
205
239k
    bit_offset_ = 0;
206
239k
    int bytes_remaining = max_bytes_ - byte_offset_;
207
239k
    if (bytes_remaining >= 8) [[likely]] {
208
61.1k
        memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
209
178k
    } else {
210
178k
        memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining);
211
178k
    }
212
239k
    return true;
213
239k
}
214
215
615k
inline bool BitReader::GetVlqInt(uint32_t* v) {
216
615k
    uint32_t tmp = 0;
217
712k
    for (int num_bytes = 0; num_bytes < MAX_VLQ_BYTE_LEN; num_bytes++) {
218
712k
        uint8_t byte = 0;
219
712k
        if (!GetAligned<uint8_t>(1, &byte)) return false;
220
699k
        tmp |= static_cast<uint32_t>(byte & 0x7F) << (7 * num_bytes);
221
699k
        if ((byte & 0x80) == 0) {
222
603k
            *v = tmp;
223
603k
            return true;
224
603k
        }
225
699k
    }
226
18.4E
    return false;
227
615k
}
228
229
64
inline bool BitReader::GetZigZagVlqInt(int32_t* v) {
230
64
    uint32_t u;
231
64
    if (!GetVlqInt(&u)) {
232
3
        return false;
233
3
    }
234
61
    u = (u >> 1) ^ (~(u & 1) + 1);
235
    // copy uint32_t to int32_t
236
61
    std::memcpy(v, &u, sizeof(uint32_t));
237
61
    return true;
238
64
}
239
240
585
inline bool BitReader::GetVlqInt(uint64_t* v) {
241
585
    uint64_t tmp = 0;
242
2.17k
    for (int num_bytes = 0; num_bytes < MAX_VLQ_BYTE_LEN_FOR_INT64; num_bytes++) {
243
2.17k
        uint8_t byte = 0;
244
2.17k
        if (!GetAligned<uint8_t>(1, &byte)) return false;
245
2.17k
        tmp |= static_cast<uint64_t>(byte & 0x7F) << (7 * num_bytes);
246
2.17k
        if ((byte & 0x80) == 0) {
247
585
            *v = tmp;
248
585
            return true;
249
585
        }
250
2.17k
    }
251
0
    return false;
252
585
}
253
254
585
inline bool BitReader::GetZigZagVlqInt(int64_t* v) {
255
585
    uint64_t u;
256
585
    if (!GetVlqInt(&u)) {
257
0
        return false;
258
0
    }
259
585
    u = (u >> 1) ^ (~(u & 1) + 1);
260
585
    std::memcpy(v, &u, sizeof(uint64_t));
261
585
    return true;
262
585
}
263
264
template <typename T>
265
15.9k
int BatchedBitReader::UnpackBatch(int bit_width, int num_values, T* v) {
266
15.9k
    DCHECK(buffer_pos_ != nullptr);
267
15.9k
    DCHECK_GE(bit_width, 0);
268
15.9k
    DCHECK_LE(bit_width, MAX_BITWIDTH);
269
15.9k
    DCHECK_LE(bit_width, sizeof(T) * 8);
270
15.9k
    DCHECK_GE(num_values, 0);
271
272
15.9k
    int64_t num_read;
273
15.9k
    std::tie(buffer_pos_, num_read) =
274
15.9k
            BitPacking::UnpackValues(bit_width, buffer_pos_, bytes_left(), num_values, v);
275
15.9k
    DCHECK_LE(buffer_pos_, buffer_end_);
276
15.9k
    DCHECK_LE(num_read, num_values);
277
15.9k
    return static_cast<int>(num_read);
278
15.9k
}
_ZN5doris16BatchedBitReader11UnpackBatchIjEEiiiPT_
Line
Count
Source
265
15.1k
int BatchedBitReader::UnpackBatch(int bit_width, int num_values, T* v) {
266
15.1k
    DCHECK(buffer_pos_ != nullptr);
267
15.1k
    DCHECK_GE(bit_width, 0);
268
15.1k
    DCHECK_LE(bit_width, MAX_BITWIDTH);
269
15.1k
    DCHECK_LE(bit_width, sizeof(T) * 8);
270
15.1k
    DCHECK_GE(num_values, 0);
271
272
15.1k
    int64_t num_read;
273
15.1k
    std::tie(buffer_pos_, num_read) =
274
15.1k
            BitPacking::UnpackValues(bit_width, buffer_pos_, bytes_left(), num_values, v);
275
15.1k
    DCHECK_LE(buffer_pos_, buffer_end_);
276
    DCHECK_LE(num_read, num_values);
277
15.1k
    return static_cast<int>(num_read);
278
15.1k
}
_ZN5doris16BatchedBitReader11UnpackBatchIhEEiiiPT_
Line
Count
Source
265
833
int BatchedBitReader::UnpackBatch(int bit_width, int num_values, T* v) {
266
833
    DCHECK(buffer_pos_ != nullptr);
267
833
    DCHECK_GE(bit_width, 0);
268
833
    DCHECK_LE(bit_width, MAX_BITWIDTH);
269
833
    DCHECK_LE(bit_width, sizeof(T) * 8);
270
833
    DCHECK_GE(num_values, 0);
271
272
833
    int64_t num_read;
273
833
    std::tie(buffer_pos_, num_read) =
274
833
            BitPacking::UnpackValues(bit_width, buffer_pos_, bytes_left(), num_values, v);
275
833
    DCHECK_LE(buffer_pos_, buffer_end_);
276
    DCHECK_LE(num_read, num_values);
277
833
    return static_cast<int>(num_read);
278
833
}
279
280
40
inline bool BatchedBitReader::SkipBatch(int bit_width, int num_values_to_skip) {
281
40
    DCHECK(buffer_pos_ != nullptr);
282
40
    DCHECK_GE(bit_width, 0);
283
40
    DCHECK_LE(bit_width, MAX_BITWIDTH);
284
40
    DCHECK_GE(num_values_to_skip, 0);
285
286
40
    int skip_bytes = BitUtil::RoundUpNumBytes(bit_width * num_values_to_skip);
287
40
    if (skip_bytes > buffer_end_ - buffer_pos_) {
288
0
        return false;
289
0
    }
290
40
    buffer_pos_ += skip_bytes;
291
40
    return true;
292
40
}
293
294
template <typename T>
295
int BatchedBitReader::UnpackAndDecodeBatch(int bit_width, T* dict, int64_t dict_len, int num_values,
296
                                           T* v, int64_t stride) {
297
    DCHECK(buffer_pos_ != nullptr);
298
    DCHECK_GE(bit_width, 0);
299
    DCHECK_LE(bit_width, MAX_BITWIDTH);
300
    DCHECK_GE(num_values, 0);
301
302
    const uint8_t* new_buffer_pos;
303
    int64_t num_read;
304
    bool decode_error = false;
305
    std::tie(new_buffer_pos, num_read) =
306
            BitPacking::UnpackAndDecodeValues(bit_width, buffer_pos_, bytes_left(), dict, dict_len,
307
                                              num_values, v, stride, &decode_error);
308
    if (UNLIKELY(decode_error)) return -1;
309
    buffer_pos_ = new_buffer_pos;
310
    DCHECK_LE(buffer_pos_, buffer_end_);
311
    DCHECK_LE(num_read, num_values);
312
    return static_cast<int>(num_read);
313
}
314
315
template <typename T>
316
26.6k
bool BatchedBitReader::GetBytes(int num_bytes, T* v) {
317
26.6k
    DCHECK(buffer_pos_ != nullptr);
318
26.6k
    DCHECK_GE(num_bytes, 0);
319
26.6k
    DCHECK_LE(num_bytes, sizeof(T));
320
26.6k
    if (UNLIKELY(buffer_pos_ + num_bytes > buffer_end_)) return false;
321
26.6k
    *v = 0; // Ensure unset bytes are initialized to zero.
322
26.6k
    memcpy(v, buffer_pos_, num_bytes);
323
26.6k
    buffer_pos_ += num_bytes;
324
26.6k
    return true;
325
26.6k
}
_ZN5doris16BatchedBitReader8GetBytesIhEEbiPT_
Line
Count
Source
316
18.8k
bool BatchedBitReader::GetBytes(int num_bytes, T* v) {
317
18.8k
    DCHECK(buffer_pos_ != nullptr);
318
18.8k
    DCHECK_GE(num_bytes, 0);
319
18.8k
    DCHECK_LE(num_bytes, sizeof(T));
320
18.8k
    if (UNLIKELY(buffer_pos_ + num_bytes > buffer_end_)) return false;
321
18.8k
    *v = 0; // Ensure unset bytes are initialized to zero.
322
18.8k
    memcpy(v, buffer_pos_, num_bytes);
323
18.8k
    buffer_pos_ += num_bytes;
324
18.8k
    return true;
325
18.8k
}
_ZN5doris16BatchedBitReader8GetBytesIjEEbiPT_
Line
Count
Source
316
7.77k
bool BatchedBitReader::GetBytes(int num_bytes, T* v) {
317
7.77k
    DCHECK(buffer_pos_ != nullptr);
318
7.77k
    DCHECK_GE(num_bytes, 0);
319
7.77k
    DCHECK_LE(num_bytes, sizeof(T));
320
7.77k
    if (UNLIKELY(buffer_pos_ + num_bytes > buffer_end_)) return false;
321
7.77k
    *v = 0; // Ensure unset bytes are initialized to zero.
322
7.77k
    memcpy(v, buffer_pos_, num_bytes);
323
7.77k
    buffer_pos_ += num_bytes;
324
7.77k
    return true;
325
7.77k
}
326
327
template <typename UINT_T>
328
18.7k
bool BatchedBitReader::GetUleb128(UINT_T* v) {
329
18.7k
    static_assert(std::is_integral<UINT_T>::value, "Integral type required.");
330
18.7k
    static_assert(std::is_unsigned<UINT_T>::value, "Unsigned type required.");
331
18.7k
    static_assert(!std::is_same<UINT_T, bool>::value, "Bools are not supported.");
332
333
18.7k
    *v = 0;
334
18.7k
    int shift = 0;
335
18.7k
    uint8_t byte = 0;
336
18.8k
    do {
337
18.8k
        if (UNLIKELY(shift >= max_vlq_byte_len<UINT_T>() * 7)) return false;
338
18.8k
        if (!GetBytes(1, &byte)) return false;
339
340
        /// We need to convert 'byte' to UINT_T so that the result of the bitwise and
341
        /// operation is at least as long an integer as '*v', otherwise the shift may be too
342
        /// big and lead to undefined behaviour.
343
18.8k
        const UINT_T byte_as_UINT_T = byte;
344
18.8k
        *v |= (byte_as_UINT_T & 0x7Fu) << shift;
345
18.8k
        shift += 7;
346
18.8k
    } while ((byte & 0x80u) != 0);
347
18.7k
    return true;
348
18.7k
}
349
350
} // namespace doris