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