Coverage Report

Created: 2026-08-27 07:23

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
18.4M
inline void BitWriter::PutValue(uint64_t v, int num_bits) {
35
18.4M
    DCHECK_LE(num_bits, 64);
36
    // Truncate the higher-order bits. This is necessary to
37
    // support signed values.
38
18.4M
    v &= ~0ULL >> (64 - num_bits);
39
40
18.4M
    buffered_values_ |= v << bit_offset_;
41
18.4M
    bit_offset_ += num_bits;
42
43
18.4M
    if (bit_offset_ >= 64) [[unlikely]] {
44
        // Flush buffered_values_ and write out bits of v that did not fit
45
279k
        buffer_->reserve(ALIGN_UP(byte_offset_ + 8, 8));
46
279k
        buffer_->resize(byte_offset_ + 8);
47
279k
        DCHECK_LE(byte_offset_ + 8, buffer_->capacity());
48
279k
        memcpy(buffer_->data() + byte_offset_, &buffered_values_, 8);
49
279k
        buffered_values_ = 0;
50
279k
        byte_offset_ += 8;
51
279k
        bit_offset_ -= 64;
52
279k
        buffered_values_ = BitUtil::ShiftRightZeroOnOverflow(v, (num_bits - bit_offset_));
53
279k
    }
54
18.4M
    DCHECK_LT(bit_offset_, 64);
55
18.4M
}
56
57
70.6M
inline void BitWriter::Flush(bool align) {
58
70.6M
    int num_bytes = BitUtil::Ceil(bit_offset_, 8);
59
70.6M
    buffer_->reserve(ALIGN_UP(byte_offset_ + num_bytes, 8));
60
70.6M
    buffer_->resize(byte_offset_ + num_bytes);
61
70.6M
    DCHECK_LE(byte_offset_ + num_bytes, buffer_->capacity());
62
70.6M
    memcpy(buffer_->data() + byte_offset_, &buffered_values_, num_bytes);
63
64
70.6M
    if (align) {
65
59.5M
        buffered_values_ = 0;
66
59.5M
        byte_offset_ += num_bytes;
67
59.5M
        bit_offset_ = 0;
68
59.5M
    }
69
70.6M
}
70
71
59.5M
inline uint8_t* BitWriter::GetNextBytePtr(int num_bytes) {
72
59.5M
    Flush(/* align */ true);
73
59.5M
    buffer_->reserve(ALIGN_UP(byte_offset_ + num_bytes, 8));
74
59.5M
    buffer_->resize(byte_offset_ + num_bytes);
75
59.5M
    uint8_t* ptr = buffer_->data() + byte_offset_;
76
59.5M
    byte_offset_ += num_bytes;
77
59.5M
    DCHECK_LE(byte_offset_, buffer_->capacity());
78
59.5M
    return ptr;
79
59.5M
}
80
81
template <typename T>
82
58.4M
void BitWriter::PutAligned(T val, int num_bytes) {
83
58.4M
    DCHECK_LE(num_bytes, sizeof(T));
84
58.4M
    uint8_t* ptr = GetNextBytePtr(num_bytes);
85
58.4M
    memcpy(ptr, &val, num_bytes);
86
58.4M
}
_ZN5doris9BitWriter10PutAlignedIhEEvT_i
Line
Count
Source
82
38.3M
void BitWriter::PutAligned(T val, int num_bytes) {
83
    DCHECK_LE(num_bytes, sizeof(T));
84
38.3M
    uint8_t* ptr = GetNextBytePtr(num_bytes);
85
38.3M
    memcpy(ptr, &val, num_bytes);
86
38.3M
}
_ZN5doris9BitWriter10PutAlignedImEEvT_i
Line
Count
Source
82
20.1M
void BitWriter::PutAligned(T val, int num_bytes) {
83
    DCHECK_LE(num_bytes, sizeof(T));
84
20.1M
    uint8_t* ptr = GetNextBytePtr(num_bytes);
85
20.1M
    memcpy(ptr, &val, num_bytes);
86
20.1M
}
87
88
20.1M
inline void BitWriter::PutVlqInt(int32_t v) {
89
38.3M
    while ((v & 0xFFFFFF80) != 0L) {
90
18.1M
        PutAligned<uint8_t>((v & 0x7F) | 0x80, 1);
91
18.1M
        v >>= 7;
92
18.1M
    }
93
20.1M
    PutAligned<uint8_t>(v & 0x7F, 1);
94
20.1M
}
95
96
inline BitReader::BitReader(const uint8_t* buffer, int buffer_len)
97
194k
        : buffer_(buffer),
