Coverage Report

Created: 2026-04-28 15:58

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