Coverage Report

Created: 2026-08-06 15:00

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
25.9M
inline void BitWriter::PutValue(uint64_t v, int num_bits) {
35
25.9M
    DCHECK_LE(num_bits, 64);
36
    // Truncate the higher-order bits. This is necessary to
37
    // support signed values.
38
25.9M
    v &= ~0ULL >> (64 - num_bits);
39
40
25.9M
    buffered_values_ |= v << bit_offset_;
41
25.9M
    bit_offset_ += num_bits;
42
43
25.9M
    if (bit_offset_ >= 64) [[unlikely]] {
44
        // Flush buffered_values_ and write out bits of v that did not fit
45
299k
        buffer_->reserve(ALIGN_UP(byte_offset_ + 8, 8));
46
299k
        buffer_->resize(byte_offset_ + 8);
47
299k
        DCHECK_LE(byte_offset_ + 8, buffer_->capacity());
48
299k
        memcpy(buffer_->data() + byte_offset_, &buffered_values_, 8);
49
299k
        buffered_values_ = 0;
50
299k
        byte_offset_ += 8;
51
299k
        bit_offset_ -= 64;
52
299k
        buffered_values_ = BitUtil::ShiftRightZeroOnOverflow(v, (num_bits - bit_offset_));
53
299k
    }
54
25.9M
    DCHECK_LT(bit_offset_, 64);
55
25.9M
}
56
57
72.0M
inline void BitWriter::Flush(bool align) {
58
72.0M
    int num_bytes = BitUtil::Ceil(bit_offset_, 8);
59
72.0M
    buffer_->reserve(ALIGN_UP(byte_offset_ + num_bytes, 8));
60
72.0M
    buffer_->resize(byte_offset_ + num_bytes);
61
72.0M
    DCHECK_LE(byte_offset_ + num_bytes, buffer_->capacity());
62
72.0M
    memcpy(buffer_->data() + byte_offset_, &buffered_values_, num_bytes);
63
64
72.0M
    if (align) {
65
60.9M
        buffered_values_ = 0;
66
60.9M
        byte_offset_ += num_bytes;
67
60.9M
        bit_offset_ = 0;
68
60.9M
    }
69
72.0M
}
70
71
60.9M
inline uint8_t* BitWriter::GetNextBytePtr(int num_bytes) {
72
60.9M
    Flush(/* align */ true);
73
60.9M
    buffer_->reserve(ALIGN_UP(byte_offset_ + num_bytes, 8));
74
60.9M
    buffer_->resize(byte_offset_ + num_bytes);
75
60.9M
    uint8_t* ptr = buffer_->data() + byte_offset_;
76
60.9M
    byte_offset_ += num_bytes;
77
60.9M
    DCHECK_LE(byte_offset_, buffer_->capacity());
78
60.9M
    return ptr;
79
60.9M
}
80
81
template <typename T>
82
59.4M
void BitWriter::PutAligned(T val, int num_bytes) {
83
59.4M
    DCHECK_LE(num_bytes, sizeof(T));
84
59.4M
    uint8_t* ptr = GetNextBytePtr(num_bytes);
85
59.4M
    memcpy(ptr, &val, num_bytes);
86
59.4M
}
_ZN5doris9BitWriter10PutAlignedIhEEvT_i
Line
Count
Source
82
38.8M
void BitWriter::PutAligned(T val, int num_bytes) {
83
    DCHECK_LE(num_bytes, sizeof(T));
84
38.8M
    uint8_t* ptr = GetNextBytePtr(num_bytes);
85
38.8M
    memcpy(ptr, &val, num_bytes);
86
38.8M
}
_ZN5doris9BitWriter10PutAlignedImEEvT_i
Line
Count
Source
82
20.6M
void BitWriter::PutAligned(T val, int num_bytes) {
83
    DCHECK_LE(num_bytes, sizeof(T));
84
20.6M
    uint8_t* ptr = GetNextBytePtr(num_bytes);
85
20.6M
    memcpy(ptr, &val, num_bytes);
86
20.6M
}
87
88
20.6M
inline void BitWriter::PutVlqInt(int32_t v) {
89
38.8M
    while ((v & 0xFFFFFF80) != 0L) {
90
18.2M
        PutAligned<uint8_t>((v & 0x7F) | 0x80, 1);
91
18.2M
        v >>= 7;
92
18.2M
    }
93
20.6M
    PutAligned<uint8_t>(v & 0x7F, 1);
94
20.6M
}
95
96
inline BitReader::BitReader(const uint8_t* buffer, int buffer_len)
97
367k
        : buffer_(buffer),
