Coverage Report

Created: 2026-06-05 06:55

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.8M
inline void BitWriter::PutValue(uint64_t v, int num_bits) {
35
22.8M
    DCHECK_LE(num_bits, 64);
36
    // Truncate the higher-order bits. This is necessary to
37
    // support signed values.
38
22.8M
    v &= ~0ULL >> (64 - num_bits);
39
40
22.8M
    buffered_values_ |= v << bit_offset_;
41
22.8M
    bit_offset_ += num_bits;
42
43
22.8M
    if (bit_offset_ >= 64) [[unlikely]] {
44
        // Flush buffered_values_ and write out bits of v that did not fit
45
291k
        buffer_->reserve(ALIGN_UP(byte_offset_ + 8, 8));
46
291k
        buffer_->resize(byte_offset_ + 8);
47
291k
        DCHECK_LE(byte_offset_ + 8, buffer_->capacity());
48
291k
        memcpy(buffer_->data() + byte_offset_, &buffered_values_, 8);
49
291k
        buffered_values_ = 0;
50
291k
        byte_offset_ += 8;
51
291k
        bit_offset_ -= 64;
52
291k
        buffered_values_ = BitUtil::ShiftRightZeroOnOverflow(v, (num_bits - bit_offset_));
53
291k
    }
54
22.8M
    DCHECK_LT(bit_offset_, 64);
55
22.8M
}
56
57
71.4M
inline void BitWriter::Flush(bool align) {
58
71.4M
    int num_bytes = BitUtil::Ceil(bit_offset_, 8);
59
71.4M
    buffer_->reserve(ALIGN_UP(byte_offset_ + num_bytes, 8));
60
71.4M
    buffer_->resize(byte_offset_ + num_bytes);
61
71.4M
    DCHECK_LE(byte_offset_ + num_bytes, buffer_->capacity());
62
71.4M
    memcpy(buffer_->data() + byte_offset_, &buffered_values_, num_bytes);
63
64
71.4M
    if (align) {
65
60.3M
        buffered_values_ = 0;
66
60.3M
        byte_offset_ += num_bytes;
67
60.3M
        bit_offset_ = 0;
68
60.3M
    }
69
71.4M
}
70
71
60.2M
inline uint8_t* BitWriter::GetNextBytePtr(int num_bytes) {
72
60.2M
    Flush(/* align */ true);
73
60.2M
    buffer_->reserve(ALIGN_UP(byte_offset_ + num_bytes, 8));
74
60.2M
    buffer_->resize(byte_offset_ + num_bytes);
75
60.2M
    uint8_t* ptr = buffer_->data() + byte_offset_;
76
60.2M
    byte_offset_ += num_bytes;
77
60.2M
    DCHECK_LE(byte_offset_, buffer_->capacity());
78
60.2M
    return ptr;
79
60.2M
}
80
81
template <typename T>
82
58.9M
void BitWriter::PutAligned(T val, int num_bytes) {
83
58.9M
    DCHECK_LE(num_bytes, sizeof(T));
84
58.9M
    uint8_t* ptr = GetNextBytePtr(num_bytes);
85
58.9M
    memcpy(ptr, &val, num_bytes);
86
58.9M
}
_ZN5doris9BitWriter10PutAlignedIhEEvT_i
Line
Count
Source
82
38.5M
void BitWriter::PutAligned(T val, int num_bytes) {
83
    DCHECK_LE(num_bytes, sizeof(T));
84
38.5M
    uint8_t* ptr = GetNextBytePtr(num_bytes);
85
38.5M
    memcpy(ptr, &val, num_bytes);
86
38.5M
}
_ZN5doris9BitWriter10PutAlignedImEEvT_i
Line
Count
Source
82
20.4M
void BitWriter::PutAligned(T val, int num_bytes) {
83
    DCHECK_LE(num_bytes, sizeof(T));
84
20.4M
    uint8_t* ptr = GetNextBytePtr(num_bytes);
85
20.4M
    memcpy(ptr, &val, num_bytes);
86
20.4M
}
87
88
20.4M
inline void BitWriter::PutVlqInt(int32_t v) {
89
38.5M
    while ((v & 0xFFFFFF80) != 0L) {
90
18.1M
        PutAligned<uint8_t>((v & 0x7F) | 0x80, 1);
91
18.1M
        v >>= 7;
92
18.1M
    }
93
20.4M
    PutAligned<uint8_t>(v & 0x7F, 1);
94
20.4M
}
95
96
inline BitReader::BitReader(const uint8_t* buffer, int buffer_len)
97
249k
        : buffer_(buffer),
