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 | 559k | inline void BitWriter::PutValue(uint64_t v, int num_bits) { |
36 | 559k | DCHECK_LE(num_bits, 64); |
37 | | // Truncate the higher-order bits. This is necessary to |
38 | | // support signed values. |
39 | 559k | v &= ~0ULL >> (64 - num_bits); |
40 | | |
41 | 559k | buffered_values_ |= v << bit_offset_; |
42 | 559k | bit_offset_ += num_bits; |
43 | | |
44 | 559k | if (bit_offset_ >= 64) [[unlikely]] { |
45 | | // Flush buffered_values_ and write out bits of v that did not fit |
46 | 201k | buffer_->reserve(ALIGN_UP(byte_offset_ + 8, 8)); |
47 | 201k | buffer_->resize(byte_offset_ + 8); |
48 | 201k | DCHECK_LE(byte_offset_ + 8, buffer_->capacity()); |
49 | 201k | memcpy(buffer_->data() + byte_offset_, &buffered_values_, 8); |
50 | 201k | buffered_values_ = 0; |
51 | 201k | byte_offset_ += 8; |
52 | 201k | bit_offset_ -= 64; |
53 | 201k | buffered_values_ = BitUtil::ShiftRightZeroOnOverflow(v, (num_bits - bit_offset_)); |
54 | 201k | } |
55 | 559k | DCHECK_LT(bit_offset_, 64); |
56 | 559k | } |
57 | | |
58 | 67.0M | inline void BitWriter::Flush(bool align) { |
59 | 67.0M | int num_bytes = BitUtil::Ceil(bit_offset_, 8); |
60 | 67.0M | buffer_->reserve(ALIGN_UP(byte_offset_ + num_bytes, 8)); |
61 | 67.0M | buffer_->resize(byte_offset_ + num_bytes); |
62 | 67.0M | DCHECK_LE(byte_offset_ + num_bytes, buffer_->capacity()); |
63 | 67.0M | memcpy(buffer_->data() + byte_offset_, &buffered_values_, num_bytes); |
64 | | |
65 | 67.0M | if (align) { |
66 | 56.0M | buffered_values_ = 0; |
67 | 56.0M | byte_offset_ += num_bytes; |
68 | 56.0M | bit_offset_ = 0; |
69 | 56.0M | } |
70 | 67.0M | } |
71 | | |
72 | 56.0M | inline uint8_t* BitWriter::GetNextBytePtr(int num_bytes) { |
73 | 56.0M | Flush(/* align */ true); |
74 | 56.0M | buffer_->reserve(ALIGN_UP(byte_offset_ + num_bytes, 8)); |
75 | 56.0M | buffer_->resize(byte_offset_ + num_bytes); |
76 | 56.0M | uint8_t* ptr = buffer_->data() + byte_offset_; |
77 | 56.0M | byte_offset_ += num_bytes; |
78 | 56.0M | DCHECK_LE(byte_offset_, buffer_->capacity()); |
79 | 56.0M | return ptr; |
80 | 56.0M | } |
81 | | |
82 | | template <typename T> |
83 | 56.0M | void BitWriter::PutAligned(T val, int num_bytes) { |
84 | 56.0M | DCHECK_LE(num_bytes, sizeof(T)); |
85 | 56.0M | uint8_t* ptr = GetNextBytePtr(num_bytes); |
86 | 56.0M | memcpy(ptr, &val, num_bytes); |
87 | 56.0M | } _ZN5doris9BitWriter10PutAlignedIhEEvT_i Line | Count | Source | 83 | 37.0M | void BitWriter::PutAligned(T val, int num_bytes) { | 84 | | DCHECK_LE(num_bytes, sizeof(T)); | 85 | 37.0M | uint8_t* ptr = GetNextBytePtr(num_bytes); | 86 | 37.0M | memcpy(ptr, &val, num_bytes); | 87 | 37.0M | } |
_ZN5doris9BitWriter10PutAlignedImEEvT_i Line | Count | Source | 83 | 19.0M | void BitWriter::PutAligned(T val, int num_bytes) { | 84 | | DCHECK_LE(num_bytes, sizeof(T)); | 85 | 19.0M | uint8_t* ptr = GetNextBytePtr(num_bytes); | 86 | 19.0M | memcpy(ptr, &val, num_bytes); | 87 | 19.0M | } |
|
88 | | |
89 | 19.0M | inline void BitWriter::PutVlqInt(int32_t v) { |
90 | 37.0M | while ((v & 0xFFFFFF80) != 0L) { |
91 | 18.0M | PutAligned<uint8_t>((v & 0x7F) | 0x80, 1); |
92 | 18.0M | v >>= 7; |
93 | 18.0M | } |
94 | 19.0M | PutAligned<uint8_t>(v & 0x7F, 1); |
95 | 19.0M | } |
96 | | |
97 | | inline BitReader::BitReader(const uint8_t* buffer, int buffer_len) |
98 | 1.49k | : buffer_(buffer), |
99 | 1.49k | max_bytes_(buffer_len), |
100 | 1.49k | buffered_values_(0), |
101 | 1.49k | byte_offset_(0), |
102 | 1.49k | bit_offset_(0) { |
103 | 1.49k | int num_bytes = std::min(8, max_bytes_); |
104 | 1.49k | memcpy(&buffered_values_, buffer_ + byte_offset_, num_bytes); |
105 | 1.49k | } |
106 | | |
107 | 201k | inline void BitReader::BufferValues() { |
108 | 201k | int bytes_remaining = max_bytes_ - byte_offset_; |
109 | 201k | if (bytes_remaining >= 8) [[likely]] { |
110 | 200k | memcpy(&buffered_values_, buffer_ + byte_offset_, 8); |
111 | 200k | } else { |
112 | 611 | memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining); |
113 | 611 | } |
114 | 201k | } |
115 | | |
116 | | template <typename T> |
117 | 500k | bool BitReader::GetValue(int num_bits, T* v) { |
118 | 500k | DCHECK_LE(num_bits, 64); |
119 | 500k | DCHECK_LE(num_bits, sizeof(T) * 8); |
120 | | |
121 | 500k | if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] { |
122 | 0 | return false; |
123 | 0 | } |
124 | | |
125 | 500k | *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >> |
126 | 500k | bit_offset_); |
127 | | |
128 | 500k | bit_offset_ += num_bits; |
129 | 500k | if (bit_offset_ >= 64) { |
130 | 201k | byte_offset_ += 8; |
131 | 201k | bit_offset_ -= 64; |
132 | 201k | BufferValues(); |
133 | | // Read bits of v that crossed into new buffered_values_ |
134 | 201k | *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_), |
135 | 201k | (num_bits - bit_offset_)); |
136 | 201k | } |
137 | 500k | DCHECK_LE(bit_offset_, 64); |
138 | 500k | return true; |
139 | 500k | } _ZN5doris9BitReader8GetValueIsEEbiPT_ Line | Count | Source | 117 | 72 | bool BitReader::GetValue(int num_bits, T* v) { | 118 | 72 | DCHECK_LE(num_bits, 64); | 119 | 72 | DCHECK_LE(num_bits, sizeof(T) * 8); | 120 | | | 121 | 72 | if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] { | 122 | 0 | return false; | 123 | 0 | } | 124 | | | 125 | 72 | *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >> | 126 | 72 | bit_offset_); | 127 | | | 128 | 72 | bit_offset_ += num_bits; | 129 | 72 | if (bit_offset_ >= 64) { | 130 | 0 | byte_offset_ += 8; | 131 | 0 | bit_offset_ -= 64; | 132 | 0 | BufferValues(); | 133 | | // Read bits of v that crossed into new buffered_values_ | 134 | 0 | *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_), | 135 | 0 | (num_bits - bit_offset_)); | 136 | 0 | } | 137 | | DCHECK_LE(bit_offset_, 64); | 138 | 72 | return true; | 139 | 72 | } |
_ZN5doris9BitReader8GetValueImEEbiPT_ Line | Count | Source | 117 | 180k | bool BitReader::GetValue(int num_bits, T* v) { | 118 | 180k | DCHECK_LE(num_bits, 64); | 119 | 180k | DCHECK_LE(num_bits, sizeof(T) * 8); | 120 | | | 121 | 180k | if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] { | 122 | 0 | return false; | 123 | 0 | } | 124 | | | 125 | 180k | *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >> | 126 | 180k | bit_offset_); | 127 | | | 128 | 180k | bit_offset_ += num_bits; | 129 | 180k | if (bit_offset_ >= 64) { | 130 | 38.2k | byte_offset_ += 8; | 131 | 38.2k | bit_offset_ -= 64; | 132 | 38.2k | BufferValues(); | 133 | | // Read bits of v that crossed into new buffered_values_ | 134 | 38.2k | *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_), | 135 | 38.2k | (num_bits - bit_offset_)); | 136 | 38.2k | } | 137 | | DCHECK_LE(bit_offset_, 64); | 138 | 180k | return true; | 139 | 180k | } |
_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 | } |
_ZN5doris9BitReader8GetValueIbEEbiPT_ Line | Count | Source | 117 | 32.3k | bool BitReader::GetValue(int num_bits, T* v) { | 118 | 32.3k | DCHECK_LE(num_bits, 64); | 119 | 32.3k | DCHECK_LE(num_bits, sizeof(T) * 8); | 120 | | | 121 | 32.3k | if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] { | 122 | 0 | return false; | 123 | 0 | } | 124 | | | 125 | 32.3k | *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >> | 126 | 32.3k | bit_offset_); | 127 | | | 128 | 32.3k | bit_offset_ += num_bits; | 129 | 32.3k | if (bit_offset_ >= 64) { | 130 | 293 | byte_offset_ += 8; | 131 | 293 | bit_offset_ -= 64; | 132 | 293 | BufferValues(); | 133 | | // Read bits of v that crossed into new buffered_values_ | 134 | 293 | *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_), | 135 | 293 | (num_bits - bit_offset_)); | 136 | 293 | } | 137 | | DCHECK_LE(bit_offset_, 64); | 138 | 32.3k | return true; | 139 | 32.3k | } |
_ZN5doris9BitReader8GetValueIhEEbiPT_ Line | Count | Source | 117 | 38 | bool BitReader::GetValue(int num_bits, T* v) { | 118 | 38 | DCHECK_LE(num_bits, 64); | 119 | 38 | DCHECK_LE(num_bits, sizeof(T) * 8); | 120 | | | 121 | 38 | if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] { | 122 | 0 | return false; | 123 | 0 | } | 124 | | | 125 | 38 | *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >> | 126 | 38 | bit_offset_); | 127 | | | 128 | 38 | bit_offset_ += num_bits; | 129 | 38 | if (bit_offset_ >= 64) { | 130 | 0 | byte_offset_ += 8; | 131 | 0 | bit_offset_ -= 64; | 132 | 0 | BufferValues(); | 133 | | // Read bits of v that crossed into new buffered_values_ | 134 | 0 | *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_), | 135 | 0 | (num_bits - bit_offset_)); | 136 | 0 | } | 137 | | DCHECK_LE(bit_offset_, 64); | 138 | 38 | return true; | 139 | 38 | } |
_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 | } |
_ZN5doris9BitReader8GetValueIjEEbiPT_ Line | Count | Source | 117 | 3 | bool BitReader::GetValue(int num_bits, T* v) { | 118 | 3 | DCHECK_LE(num_bits, 64); | 119 | 3 | DCHECK_LE(num_bits, sizeof(T) * 8); | 120 | | | 121 | 3 | if (byte_offset_ * 8 + bit_offset_ + num_bits > max_bytes_ * 8) [[unlikely]] { | 122 | 0 | return false; | 123 | 0 | } | 124 | | | 125 | 3 | *v = static_cast<T>(BitUtil::TrailingBits(buffered_values_, bit_offset_ + num_bits) >> | 126 | 3 | bit_offset_); | 127 | | | 128 | 3 | bit_offset_ += num_bits; | 129 | 3 | if (bit_offset_ >= 64) { | 130 | 1 | byte_offset_ += 8; | 131 | 1 | bit_offset_ -= 64; | 132 | 1 | BufferValues(); | 133 | | // Read bits of v that crossed into new buffered_values_ | 134 | 1 | *v |= BitUtil::ShiftLeftZeroOnOverflow(BitUtil::TrailingBits(buffered_values_, bit_offset_), | 135 | 1 | (num_bits - bit_offset_)); | 136 | 1 | } | 137 | | DCHECK_LE(bit_offset_, 64); | 138 | 3 | return true; | 139 | 3 | } |
_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 | 31.5k | inline void BitReader::Rewind(int num_bits) { |
142 | 31.5k | bit_offset_ -= num_bits; |
143 | 31.5k | if (bit_offset_ >= 0) { |
144 | 31.2k | return; |
145 | 31.2k | } |
146 | 572 | while (bit_offset_ < 0) { |
147 | 286 | int seek_back = std::min(byte_offset_, 8); |
148 | 286 | byte_offset_ -= seek_back; |
149 | 286 | bit_offset_ += seek_back * 8; |
150 | 286 | } |
151 | | // This should only be executed *if* rewinding by 'num_bits' |
152 | | // make the existing buffered_values_ invalid |
153 | 286 | DCHECK_GE(byte_offset_, 0); // Check for underflow |
154 | 286 | memcpy(&buffered_values_, buffer_ + byte_offset_, 8); |
155 | 286 | } |
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 | 2 | inline void BitReader::SeekToBit(unsigned int stream_position) { |
170 | 2 | DCHECK_LE(stream_position, max_bytes_ * 8); |
171 | | |
172 | 2 | int delta = static_cast<int>(stream_position) - position(); |
173 | 2 | if (delta == 0) { |
174 | 0 | return; |
175 | 2 | } else if (delta < 0) { |
176 | 1 | Rewind(position() - stream_position); |
177 | 1 | } else { |
178 | 1 | bit_offset_ += delta; |
179 | 1 | while (bit_offset_ >= 64) { |
180 | 0 | byte_offset_ += 8; |
181 | 0 | bit_offset_ -= 64; |
182 | 0 | if (bit_offset_ < 64) { |
183 | | // This should only be executed if seeking to |
184 | | // 'stream_position' makes the existing buffered_values_ |
185 | | // invalid. |
186 | 0 | BufferValues(); |
187 | 0 | } |
188 | 0 | } |
189 | 1 | } |
190 | 2 | } |
191 | | |
192 | | template <typename T> |
193 | 50.2k | bool BitReader::GetAligned(int num_bytes, T* v) { |
194 | 50.2k | DCHECK_LE(num_bytes, sizeof(T)); |
195 | 50.2k | int bytes_read = BitUtil::Ceil(bit_offset_, 8); |
196 | 50.2k | if (byte_offset_ + bytes_read + num_bytes > max_bytes_) [[unlikely]] { |
197 | 311 | return false; |
198 | 311 | } |
199 | | |
200 | | // Advance byte_offset to next unread byte and read num_bytes |
201 | 49.9k | byte_offset_ += bytes_read; |
202 | 49.9k | memcpy(v, buffer_ + byte_offset_, num_bytes); |
203 | 49.9k | byte_offset_ += num_bytes; |
204 | | |
205 | | // Reset buffered_values_ |
206 | 49.9k | bit_offset_ = 0; |
207 | 49.9k | int bytes_remaining = max_bytes_ - byte_offset_; |
208 | 49.9k | if (bytes_remaining >= 8) [[likely]] { |
209 | 45.9k | memcpy(&buffered_values_, buffer_ + byte_offset_, 8); |
210 | 45.9k | } else { |
211 | 3.98k | memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining); |
212 | 3.98k | } |
213 | 49.9k | return true; |
214 | 50.2k | } _ZN5doris9BitReader10GetAlignedIhEEbiPT_ Line | Count | Source | 193 | 26.9k | bool BitReader::GetAligned(int num_bytes, T* v) { | 194 | 26.9k | DCHECK_LE(num_bytes, sizeof(T)); | 195 | 26.9k | int bytes_read = BitUtil::Ceil(bit_offset_, 8); | 196 | 26.9k | if (byte_offset_ + bytes_read + num_bytes > max_bytes_) [[unlikely]] { | 197 | 311 | return false; | 198 | 311 | } | 199 | | | 200 | | // Advance byte_offset to next unread byte and read num_bytes | 201 | 26.6k | byte_offset_ += bytes_read; | 202 | 26.6k | memcpy(v, buffer_ + byte_offset_, num_bytes); | 203 | 26.6k | byte_offset_ += num_bytes; | 204 | | | 205 | | // Reset buffered_values_ | 206 | 26.6k | bit_offset_ = 0; | 207 | 26.6k | int bytes_remaining = max_bytes_ - byte_offset_; | 208 | 26.6k | if (bytes_remaining >= 8) [[likely]] { | 209 | 24.4k | memcpy(&buffered_values_, buffer_ + byte_offset_, 8); | 210 | 24.4k | } else { | 211 | 2.20k | memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining); | 212 | 2.20k | } | 213 | 26.6k | return true; | 214 | 26.9k | } |
_ZN5doris9BitReader10GetAlignedIsEEbiPT_ Line | Count | Source | 193 | 112 | bool BitReader::GetAligned(int num_bytes, T* v) { | 194 | 112 | DCHECK_LE(num_bytes, sizeof(T)); | 195 | 112 | int bytes_read = BitUtil::Ceil(bit_offset_, 8); | 196 | 112 | 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 | 112 | byte_offset_ += bytes_read; | 202 | 112 | memcpy(v, buffer_ + byte_offset_, num_bytes); | 203 | 112 | byte_offset_ += num_bytes; | 204 | | | 205 | | // Reset buffered_values_ | 206 | 112 | bit_offset_ = 0; | 207 | 112 | int bytes_remaining = max_bytes_ - byte_offset_; | 208 | 112 | if (bytes_remaining >= 8) [[likely]] { | 209 | 0 | memcpy(&buffered_values_, buffer_ + byte_offset_, 8); | 210 | 112 | } else { | 211 | 112 | memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining); | 212 | 112 | } | 213 | 112 | return true; | 214 | 112 | } |
_ZN5doris9BitReader10GetAlignedIbEEbiPT_ Line | Count | Source | 193 | 21.1k | bool BitReader::GetAligned(int num_bytes, T* v) { | 194 | 21.1k | DCHECK_LE(num_bytes, sizeof(T)); | 195 | 21.1k | int bytes_read = BitUtil::Ceil(bit_offset_, 8); | 196 | 21.1k | 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 | 21.1k | byte_offset_ += bytes_read; | 202 | 21.1k | memcpy(v, buffer_ + byte_offset_, num_bytes); | 203 | 21.1k | byte_offset_ += num_bytes; | 204 | | | 205 | | // Reset buffered_values_ | 206 | 21.1k | bit_offset_ = 0; | 207 | 21.1k | int bytes_remaining = max_bytes_ - byte_offset_; | 208 | 21.1k | if (bytes_remaining >= 8) [[likely]] { | 209 | 19.8k | memcpy(&buffered_values_, buffer_ + byte_offset_, 8); | 210 | 19.8k | } else { | 211 | 1.34k | memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining); | 212 | 1.34k | } | 213 | 21.1k | return true; | 214 | 21.1k | } |
_ZN5doris9BitReader10GetAlignedImEEbiPT_ Line | Count | Source | 193 | 1.94k | bool BitReader::GetAligned(int num_bytes, T* v) { | 194 | 1.94k | DCHECK_LE(num_bytes, sizeof(T)); | 195 | 1.94k | int bytes_read = BitUtil::Ceil(bit_offset_, 8); | 196 | 1.94k | 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 | 1.94k | byte_offset_ += bytes_read; | 202 | 1.94k | memcpy(v, buffer_ + byte_offset_, num_bytes); | 203 | 1.94k | byte_offset_ += num_bytes; | 204 | | | 205 | | // Reset buffered_values_ | 206 | 1.94k | bit_offset_ = 0; | 207 | 1.94k | int bytes_remaining = max_bytes_ - byte_offset_; | 208 | 1.94k | if (bytes_remaining >= 8) [[likely]] { | 209 | 1.61k | memcpy(&buffered_values_, buffer_ + byte_offset_, 8); | 210 | 1.61k | } else { | 211 | 330 | memcpy(&buffered_values_, buffer_ + byte_offset_, bytes_remaining); | 212 | 330 | } | 213 | 1.94k | return true; | 214 | 1.94k | } |
|
215 | | |
216 | 26.3k | inline bool BitReader::GetVlqInt(uint32_t* v) { |
217 | 26.3k | uint32_t tmp = 0; |
218 | 26.8k | for (int num_bytes = 0; num_bytes < MAX_VLQ_BYTE_LEN; num_bytes++) { |
219 | 26.8k | uint8_t byte = 0; |
220 | 26.8k | if (!GetAligned<uint8_t>(1, &byte)) return false; |
221 | 26.5k | tmp |= static_cast<uint32_t>(byte & 0x7F) << (7 * num_bytes); |
222 | 26.5k | if ((byte & 0x80) == 0) { |
223 | 26.0k | *v = tmp; |
224 | 26.0k | return true; |
225 | 26.0k | } |
226 | 26.5k | } |
227 | 0 | return false; |
228 | 26.3k | } |
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 | 35 | int BatchedBitReader::UnpackBatch(int bit_width, int num_values, T* v) { |
267 | 35 | DCHECK(buffer_pos_ != nullptr); |
268 | 35 | DCHECK_GE(bit_width, 0); |
269 | 35 | DCHECK_LE(bit_width, MAX_BITWIDTH); |
270 | 35 | DCHECK_LE(bit_width, sizeof(T) * 8); |
271 | 35 | DCHECK_GE(num_values, 0); |
272 | | |
273 | 35 | int64_t num_read; |
274 | 35 | std::tie(buffer_pos_, num_read) = |
275 | 35 | BitPacking::UnpackValues(bit_width, buffer_pos_, bytes_left(), num_values, v); |
276 | 35 | DCHECK_LE(buffer_pos_, buffer_end_); |
277 | 35 | DCHECK_LE(num_read, num_values); |
278 | 35 | return static_cast<int>(num_read); |
279 | 35 | } _ZN5doris16BatchedBitReader11UnpackBatchIjEEiiiPT_ Line | Count | Source | 266 | 20 | int BatchedBitReader::UnpackBatch(int bit_width, int num_values, T* v) { | 267 | 20 | DCHECK(buffer_pos_ != nullptr); | 268 | 20 | DCHECK_GE(bit_width, 0); | 269 | 20 | DCHECK_LE(bit_width, MAX_BITWIDTH); | 270 | 20 | DCHECK_LE(bit_width, sizeof(T) * 8); | 271 | 20 | DCHECK_GE(num_values, 0); | 272 | | | 273 | 20 | int64_t num_read; | 274 | 20 | std::tie(buffer_pos_, num_read) = | 275 | 20 | BitPacking::UnpackValues(bit_width, buffer_pos_, bytes_left(), num_values, v); | 276 | 20 | DCHECK_LE(buffer_pos_, buffer_end_); | 277 | | DCHECK_LE(num_read, num_values); | 278 | 20 | return static_cast<int>(num_read); | 279 | 20 | } |
_ZN5doris16BatchedBitReader11UnpackBatchIhEEiiiPT_ Line | Count | Source | 266 | 15 | int BatchedBitReader::UnpackBatch(int bit_width, int num_values, T* v) { | 267 | 15 | DCHECK(buffer_pos_ != nullptr); | 268 | 15 | DCHECK_GE(bit_width, 0); | 269 | 15 | DCHECK_LE(bit_width, MAX_BITWIDTH); | 270 | 15 | DCHECK_LE(bit_width, sizeof(T) * 8); | 271 | 15 | DCHECK_GE(num_values, 0); | 272 | | | 273 | 15 | int64_t num_read; | 274 | 15 | std::tie(buffer_pos_, num_read) = | 275 | 15 | BitPacking::UnpackValues(bit_width, buffer_pos_, bytes_left(), num_values, v); | 276 | 15 | DCHECK_LE(buffer_pos_, buffer_end_); | 277 | | DCHECK_LE(num_read, num_values); | 278 | 15 | return static_cast<int>(num_read); | 279 | 15 | } |
|
280 | | |
281 | 0 | inline bool BatchedBitReader::SkipBatch(int bit_width, int num_values_to_skip) { |
282 | 0 | DCHECK(buffer_pos_ != nullptr); |
283 | 0 | DCHECK_GE(bit_width, 0); |
284 | 0 | DCHECK_LE(bit_width, MAX_BITWIDTH); |
285 | 0 | DCHECK_GE(num_values_to_skip, 0); |
286 | |
|
287 | 0 | int skip_bytes = BitUtil::RoundUpNumBytes(bit_width * num_values_to_skip); |
288 | 0 | if (skip_bytes > buffer_end_ - buffer_pos_) { |
289 | 0 | return false; |
290 | 0 | } |
291 | 0 | buffer_pos_ += skip_bytes; |
292 | 0 | return true; |
293 | 0 | } |
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 | 112 | bool BatchedBitReader::GetBytes(int num_bytes, T* v) { |
318 | 112 | DCHECK(buffer_pos_ != nullptr); |
319 | 112 | DCHECK_GE(num_bytes, 0); |
320 | 112 | DCHECK_LE(num_bytes, sizeof(T)); |
321 | 112 | if (UNLIKELY(buffer_pos_ + num_bytes > buffer_end_)) return false; |
322 | 112 | *v = 0; // Ensure unset bytes are initialized to zero. |
323 | 112 | memcpy(v, buffer_pos_, num_bytes); |
324 | 112 | buffer_pos_ += num_bytes; |
325 | 112 | return true; |
326 | 112 | } _ZN5doris16BatchedBitReader8GetBytesIhEEbiPT_ Line | Count | Source | 317 | 72 | bool BatchedBitReader::GetBytes(int num_bytes, T* v) { | 318 | 72 | DCHECK(buffer_pos_ != nullptr); | 319 | 72 | DCHECK_GE(num_bytes, 0); | 320 | 72 | DCHECK_LE(num_bytes, sizeof(T)); | 321 | 72 | if (UNLIKELY(buffer_pos_ + num_bytes > buffer_end_)) return false; | 322 | 72 | *v = 0; // Ensure unset bytes are initialized to zero. | 323 | 72 | memcpy(v, buffer_pos_, num_bytes); | 324 | 72 | buffer_pos_ += num_bytes; | 325 | 72 | return true; | 326 | 72 | } |
_ZN5doris16BatchedBitReader8GetBytesIjEEbiPT_ Line | Count | Source | 317 | 40 | bool BatchedBitReader::GetBytes(int num_bytes, T* v) { | 318 | 40 | DCHECK(buffer_pos_ != nullptr); | 319 | 40 | DCHECK_GE(num_bytes, 0); | 320 | 40 | DCHECK_LE(num_bytes, sizeof(T)); | 321 | 40 | if (UNLIKELY(buffer_pos_ + num_bytes > buffer_end_)) return false; | 322 | 40 | *v = 0; // Ensure unset bytes are initialized to zero. | 323 | 40 | memcpy(v, buffer_pos_, num_bytes); | 324 | 40 | buffer_pos_ += num_bytes; | 325 | 40 | return true; | 326 | 40 | } |
|
327 | | |
328 | | template <typename UINT_T> |
329 | 60 | bool BatchedBitReader::GetUleb128(UINT_T* v) { |
330 | 60 | static_assert(std::is_integral<UINT_T>::value, "Integral type required."); |
331 | 60 | static_assert(std::is_unsigned<UINT_T>::value, "Unsigned type required."); |
332 | 60 | static_assert(!std::is_same<UINT_T, bool>::value, "Bools are not supported."); |
333 | | |
334 | 60 | *v = 0; |
335 | 60 | int shift = 0; |
336 | 60 | uint8_t byte = 0; |
337 | 72 | do { |
338 | 72 | if (UNLIKELY(shift >= max_vlq_byte_len<UINT_T>() * 7)) return false; |
339 | 72 | 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 | 72 | const UINT_T byte_as_UINT_T = byte; |
345 | 72 | *v |= (byte_as_UINT_T & 0x7Fu) << shift; |
346 | 72 | shift += 7; |
347 | 72 | } while ((byte & 0x80u) != 0); |
348 | 60 | return true; |
349 | 60 | } |
350 | | |
351 | | #include "common/compile_check_end.h" |
352 | | } // namespace doris |