forked from mysql/mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverflow_bitset.cc
146 lines (129 loc) · 5.13 KB
/
overflow_bitset.cc
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
/* Copyright (c) 2021, 2024, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include "sql/join_optimizer/overflow_bitset.h"
#include <stdint.h>
#include <string.h>
#include <bit>
#include "my_alloc.h"
#include "template_utils.h"
void OverflowBitset::InitOverflow(MEM_ROOT *mem_root, size_t capacity) {
size_t num_blocks = (capacity + 63) / 64;
m_ext = pointer_cast<Ext *>(
mem_root->Alloc(sizeof(Ext) + sizeof(uint64_t) * (num_blocks - 1)));
m_ext->m_num_blocks = num_blocks;
memset(m_ext->m_bits, 0, sizeof(uint64_t) * num_blocks);
assert(!is_inline());
}
MutableOverflowBitset OverflowBitset::OrOverflow(MEM_ROOT *mem_root,
OverflowBitset a,
OverflowBitset b) {
assert(!a.is_inline());
assert(!b.is_inline());
assert(a.capacity() == b.capacity());
MutableOverflowBitset ret{mem_root, a.capacity()};
for (unsigned i = 0; i < a.m_ext->m_num_blocks; ++i) {
ret.m_ext->m_bits[i] = a.m_ext->m_bits[i] | b.m_ext->m_bits[i];
}
return ret;
}
MutableOverflowBitset OverflowBitset::AndOverflow(MEM_ROOT *mem_root,
OverflowBitset a,
OverflowBitset b) {
assert(!a.is_inline());
assert(!b.is_inline());
assert(a.capacity() == b.capacity());
MutableOverflowBitset ret{mem_root, a.capacity()};
for (unsigned i = 0; i < a.m_ext->m_num_blocks; ++i) {
ret.m_ext->m_bits[i] = a.m_ext->m_bits[i] & b.m_ext->m_bits[i];
}
return ret;
}
MutableOverflowBitset OverflowBitset::XorOverflow(MEM_ROOT *mem_root,
OverflowBitset a,
OverflowBitset b) {
assert(!a.is_inline());
assert(!b.is_inline());
assert(a.capacity() == b.capacity());
MutableOverflowBitset ret{mem_root, a.capacity()};
for (unsigned i = 0; i < a.m_ext->m_num_blocks; ++i) {
ret.m_ext->m_bits[i] = a.m_ext->m_bits[i] ^ b.m_ext->m_bits[i];
}
return ret;
}
void MutableOverflowBitset::ClearBitsOverflow(int begin_bit_num,
int end_bit_num) {
assert(!is_inline());
assert(begin_bit_num >= 0);
assert(end_bit_num >= 0);
assert(begin_bit_num <= end_bit_num);
assert(static_cast<size_t>(begin_bit_num) <= capacity());
assert(static_cast<size_t>(end_bit_num) <= capacity());
if (begin_bit_num / 64 == end_bit_num / 64) {
// Begin and end are in the same block.
m_ext->m_bits[begin_bit_num / 64] &=
~BitsBetween(begin_bit_num % 64, end_bit_num % 64);
return;
}
// Schematically, where x is untouched bits and 0 are the bits to clear
// (shown here with 8-bit blocks instead of 64 for brevity):
//
// xxxxx000 [ 00000000 00000000 ... ] 00000xxx
if (begin_bit_num % 64 != 0) {
m_ext->m_bits[begin_bit_num / 64] &= ~BitsBetween(begin_bit_num % 64, 64);
begin_bit_num += 64 - (begin_bit_num % 64);
}
if (end_bit_num % 64 != 0) {
m_ext->m_bits[end_bit_num / 64] &= ~BitsBetween(0, end_bit_num % 64);
end_bit_num &= ~63;
}
for (int block_num = begin_bit_num / 64; block_num < end_bit_num / 64;
++block_num) {
m_ext->m_bits[block_num] = 0;
}
}
bool OverlapsOverflow(OverflowBitset a, OverflowBitset b) {
assert(!a.is_inline());
assert(!b.is_inline());
assert(a.capacity() == b.capacity());
for (unsigned i = 0; i < a.m_ext->m_num_blocks; ++i) {
if (Overlaps(a.m_ext->m_bits[i], b.m_ext->m_bits[i])) {
return true;
}
}
return false;
}
bool IsSubsetOverflow(OverflowBitset a, OverflowBitset b) {
assert(!a.is_inline());
assert(!b.is_inline());
assert(a.capacity() == b.capacity());
for (unsigned i = 0; i < a.m_ext->m_num_blocks; ++i) {
if (!IsSubset(a.m_ext->m_bits[i], b.m_ext->m_bits[i])) {
return false;
}
}
return true;
}
int PopulationCountOverflow(OverflowBitset x) {
assert(!x.is_inline());
int count = 0;
for (unsigned i = 0; i < x.m_ext->m_num_blocks; ++i) {
count += std::popcount(x.m_ext->m_bits[i]);
}
return count;
}