Coverage Report

Created: 2026-04-10 10:30

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