98
194k
          max_bytes_(buffer_len),
99
194k
          buffered_values_(0),
100
194k
          byte_offset_(0),
101
194k
          bit_offset_(0) {
102
194k
    int num_bytes = std::min(8, max_bytes_);
103
194k
    memcpy(&buffered_values_, buffer_ + byte_offset_, num_bytes);
104
194k
}
105
106
581k
inline void BitReader::BufferValues() {
107
581k
    int bytes_remaining = max_bytes_ - byte_offset_;
108
581k
    if (bytes_remaining >= 8) [[likely]] {
109
579k
        memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
110
579k
    } else {
111
2.22k
        memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining);
112
2.22k
    }
113
581k
}
114
115
template <typename T>
116
26.5M
bool BitReader::GetValue(int num_bits, T* v) {
117
26.5M
    DCHECK_LE(num_bits, 64);
118
26.5M
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
26.5M
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
26.5M
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
26.5M
                        bit_offset_);
126
127
26.5M
    bit_offset_ += num_bits;
128
26.5M
    if (bit_offset_ >= 64) {
129
581k
        byte_offset_ += 8;
130
581k
        bit_offset_ -= 64;
131
581k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
581k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
581k
                                               (num_bits - bit_offset_));
135
581k
    }
136
26.5M
    DCHECK_LE(bit_offset_, 64);
137
26.5M
    return true;
138
26.5M
}
_ZN5doris9BitReader8GetValueIsEEbiPT_
Line
Count
Source
116
824k
bool BitReader::GetValue(int num_bits, T* v) {
117
824k
    DCHECK_LE(num_bits, 64);
118
824k
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
824k
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
824k
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
824k
                        bit_offset_);
126
127
824k
    bit_offset_ += num_bits;
128
824k
    if (bit_offset_ >= 64) {
129
4.55k
        byte_offset_ += 8;
130
4.55k
        bit_offset_ -= 64;
131
4.55k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
4.55k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
4.55k
                                               (num_bits - bit_offset_));
135
4.55k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
824k
    return true;
138
824k
}
_ZN5doris9BitReader8GetValueImEEbiPT_
Line
Count
Source
116
1.54M
bool BitReader::GetValue(int num_bits, T* v) {
117
1.54M
    DCHECK_LE(num_bits, 64);
118
1.54M
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
1.54M
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
1.54M
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
1.54M
                        bit_offset_);
126
127
1.54M
    bit_offset_ += num_bits;
128
1.54M
    if (bit_offset_ >= 64) {
129
47.0k
        byte_offset_ += 8;
130
47.0k
        bit_offset_ -= 64;
131
47.0k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
47.0k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
47.0k
                                               (num_bits - bit_offset_));
135
47.0k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
1.54M
    return true;
138
1.54M
}
_ZN5doris9BitReader8GetValueIlEEbiPT_
Line
Count
Source
116
286k
bool BitReader::GetValue(int num_bits, T* v) {
117
286k
    DCHECK_LE(num_bits, 64);
118
286k
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
286k
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
286k
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
286k
                        bit_offset_);
126
127
286k
    bit_offset_ += num_bits;