98
249k
          max_bytes_(buffer_len),
99
249k
          buffered_values_(0),
100
249k
          byte_offset_(0),
101
249k
          bit_offset_(0) {
102
249k
    int num_bytes = std::min(8, max_bytes_);
103
249k
    memcpy(&buffered_values_, buffer_ + byte_offset_, num_bytes);
104
249k
}
105
106
564k
inline void BitReader::BufferValues() {
107
564k
    int bytes_remaining = max_bytes_ - byte_offset_;
108
564k
    if (bytes_remaining >= 8) [[likely]] {
109
561k
        memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
110
561k
    } else {
111
2.35k
        memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining);
112
2.35k
    }
113
564k
}
114
115
template <typename T>
116
35.9M
bool BitReader::GetValue(int num_bits, T* v) {
117
35.9M
    DCHECK_LE(num_bits, 64);
118
35.9M
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
35.9M
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
35.9M
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
35.9M
                        bit_offset_);
126
127
35.9M
    bit_offset_ += num_bits;
128
35.9M
    if (bit_offset_ >= 64) {
129
564k
        byte_offset_ += 8;
130
564k
        bit_offset_ -= 64;
131
564k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
564k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
564k
                                               (num_bits - bit_offset_));
135
564k
    }
136
35.9M
    DCHECK_LE(bit_offset_, 64);
137
35.9M
    return true;
138
35.9M
}
_ZN5doris9BitReader8GetValueIsEEbiPT_
Line
Count
Source
116
839k
bool BitReader::GetValue(int num_bits, T* v) {
117
839k
    DCHECK_LE(num_bits, 64);
118
839k
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
839k
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
839k
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
839k
                        bit_offset_);
126
127
839k
    bit_offset_ += num_bits;
128
839k
    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
839k
    return true;
138
839k
}
_ZN5doris9BitReader8GetValueImEEbiPT_
Line
Count
Source
116
6.41M
bool BitReader::GetValue(int num_bits, T* v) {
117
6.41M
    DCHECK_LE(num_bits, 64);
118
6.41M
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
6.41M
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
6.41M
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
6.41M
                        bit_offset_);
126
127
6.41M
    bit_offset_ += num_bits;
128
6.41M
    if (bit_offset_ >= 64) {
129
59.0k
        byte_offset_ += 8;
130
59.0k
        bit_offset_ -= 64;
131
59.0k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
59.0k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
59.0k
                                               (num_bits - bit_offset_));
135
59.0k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
6.41M
    return true;
138
6.41M
}
_ZN5doris9BitReader8GetValueIbEEbiPT_
Line
Count
Source
116
6.25M
bool BitReader::GetValue(int num_bits, T* v) {
117
6.25M
    DCHECK_LE(num_bits, 64);
118
6.25M
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
6.25M
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
6.25M
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
6.25M
                        bit_offset_);
126
127
6.25M
    bit_offset_ += num_bits;
128
6.25M
    if (bit_offset_ >= 64) {
129
15.9k
        byte_offset_ += 8;
130
15.9k
        bit_offset_ -= 64;
131
15.9k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
15.9k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
15.9k
                                               (num_bits - bit_offset_));
135
15.9k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
6.25M
    return true;
138
6.25M
}
_ZN5doris9BitReader8GetValueIhEEbiPT_
Line
Count
Source
116
22.1M
bool BitReader::GetValue(int num_bits, T* v) {
117
22.1M
    DCHECK_LE(num_bits, 64);
118
22.1M
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
22.1M
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
22.1M
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
22.1M
                        bit_offset_);
126
127
22.1M
    bit_offset_ += num_bits;
