Coverage Report

Created: 2026-05-20 07:32

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
13.9M
inline void BitWriter::PutValue(uint64_t v, int num_bits) {
35
13.9M
    DCHECK_LE(num_bits, 64);
36
    // Truncate the higher-order bits. This is necessary to
37
    // support signed values.
38
13.9M
    v &= ~0ULL >> (64 - num_bits);
39
40
13.9M
    buffered_values_ |= v << bit_offset_;
41
13.9M
    bit_offset_ += num_bits;
42
43
13.9M
    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
13.9M
    DCHECK_LT(bit_offset_, 64);
55
13.9M
}
56
57
69.2M
inline void BitWriter::Flush(bool align) {
58
69.2M
    int num_bytes = BitUtil::Ceil(bit_offset_, 8);
59
69.2M
    buffer_->reserve(ALIGN_UP(byte_offset_ + num_bytes, 8));
60
69.2M
    buffer_->resize(byte_offset_ + num_bytes);
61
69.2M
    DCHECK_LE(byte_offset_ + num_bytes, buffer_->capacity());
62
69.2M
    memcpy(buffer_->data() + byte_offset_, &buffered_values_, num_bytes);
63
64
69.2M
    if (align) {
65
58.1M
        buffered_values_ = 0;
66
58.1M
        byte_offset_ += num_bytes;
67
58.1M
        bit_offset_ = 0;
68
58.1M
    }
69
69.2M
}
70
71
58.1M
inline uint8_t* BitWriter::GetNextBytePtr(int num_bytes) {
72
58.1M
    Flush(/* align */ true);
73
58.1M
    buffer_->reserve(ALIGN_UP(byte_offset_ + num_bytes, 8));
74
58.1M
    buffer_->resize(byte_offset_ + num_bytes);
75
58.1M
    uint8_t* ptr = buffer_->data() + byte_offset_;
76
58.1M
    byte_offset_ += num_bytes;
77
58.1M
    DCHECK_LE(byte_offset_, buffer_->capacity());
78
58.1M
    return ptr;
79
58.1M
}
80
81
template <typename T>
82
57.5M
void BitWriter::PutAligned(T val, int num_bytes) {
83
57.5M
    DCHECK_LE(num_bytes, sizeof(T));
84
57.5M
    uint8_t* ptr = GetNextBytePtr(num_bytes);
85
57.5M
    memcpy(ptr, &val, num_bytes);
86
57.5M
}
_ZN5doris9BitWriter10PutAlignedIhEEvT_i
Line
Count
Source
82
37.8M
void BitWriter::PutAligned(T val, int num_bytes) {
83
    DCHECK_LE(num_bytes, sizeof(T));
84
37.8M
    uint8_t* ptr = GetNextBytePtr(num_bytes);
85
37.8M
    memcpy(ptr, &val, num_bytes);
86
37.8M
}
_ZN5doris9BitWriter10PutAlignedImEEvT_i
Line
Count
Source
82
19.7M
void BitWriter::PutAligned(T val, int num_bytes) {
83
    DCHECK_LE(num_bytes, sizeof(T));
84
19.7M
    uint8_t* ptr = GetNextBytePtr(num_bytes);
85
19.7M
    memcpy(ptr, &val, num_bytes);
86
19.7M
}
87
88
19.7M
inline void BitWriter::PutVlqInt(int32_t v) {
89
37.8M
    while ((v & 0xFFFFFF80) != 0L) {
90
18.0M
        PutAligned<uint8_t>((v & 0x7F) | 0x80, 1);
91
18.0M
        v >>= 7;
92
18.0M
    }
93
19.7M
    PutAligned<uint8_t>(v & 0x7F, 1);
94
19.7M
}
95
96
inline BitReader::BitReader(const uint8_t* buffer, int buffer_len)
97
532k
        : buffer_(buffer),
98
532k
          max_bytes_(buffer_len),
99
532k
          buffered_values_(0),
100
532k
          byte_offset_(0),
