Coverage Report

Created: 2026-03-16 06:38

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