128
286k
    if (bit_offset_ >= 64) {
129
162k
        byte_offset_ += 8;
130
162k
        bit_offset_ -= 64;
131
162k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
162k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
162k
                                               (num_bits - bit_offset_));
135
162k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
286k
    return true;
138
286k
}
_ZN5doris9BitReader8GetValueIiEEbiPT_
Line
Count
Source
116
1.44M
bool BitReader::GetValue(int num_bits, T* v) {
117
1.44M
    DCHECK_LE(num_bits, 64);
118
1.44M
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
1.44M
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
1.44M
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
1.44M
                        bit_offset_);
126
127
1.44M
    bit_offset_ += num_bits;
128
1.44M
    if (bit_offset_ >= 64) {
129
264
        byte_offset_ += 8;
130
264
        bit_offset_ -= 64;
131
264
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
264
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
264
                                               (num_bits - bit_offset_));
135
264
    }
136
    DCHECK_LE(bit_offset_, 64);
137
1.44M
    return true;
138
1.44M
}
_ZN5doris9BitReader8GetValueIcEEbiPT_
Line
Count
Source
116
529k
bool BitReader::GetValue(int num_bits, T* v) {
117
529k
    DCHECK_LE(num_bits, 64);
118
529k
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
529k
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
529k
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
529k
                        bit_offset_);
126
127
529k
    bit_offset_ += num_bits;
128
529k
    if (bit_offset_ >= 64) {
129
66.1k
        byte_offset_ += 8;
130
66.1k
        bit_offset_ -= 64;
131
66.1k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
66.1k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
66.1k
                                               (num_bits - bit_offset_));
135
66.1k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
529k
    return true;
138
529k
}
_ZN5doris9BitReader8GetValueIhEEbiPT_
Line
Count
Source
116
21.1M
bool BitReader::GetValue(int num_bits, T* v) {
117
21.1M
    DCHECK_LE(num_bits, 64);
118
21.1M
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
21.1M
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
21.1M
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
21.1M
                        bit_offset_);
126
127
21.1M
    bit_offset_ += num_bits;
128
21.1M
    if (bit_offset_ >= 64) {
129
295k
        byte_offset_ += 8;
130
295k
        bit_offset_ -= 64;
131
295k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
295k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
295k
                                               (num_bits - bit_offset_));
135
295k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
21.1M
    return true;
138
21.1M
}
_ZN5doris9BitReader8GetValueIbEEbiPT_
Line
Count
Source
116
797k
bool BitReader::GetValue(int num_bits, T* v) {
117
797k
    DCHECK_LE(num_bits, 64);
118
797k
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
797k
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
797k
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
797k
                        bit_offset_);
126
127
797k
    bit_offset_ += num_bits;
128
797k
    if (bit_offset_ >= 64) {
129
4.69k
        byte_offset_ += 8;
130
4.69k
        bit_offset_ -= 64;
131
4.69k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
4.69k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
4.69k
                                               (num_bits - bit_offset_));
135
4.69k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
797k
    return true;