128
22.1M
    if (bit_offset_ >= 64) {
129
321k
        byte_offset_ += 8;
130
321k
        bit_offset_ -= 64;
131
321k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
321k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
321k
                                               (num_bits - bit_offset_));
135
321k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
22.1M
    return true;
138
22.1M
}
_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
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
}
_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
2.44M
inline void BitReader::Rewind(int num_bits) {
141
2.44M
    bit_offset_ -= num_bits;
142
2.44M
    if (bit_offset_ >= 0) {
143
2.43M
        return;
144
2.43M
    }
145
19.0k
    while (bit_offset_ < 0) {
146
9.68k
        int seek_back = std::min(byte_offset_, 8);
147
9.68k
        byte_offset_ -= seek_back;
148
9.68k
        bit_offset_ += seek_back * 8;
149
9.68k
    }
150
    // This should only be executed *if* rewinding by 'num_bits'
151
    // make the existing buffered_values_ invalid
152
9.41k
    DCHECK_GE(byte_offset_, 0); // Check for underflow
153
9.41k
    memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
154
9.41k
}
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
3.03M
bool BitReader::GetAligned(int num_bytes, T* v) {
193
3.03M
    DCHECK_LE(num_bytes, sizeof(T));
194
3.03M
    int bytes_read = BitUtil::Ceil(bit_offset_, 8);
195
3.03M
    if (byte_offset_ + bytes_read + num_bytes > max_bytes_) [[unlikely]] {
196
11.9k
        return false;
197
11.9k
    }
198
199
    // Advance byte_offset to next unread byte and read num_bytes
200
3.02M
    byte_offset_ += bytes_read;
201
3.02M
    memcpy(v, buffer_ + byte_offset_, num_bytes);
202
3.02M
    byte_offset_ += num_bytes;
203
204
    // Reset buffered_values_
205
3.02M
    bit_offset_ = 0;
206
3.02M
    int bytes_remaining = max_bytes_ - byte_offset_;
207
3.02M
    if (bytes_remaining >= 8) [[likely]] {
208
2.56M
        memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
209
2.56M
    } else {
210
462k
        memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining);
211
462k
    }
212
3.02M
    return true;
213
3.03M
}
_ZN5doris9BitReader10GetAlignedIhEEbiPT_
Line
Count
Source
192
2.16M
bool BitReader::GetAligned(int num_bytes, T* v) {
193
2.16M
    DCHECK_LE(num_bytes, sizeof(T));
194
2.16M
    int bytes_read = BitUtil::Ceil(bit_offset_, 8);
195
2.16M
    if (byte_offset_ + bytes_read + num_bytes > max_bytes_) [[unlikely]] {
196
11.9k
        return false;
197
11.9k
    }
198
199
    // Advance byte_offset to next unread byte and read num_bytes
200
2.15M
    byte_offset_ += bytes_read;
201
2.15M
    memcpy(v, buffer_ + byte_offset_, num_bytes);
202
2.15M
    byte_offset_ += num_bytes;
203
204
    // Reset buffered_values_
205
2.15M
    bit_offset_ = 0;
206
2.15M
    int bytes_remaining = max_bytes_ - byte_offset_;
207
2.15M
    if (bytes_remaining >= 8) [[likely]] {
208
1.84M
        memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
209
1.84M
    } else {
210
310k
        memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining);
211
310k
    }
212
2.15M
    return true;
213
2.16M
}
_ZN5doris9BitReader10GetAlignedIsEEbiPT_
Line
Count
Source
192
88.4k
bool BitReader::GetAligned(int num_bytes, T* v) {
193
88.4k
    DCHECK_LE(num_bytes, sizeof(T));
194
88.4k
    int bytes_read = BitUtil::Ceil(bit_offset_, 8);
195
88.4k
    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
88.4k
    byte_offset_ += bytes_read;
201
88.4k
    memcpy(v, buffer_ + byte_offset_, num_bytes);
202
88.4k
    byte_offset_ += num_bytes;
203
204
    // Reset buffered_values_
205
88.4k
    bit_offset_ = 0;
206
88.4k
    int bytes_remaining = max_bytes_ - byte_offset_;
207
88.4k
    if (bytes_remaining >= 8) [[likely]] {
208
86.7k
        memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
209
86.7k
    } else {
210
1.73k
        memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining);
211
1.73k
    }