101
532k
          bit_offset_(0) {
102
532k
    int num_bytes = std::min(8, max_bytes_);
103
532k
    memcpy(&buffered_values_, buffer_ + byte_offset_, num_bytes);
104
532k
}
105
106
1.48M
inline void BitReader::BufferValues() {
107
1.48M
    int bytes_remaining = max_bytes_ - byte_offset_;
108
1.48M
    if (bytes_remaining >= 8) [[likely]] {
109
1.48M
        memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
110
1.48M
    } else {
111
2.40k
        memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining);
112
2.40k
    }
113
1.48M
}
114
115
template <typename T>
116
65.0M
bool BitReader::GetValue(int num_bits, T* v) {
117
65.0M
    DCHECK_LE(num_bits, 64);
118
65.0M
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
65.0M
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
65.0M
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
65.0M
                        bit_offset_);
126
127
65.0M
    bit_offset_ += num_bits;
128
65.0M
    if (bit_offset_ >= 64) {
129
1.48M
        byte_offset_ += 8;
130
1.48M
        bit_offset_ -= 64;
131
1.48M
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
1.48M
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
1.48M
                                               (num_bits - bit_offset_));
135
1.48M
    }
136
65.0M
    DCHECK_LE(bit_offset_, 64);
137
65.0M
    return true;
138
65.0M
}
_ZN5doris9BitReader8GetValueIsEEbiPT_
Line
Count
Source
116
38.8M
bool BitReader::GetValue(int num_bits, T* v) {
117
38.8M
    DCHECK_LE(num_bits, 64);
118
38.8M
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
38.8M
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
38.8M
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
38.8M
                        bit_offset_);
126
127
38.8M
    bit_offset_ += num_bits;
128
38.8M
    if (bit_offset_ >= 64) {
129
966k
        byte_offset_ += 8;
130
966k
        bit_offset_ -= 64;
131
966k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
966k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
966k
                                               (num_bits - bit_offset_));
135
966k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
38.8M
    return true;
138
38.8M
}
_ZN5doris9BitReader8GetValueImEEbiPT_
Line
Count
Source
116
5.91M
bool BitReader::GetValue(int num_bits, T* v) {
117
5.91M
    DCHECK_LE(num_bits, 64);
118
5.91M
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
5.91M
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
5.91M
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
5.91M
                        bit_offset_);
126
127
5.91M
    bit_offset_ += num_bits;
128
5.91M
    if (bit_offset_ >= 64) {
129
62.2k
        byte_offset_ += 8;
130
62.2k
        bit_offset_ -= 64;
131
62.2k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
62.2k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
62.2k
                                               (num_bits - bit_offset_));
135
62.2k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
5.91M
    return true;
138
5.91M
}
_ZN5doris9BitReader8GetValueIbEEbiPT_
Line
Count
Source
116
994k
bool BitReader::GetValue(int num_bits, T* v) {
117
994k
    DCHECK_LE(num_bits, 64);
118
994k
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
994k
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
994k
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
994k
                        bit_offset_);
126
127
994k
    bit_offset_ += num_bits;
128
994k
    if (bit_offset_ >= 64) {
129
6.42k
        byte_offset_ += 8;
130
6.42k
        bit_offset_ -= 64;
131
6.42k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
6.42k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
6.42k
                                               (num_bits - bit_offset_));
135
6.42k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
994k
    return true;
138
994k
}
_ZN5doris9BitReader8GetValueIhEEbiPT_
Line
Count
Source
116
18.8M
bool BitReader::GetValue(int num_bits, T* v) {
117
18.8M
    DCHECK_LE(num_bits, 64);
118
18.8M
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
18.8M
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
18.8M
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
18.8M
                        bit_offset_);
126
127
18.8M
    bit_offset_ += num_bits;
128
18.8M
    if (bit_offset_ >= 64) {
129
262k
        byte_offset_ += 8;
130
262k
        bit_offset_ -= 64;
131
262k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
262k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
262k
                                               (num_bits - bit_offset_));
135
262k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
18.8M
    return true;
138
18.8M
}
_ZN5doris9BitReader8GetValueIiEEbiPT_
Line
Count
Source
116
77.0k
bool BitReader::GetValue(int num_bits, T* v) {
117
77.0k
    DCHECK_LE(num_bits, 64);
118
77.0k
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
77.0k
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
77.0k
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
77.0k
                        bit_offset_);
126
127
77.0k
    bit_offset_ += num_bits;
