Coverage Report

Created: 2026-05-29 12: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.7M
inline void BitWriter::PutValue(uint64_t v, int num_bits) {
35
18.7M
    DCHECK_LE(num_bits, 64);
36
    // Truncate the higher-order bits. This is necessary to
37
    // support signed values.
38
18.7M
    v &= ~0ULL >> (64 - num_bits);
39
40
18.7M
    buffered_values_ |= v << bit_offset_;
41
18.7M
    bit_offset_ += num_bits;
42
43
18.7M
    if (bit_offset_ >= 64) [[unlikely]] {
44
        // Flush buffered_values_ and write out bits of v that did not fit
45
278k
        buffer_->reserve(ALIGN_UP(byte_offset_ + 8, 8));
46
278k
        buffer_->resize(byte_offset_ + 8);
47
278k
        DCHECK_LE(byte_offset_ + 8, buffer_->capacity());
48
278k
        memcpy(buffer_->data() + byte_offset_, &buffered_values_, 8);
49
278k
        buffered_values_ = 0;
50
278k
        byte_offset_ += 8;
51
278k
        bit_offset_ -= 64;
52
278k
        buffered_values_ = BitUtil::ShiftRightZeroOnOverflow(v, (num_bits - bit_offset_));
53
278k
    }
54
18.7M
    DCHECK_LT(bit_offset_, 64);
55
18.7M
}
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.4M
        buffered_values_ = 0;
66
59.4M
        byte_offset_ += num_bytes;
67
59.4M
        bit_offset_ = 0;
68
59.4M
    }
69
70.6M
}
70
71
59.4M
inline uint8_t* BitWriter::GetNextBytePtr(int num_bytes) {
72
59.4M
    Flush(/* align */ true);
73
59.4M
    buffer_->reserve(ALIGN_UP(byte_offset_ + num_bytes, 8));
74
59.4M
    buffer_->resize(byte_offset_ + num_bytes);
75
59.4M
    uint8_t* ptr = buffer_->data() + byte_offset_;
76
59.4M
    byte_offset_ += num_bytes;
77
59.4M
    DCHECK_LE(byte_offset_, buffer_->capacity());
78
59.4M
    return ptr;
79
59.4M
}
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.2M
void BitWriter::PutAligned(T val, int num_bytes) {
83
    DCHECK_LE(num_bytes, sizeof(T));
84
38.2M
    uint8_t* ptr = GetNextBytePtr(num_bytes);
85
38.2M
    memcpy(ptr, &val, num_bytes);
86
38.2M
}
_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.2M
    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
875k
        : buffer_(buffer),
98
875k
          max_bytes_(buffer_len),
99
875k
          buffered_values_(0),
100
875k
          byte_offset_(0),
101
875k
          bit_offset_(0) {
102
875k
    int num_bytes = std::min(8, max_bytes_);
103
875k
    memcpy(&buffered_values_, buffer_ + byte_offset_, num_bytes);
104
875k
}
105
106
2.87M
inline void BitReader::BufferValues() {
107
2.87M
    int bytes_remaining = max_bytes_ - byte_offset_;
108
2.87M
    if (bytes_remaining >= 8) [[likely]] {
109
2.86M
        memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
110
2.86M
    } else {
111
2.64k
        memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining);
112
2.64k
    }
113
2.87M
}
114
115
template <typename T>
116
133M
bool BitReader::GetValue(int num_bits, T* v) {
117
133M
    DCHECK_LE(num_bits, 64);
118
133M
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
133M
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
133M
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
133M
                        bit_offset_);
126
127
133M
    bit_offset_ += num_bits;
128
133M
    if (bit_offset_ >= 64) {
129
2.87M
        byte_offset_ += 8;
130
2.87M
        bit_offset_ -= 64;
131
2.87M
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
2.87M
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
2.87M
                                               (num_bits - bit_offset_));
135
2.87M
    }
136
133M
    DCHECK_LE(bit_offset_, 64);
137
133M
    return true;
138
133M
}
_ZN5doris9BitReader8GetValueIsEEbiPT_
Line
Count
Source
116
76.8M
bool BitReader::GetValue(int num_bits, T* v) {
117
76.8M
    DCHECK_LE(num_bits, 64);
118
76.8M
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
76.8M
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
76.8M
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
76.8M
                        bit_offset_);
126
127
76.8M
    bit_offset_ += num_bits;
128
76.8M
    if (bit_offset_ >= 64) {
129
1.92M
        byte_offset_ += 8;
130
1.92M
        bit_offset_ -= 64;
131
1.92M
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
1.92M
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
1.92M
                                               (num_bits - bit_offset_));
