Coverage Report

Created: 2026-03-14 13:34

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