forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbit_util.h
More file actions
403 lines (340 loc) · 13.3 KB
/
Copy pathbit_util.h
File metadata and controls
403 lines (340 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#pragma once
#include <bit>
#include <cassert>
#include <cstdint>
#include <cstring>
#include <type_traits>
#include "arrow/util/endian.h"
#include "arrow/util/macros.h"
#include "arrow/util/visibility.h"
namespace arrow {
namespace detail {
template <typename Integer>
typename std::make_unsigned<Integer>::type as_unsigned(Integer x) {
return static_cast<typename std::make_unsigned<Integer>::type>(x);
}
} // namespace detail
namespace bit_util {
//
// Bit-related computations on integer values
//
// Returns the ceil of value/divisor
constexpr int64_t CeilDiv(int64_t value, int64_t divisor) {
return (value == 0) ? 0 : 1 + (value - 1) / divisor;
}
// Return the number of bytes needed to fit the given number of bits
constexpr int64_t BytesForBits(int64_t bits) {
// This formula avoids integer overflow on very large `bits`
return (bits >> 3) + ((bits & 7) != 0);
}
// Returns the smallest power of two that contains v. If v is already a
// power of two, it is returned as is.
static inline int64_t NextPower2(int64_t n) {
// Taken from
// http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n |= n >> 32;
n++;
return n;
}
constexpr bool IsMultipleOf64(int64_t n) { return (n & 63) == 0; }
constexpr bool IsMultipleOf8(int64_t n) { return (n & 7) == 0; }
// Returns a mask for the bit_index lower order bits.
// Valid in the range `[0, 8*sizof(Uint)]` if `kAllowUpperBound`
// otherwise `[0, 8*sizof(Uint)[`
template <typename Uint, bool kAllowUpperBound = false>
constexpr auto LeastSignificantBitMask(Uint bit_index) {
if constexpr (kAllowUpperBound) {
if (bit_index == 8 * sizeof(Uint)) {
return ~Uint{0};
}
}
return (Uint{1} << bit_index) - Uint{1};
}
// Returns 'value' rounded up to the nearest multiple of 'factor'
constexpr int64_t RoundUp(int64_t value, int64_t factor) {
return CeilDiv(value, factor) * factor;
}
// Returns 'value' rounded down to the nearest multiple of 'factor'
constexpr int64_t RoundDown(int64_t value, int64_t factor) {
return (value / factor) * factor;
}
// Returns 'value' rounded up to the nearest multiple of 'factor' when factor
// is a power of two.
// The result is undefined on overflow, i.e. if `value > 2**64 - factor`,
// since we cannot return the correct result which would be 2**64.
constexpr int64_t RoundUpToPowerOf2(int64_t value, int64_t factor) {
return (value + (factor - 1)) & ~(factor - 1);
}
constexpr uint64_t RoundUpToPowerOf2(uint64_t value, uint64_t factor) {
return (value + (factor - 1)) & ~(factor - 1);
}
constexpr int64_t RoundUpToMultipleOf8(int64_t num) { return RoundUpToPowerOf2(num, 8); }
constexpr int64_t RoundUpToMultipleOf64(int64_t num) {
return RoundUpToPowerOf2(num, 64);
}
// Returns the number of bytes covering a sliced bitmap. Find the length
// rounded to cover full bytes on both extremities.
//
// The following example represents a slice (offset=10, length=9)
//
// 0 8 16 24
// |-------|-------|------|
// [ ] (slice)
// [ ] (same slice aligned to bytes bounds, length=16)
//
// The covering bytes is the length (in bytes) of this new aligned slice.
constexpr int64_t CoveringBytes(int64_t offset, int64_t length) {
return (bit_util::RoundUp(length + offset, 8) - bit_util::RoundDown(offset, 8)) / 8;
}
// Returns the 'num_bits' least-significant bits of 'v'.
static inline uint64_t TrailingBits(uint64_t v, int num_bits) {
if (ARROW_PREDICT_FALSE(num_bits == 0)) return 0;
if (ARROW_PREDICT_FALSE(num_bits >= 64)) return v;
int n = 64 - num_bits;
return (v << n) >> n;
}
// Returns ceil(log2(x)).
static inline int Log2(uint64_t x) {
// DCHECK_GT(x, 0);
// TODO: We can remove this condition once CRAN upgrades its macOS
// SDK from 11.3.
// __apple_build_version__ should be defined only on Apple clang
#if defined(__apple_build_version__) && !defined(__cpp_lib_bitops)
return std::log2p1(x - 1);
#else
return std::bit_width(x - 1);
#endif
}
//
// Utilities for reading and writing individual bits by their index
// in a memory area.
//
// Bitmask selecting the k-th bit in a byte
static constexpr uint8_t kBitmask[] = {1, 2, 4, 8, 16, 32, 64, 128};
// the bitwise complement version of kBitmask
static constexpr uint8_t kFlippedBitmask[] = {254, 253, 251, 247, 239, 223, 191, 127};
// Bitmask selecting the (k - 1) preceding bits in a byte
static constexpr uint8_t kPrecedingBitmask[] = {0, 1, 3, 7, 15, 31, 63, 127};
static constexpr uint8_t kPrecedingWrappingBitmask[] = {255, 1, 3, 7, 15, 31, 63, 127};
// the bitwise complement version of kPrecedingBitmask
static constexpr uint8_t kTrailingBitmask[] = {255, 254, 252, 248, 240, 224, 192, 128};
static constexpr bool GetBit(const uint8_t* bits, uint64_t i) {
return (bits[i >> 3] >> (i & 0x07)) & 1;
}
// Gets the i-th bit from a byte. Should only be used with i <= 7.
static constexpr bool GetBitFromByte(uint8_t byte, uint8_t i) {
return byte & kBitmask[i];
}
template <typename Uint>
struct CopyBitsParams {
Uint src = {};
Uint dst = {};
int start = {};
int end = {};
};
/// Copy a contiguous span of bits from src into dst.
///
/// Copy bits [start, end[ from src into the position [start, end[ in dst
/// and return the result (inputs are unmodified).
/// Setting ``kAllowFullCopy`` to false is an optimization when the caller can
/// guarantee that the range of bits to copy does not cover the whole range.
template <typename Uint, bool kAllowFullCopy = true>
[[nodiscard]] constexpr Uint CopyBitsInInteger(const CopyBitsParams<Uint>& params) {
constexpr auto kUintSizeBits = static_cast<int>(sizeof(Uint) * 8);
assert(params.start <= params.end);
assert(params.start < kUintSizeBits);
assert(params.end <= kUintSizeBits);
const int length = params.end - params.start;
if constexpr (kAllowFullCopy) {
if (length == kUintSizeBits) {
return params.src;
}
}
assert(length < kUintSizeBits);
const Uint mask =
static_cast<Uint>(LeastSignificantBitMask<Uint, false>(length) << params.start);
return (~mask & params.dst) | (mask & params.src);
}
static inline void ClearBit(uint8_t* bits, int64_t i) {
bits[i / 8] &= kFlippedBitmask[i % 8];
}
static inline void SetBit(uint8_t* bits, int64_t i) { bits[i / 8] |= kBitmask[i % 8]; }
static inline void SetBitTo(uint8_t* bits, int64_t i, bool bit_is_set) {
// https://graphics.stanford.edu/~seander/bithacks.html
// "Conditionally set or clear bits without branching"
// NOTE: this seems to confuse Valgrind as it reads from potentially
// uninitialized memory
bits[i / 8] ^= static_cast<uint8_t>(-static_cast<uint8_t>(bit_is_set) ^ bits[i / 8]) &
kBitmask[i % 8];
}
/// \brief set or clear a range of bits quickly
ARROW_EXPORT
void SetBitsTo(uint8_t* bits, int64_t start_offset, int64_t length, bool bits_are_set);
/// \brief Sets all bits in the bitmap to true
ARROW_EXPORT
void SetBitmap(uint8_t* data, int64_t offset, int64_t length);
/// \brief Clears all bits in the bitmap (set to false)
ARROW_EXPORT
void ClearBitmap(uint8_t* data, int64_t offset, int64_t length);
/// Returns a mask with lower i bits set to 1. If i >= sizeof(Word)*8, all-ones will be
/// returned
/// ex:
/// ref: https://stackoverflow.com/a/59523400
template <typename Word>
constexpr Word PrecedingWordBitmask(const unsigned int i) {
return static_cast<Word>(static_cast<Word>(i < sizeof(Word) * 8)
<< (i & (sizeof(Word) * 8 - 1))) -
1;
}
static_assert(PrecedingWordBitmask<uint8_t>(0) == 0x00, "");
static_assert(PrecedingWordBitmask<uint8_t>(4) == 0x0f, "");
static_assert(PrecedingWordBitmask<uint8_t>(8) == 0xff, "");
static_assert(PrecedingWordBitmask<uint16_t>(8) == 0x00ff, "");
/// \brief Create a word with low `n` bits from `low` and high `sizeof(Word)-n` bits
/// from `high`.
/// Word ret
/// for (i = 0; i < sizeof(Word)*8; i++){
/// ret[i]= i < n ? low[i]: high[i];
/// }
template <typename Word>
constexpr Word SpliceWord(int n, Word low, Word high) {
return (high & ~PrecedingWordBitmask<Word>(n)) | (low & PrecedingWordBitmask<Word>(n));
}
/// \brief Pack integers into a bitmap in batches of 8
template <int batch_size>
void PackBits(const uint32_t* values, uint8_t* out) {
for (int i = 0; i < batch_size / 8; ++i) {
*out++ = static_cast<uint8_t>(values[0] | values[1] << 1 | values[2] << 2 |
values[3] << 3 | values[4] << 4 | values[5] << 5 |
values[6] << 6 | values[7] << 7);
values += 8;
}
}
constexpr int32_t MaxLEB128ByteLen(int32_t n_bits) {
return static_cast<int32_t>(CeilDiv(n_bits, 7));
}
template <typename Int>
constexpr int32_t kMaxLEB128ByteLenFor = MaxLEB128ByteLen(sizeof(Int) * 8);
/// Write a integer as LEB128
///
/// Write the input value as LEB128 into the outptut buffer and return the number of bytes
/// written.
/// If the output buffer size is insufficient, return 0 but the output may have been
/// written to.
/// The input value can be a signed integer, but must be non negative.
///
/// \see https://en.wikipedia.org/wiki/LEB128
/// \see MaxLEB128ByteLenFor
template <typename Int>
constexpr int32_t WriteLEB128(Int value, uint8_t* out, int32_t max_out_size) {
constexpr Int kLow7Mask = Int(0x7F);
constexpr Int kHigh7Mask = ~kLow7Mask;
constexpr uint8_t kContinuationBit = 0x80;
// This encoding does not work for negative values
if constexpr (std::is_signed_v<Int>) {
if (ARROW_PREDICT_FALSE(value < 0)) {
return 0;
}
}
const auto out_first = out;
// Write as many bytes as we could be for the given input
while ((value & kHigh7Mask) != Int(0)) {
// We do not have enough room to write the LEB128
if (ARROW_PREDICT_FALSE(out - out_first >= max_out_size)) {
return 0;
}
// Write the encoded byte with continuation bit
*out = static_cast<uint8_t>(value & kLow7Mask) | kContinuationBit;
++out;
// Shift remaining data
value >>= 7;
}
// We do not have enough room to write the LEB128
if (ARROW_PREDICT_FALSE(out - out_first >= max_out_size)) {
return 0;
}
// Write last non-continuing byte
*out = static_cast<uint8_t>(value & kLow7Mask);
++out;
return static_cast<int32_t>(out - out_first);
}
/// Parse a leading LEB128
///
/// Take as input a data pointer and the maximum number of bytes that can be read from it
/// (typically the array size).
/// When a valid LEB128 is found at the start of the data, the function writes it to the
/// out pointer and return the number of bytes read.
/// Otherwise, the out pointer is unmodified and zero is returned.
///
/// \see https://en.wikipedia.org/wiki/LEB128
/// \see MaxLEB128ByteLenFor
template <typename Int>
constexpr int32_t ParseLeadingLEB128(const uint8_t* data, int32_t max_data_size,
Int* out) {
constexpr auto kMaxBytes = kMaxLEB128ByteLenFor<Int>;
static_assert(kMaxBytes >= 1);
constexpr uint8_t kLow7Mask = 0x7F;
constexpr uint8_t kContinuationBit = 0x80;
constexpr int32_t kSignBitCount = std::is_signed_v<Int> ? 1 : 0;
// Number of bits allowed for encoding data on the last byte to avoid overflow
constexpr uint8_t kHighBitCount = (8 * sizeof(Int) - kSignBitCount) % 7;
// kHighBitCount least significant `0` bits and the rest with `1`
constexpr uint8_t kHighForbiddenMask = ~((1 << kHighBitCount) - 1);
// Iteratively building the value
std::make_unsigned_t<Int> value = 0;
// Read as many bytes as we could be for the given output.
for (int32_t i = 0; i < kMaxBytes - 1; i++) {
// We have not finished reading a valid LEB128, yet we run out of data
if (ARROW_PREDICT_FALSE(i >= max_data_size)) {
return 0;
}
// Read the byte and set its 7 LSB to in the final value
const uint8_t byte = data[i];
value |= static_cast<Int>(byte & kLow7Mask) << (7 * i);
// Check for lack of continuation flag in MSB
if ((byte & kContinuationBit) == 0) {
*out = value;
return i + 1;
}
}
// Process the last index avoiding overflowing
constexpr int32_t last = kMaxBytes - 1;
// We have not finished reading a valid LEB128, yet we run out of data
if (ARROW_PREDICT_FALSE(last >= max_data_size)) {
return 0;
}
const uint8_t byte = data[last];
// Need to check if there are bits that would overflow the output.
// Also checks that there is no continuation.
if (ARROW_PREDICT_FALSE((byte & kHighForbiddenMask) != 0)) {
return 0;
}
// No longer need to mask since we ensured
value |= static_cast<Int>(byte) << (7 * last);
*out = value;
return last + 1;
}
} // namespace bit_util
} // namespace arrow