98
367k
          max_bytes_(buffer_len),
99
367k
          buffered_values_(0),
100
367k
          byte_offset_(0),
101
367k
          bit_offset_(0) {
102
367k
    int num_bytes = std::min(8, max_bytes_);
103
367k
    memcpy(&buffered_values_, buffer_ + byte_offset_, num_bytes);
104
367k
}
105
106
1.01M
inline void BitReader::BufferValues() {
107
1.01M
    int bytes_remaining = max_bytes_ - byte_offset_;
108
1.01M
    if (bytes_remaining >= 8) [[likely]] {
109
1.01M
        memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
110
1.01M
    } else {
111
2.68k
        memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining);
112
2.68k
    }
113
1.01M
}
114
115
template <typename T>
116
63.7M
bool BitReader::GetValue(int num_bits, T* v) {
117
63.7M
    DCHECK_LE(num_bits, 64);
118
63.7M
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
63.7M
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
63.7M
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
63.7M
                        bit_offset_);
126
127
63.7M
    bit_offset_ += num_bits;
128
63.7M
    if (bit_offset_ >= 64) {
129
1.01M
        byte_offset_ += 8;
130
1.01M
        bit_offset_ -= 64;
131
1.01M
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
1.01M
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
1.01M
                                               (num_bits - bit_offset_));
135
1.01M
    }
136
63.7M
    DCHECK_LE(bit_offset_, 64);
137
63.7M
    return true;
138
63.7M
}
_ZN5doris9BitReader8GetValueIsEEbiPT_
Line
Count
Source
116
201k
bool BitReader::GetValue(int num_bits, T* v) {
117
201k
    DCHECK_LE(num_bits, 64);
118
201k
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
201k
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
201k
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
201k
                        bit_offset_);
126
127
201k
    bit_offset_ += num_bits;
128
201k
    if (bit_offset_ >= 64) {
129
528
        byte_offset_ += 8;
130
528
        bit_offset_ -= 64;
131
528
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
528
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
528
                                               (num_bits - bit_offset_));
135
528
    }
136
    DCHECK_LE(bit_offset_, 64);
137
201k
    return true;
138
201k
}
_ZN5doris9BitReader8GetValueImEEbiPT_
Line
Count
Source
116
10.1M
bool BitReader::GetValue(int num_bits, T* v) {
117
10.1M
    DCHECK_LE(num_bits, 64);
118
10.1M
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
10.1M
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
10.1M
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
10.1M
                        bit_offset_);
126
127
10.1M
    bit_offset_ += num_bits;
128
10.1M
    if (bit_offset_ >= 64) {
129
60.2k
        byte_offset_ += 8;
130
60.2k
        bit_offset_ -= 64;
131
60.2k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
60.2k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
60.2k
                                               (num_bits - bit_offset_));
135
60.2k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
10.1M
    return true;
138
10.1M
}
_ZN5doris9BitReader8GetValueIbEEbiPT_
Line
Count
Source
116
5.28M
bool BitReader::GetValue(int num_bits, T* v) {
117
5.28M
    DCHECK_LE(num_bits, 64);
118
5.28M
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
5.28M
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
5.28M
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
5.28M
                        bit_offset_);
126
127
5.28M
    bit_offset_ += num_bits;
128
5.28M
    if (bit_offset_ >= 64) {
129
13.1k
        byte_offset_ += 8;
130
13.1k
        bit_offset_ -= 64;
131
13.1k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
13.1k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
13.1k
                                               (num_bits - bit_offset_));