128
77.0k
    if (bit_offset_ >= 64) {
129
3.83k
        byte_offset_ += 8;
130
3.83k
        bit_offset_ -= 64;
131
3.83k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
3.83k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
3.83k
                                               (num_bits - bit_offset_));
135
3.83k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
77.0k
    return true;
138
77.0k
}
_ZN5doris9BitReader8GetValueIlEEbiPT_
Line
Count
Source
116
324k
bool BitReader::GetValue(int num_bits, T* v) {
117
324k
    DCHECK_LE(num_bits, 64);
118
324k
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
324k
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
324k
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
324k
                        bit_offset_);
126
127
324k
    bit_offset_ += num_bits;
128
324k
    if (bit_offset_ >= 64) {
129
181k
        byte_offset_ += 8;
130
181k
        bit_offset_ -= 64;
131
181k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
181k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
181k
                                               (num_bits - bit_offset_));
135
181k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
324k
    return true;
138
324k
}
_ZN5doris9BitReader8GetValueIcEEbiPT_
Line
Count
Source
116
22.5k
bool BitReader::GetValue(int num_bits, T* v) {
117
22.5k
    DCHECK_LE(num_bits, 64);
118
22.5k
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
22.5k
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
22.5k
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
22.5k
                        bit_offset_);
126
127
22.5k
    bit_offset_ += num_bits;
128
22.5k
    if (bit_offset_ >= 64) {
129
2.78k
        byte_offset_ += 8;
130
2.78k
        bit_offset_ -= 64;
131
2.78k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
2.78k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
2.78k
                                               (num_bits - bit_offset_));
135
2.78k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
22.5k
    return true;
138
22.5k
}
139
140
2.07M
inline void BitReader::Rewind(int num_bits) {
141
2.07M
    bit_offset_ -= num_bits;
142
2.07M
    if (bit_offset_ >= 0) {
143
2.06M
        return;
144
2.06M
    }
145
21.8k
    while (bit_offset_ < 0) {
146
10.9k
        int seek_back = std::min(byte_offset_, 8);
147
10.9k
        byte_offset_ -= seek_back;
148
10.9k
        bit_offset_ += seek_back * 8;
149
10.9k
    }
150
    // This should only be executed *if* rewinding by 'num_bits'
151
    // make the existing buffered_values_ invalid
152
10.9k
    DCHECK_GE(byte_offset_, 0); // Check for underflow
153
10.9k
    memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
154
10.9k
}
155
156
214
inline bool BitReader::Advance(int64_t num_bits) {
157
214
    int64_t bits_required = bit_offset_ + num_bits;
158
214
    int64_t bytes_required = (bits_required >> 3) + ((bits_required & 7) != 0);
159
214
    if (bytes_required > max_bytes_ - byte_offset_) {
160
0
        return false;
161
0
    }
162
214
    byte_offset_ += static_cast<int>(bits_required >> 3);
163
214
    bit_offset_ = static_cast<int>(bits_required & 7);
164
214
    BufferValues();
165
214
    return true;
166
214
}
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
5.89M
bool BitReader::GetAligned(int num_bytes, T* v) {
193
5.89M
    DCHECK_LE(num_bytes, sizeof(T));
194
5.89M
    int bytes_read = BitUtil::Ceil(bit_offset_, 8);
195
5.89M
    if (byte_offset_ + bytes_read + num_bytes > max_bytes_) [[unlikely]] {
196
2.91k
        return false;
197
2.91k
    }
198
199
    // Advance byte_offset to next unread byte and read num_bytes
200
5.88M
    byte_offset_ += bytes_read;
201
5.88M
    memcpy(v, buffer_ + byte_offset_, num_bytes);
202
5.88M
    byte_offset_ += num_bytes;
203
204
    // Reset buffered_values_
205
5.88M
    bit_offset_ = 0;
206
5.88M
    int bytes_remaining = max_bytes_ - byte_offset_;
207
5.88M
    if (bytes_remaining >= 8) [[likely]] {
208
4.75M
        memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
209
4.75M
    } else {
210
1.13M
        memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining);
211
1.13M
    }
212
5.88M
    return true;