138
797k
}
139
140
667k
inline void BitReader::Rewind(int num_bits) {
141
667k
    bit_offset_ -= num_bits;
142
667k
    if (bit_offset_ >= 0) {
143
664k
        return;
144
664k
    }
145
8.07k
    while (bit_offset_ < 0) {
146
4.51k
        int seek_back = std::min(byte_offset_, 8);
147
4.51k
        byte_offset_ -= seek_back;
148
4.51k
        bit_offset_ += seek_back * 8;
149
4.51k
    }
150
    // This should only be executed *if* rewinding by 'num_bits'
151
    // make the existing buffered_values_ invalid
152
3.55k
    DCHECK_GE(byte_offset_, 0); // Check for underflow
153
3.55k
    memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
154
3.55k
}
155
156
80
inline bool BitReader::Advance(int64_t num_bits) {
157
80
    int64_t bits_required = bit_offset_ + num_bits;
158
80
    int64_t bytes_required = (bits_required >> 3) + ((bits_required & 7) != 0);
159
80
    if (bytes_required > max_bytes_ - byte_offset_) {
160
0
        return false;
161
0
    }
162
80
    byte_offset_ += static_cast<int>(bits_required >> 3);
163
80
    bit_offset_ = static_cast<int>(bits_required & 7);
164
80
    BufferValues();
165
80
    return true;
166
80
}
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.00M
bool BitReader::GetAligned(int num_bytes, T* v) {
193
1.00M
    DCHECK_LE(num_bytes, sizeof(T));
194
1.00M
    int bytes_read = BitUtil::Ceil(bit_offset_, 8);
195
1.00M
    if (byte_offset_ + bytes_read + num_bytes > max_bytes_) [[unlikely]] {
196
2.20k
        return false;
197
2.20k
    }
198
199
    // Advance byte_offset to next unread byte and read num_bytes
200
1.00M
    byte_offset_ += bytes_read;
201
1.00M
    memcpy(v, buffer_ + byte_offset_, num_bytes);
202
1.00M
    byte_offset_ += num_bytes;
203
204
    // Reset buffered_values_
205
1.00M
    bit_offset_ = 0;
206
1.00M
    int bytes_remaining = max_bytes_ - byte_offset_;
207
1.00M
    if (bytes_remaining >= 8) [[likely]] {
208
592k
        memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
209
592k
    } else {
210
411k
        memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining);
211
411k
    }
212
1.00M
    return true;
213
1.00M
}
_ZN5doris9BitReader10GetAlignedIhEEbiPT_
Line
Count
Source
192
746k
bool BitReader::GetAligned(int num_bytes, T* v) {
193
746k
    DCHECK_LE(num_bytes, sizeof(T));
194
746k
    int bytes_read = BitUtil::Ceil(bit_offset_, 8);
195
746k
    if (byte_offset_ + bytes_read + num_bytes > max_bytes_) [[unlikely]] {
196
2.20k
        return false;
197
2.20k
    }
198
199
    // Advance byte_offset to next unread byte and read num_bytes
200
744k
    byte_offset_ += bytes_read;
201
744k
    memcpy(v, buffer_ + byte_offset_, num_bytes);
202
744k
    byte_offset_ += num_bytes;
203
204
    // Reset buffered_values_
205
744k
    bit_offset_ = 0;
206
744k
    int bytes_remaining = max_bytes_ - byte_offset_;
207
744k
    if (bytes_remaining >= 8) [[likely]] {
208
459k
        memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
209
459k
    } else {
210
285k
        memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining);
211
285k
    }
212
744k
    return true;
213
746k
}
_ZN5doris9BitReader10GetAlignedIsEEbiPT_
Line
Count
Source
192
87.9k
bool BitReader::GetAligned(int num_bytes, T* v) {
193
87.9k
    DCHECK_LE(num_bytes, sizeof(T));
194
87.9k
    int bytes_read = BitUtil::Ceil(bit_offset_, 8);
195
87.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
87.9k
    byte_offset_ += bytes_read;
201
87.9k
    memcpy(v, buffer_ + byte_offset_, num_bytes);
202
87.9k
    byte_offset_ += num_bytes;
203
204
    // Reset buffered_values_
205
87.9k
    bit_offset_ = 0;
206
87.9k
    int bytes_remaining = max_bytes_ - byte_offset_;
207
87.9k
    if (bytes_remaining >= 8) [[likely]] {
208
85.2k
        memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
209
85.2k
    } else {
210
2.72k
        memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining);
211
2.72k
    }
212
87.9k
    return true;
