]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/cpp/src/arrow/util/bitmap_generate.h
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / cpp / src / arrow / util / bitmap_generate.h
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
18 #pragma once
19
20 #include <cstdint>
21 #include <memory>
22
23 #include "arrow/buffer.h"
24 #include "arrow/memory_pool.h"
25 #include "arrow/result.h"
26 #include "arrow/util/bit_util.h"
27 #include "arrow/util/visibility.h"
28
29 namespace arrow {
30 namespace internal {
31
32 // A std::generate() like function to write sequential bits into a bitmap area.
33 // Bits preceding the bitmap area are preserved, bits following the bitmap
34 // area may be clobbered.
35
36 template <class Generator>
37 void GenerateBits(uint8_t* bitmap, int64_t start_offset, int64_t length, Generator&& g) {
38 if (length == 0) {
39 return;
40 }
41 uint8_t* cur = bitmap + start_offset / 8;
42 uint8_t bit_mask = BitUtil::kBitmask[start_offset % 8];
43 uint8_t current_byte = *cur & BitUtil::kPrecedingBitmask[start_offset % 8];
44
45 for (int64_t index = 0; index < length; ++index) {
46 const bool bit = g();
47 current_byte = bit ? (current_byte | bit_mask) : current_byte;
48 bit_mask = static_cast<uint8_t>(bit_mask << 1);
49 if (bit_mask == 0) {
50 bit_mask = 1;
51 *cur++ = current_byte;
52 current_byte = 0;
53 }
54 }
55 if (bit_mask != 1) {
56 *cur++ = current_byte;
57 }
58 }
59
60 // Like GenerateBits(), but unrolls its main loop for higher performance.
61
62 template <class Generator>
63 void GenerateBitsUnrolled(uint8_t* bitmap, int64_t start_offset, int64_t length,
64 Generator&& g) {
65 static_assert(std::is_same<decltype(std::declval<Generator>()()), bool>::value,
66 "Functor passed to GenerateBitsUnrolled must return bool");
67
68 if (length == 0) {
69 return;
70 }
71 uint8_t current_byte;
72 uint8_t* cur = bitmap + start_offset / 8;
73 const uint64_t start_bit_offset = start_offset % 8;
74 uint8_t bit_mask = BitUtil::kBitmask[start_bit_offset];
75 int64_t remaining = length;
76
77 if (bit_mask != 0x01) {
78 current_byte = *cur & BitUtil::kPrecedingBitmask[start_bit_offset];
79 while (bit_mask != 0 && remaining > 0) {
80 current_byte |= g() * bit_mask;
81 bit_mask = static_cast<uint8_t>(bit_mask << 1);
82 --remaining;
83 }
84 *cur++ = current_byte;
85 }
86
87 int64_t remaining_bytes = remaining / 8;
88 uint8_t out_results[8];
89 while (remaining_bytes-- > 0) {
90 for (int i = 0; i < 8; ++i) {
91 out_results[i] = g();
92 }
93 *cur++ = (out_results[0] | out_results[1] << 1 | out_results[2] << 2 |
94 out_results[3] << 3 | out_results[4] << 4 | out_results[5] << 5 |
95 out_results[6] << 6 | out_results[7] << 7);
96 }
97
98 int64_t remaining_bits = remaining % 8;
99 if (remaining_bits) {
100 current_byte = 0;
101 bit_mask = 0x01;
102 while (remaining_bits-- > 0) {
103 current_byte |= g() * bit_mask;
104 bit_mask = static_cast<uint8_t>(bit_mask << 1);
105 }
106 *cur++ = current_byte;
107 }
108 }
109
110 } // namespace internal
111 } // namespace arrow