213
5.89M
}
_ZN5doris9BitReader10GetAlignedIhEEbiPT_
Line
Count
Source
192
4.07M
bool BitReader::GetAligned(int num_bytes, T* v) {
193
4.07M
    DCHECK_LE(num_bytes, sizeof(T));
194
4.07M
    int bytes_read = BitUtil::Ceil(bit_offset_, 8);
195
4.07M
    if (byte_offset_ + bytes_read + num_bytes > max_bytes_) [[unlikely]] {
196
2.91k
        return false;
197
2.91k
    }
198
199
    // Advance byte_offset to next unread byte and read num_bytes
200
4.07M
    byte_offset_ += bytes_read;
201
4.07M
    memcpy(v, buffer_ + byte_offset_, num_bytes);
202
4.07M
    byte_offset_ += num_bytes;
203
204
    // Reset buffered_values_
205
4.07M
    bit_offset_ = 0;
206
4.07M
    int bytes_remaining = max_bytes_ - byte_offset_;
207
4.07M
    if (bytes_remaining >= 8) [[likely]] {
208
3.33M
        memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
209
3.33M
    } else {
210
731k
        memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining);
211
731k
    }
212
4.07M
    return true;
213
4.07M
}
_ZN5doris9BitReader10GetAlignedIsEEbiPT_
Line
Count
Source
192
1.60M
bool BitReader::GetAligned(int num_bytes, T* v) {
193
1.60M
    DCHECK_LE(num_bytes, sizeof(T));
194
1.60M
    int bytes_read = BitUtil::Ceil(bit_offset_, 8);
195
1.60M
    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
1.60M
    byte_offset_ += bytes_read;
201
1.60M
    memcpy(v, buffer_ + byte_offset_, num_bytes);
202
1.60M
    byte_offset_ += num_bytes;
203
204
    // Reset buffered_values_
205
1.60M
    bit_offset_ = 0;
206
1.60M
    int bytes_remaining = max_bytes_ - byte_offset_;
207
1.60M
    if (bytes_remaining >= 8) [[likely]] {
208
1.36M
        memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
209
1.36M
    } else {
210
235k
        memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining);
211
235k
    }
212
1.60M
    return true;
213
1.60M
}
_ZN5doris9BitReader10GetAlignedIbEEbiPT_
Line
Count
Source
192
216k
bool BitReader::GetAligned(int num_bytes, T* v) {
193
216k
    DCHECK_LE(num_bytes, sizeof(T));
194
216k
    int bytes_read = BitUtil::Ceil(bit_offset_, 8);
195
216k
    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
216k
    byte_offset_ += bytes_read;
201
216k
    memcpy(v, buffer_ + byte_offset_, num_bytes);
202
216k
    byte_offset_ += num_bytes;
203
204
    // Reset buffered_values_
205
216k
    bit_offset_ = 0;
206
216k
    int bytes_remaining = max_bytes_ - byte_offset_;
207
216k
    if (bytes_remaining >= 8) [[likely]] {
208
53.1k
        memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
209
163k
    } else {
210
163k
        memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining);
211
163k
    }
212
216k
    return true;