213
87.9k
}
_ZN5doris9BitReader10GetAlignedIbEEbiPT_
Line
Count
Source
192
171k
bool BitReader::GetAligned(int num_bytes, T* v) {
193
171k
    DCHECK_LE(num_bytes, sizeof(T));
194
171k
    int bytes_read = BitUtil::Ceil(bit_offset_, 8);
195
171k
    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
171k
    byte_offset_ += bytes_read;
201
171k
    memcpy(v, buffer_ + byte_offset_, num_bytes);
202
171k
    byte_offset_ += num_bytes;
203
204
    // Reset buffered_values_
205
171k
    bit_offset_ = 0;
206
171k
    int bytes_remaining = max_bytes_ - byte_offset_;
207
171k
    if (bytes_remaining >= 8) [[likely]] {
208
47.8k
        memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
209
123k
    } else {
210
123k
        memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining);
211
123k
    }
212
171k
    return true;
213
171k
}
214
215
565k
inline bool BitReader::GetVlqInt(uint32_t* v) {
216
565k
    uint32_t tmp = 0;
217
697k
    for (int num_bytes = 0; num_bytes < MAX_VLQ_BYTE_LEN; num_bytes++) {
218
697k
        uint8_t byte = 0;
219
697k
        if (!GetAligned<uint8_t>(1, &byte)) return false;
220
695k
        tmp |= static_cast<uint32_t>(byte & 0x7F) << (7 * num_bytes);
221
695k
        if ((byte & 0x80) == 0) {
222
563k
            *v = tmp;
223
563k
            return true;
224
563k
        }
225
695k
    }
226
18.4E
    return false;
227
565k
}
228
229
3.23k
inline bool BitReader::GetZigZagVlqInt(int32_t* v) {
230
3.23k
    uint32_t u;
231
3.23k
    if (!GetVlqInt(&u)) {
232
3
        return false;
233
3
    }
234
3.22k
    u = (u >> 1) ^ (~(u & 1) + 1);
235
    // copy uint32_t to int32_t
236
3.22k
    std::memcpy(v, &u, sizeof(uint32_t));
237
3.22k
    return true;
238
3.23k
}
239
240
0
inline bool BitReader::GetVlqInt(uint64_t* v) {
241
0
    uint64_t tmp = 0;
242
0
    for (int num_bytes = 0; num_bytes < MAX_VLQ_BYTE_LEN_FOR_INT64; num_bytes++) {
243
0
        uint8_t byte = 0;
244
0
        if (!GetAligned<uint8_t>(1, &byte)) return false;
245
0
        tmp |= static_cast<uint64_t>(byte & 0x7F) << (7 * num_bytes);
246
0
        if ((byte & 0x80) == 0) {
247
0
            *v = tmp;
248
0
            return true;
249
0
        }
250
0
    }
251
0
    return false;
252
0
}
253
254
0
inline bool BitReader::GetZigZagVlqInt(int64_t* v) {
255
0
    uint64_t u;
256
0
    if (!GetVlqInt(&u)) {
257
0
        return false;
258
0
    }
259
0
    u = (u >> 1) ^ (~(u & 1) + 1);
260
0
    std::memcpy(v, &u, sizeof(uint64_t));
261
0
    return true;
262
0
}
263
264
template <typename T>
265
40.5k
int BatchedBitReader::UnpackBatch(int bit_width, int num_values, T* v) {
266
40.5k
    DCHECK(buffer_pos_ != nullptr);
267
40.5k
    DCHECK_GE(bit_width, 0);
268
40.5k
    DCHECK_LE(bit_width, MAX_BITWIDTH);
269
40.5k
    DCHECK_LE(bit_width, sizeof(T) * 8);
270
40.5k
    DCHECK_GE(num_values, 0);
271
272
40.5k
    int64_t num_read;
273
40.5k
    std::tie(buffer_pos_, num_read) =
274
40.5k
            BitPacking::UnpackValues(bit_width, buffer_pos_, bytes_left(), num_values, v);
275
40.5k
    DCHECK_LE(buffer_pos_, buffer_end_);
276
40.5k
    DCHECK_LE(num_read, num_values);
277
40.5k
    return static_cast<int>(num_read);
278
40.5k
}
_ZN5doris16BatchedBitReader11UnpackBatchIjEEiiiPT_
Line
Count
Source
265
30.8k
int BatchedBitReader::UnpackBatch(int bit_width, int num_values, T* v) {
266
30.8k
    DCHECK(buffer_pos_ != nullptr);
267
30.8k
    DCHECK_GE(bit_width, 0);
268
30.8k
    DCHECK_LE(bit_width, MAX_BITWIDTH);
269
30.8k
    DCHECK_LE(bit_width, sizeof(T) * 8);
270
30.8k
    DCHECK_GE(num_values, 0);
271
272
30.8k
    int64_t num_read;
273
30.8k
    std::tie(buffer_pos_, num_read) =
274
30.8k
            BitPacking::UnpackValues(bit_width, buffer_pos_, bytes_left(), num_values, v);
275
30.8k
    DCHECK_LE(buffer_pos_, buffer_end_);
276
    DCHECK_LE(num_read, num_values);
277
30.8k
    return static_cast<int>(num_read);
278
30.8k
}
_ZN5doris16BatchedBitReader11UnpackBatchItEEiiiPT_
Line
Count
Source
265
8.84k
int BatchedBitReader::UnpackBatch(int bit_width, int num_values, T* v) {
266
8.84k
    DCHECK(buffer_pos_ != nullptr);
267
8.84k
    DCHECK_GE(bit_width, 0);
268
8.84k
    DCHECK_LE(bit_width, MAX_BITWIDTH);
269
8.84k
    DCHECK_LE(bit_width, sizeof(T) * 8);
270
8.84k
    DCHECK_GE(num_values, 0);
271
272
8.84k
    int64_t num_read;
273
8.84k
    std::tie(buffer_pos_, num_read) =
274
8.84k
            BitPacking::UnpackValues(bit_width, buffer_pos_, bytes_left(), num_values, v);
275
8.84k
    DCHECK_LE(buffer_pos_, buffer_end_);
276
    DCHECK_LE(num_read, num_values);
277
8.84k
    return static_cast<int>(num_read);
278
8.84k
}
_ZN5doris16BatchedBitReader11UnpackBatchIhEEiiiPT_
Line
Count
Source
265
779
int BatchedBitReader::UnpackBatch(int bit_width, int num_values, T* v) {
266
779
    DCHECK(buffer_pos_ != nullptr);
267
779
    DCHECK_GE(bit_width, 0);
268
779
    DCHECK_LE(bit_width, MAX_BITWIDTH);
269
779
    DCHECK_LE(bit_width, sizeof(T) * 8);
270
779
    DCHECK_GE(num_values, 0);
271
272
779
    int64_t num_read;
273
779
    std::tie(buffer_pos_, num_read) =
274
779
            BitPacking::UnpackValues(bit_width, buffer_pos_, bytes_left(), num_values, v);
275
779
    DCHECK_LE(buffer_pos_, buffer_end_);
276
    DCHECK_LE(num_read, num_values);
277
779
    return static_cast<int>(num_read);
278
779
}
279
280
4
inline bool BatchedBitReader::SkipBatch(int bit_width, int num_values_to_skip) {
281
4
    DCHECK(buffer_pos_ != nullptr);
282
4
    DCHECK_GE(bit_width, 0);
283
4
    DCHECK_LE(bit_width, MAX_BITWIDTH);
284
4
    DCHECK_GE(num_values_to_skip, 0);
285
286
4
    int skip_bytes = BitUtil::RoundUpNumBytes(bit_width * num_values_to_skip);
287
4
    if (skip_bytes > buffer_end_ - buffer_pos_) {
288
0
        return false;
289
0
    }
290
4
    buffer_pos_ += skip_bytes;
291
4
    return true;
292
4
}
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
58.9k
bool BatchedBitReader::GetBytes(int num_bytes, T* v) {
317
58.9k
    DCHECK(buffer_pos_ != nullptr);
318
58.9k
    DCHECK_GE(num_bytes, 0);
319
58.9k
    DCHECK_LE(num_bytes, sizeof(T));
320
58.9k
    if (UNLIKELY(buffer_pos_ + num_bytes > buffer_end_)) return false;
321
58.9k
    *v = 0; // Ensure unset bytes are initialized to zero.
322
58.9k
    memcpy(v, buffer_pos_, num_bytes);
323
58.9k
    buffer_pos_ += num_bytes;
324
58.9k
    return true;
325
58.9k
}
_ZN5doris16BatchedBitReader8GetBytesIhEEbiPT_
Line
Count
Source
316
47.1k
bool BatchedBitReader::GetBytes(int num_bytes, T* v) {
317
47.1k
    DCHECK(buffer_pos_ != nullptr);
318
47.1k
    DCHECK_GE(num_bytes, 0);
319
47.1k
    DCHECK_LE(num_bytes, sizeof(T));
320
47.1k
    if (UNLIKELY(buffer_pos_ + num_bytes > buffer_end_)) return false;
321
47.1k
    *v = 0; // Ensure unset bytes are initialized to zero.
322
47.1k
    memcpy(v, buffer_pos_, num_bytes);
323
47.1k
    buffer_pos_ += num_bytes;
324
47.1k
    return true;
325
47.1k
}
_ZN5doris16BatchedBitReader8GetBytesIjEEbiPT_
Line
Count
Source
316
712
bool BatchedBitReader::GetBytes(int num_bytes, T* v) {
317
712
    DCHECK(buffer_pos_ != nullptr);
318
712
    DCHECK_GE(num_bytes, 0);
319
712
    DCHECK_LE(num_bytes, sizeof(T));
320
712
    if (UNLIKELY(buffer_pos_ + num_bytes > buffer_end_)) return false;
321
712
    *v = 0; // Ensure unset bytes are initialized to zero.
322
712
    memcpy(v, buffer_pos_, num_bytes);
323
712
    buffer_pos_ += num_bytes;
324
712
    return true;
325
712
}
_ZN5doris16BatchedBitReader8GetBytesItEEbiPT_
Line
Count
Source
316
11.1k
bool BatchedBitReader::GetBytes(int num_bytes, T* v) {
317
11.1k
    DCHECK(buffer_pos_ != nullptr);
318
11.1k
    DCHECK_GE(num_bytes, 0);
319
11.1k
    DCHECK_LE(num_bytes, sizeof(T));
320
11.1k
    if (UNLIKELY(buffer_pos_ + num_bytes > buffer_end_)) return false;
321
11.1k
    *v = 0; // Ensure unset bytes are initialized to zero.
322
11.1k
    memcpy(v, buffer_pos_, num_bytes);
323
11.1k
    buffer_pos_ += num_bytes;
324
11.1k
    return true;
325
11.1k
}
326
327
template <typename UINT_T>
328
37.8k
bool BatchedBitReader::GetUleb128(UINT_T* v) {
329
37.8k
    static_assert(std::is_integral<UINT_T>::value, "Integral type required.");
330
37.8k
    static_assert(std::is_unsigned<UINT_T>::value, "Unsigned type required.");
331
37.8k
    static_assert(!std::is_same<UINT_T, bool>::value, "Bools are not supported.");
332
333
37.8k
    *v = 0;
334
37.8k
    int shift = 0;
335
37.8k
    uint8_t byte = 0;
336
47.1k
    do {
337
47.1k
        if (UNLIKELY(shift >= max_vlq_byte_len<UINT_T>() * 7)) return false;
338
47.1k
        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
47.1k
        const UINT_T byte_as_UINT_T = byte;
344
47.1k
        *v |= (byte_as_UINT_T & 0x7Fu) << shift;
345
47.1k
        shift += 7;
346
47.1k
    } while ((byte & 0x80u) != 0);
347
37.8k
    return true;
348
37.8k
}
349
350
} // namespace doris