135
13.1k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
5.28M
    return true;
138
5.28M
}
_ZN5doris9BitReader8GetValueIhEEbiPT_
Line
Count
Source
116
45.5M
bool BitReader::GetValue(int num_bits, T* v) {
117
45.5M
    DCHECK_LE(num_bits, 64);
118
45.5M
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
45.5M
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
45.5M
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
45.5M
                        bit_offset_);
126
127
45.5M
    bit_offset_ += num_bits;
128
45.5M
    if (bit_offset_ >= 64) {
129
668k
        byte_offset_ += 8;
130
668k
        bit_offset_ -= 64;
131
668k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
668k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
668k
                                               (num_bits - bit_offset_));
135
668k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
45.5M
    return true;
138
45.5M
}
_ZN5doris9BitReader8GetValueIiEEbiPT_
Line
Count
Source
116
1.57M
bool BitReader::GetValue(int num_bits, T* v) {
117
1.57M
    DCHECK_LE(num_bits, 64);
118
1.57M
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
1.57M
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
1.57M
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
1.57M
                        bit_offset_);
126
127
1.57M
    bit_offset_ += num_bits;
128
1.57M
    if (bit_offset_ >= 64) {
129
6.67k
        byte_offset_ += 8;
130
6.67k
        bit_offset_ -= 64;
131
6.67k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
6.67k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
6.67k
                                               (num_bits - bit_offset_));
135
6.67k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
1.57M
    return true;
138
1.57M
}
_ZN5doris9BitReader8GetValueIlEEbiPT_
Line
Count
Source
116
358k
bool BitReader::GetValue(int num_bits, T* v) {
117
358k
    DCHECK_LE(num_bits, 64);
118
358k
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
358k
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
358k
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
358k
                        bit_offset_);
126
127
358k
    bit_offset_ += num_bits;
128
358k
    if (bit_offset_ >= 64) {
129
197k
        byte_offset_ += 8;
130
197k
        bit_offset_ -= 64;
131
197k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
197k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
197k
                                               (num_bits - bit_offset_));
135
197k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
358k
    return true;
138
358k
}
_ZN5doris9BitReader8GetValueIcEEbiPT_
Line
Count
Source
116
569k
bool BitReader::GetValue(int num_bits, T* v) {
117
569k
    DCHECK_LE(num_bits, 64);
118
569k
    DCHECK_LE(num_bits, sizeof(T) * 8);
119
120
569k
    if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] {
121
0
        return false;
122
0
    }
123
124
569k
    *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >>
125
569k
                        bit_offset_);
126
127
569k
    bit_offset_ += num_bits;
128
569k
    if (bit_offset_ >= 64) {
129
71.1k
        byte_offset_ += 8;
130
71.1k
        bit_offset_ -= 64;
131
71.1k
        BufferValues();
132
        // Read bits of v that crossed into new buffered_values_
133
71.1k
        *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_),
134
71.1k
                                               (num_bits - bit_offset_));
135
71.1k
    }
136
    DCHECK_LE(bit_offset_, 64);
137
569k
    return true;