212
88.4k
    return true;
213
88.4k
}
_ZN5doris9BitReader10GetAlignedIbEEbiPT_
Line
Count
Source
192
783k
bool BitReader::GetAligned(int num_bytes, T* v) {
193
783k
    DCHECK_LE(num_bytes, sizeof(T));
194
783k
    int bytes_read = BitUtil::Ceil(bit_offset_, 8);
195
783k
    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
783k
    byte_offset_ += bytes_read;
201
783k
    memcpy(v, buffer_ + byte_offset_, num_bytes);
202
783k
    byte_offset_ += num_bytes;
203
204
    // Reset buffered_values_
205
783k
    bit_offset_ = 0;
206
783k
    int bytes_remaining = max_bytes_ - byte_offset_;
207
783k
    if (bytes_remaining >= 8) [[likely]] {
208
633k
        memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
209
633k
    } else {
210
150k
        memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining);
211
150k
    }
212
783k
    return true;
213
783k
}
214
215
1.87M
inline bool BitReader::GetVlqInt(uint32_t* v) {
216
1.87M
    uint32_t tmp = 0;
217
2.12M
    for (int num_bytes = 0; num_bytes < MAX_VLQ_BYTE_LEN; num_bytes++) {
218
2.12M
        uint8_t byte = 0;
219
2.12M
        if (!GetAligned<uint8_t>(1, &byte)) return false;
220
2.10M
        tmp |= static_cast<uint32_t>(byte & 0x7F) << (7 * num_bytes);
221
2.10M
        if ((byte & 0x80) == 0) {
222
1.85M
            *v = tmp;
223
1.85M
            return true;
224
1.85M
        }
225
2.10M
    }
226
3.74k
    return false;
227
1.87M
}
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
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
24.6k
int BatchedBitReader::UnpackBatch(int bit_width, int num_values, T* v) {
266
24.6k
    DCHECK(buffer_pos_ != nullptr);
267
24.6k
    DCHECK_GE(bit_width, 0);
268
24.6k
    DCHECK_LE(bit_width, MAX_BITWIDTH);
269
24.6k
    DCHECK_LE(bit_width, sizeof(T) * 8);
270
24.6k
    DCHECK_GE(num_values, 0);
271
272
24.6k
    int64_t num_read;
273
24.6k
    std::tie(buffer_pos_, num_read) =
274
24.6k
            BitPacking::UnpackValues(bit_width, buffer_pos_, bytes_left(), num_values, v);
275
24.6k
    DCHECK_LE(buffer_pos_, buffer_end_);
276
24.6k
    DCHECK_LE(num_read, num_values);
277
24.6k
    return static_cast<int>(num_read);
278
24.6k
}
_ZN5doris16BatchedBitReader11UnpackBatchIjEEiiiPT_
Line
Count
Source
265
24.3k
int BatchedBitReader::UnpackBatch(int bit_width, int num_values, T* v) {
266
24.3k
    DCHECK(buffer_pos_ != nullptr);
267
24.3k
    DCHECK_GE(bit_width, 0);
268
24.3k
    DCHECK_LE(bit_width, MAX_BITWIDTH);
269
24.3k
    DCHECK_LE(bit_width, sizeof(T) * 8);
270
24.3k
    DCHECK_GE(num_values, 0);
271
272
24.3k
    int64_t num_read;
273
24.3k
    std::tie(buffer_pos_, num_read) =
274
24.3k
            BitPacking::UnpackValues(bit_width, buffer_pos_, bytes_left(), num_values, v);
275
24.3k
    DCHECK_LE(buffer_pos_, buffer_end_);
276
    DCHECK_LE(num_read, num_values);
277
24.3k
    return static_cast<int>(num_read);
278
24.3k
}
_ZN5doris16BatchedBitReader11UnpackBatchIhEEiiiPT_
Line
Count
Source
265
257
int BatchedBitReader::UnpackBatch(int bit_width, int num_values, T* v) {
266
257
    DCHECK(buffer_pos_ != nullptr);
267
257
    DCHECK_GE(bit_width, 0);
268
257
    DCHECK_LE(bit_width, MAX_BITWIDTH);
269
257
    DCHECK_LE(bit_width, sizeof(T) * 8);
270
257
    DCHECK_GE(num_values, 0);
271
272
257
    int64_t num_read;
273
257
    std::tie(buffer_pos_, num_read) =
274
257
            BitPacking::UnpackValues(bit_width, buffer_pos_, bytes_left(), num_values, v);
275
257
    DCHECK_LE(buffer_pos_, buffer_end_);
276
    DCHECK_LE(num_read, num_values);
277
257
    return static_cast<int>(num_read);
278
257
}
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
13.4k
bool BatchedBitReader::GetBytes(int num_bytes, T* v) {
317
13.4k
    DCHECK(buffer_pos_ != nullptr);
318
13.4k
    DCHECK_GE(num_bytes, 0);
319
13.4k
    DCHECK_LE(num_bytes, sizeof(T));
320
13.4k
    if (UNLIKELY(buffer_pos_ + num_bytes > buffer_end_)) return false;
321
13.4k
    *v = 0; // Ensure unset bytes are initialized to zero.
322
13.4k
    memcpy(v, buffer_pos_, num_bytes);
323
13.4k
    buffer_pos_ += num_bytes;
324
13.4k
    return true;
325
13.4k
}
_ZN5doris16BatchedBitReader8GetBytesIhEEbiPT_
Line
Count
Source
316
12.9k
bool BatchedBitReader::GetBytes(int num_bytes, T* v) {
317
12.9k
    DCHECK(buffer_pos_ != nullptr);
318
12.9k
    DCHECK_GE(num_bytes, 0);
319
12.9k
    DCHECK_LE(num_bytes, sizeof(T));
320
12.9k
    if (UNLIKELY(buffer_pos_ + num_bytes > buffer_end_)) return false;
321
12.9k
    *v = 0; // Ensure unset bytes are initialized to zero.
322
12.9k
    memcpy(v, buffer_pos_, num_bytes);
323
12.9k
    buffer_pos_ += num_bytes;
324
12.9k
    return true;
325
12.9k
}
_ZN5doris16BatchedBitReader8GetBytesIjEEbiPT_
Line
Count
Source
316
508
bool BatchedBitReader::GetBytes(int num_bytes, T* v) {
317
508
    DCHECK(buffer_pos_ != nullptr);
318
508
    DCHECK_GE(num_bytes, 0);
319
508
    DCHECK_LE(num_bytes, sizeof(T));
320
508
    if (UNLIKELY(buffer_pos_ + num_bytes > buffer_end_)) return false;
321
508
    *v = 0; // Ensure unset bytes are initialized to zero.
322
508
    memcpy(v, buffer_pos_, num_bytes);
323
508
    buffer_pos_ += num_bytes;
324
508
    return true;
325
508
}
326
327
template <typename UINT_T>
328
12.7k
bool BatchedBitReader::GetUleb128(UINT_T* v) {
329
12.7k
    static_assert(std::is_integral<UINT_T>::value, "Integral type required.");
330
12.7k
    static_assert(std::is_unsigned<UINT_T>::value, "Unsigned type required.");
331
12.7k
    static_assert(!std::is_same<UINT_T, bool>::value, "Bools are not supported.");
332
333
12.7k
    *v = 0;
334
12.7k
    int shift = 0;
335
12.7k
    uint8_t byte = 0;
336
12.9k
    do {
337
12.9k
        if (UNLIKELY(shift >= max_vlq_byte_len<UINT_T>() * 7)) return false;
338
12.9k
        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
12.9k
        const UINT_T byte_as_UINT_T = byte;
344
12.9k
        *v |= (byte_as_UINT_T & 0x7Fu) << shift;
345
12.9k
        shift += 7;
346
12.9k
    } while ((byte & 0x80u) != 0);
347
12.7k
    return true;
348
12.7k
}
349
350
} // namespace doris