135
1.92M
    }
136
    DCHECK_LE(bit_offset_, 64);
137
76.8M
    return true;
138
76.8M
}
_ZN5doris9BitReader8GetValueImEEbiPT_
Line
Count
Source
116
9.74M
bool BitReader::GetValue(int num_bits, T* v) {
117
9.74M
    DCHECK_LE(num_bits, 64);
118
9.74M
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
9.74M
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
9.74M
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
9.74M
                        bit_offset_);
126
127
9.74M
    bit_offset_ += num_bits;
128
9.74M
    if (bit_offset_ >= 64) {
129
72.2k
        byte_offset_ += 8;
130
72.2k
        bit_offset_ -= 64;
131
72.2k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
72.2k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
72.2k
                                               (num_bits - bit_offset_));
135
72.2k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
9.74M
    return true;
138
9.74M
}
_ZN5doris9BitReader8GetValueIbEEbiPT_
Line
Count
Source
116
982k
bool BitReader::GetValue(int num_bits, T* v) {
117
982k
    DCHECK_LE(num_bits, 64);
118
982k
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
982k
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
982k
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
982k
                        bit_offset_);
126
127
982k
    bit_offset_ += num_bits;
128
982k
    if (bit_offset_ >= 64) {
129
5.43k
        byte_offset_ += 8;
130
5.43k
        bit_offset_ -= 64;
131
5.43k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
5.43k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
5.43k
                                               (num_bits - bit_offset_));
135
5.43k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
982k
    return true;
138
982k
}
_ZN5doris9BitReader8GetValueIhEEbiPT_
Line
Count
Source
116
45.3M
bool BitReader::GetValue(int num_bits, T* v) {
117
45.3M
    DCHECK_LE(num_bits, 64);
118
45.3M
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
45.3M
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
45.3M
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
45.3M
                        bit_offset_);
126
127
45.3M
    bit_offset_ += num_bits;
128
45.3M
    if (bit_offset_ >= 64) {
129
653k
        byte_offset_ += 8;
130
653k
        bit_offset_ -= 64;
131
653k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
653k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
653k
                                               (num_bits - bit_offset_));
135
653k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
45.3M
    return true;
138
45.3M
}
_ZN5doris9BitReader8GetValueIiEEbiPT_
Line
Count
Source
116
153k
bool BitReader::GetValue(int num_bits, T* v) {
117
153k
    DCHECK_LE(num_bits, 64);
118
153k
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
153k
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
153k
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
153k
                        bit_offset_);
126
127
153k
    bit_offset_ += num_bits;
128
153k
    if (bit_offset_ >= 64) {
129
7.59k
        byte_offset_ += 8;
130
7.59k
        bit_offset_ -= 64;
131
7.59k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
7.59k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
7.59k
                                               (num_bits - bit_offset_));
135
7.59k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
153k
    return true;
138
153k
}
_ZN5doris9BitReader8GetValueIlEEbiPT_
Line
Count
Source
116
362k
bool BitReader::GetValue(int num_bits, T* v) {
117
362k
    DCHECK_LE(num_bits, 64);
118
362k
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
362k
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
362k
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
362k
                        bit_offset_);
126
127
362k
    bit_offset_ += num_bits;
128
362k
    if (bit_offset_ >= 64) {
129
199k
        byte_offset_ += 8;
130
199k
        bit_offset_ -= 64;
131
199k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
199k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
199k
                                               (num_bits - bit_offset_));
135
199k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
362k
    return true;
138
362k
}
_ZN5doris9BitReader8GetValueIcEEbiPT_
Line
Count
Source
116
44.8k
bool BitReader::GetValue(int num_bits, T* v) {
117
44.8k
    DCHECK_LE(num_bits, 64);
118
44.8k
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
44.8k
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
44.8k
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
44.8k
                        bit_offset_);
126
127
44.8k
    bit_offset_ += num_bits;
128
44.8k
    if (bit_offset_ >= 64) {
129
5.54k
        byte_offset_ += 8;
130
5.54k
        bit_offset_ -= 64;
131
5.54k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
5.54k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
5.54k
                                               (num_bits - bit_offset_));
135
5.54k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
44.8k
    return true;