138
569k
}
139
140
3.43M
inline void BitReader::Rewind(int num_bits) {
141
3.43M
    bit_offset_ -= num_bits;
142
3.43M
    if (bit_offset_ >= 0) {
143
3.42M
        return;
144
3.42M
    }
145
19.3k
    while (bit_offset_ < 0) {
146
10.0k
        int seek_back = std::min(byte_offset_, 8);
147
10.0k
        byte_offset_ -= seek_back;
148
10.0k
        bit_offset_ += seek_back * 8;
149
10.0k
    }
150
    // This should only be executed *if* rewinding by 'num_bits'
151
    // make the existing buffered_values_ invalid
152
9.26k
    DCHECK_GE(byte_offset_, 0); // Check for underflow
153
9.26k
    memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
154
9.26k
}
155
156
444
inline bool BitReader::Advance(int64_t num_bits) {
157
444
    int64_t bits_required = bit_offset_ + num_bits;
158
444
    int64_t bytes_required = (bits_required >> 3) + ((bits_required & 7) != 0);
159
444
    if (bytes_required > max_bytes_ - byte_offset_) {
160
0
        return false;
161
0
    }
162
444
    byte_offset_ += static_cast<int>(bits_required >> 3);
163
444
    bit_offset_ = static_cast<int>(bits_required & 7);
164
444
    BufferValues();
165
444
    return true;
166
444
}
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.59M
bool BitReader::GetAligned(int num_bytes, T* v) {
193
3.59M
    DCHECK_LE(num_bytes, sizeof(T));
194
3.59M
    int bytes_read = BitUtil::Ceil(bit_offset_, 8);
195
3.59M
    if (byte_offset_ + bytes_read + num_bytes > max_bytes_) [[unlikely]] {
196
12.2k
        return false;
197
12.2k
    }
198
199
    // Advance byte_offset to next unread byte and read num_bytes
200
3.58M
    byte_offset_ += bytes_read;
201
3.58M
    memcpy(v, buffer_ + byte_offset_, num_bytes);
202
3.58M
    byte_offset_ += num_bytes;
203
204
    // Reset buffered_values_
205
3.58M
    bit_offset_ = 0;
206
3.58M
    int bytes_remaining = max_bytes_ - byte_offset_;
207
3.58M
    if (bytes_remaining >= 8) [[likely]] {
208
2.74M
        memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
209
2.74M
    } else {
210
834k
        memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining);
211
834k
    }
212
3.58M
    return true;
213
3.59M
}
_ZN5doris9BitReader10GetAlignedIhEEbiPT_
Line
Count
Source
192
2.55M
bool BitReader::GetAligned(int num_bytes, T* v) {
193
2.55M
    DCHECK_LE(num_bytes, sizeof(T));
194
2.55M
    int bytes_read = BitUtil::Ceil(bit_offset_, 8);
195
2.55M
    if (byte_offset_ + bytes_read + num_bytes > max_bytes_) [[unlikely]] {
196
12.2k
        return false;
197
12.2k
    }
198
199
    // Advance byte_offset to next unread byte and read num_bytes
200
2.54M
    byte_offset_ += bytes_read;
201
2.54M
    memcpy(v, buffer_ + byte_offset_, num_bytes);
202
2.54M
    byte_offset_ += num_bytes;
203
204
    // Reset buffered_values_
205
2.54M
    bit_offset_ = 0;
206
2.54M
    int bytes_remaining = max_bytes_ - byte_offset_;
207
2.54M
    if (bytes_remaining >= 8) [[likely]] {
208
1.98M
        memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
209
1.98M
    } else {
210
560k
        memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining);
211
560k
    }
212
2.54M
    return true;
213
2.55M
}
_ZN5doris9BitReader10GetAlignedIsEEbiPT_
Line
Count
Source
192
99.3k
bool BitReader::GetAligned(int num_bytes, T* v) {
193
99.3k
    DCHECK_LE(num_bytes, sizeof(T));
194
99.3k
    int bytes_read = BitUtil::Ceil(bit_offset_, 8);
195
99.3k
    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
99.3k
    byte_offset_ += bytes_read;
201
99.3k
    memcpy(v, buffer_ + byte_offset_, num_bytes);
202
99.3k
    byte_offset_ += num_bytes;
203
204
    // Reset buffered_values_
205
99.3k
    bit_offset_ = 0;
206
99.3k
    int bytes_remaining = max_bytes_ - byte_offset_;
207
99.3k
    if (bytes_remaining >= 8) [[likely]] {
208
30.6k
        memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
209
68.6k
    } else {
210
68.6k
        memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining);
211
68.6k
    }
212
99.3k
    return true;