213
216k
}
214
215
3.53M
inline bool BitReader::GetVlqInt(uint32_t* v) {
216
3.53M
    uint32_t tmp = 0;
217
4.03M
    for (int num_bytes = 0; num_bytes < MAX_VLQ_BYTE_LEN; num_bytes++) {
218
4.03M
        uint8_t byte = 0;
219
4.03M
        if (!GetAligned<uint8_t>(1, &byte)) return false;
220
4.03M
        tmp |= static_cast<uint32_t>(byte & 0x7F) << (7 * num_bytes);
221
4.03M
        if ((byte & 0x80) == 0) {
222
3.52M
            *v = tmp;
223
3.52M
            return true;
224
3.52M
        }
225
4.03M
    }
226
18.4E
    return false;
227
3.53M
}
228
229
961
inline bool BitReader::GetZigZagVlqInt(int32_t* v) {
230
961
    uint32_t u;
231
961
    if (!GetVlqInt(&u)) {
232
3
        return false;
233
3
    }
234
958
    u = (u >> 1) ^ (~(u & 1) + 1);
235
    // copy uint32_t to int32_t
236
958
    std::memcpy(v, &u, sizeof(uint32_t));
237
958
    return true;
238
961
}
239
240
1.17k
inline bool BitReader::GetVlqInt(uint64_t* v) {
241
1.17k
    uint64_t tmp = 0;
242
3.93k
    for (int num_bytes = 0; num_bytes < MAX_VLQ_BYTE_LEN_FOR_INT64; num_bytes++) {
243
3.93k
        uint8_t byte = 0;
244
3.93k
        if (!GetAligned<uint8_t>(1, &byte)) return false;
245
3.93k
        tmp |= static_cast<uint64_t>(byte & 0x7F) << (7 * num_bytes);
246
3.93k
        if ((byte & 0x80) == 0) {
247
1.17k
            *v = tmp;
248
1.17k
            return true;
249
1.17k
        }
250
3.93k
    }
251
0
    return false;
252
1.17k
}
253
254
1.17k
inline bool BitReader::GetZigZagVlqInt(int64_t* v) {
255
1.17k
    uint64_t u;
256
1.17k
    if (!GetVlqInt(&u)) {
257
0
        return false;
258
0
    }
259
1.17k
    u = (u >> 1) ^ (~(u & 1) + 1);
260
1.17k
    std::memcpy(v, &u, sizeof(uint64_t));
261
1.17k
    return true;
262
1.17k
}
263
264
template <typename T>
265
3.07M
int BatchedBitReader::UnpackBatch(int bit_width, int num_values, T* v) {
266
3.07M
    DCHECK(buffer_pos_ != nullptr);
267
3.07M
    DCHECK_GE(bit_width, 0);
268
3.07M
    DCHECK_LE(bit_width, MAX_BITWIDTH);
269
3.07M
    DCHECK_LE(bit_width, sizeof(T) * 8);
270
3.07M
    DCHECK_GE(num_values, 0);
271
272
3.07M
    int64_t num_read;
273
3.07M
    std::tie(buffer_pos_, num_read) =
274
3.07M
            BitPacking::UnpackValues(bit_width, buffer_pos_, bytes_left(), num_values, v);
275
3.07M
    DCHECK_LE(buffer_pos_, buffer_end_);
276
3.07M
    DCHECK_LE(num_read, num_values);
277
3.07M
    return static_cast<int>(num_read);
278
3.07M
}
_ZN5doris16BatchedBitReader11UnpackBatchIjEEiiiPT_
Line
Count
Source
265
3.06M
int BatchedBitReader::UnpackBatch(int bit_width, int num_values, T* v) {
266
3.06M
    DCHECK(buffer_pos_ != nullptr);
267
3.06M
    DCHECK_GE(bit_width, 0);
268
3.06M
    DCHECK_LE(bit_width, MAX_BITWIDTH);
269
3.06M
    DCHECK_LE(bit_width, sizeof(T) * 8);
270
3.06M
    DCHECK_GE(num_values, 0);
271
272
3.06M
    int64_t num_read;
273
3.06M
    std::tie(buffer_pos_, num_read) =
274
3.06M
            BitPacking::UnpackValues(bit_width, buffer_pos_, bytes_left(), num_values, v);
275
3.06M
    DCHECK_LE(buffer_pos_, buffer_end_);
276
    DCHECK_LE(num_read, num_values);
277
3.06M
    return static_cast<int>(num_read);
278
3.06M
}
_ZN5doris16BatchedBitReader11UnpackBatchIhEEiiiPT_
Line
Count
Source
265
4.97k
int BatchedBitReader::UnpackBatch(int bit_width, int num_values, T* v) {
266
4.97k
    DCHECK(buffer_pos_ != nullptr);
267
4.97k
    DCHECK_GE(bit_width, 0);
268
4.97k
    DCHECK_LE(bit_width, MAX_BITWIDTH);
269
4.97k
    DCHECK_LE(bit_width, sizeof(T) * 8);
270
4.97k
    DCHECK_GE(num_values, 0);
271
272
4.97k
    int64_t num_read;
273
4.97k
    std::tie(buffer_pos_, num_read) =
274
4.97k
            BitPacking::UnpackValues(bit_width, buffer_pos_, bytes_left(), num_values, v);
275
4.97k
    DCHECK_LE(buffer_pos_, buffer_end_);
276
    DCHECK_LE(num_read, num_values);
277
4.97k
    return static_cast<int>(num_read);
278
4.97k
}
279
280
43
inline bool BatchedBitReader::SkipBatch(int bit_width, int num_values_to_skip) {
281
43
    DCHECK(buffer_pos_ != nullptr);
282
43
    DCHECK_GE(bit_width, 0);
283
43
    DCHECK_LE(bit_width, MAX_BITWIDTH);
284
43
    DCHECK_GE(num_values_to_skip, 0);
285
286
43
    int skip_bytes = BitUtil::RoundUpNumBytes(bit_width * num_values_to_skip);
287
43
    if (skip_bytes > buffer_end_ - buffer_pos_) {
288
0
        return false;
289
0
    }
290
43
    buffer_pos_ += skip_bytes;
291
43
    return true;
292
43
}
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
5.30M
bool BatchedBitReader::GetBytes(int num_bytes, T* v) {
317
5.30M
    DCHECK(buffer_pos_ != nullptr);
318
5.30M
    DCHECK_GE(num_bytes, 0);
319
5.30M
    DCHECK_LE(num_bytes, sizeof(T));
320
5.30M
    if (UNLIKELY(buffer_pos_ + num_bytes > buffer_end_)) return false;
321
5.30M
    *v = 0; // Ensure unset bytes are initialized to zero.
322
5.30M
    memcpy(v, buffer_pos_, num_bytes);
323
5.30M
    buffer_pos_ += num_bytes;
324
5.30M
    return true;
325
5.30M
}
_ZN5doris16BatchedBitReader8GetBytesIhEEbiPT_
Line
Count
Source
316
3.63M
bool BatchedBitReader::GetBytes(int num_bytes, T* v) {
317
3.63M
    DCHECK(buffer_pos_ != nullptr);
318
3.63M
    DCHECK_GE(num_bytes, 0);
319
3.63M
    DCHECK_LE(num_bytes, sizeof(T));
320
3.63M
    if (UNLIKELY(buffer_pos_ + num_bytes > buffer_end_)) return false;
321
3.63M
    *v = 0; // Ensure unset bytes are initialized to zero.
322
3.63M
    memcpy(v, buffer_pos_, num_bytes);
323
3.63M
    buffer_pos_ += num_bytes;
324
3.63M
    return true;
325
3.63M
}
_ZN5doris16BatchedBitReader8GetBytesIjEEbiPT_
Line
Count
Source
316
1.66M
bool BatchedBitReader::GetBytes(int num_bytes, T* v) {
317
1.66M
    DCHECK(buffer_pos_ != nullptr);
318
1.66M
    DCHECK_GE(num_bytes, 0);
319
1.66M
    DCHECK_LE(num_bytes, sizeof(T));
320
1.66M
    if (UNLIKELY(buffer_pos_ + num_bytes > buffer_end_)) return false;
321
1.66M
    *v = 0; // Ensure unset bytes are initialized to zero.
322
1.66M
    memcpy(v, buffer_pos_, num_bytes);
323
1.66M
    buffer_pos_ += num_bytes;
324
1.66M
    return true;
325
1.66M
}
326
327
template <typename UINT_T>
328
3.62M
bool BatchedBitReader::GetUleb128(UINT_T* v) {
329
3.62M
    static_assert(std::is_integral<UINT_T>::value, "Integral type required.");
330
3.62M
    static_assert(std::is_unsigned<UINT_T>::value, "Unsigned type required.");
331
3.62M
    static_assert(!std::is_same<UINT_T, bool>::value, "Bools are not supported.");
332
333
3.62M
    *v = 0;
334
3.62M
    int shift = 0;
335
3.62M
    uint8_t byte = 0;
336
3.64M
    do {
337
3.64M
        if (UNLIKELY(shift >= max_vlq_byte_len<UINT_T>() * 7)) return false;
338
3.64M
        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
3.64M
        const UINT_T byte_as_UINT_T = byte;
344
3.64M
        *v |= (byte_as_UINT_T & 0x7Fu) << shift;
345
3.64M
        shift += 7;
346
3.64M
    } while ((byte & 0x80u) != 0);
347
3.62M
    return true;
348
3.62M
}
349
350
} // namespace doris