138
44.8k
}
139
140
3.25M
inline void BitReader::Rewind(int num_bits) {
141
3.25M
    bit_offset_ -= num_bits;
142
3.25M
    if (bit_offset_ >= 0) {
143
3.24M
        return;
144
3.24M
    }
145
28.6k
    while (bit_offset_ < 0) {
146
14.5k
        int seek_back = std::min(byte_offset_, 8);
147
14.5k
        byte_offset_ -= seek_back;
148
14.5k
        bit_offset_ += seek_back * 8;
149
14.5k
    }
150
    // This should only be executed *if* rewinding by 'num_bits'
151
    // make the existing buffered_values_ invalid
152
14.1k
    DCHECK_GE(byte_offset_, 0); // Check for underflow
153
14.1k
    memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
154
14.1k
}
155
156
407
inline bool BitReader::Advance(int64_t num_bits) {
157
407
    int64_t bits_required = bit_offset_ + num_bits;
158
407
    int64_t bytes_required = (bits_required >> 3) + ((bits_required & 7) != 0);
159
407
    if (bytes_required > max_bytes_ - byte_offset_) {
160
0
        return false;
161
0
    }
162
407
    byte_offset_ += static_cast<int>(bits_required >> 3);
163
407
    bit_offset_ = static_cast<int>(bits_required & 7);
164
407
    BufferValues();
165
407
    return true;
166
407
}
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
10.8M
bool BitReader::GetAligned(int num_bytes, T* v) {
193
10.8M
    DCHECK_LE(num_bytes, sizeof(T));
194
10.8M
    int bytes_read = BitUtil::Ceil(bit_offset_, 8);
195
10.8M
    if (byte_offset_ + bytes_read + num_bytes > max_bytes_) [[unlikely]] {
196
2.85k
        return false;
197
2.85k
    }
198
199
    // Advance byte_offset to next unread byte and read num_bytes
200
10.8M
    byte_offset_ += bytes_read;
201
10.8M
    memcpy(v, buffer_ + byte_offset_, num_bytes);
202
10.8M
    byte_offset_ += num_bytes;
203
204
    // Reset buffered_values_
205
10.8M
    bit_offset_ = 0;
206
10.8M
    int bytes_remaining = max_bytes_ - byte_offset_;
207
10.8M
    if (bytes_remaining >= 8) [[likely]] {
208
9.08M
        memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
209
9.08M
    } else {
210
1.77M
        memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining);
211
1.77M
    }
212
10.8M
    return true;
213
10.8M
}
_ZN5doris9BitReader10GetAlignedIhEEbiPT_
Line
Count
Source
192
7.50M
bool BitReader::GetAligned(int num_bytes, T* v) {
193
7.50M
    DCHECK_LE(num_bytes, sizeof(T));
194
7.50M
    int bytes_read = BitUtil::Ceil(bit_offset_, 8);
195
7.50M
    if (byte_offset_ + bytes_read + num_bytes > max_bytes_) [[unlikely]] {
196
2.85k
        return false;
197
2.85k
    }
198
199
    // Advance byte_offset to next unread byte and read num_bytes
200
7.50M
    byte_offset_ += bytes_read;
201
7.50M
    memcpy(v, buffer_ + byte_offset_, num_bytes);
202
7.50M
    byte_offset_ += num_bytes;
203
204
    // Reset buffered_values_
205
7.50M
    bit_offset_ = 0;
206
7.50M
    int bytes_remaining = max_bytes_ - byte_offset_;
207
7.50M
    if (bytes_remaining >= 8) [[likely]] {
208
6.37M
        memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
209
6.37M
    } else {
210
1.13M
        memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining);
211
1.13M
    }
212
7.50M
    return true;
213
7.50M
}
_ZN5doris9BitReader10GetAlignedIsEEbiPT_
Line
Count
Source
192
3.11M
bool BitReader::GetAligned(int num_bytes, T* v) {
193
3.11M
    DCHECK_LE(num_bytes, sizeof(T));
194
3.11M
    int bytes_read = BitUtil::Ceil(bit_offset_, 8);
195
3.11M
    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
3.11M
    byte_offset_ += bytes_read;
201
3.11M
    memcpy(v, buffer_ + byte_offset_, num_bytes);
202
3.11M
    byte_offset_ += num_bytes;
203
204
    // Reset buffered_values_
205
3.11M
    bit_offset_ = 0;
206
3.11M
    int bytes_remaining = max_bytes_ - byte_offset_;
207
3.11M
    if (bytes_remaining >= 8) [[likely]] {
208
2.64M
        memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
209
2.64M
    } else {
210
465k
        memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining);
211
465k
    }
212
3.11M
    return true;