213
99.3k
}
_ZN5doris9BitReader10GetAlignedIbEEbiPT_
Line
Count
Source
192
934k
bool BitReader::GetAligned(int num_bytes, T* v) {
193
934k
    DCHECK_LE(num_bytes, sizeof(T));
194
934k
    int bytes_read = BitUtil::Ceil(bit_offset_, 8);
195
934k
    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
934k
    byte_offset_ += bytes_read;
201
934k
    memcpy(v, buffer_ + byte_offset_, num_bytes);
202
934k
    byte_offset_ += num_bytes;
203
204
    // Reset buffered_values_
205
934k
    bit_offset_ = 0;
206
934k
    int bytes_remaining = max_bytes_ - byte_offset_;
207
934k
    if (bytes_remaining >= 8) [[likely]] {
208
728k
        memcpy(&buffered_values_, buffer_ + byte_offset_, 8);
209
728k
    } else {
210
205k
        memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining);
211
205k
    }
212
934k
    return true;
213
934k
}
214
215
2.21M
inline bool BitReader::GetVlqInt(uint32_t* v) {
216
2.21M
    uint32_t tmp = 0;
217
2.42M
    for (int num_bytes = 0; num_bytes < MAX_VLQ_BYTE_LEN; num_bytes++) {
218
2.42M
        uint8_t byte = 0;
219
2.42M
        if (!GetAligned<uint8_t>(1, &byte)) return false;
220
2.41M
        tmp |= static_cast<uint32_t>(byte & 0x7F) << (7 * num_bytes);
221
2.41M
        if ((byte & 0x80) == 0) {
222
2.20M
            *v = tmp;
223
2.20M
            return true;
224
2.20M
        }
225
2.41M
    }
226
247
    return false;
227
2.21M
}
228
229
5.41k
inline bool BitReader::GetZigZagVlqInt(int32_t* v) {
230
5.41k
    uint32_t u;
231
5.41k
    if (!GetVlqInt(&u)) {
232
3
        return false;
233
3
    }
234
5.40k
    u = (u >> 1) ^ (~(u & 1) + 1);
235
    // copy uint32_t to int32_t
236
5.40k
    std::memcpy(v, &u, sizeof(uint32_t));
237
5.40k
    return true;
238
5.41k
}
239
240
2.08k
inline bool BitReader::GetVlqInt(uint64_t* v) {
241
2.08k
    uint64_t tmp = 0;
242
7.06k
    for (int num_bytes = 0; num_bytes < MAX_VLQ_BYTE_LEN_FOR_INT64; num_bytes++) {
243
7.06k
        uint8_t byte = 0;
244
7.06k
        if (!GetAligned<uint8_t>(1, &byte)) return false;
245
7.06k
        tmp |= static_cast<uint64_t>(byte & 0x7F) << (7 * num_bytes);
246
7.06k
        if ((byte & 0x80) == 0) {
247
2.08k
            *v = tmp;
248
2.08k
            return true;
249
2.08k
        }
250
7.06k
    }
251
0
    return false;
252
2.08k
}
253
254
2.08k
inline bool BitReader::GetZigZagVlqInt(int64_t* v) {
255
2.08k
    uint64_t u;
256
2.08k
    if (!GetVlqInt(&u)) {
257
0
        return false;
258
0
    }
259
2.08k
    u = (u >> 1) ^ (~(u & 1) + 1);
260
2.08k
    std::memcpy(v, &u, sizeof(uint64_t));
261
2.08k
    return true;
262
2.08k
}
263
264
template <typename T>
265
17.2M
int BatchedBitReader::UnpackBatch(int bit_width, int num_values, T* v) {
266
17.2M
    DCHECK(buffer_pos_ != nullptr);
267
17.2M
    DCHECK_GE(bit_width, 0);
268
17.2M
    DCHECK_LE(bit_width, MAX_BITWIDTH);
269
17.2M
    DCHECK_LE(bit_width, sizeof(T) * 8);
270
17.2M
    DCHECK_GE(num_values, 0);
271
272
17.2M
    int64_t num_read;
273
17.2M
    std::tie(buffer_pos_, num_read) =
274
17.2M
            BitPacking::UnpackValues(bit_width, buffer_pos_, bytes_left(), num_values, v);
275
17.2M
    DCHECK_LE(buffer_pos_, buffer_end_);
276
17.2M
    DCHECK_LE(num_read, num_values);
277
17.2M
    return static_cast<int>(num_read);
278
17.2M
}
_ZN5doris16BatchedBitReader11UnpackBatchIjEEiiiPT_
Line
Count
Source
265
12.6M
int BatchedBitReader::UnpackBatch(int bit_width, int num_values, T* v) {
266
12.6M
    DCHECK(buffer_pos_ != nullptr);
267
12.6M
    DCHECK_GE(bit_width, 0);
268
12.6M
    DCHECK_LE(bit_width, MAX_BITWIDTH);
269
12.6M
    DCHECK_LE(bit_width, sizeof(T) * 8);
270
12.6M
    DCHECK_GE(num_values, 0);
271
272
12.6M
    int64_t num_read;
273
12.6M
    std::tie(buffer_pos_, num_read) =
274
12.6M
            BitPacking::UnpackValues(bit_width, buffer_pos_, bytes_left(), num_values, v);
275
12.6M
    DCHECK_LE(buffer_pos_, buffer_end_);
276
    DCHECK_LE(num_read, num_values);
277
12.6M
    return static_cast<int>(num_read);
278
12.6M
}
_ZN5doris16BatchedBitReader11UnpackBatchItEEiiiPT_
Line
Count
Source
265
4.53M
int BatchedBitReader::UnpackBatch(int bit_width, int num_values, T* v) {
266
4.53M
    DCHECK(buffer_pos_ != nullptr);
267
4.53M
    DCHECK_GE(bit_width, 0);
268
4.53M
    DCHECK_LE(bit_width, MAX_BITWIDTH);
269
4.53M
    DCHECK_LE(bit_width, sizeof(T) * 8);
270
4.53M
    DCHECK_GE(num_values, 0);
271
272
4.53M
    int64_t num_read;
273
4.53M
    std::tie(buffer_pos_, num_read) =
274
4.53M
            BitPacking::UnpackValues(bit_width, buffer_pos_, bytes_left(), num_values, v);
275
4.53M
    DCHECK_LE(buffer_pos_, buffer_end_);
276
    DCHECK_LE(num_read, num_values);
277
4.53M
    return static_cast<int>(num_read);
278
4.53M
}
_ZN5doris16BatchedBitReader11UnpackBatchIhEEiiiPT_
Line
Count
Source
265
10.1k
int BatchedBitReader::UnpackBatch(int bit_width, int num_values, T* v) {
266
10.1k
    DCHECK(buffer_pos_ != nullptr);
267
10.1k
    DCHECK_GE(bit_width, 0);
268
10.1k
    DCHECK_LE(bit_width, MAX_BITWIDTH);
269
10.1k
    DCHECK_LE(bit_width, sizeof(T) * 8);
270
10.1k
    DCHECK_GE(num_values, 0);
271
272
10.1k
    int64_t num_read;
273
10.1k
    std::tie(buffer_pos_, num_read) =
274
10.1k
            BitPacking::UnpackValues(bit_width, buffer_pos_, bytes_left(), num_values, v);
275
10.1k
    DCHECK_LE(buffer_pos_, buffer_end_);
276
    DCHECK_LE(num_read, num_values);
277
10.1k
    return static_cast<int>(num_read);
278
10.1k
}
279
280
224
inline bool BatchedBitReader::SkipBatch(int bit_width, int num_values_to_skip) {
281
224
    DCHECK(buffer_pos_ != nullptr);
282
224
    DCHECK_GE(bit_width, 0);
283
224
    DCHECK_LE(bit_width, MAX_BITWIDTH);
284
224
    DCHECK_GE(num_values_to_skip, 0);
285
286
224
    int skip_bytes = BitUtil::RoundUpNumBytes(bit_width * num_values_to_skip);
287
224
    if (skip_bytes > buffer_end_ - buffer_pos_) {
288
0
        return false;
289
0
    }
290
224
    buffer_pos_ += skip_bytes;
291
224
    return true;
292
224
}
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
21.1M
bool BatchedBitReader::GetBytes(int num_bytes, T* v) {
317
21.1M
    DCHECK(buffer_pos_ != nullptr);
318
21.1M
    DCHECK_GE(num_bytes, 0);
319
21.1M
    DCHECK_LE(num_bytes, sizeof(T));
320
21.1M
    if (UNLIKELY(buffer_pos_ + num_bytes > buffer_end_)) return false;
321
21.1M
    *v = 0; // Ensure unset bytes are initialized to zero.
322
21.1M
    memcpy(v, buffer_pos_, num_bytes);
323
21.1M
    buffer_pos_ += num_bytes;
324
21.1M
    return true;
325
21.1M
}
_ZN5doris16BatchedBitReader8GetBytesIhEEbiPT_
Line
Count
Source
316
14.5M
bool BatchedBitReader::GetBytes(int num_bytes, T* v) {
317
14.5M
    DCHECK(buffer_pos_ != nullptr);
318
14.5M
    DCHECK_GE(num_bytes, 0);
319
14.5M
    DCHECK_LE(num_bytes, sizeof(T));
320
14.5M
    if (UNLIKELY(buffer_pos_ + num_bytes > buffer_end_)) return false;
321
14.5M
    *v = 0; // Ensure unset bytes are initialized to zero.
322
14.5M
    memcpy(v, buffer_pos_, num_bytes);
323
14.5M
    buffer_pos_ += num_bytes;
324
14.5M
    return true;
325
14.5M
}
_ZN5doris16BatchedBitReader8GetBytesIjEEbiPT_
Line
Count
Source
316
3.35M
bool BatchedBitReader::GetBytes(int num_bytes, T* v) {
317
3.35M
    DCHECK(buffer_pos_ != nullptr);
318
3.35M
    DCHECK_GE(num_bytes, 0);
319
3.35M
    DCHECK_LE(num_bytes, sizeof(T));
320
3.35M
    if (UNLIKELY(buffer_pos_ + num_bytes > buffer_end_)) return false;
321
3.35M
    *v = 0; // Ensure unset bytes are initialized to zero.
322
3.35M
    memcpy(v, buffer_pos_, num_bytes);
323
3.35M
    buffer_pos_ += num_bytes;
324
3.35M
    return true;
325
3.35M
}
_ZN5doris16BatchedBitReader8GetBytesItEEbiPT_
Line
Count
Source
316
3.23M
bool BatchedBitReader::GetBytes(int num_bytes, T* v) {
317
3.23M
    DCHECK(buffer_pos_ != nullptr);
318
3.23M
    DCHECK_GE(num_bytes, 0);
319
3.23M
    DCHECK_LE(num_bytes, sizeof(T));
320
3.23M
    if (UNLIKELY(buffer_pos_ + num_bytes > buffer_end_)) return false;
321
3.23M
    *v = 0; // Ensure unset bytes are initialized to zero.
322
3.23M
    memcpy(v, buffer_pos_, num_bytes);
323
3.23M
    buffer_pos_ += num_bytes;
324
3.23M
    return true;
325
3.23M
}
326
327
template <typename UINT_T>
328
13.5M
bool BatchedBitReader::GetUleb128(UINT_T* v) {
329
13.5M
    static_assert(std::is_integral<UINT_T>::value, "Integral type required.");
330
13.5M
    static_assert(std::is_unsigned<UINT_T>::value, "Unsigned type required.");
331
13.5M
    static_assert(!std::is_same<UINT_T, bool>::value, "Bools are not supported.");
332
333
13.5M
    *v = 0;
334
13.5M
    int shift = 0;
335
13.5M
    uint8_t byte = 0;
336
14.5M
    do {
337
14.5M
        if (UNLIKELY(shift >= max_vlq_byte_len<UINT_T>() * 7)) return false;
338
14.5M
        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
14.5M
        const UINT_T byte_as_UINT_T = byte;
344
14.5M
        *v |= (byte_as_UINT_T & 0x7Fu) << shift;
345
14.5M
        shift += 7;
346
14.5M
    } while ((byte & 0x80u) != 0);
347
13.5M
    return true;
348
13.5M
}
349
350
} // namespace doris