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