213
3.11M
}
_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
6.58M
inline bool BitReader::GetVlqInt(uint32_t* v) {
216
6.58M
    uint32_t tmp = 0;
217
7.43M
    for (int num_bytes = 0; num_bytes < MAX_VLQ_BYTE_LEN; num_bytes++) {
218
7.43M
        uint8_t byte = 0;
219
7.43M
        if (!GetAligned<uint8_t>(1, &byte)) return false;
220
7.43M
        tmp |= static_cast<uint32_t>(byte & 0x7F) << (7 * num_bytes);
221
7.43M
        if ((byte & 0x80) == 0) {
222
6.57M
            *v = tmp;
223
6.57M
            return true;
224
6.57M
        }
225
7.43M
    }
226
18.4E
    return false;
227
6.58M
}
228
229
1.87k
inline bool BitReader::GetZigZagVlqInt(int32_t* v) {
230
1.87k
    uint32_t u;
231
1.87k
    if (!GetVlqInt(&u)) {
232
3
        return false;
233
3
    }
234
1.87k
    u = (u >> 1) ^ (~(u & 1) + 1);
235
    // copy uint32_t to int32_t
236
1.87k
    std::memcpy(v, &u, sizeof(uint32_t));
237
1.87k
    return true;
238
1.87k
}
239
240
2.34k
inline bool BitReader::GetVlqInt(uint64_t* v) {
241
2.34k
    uint64_t tmp = 0;
242
7.86k
    for (int num_bytes = 0; num_bytes < MAX_VLQ_BYTE_LEN_FOR_INT64; num_bytes++) {
243
7.86k
        uint8_t byte = 0;
244
7.86k
        if (!GetAligned<uint8_t>(1, &byte)) return false;
245
7.86k
        tmp |= static_cast<uint64_t>(byte & 0x7F) << (7 * num_bytes);
246
7.86k
        if ((byte & 0x80) == 0) {
247
2.34k
            *v = tmp;
248
2.34k
            return true;
249
2.34k
        }
250
7.86k
    }
251
0
    return false;
252
2.34k
}
253
254
2.34k
inline bool BitReader::GetZigZagVlqInt(int64_t* v) {
255
2.34k
    uint64_t u;
256
2.34k
    if (!GetVlqInt(&u)) {
257
0
        return false;
258
0
    }
259
2.34k
    u = (u >> 1) ^ (~(u & 1) + 1);
260
2.34k
    std::memcpy(v, &u, sizeof(uint64_t));
261
2.34k
    return true;
262
2.34k
}
263
264
template <typename T>
265
5.35M
int BatchedBitReader::UnpackBatch(int bit_width, int num_values, T* v) {
266
5.35M
    DCHECK(buffer_pos_ != nullptr);
267
5.35M
    DCHECK_GE(bit_width, 0);
268
5.35M
    DCHECK_LE(bit_width, MAX_BITWIDTH);
269
5.35M
    DCHECK_LE(bit_width, sizeof(T) * 8);
270
5.35M
    DCHECK_GE(num_values, 0);
271
272
5.35M
    int64_t num_read;
273
5.35M
    std::tie(buffer_pos_, num_read) =
274
5.35M
            BitPacking::UnpackValues(bit_width, buffer_pos_, bytes_left(), num_values, v);
275
5.35M
    DCHECK_LE(buffer_pos_, buffer_end_);
276
5.35M
    DCHECK_LE(num_read, num_values);
277
5.35M
    return static_cast<int>(num_read);
278
5.35M
}
_ZN5doris16BatchedBitReader11UnpackBatchIjEEiiiPT_
Line
Count
Source
265
5.34M
int BatchedBitReader::UnpackBatch(int bit_width, int num_values, T* v) {
266
5.34M
    DCHECK(buffer_pos_ != nullptr);
267
5.34M
    DCHECK_GE(bit_width, 0);
268
5.34M
    DCHECK_LE(bit_width, MAX_BITWIDTH);
269
5.34M
    DCHECK_LE(bit_width, sizeof(T) * 8);
270
5.34M
    DCHECK_GE(num_values, 0);
271
272
5.34M
    int64_t num_read;
273
5.34M
    std::tie(buffer_pos_, num_read) =
274
5.34M
            BitPacking::UnpackValues(bit_width, buffer_pos_, bytes_left(), num_values, v);
275
5.34M
    DCHECK_LE(buffer_pos_, buffer_end_);
276
    DCHECK_LE(num_read, num_values);
277
5.34M
    return static_cast<int>(num_read);
278
5.34M
}
_ZN5doris16BatchedBitReader11UnpackBatchIhEEiiiPT_
Line
Count
Source
265
9.70k
int BatchedBitReader::UnpackBatch(int bit_width, int num_values, T* v) {
266
9.70k
    DCHECK(buffer_pos_ != nullptr);
267
9.70k
    DCHECK_GE(bit_width, 0);
268
9.70k
    DCHECK_LE(bit_width, MAX_BITWIDTH);
269
9.70k
    DCHECK_LE(bit_width, sizeof(T) * 8);
270
9.70k
    DCHECK_GE(num_values, 0);
271
272
9.70k
    int64_t num_read;
273
9.70k
    std::tie(buffer_pos_, num_read) =
274
9.70k
            BitPacking::UnpackValues(bit_width, buffer_pos_, bytes_left(), num_values, v);
275
9.70k
    DCHECK_LE(buffer_pos_, buffer_end_);
276
    DCHECK_LE(num_read, num_values);
277
9.70k
    return static_cast<int>(num_read);
278
9.70k
}
279
280
82
inline bool BatchedBitReader::SkipBatch(int bit_width, int num_values_to_skip) {
281
82
    DCHECK(buffer_pos_ != nullptr);
282
82
    DCHECK_GE(bit_width, 0);
283
82
    DCHECK_LE(bit_width, MAX_BITWIDTH);
284
82
    DCHECK_GE(num_values_to_skip, 0);
285
286
82
    int skip_bytes = BitUtil::RoundUpNumBytes(bit_width * num_values_to_skip);
287
82
    if (skip_bytes > buffer_end_ - buffer_pos_) {
288
0
        return false;
289
0
    }
290
82
    buffer_pos_ += skip_bytes;
291
82
    return true;
292
82
}
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
8.88M
bool BatchedBitReader::GetBytes(int num_bytes, T* v) {
317
8.88M
    DCHECK(buffer_pos_ != nullptr);
318
8.88M
    DCHECK_GE(num_bytes, 0);
319
8.88M
    DCHECK_LE(num_bytes, sizeof(T));
320
8.88M
    if (UNLIKELY(buffer_pos_ + num_bytes > buffer_end_)) return false;
321
8.88M
    *v = 0; // Ensure unset bytes are initialized to zero.
322
8.88M
    memcpy(v, buffer_pos_, num_bytes);
323
8.88M
    buffer_pos_ += num_bytes;
324
8.88M
    return true;
325
8.88M
}
_ZN5doris16BatchedBitReader8GetBytesIhEEbiPT_
Line
Count
Source
316
6.14M
bool BatchedBitReader::GetBytes(int num_bytes, T* v) {
317
6.14M
    DCHECK(buffer_pos_ != nullptr);
318
6.14M
    DCHECK_GE(num_bytes, 0);
319
6.14M
    DCHECK_LE(num_bytes, sizeof(T));
320
6.14M
    if (UNLIKELY(buffer_pos_ + num_bytes > buffer_end_)) return false;
321
6.14M
    *v = 0; // Ensure unset bytes are initialized to zero.
322
6.14M
    memcpy(v, buffer_pos_, num_bytes);
323
6.14M
    buffer_pos_ += num_bytes;
324
6.14M
    return true;
325
6.14M
}
_ZN5doris16BatchedBitReader8GetBytesIjEEbiPT_
Line
Count
Source
316
2.73M
bool BatchedBitReader::GetBytes(int num_bytes, T* v) {
317
2.73M
    DCHECK(buffer_pos_ != nullptr);
318
2.73M
    DCHECK_GE(num_bytes, 0);
319
2.73M
    DCHECK_LE(num_bytes, sizeof(T));
320
2.73M
    if (UNLIKELY(buffer_pos_ + num_bytes > buffer_end_)) return false;
321
2.73M
    *v = 0; // Ensure unset bytes are initialized to zero.
322
2.73M
    memcpy(v, buffer_pos_, num_bytes);
323
2.73M
    buffer_pos_ += num_bytes;
324
2.73M
    return true;
325
2.73M
}
326
327
template <typename UINT_T>
328
6.12M
bool BatchedBitReader::GetUleb128(UINT_T* v) {
329
6.12M
    static_assert(std::is_integral<UINT_T>::value, "Integral type required.");
330
6.12M
    static_assert(std::is_unsigned<UINT_T>::value, "Unsigned type required.");
331
6.12M
    static_assert(!std::is_same<UINT_T, bool>::value, "Bools are not supported.");
332
333
6.12M
    *v = 0;
334
6.12M
    int shift = 0;
335
6.12M
    uint8_t byte = 0;
336
6.15M
    do {
337
6.15M
        if (UNLIKELY(shift >= max_vlq_byte_len<UINT_T>() * 7)) return false;
338
6.15M
        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
6.15M
        const UINT_T byte_as_UINT_T = byte;
344
6.15M
        *v |= (byte_as_UINT_T & 0x7Fu) << shift;
345
6.15M
        shift += 7;
346
6.15M
    } while ((byte & 0x80u) != 0);
347
6.12M
    return true;
348
6.12M
}
349
350
} // namespace doris