-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFixedBitSet.cpp
More file actions
307 lines (255 loc) · 9.02 KB
/
FixedBitSet.cpp
File metadata and controls
307 lines (255 loc) · 9.02 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
//
// Created by cai on 2020-09-24.
//
#include "FixedBitSet.h"
#include <stdexcept>
#include <algorithm>
#include <iostream>
#ifdef __AVX2__
#include <immintrin.h>
#endif
#ifdef SAFETY_CHECKS
namespace {
const std::string OUT_OF_SPACE = "BitSet doesn't have enough capacity left!!";
const std::string OUT_OF_BOUND = "BitSet operation out of bound!";
const std::string SIZE_NOT_MATCHED = "FixedBitSets should have the same maxSize.";
}
#endif
typedef boost::dynamic_bitset<unsigned long, std::allocator<unsigned long>> db;
FixedBitSet::FixedBitSet(int maxSize)
: maxSize(maxSize) {
// allocate one more block for safety concerns
auto actualSize = maxSize + db::bits_per_block;
bs = boost::dynamic_bitset<>(actualSize);
}
void FixedBitSet::removeFirst(int nbits) {
nbits = std::min(nbits, length);
start = (maxSize - start > nbits) ? start + nbits : maxSize;
length = std::max(0, length - nbits);
if (start == maxSize) {
trim();
}
}
void FixedBitSet::append(int nbits, bool value) {
#ifdef SAFETY_CHECKS
check(maxSize - length >= nbits, OUT_OF_SPACE);
#endif
if (maxSize - length - start < nbits) {
trim();
}
if (value) {
bs.set(start + length, nbits, true);
}
length += nbits;
}
void FixedBitSet::append1101() {
if (maxSize - length - start < 4) {
trim();
}
bs.set(start + length);
bs.set(start + length + 1);
bs.set(start + length + 3);
length += 4;
}
void FixedBitSet::append00() {
if (maxSize - length - start < 2) {
trim();
}
length += 2;
}
void FixedBitSet::clear(int i) {
#ifdef SAFETY_CHECKS
check(i < length, OUT_OF_BOUND);
#endif
bs.reset(start + i);
}
void FixedBitSet::set(int i) {
#ifdef SAFETY_CHECKS
check(i < length, OUT_OF_BOUND);
#endif
bs.set(start + i);
}
bool FixedBitSet::get(int i) {
#ifdef SAFETY_CHECKS
check(i < length, OUT_OF_BOUND);
#endif
return bs[start + i];
}
// this is a very expensive function
void FixedBitSet::trim() {
if (start == 0) return;
// removes everything before the actual start
bs >>= start;
start = 0;
}
// created by modifying the code of operator >>= in dynamic_bitset
void FixedBitSet::replaceBy(FixedBitSet &other) {
#ifdef SAFETY_CHECKS
check(maxSize == other.maxSize, SIZE_NOT_MATCHED);
#endif
auto clearStart = 0UL;
auto *const b1 = &(bs.m_bits[0]);
if (other.length > 0) {
auto const last2 = (other.start + other.length - 1) / db::bits_per_block;
auto const div2 = other.start / db::bits_per_block;
auto const r2 = other.start % db::bits_per_block;
auto *const b2 = &(other.bs.m_bits[0]);
clearStart = last2 - div2 + 1;
if (r2 != 0) {
auto const ls2 = db::bits_per_block - r2;
// auto vectorization by compiler
for (auto j = div2; j < last2; ++j) {
b1[j-div2] = (b2[j] >> r2) | (b2[j+1] << ls2);
}
b1[last2-div2] = b2[last2] >> r2;
} else {
memcpy(b1, b2 + div2, sizeof(unsigned long) * clearStart);
}
}
// reset the extra bits
if (length > 0 || start > 0) {
auto const last1 = (start + length - 1) / db::bits_per_block;
if (clearStart <= last1) {
memset(b1 + clearStart, 0, sizeof(unsigned long) * (last1 - clearStart + 1));
}
}
start = 0;
length = other.length;
}
// created by modifying the code of operator >>= in dynamic_bitset
bool operator==(const FixedBitSet &lhs, const FixedBitSet &rhs) {
if (lhs.length != rhs.length) return false;
if (lhs.length == 0) return true; // both are empty
auto &bs1 = lhs.bs;
auto &bs2 = rhs.bs;
// length > 0 implies start <= start + length - 1
auto last1 = (lhs.start + lhs.length - 1) / db::bits_per_block;
auto last2 = (rhs.start + rhs.length - 1) / db::bits_per_block;
auto const div1 = lhs.start / db::bits_per_block;
auto const div2 = rhs.start / db::bits_per_block;
auto const r1 = lhs.start % db::bits_per_block;
auto const r2 = rhs.start % db::bits_per_block;
auto *const b1 = &(bs1.m_bits[0]);
auto *const b2 = &(bs2.m_bits[0]);
// safe because maxSize cannot exceed INT_MAX
long rangeDiff = (last1 - div1) - (last2 - div2);
if (rangeDiff == -1L) {
++last1;
} else if (rangeDiff == 1L) {
++last2;
} else if (rangeDiff != 0L) {
throw std::runtime_error("FixedBitSet comparison assertion failed.");
}
if (r1 != 0 && r2 == 0) {
auto const ls1 = db::bits_per_block - r1;
auto i = div1;
auto j = div2;
#ifdef __AVX2__
if (last1 >= 4) {
// avoid unsigned long overflow
for (; i < last1 - 3; i += 4, j += 4) {
auto word1a = _mm256_lddqu_si256(reinterpret_cast<const __m256i *>(b1 + i));
auto word1b = _mm256_lddqu_si256(reinterpret_cast<const __m256i *>(b1 + i + 1));
auto word1 = _mm256_or_si256(
_mm256_srli_epi64(word1a, (int)r1),
_mm256_slli_epi64(word1b, (int)ls1)
);
auto word2 = _mm256_lddqu_si256(reinterpret_cast<const __m256i *>(b2 + j));
auto cmpResult = _mm256_cmpeq_epi64(word1, word2);
if (_mm256_movemask_epi8(cmpResult) != 0xffffffffU) return false;
}
}
#endif
for (; i < last1; ++i, ++j) {
auto word1 = (b1[i] >> r1) | (b1[i+1] << ls1);
if (word1 != b2[j]) return false;
}
if ((b1[last1] >> r1) != b2[last2]) return false;
} else if (r1 != 0) {
auto const ls1 = db::bits_per_block - r1;
auto const ls2 = db::bits_per_block - r2;
auto i = div1;
auto j = div2;
#ifdef __AVX2__
if (last1 >= 4) {
// avoid unsigned long overflow
for (; i < last1 - 3; i += 4, j += 4) {
auto word1a = _mm256_lddqu_si256(reinterpret_cast<const __m256i *>(b1 + i));
auto word1b = _mm256_lddqu_si256(reinterpret_cast<const __m256i *>(b1 + i + 1));
auto word1 = _mm256_or_si256(
_mm256_srli_epi64(word1a, (int)r1),
_mm256_slli_epi64(word1b, (int)ls1)
);
auto word2a = _mm256_lddqu_si256(reinterpret_cast<const __m256i *>(b2 + j));
auto word2b = _mm256_lddqu_si256(reinterpret_cast<const __m256i *>(b2 + j + 1));
auto word2 = _mm256_or_si256(
_mm256_srli_epi64(word2a, (int)r2),
_mm256_slli_epi64(word2b, (int)ls2)
);
auto cmpResult = _mm256_cmpeq_epi64(word1, word2);
if (_mm256_movemask_epi8(cmpResult) != 0xffffffffU) return false;
}
}
#endif
for (; i < last1; ++i, ++j) {
auto word1 = (b1[i] >> r1) | (b1[i+1] << ls1);
auto word2 = (b2[j] >> r2) | (b2[j+1] << ls2);
if (word1 != word2) return false;
}
if ((b1[last1] >> r1) != (b2[last2] >> r2)) return false;
} else if (r2 == 0) {
int result = memcmp(b1 + div1, b2 + div2, sizeof(unsigned long) * (last1 - div1 + 1));
if (result != 0) return false;
} else { // r2 != 0
auto const ls2 = db::bits_per_block - r2;
auto i = div1;
auto j = div2;
#ifdef __AVX2__
if (last1 >= 4) {
// avoid unsigned long overflow
for (; i < last1 - 3; i += 4, j += 4) {
auto word2a = _mm256_lddqu_si256(reinterpret_cast<const __m256i *>(b2 + j));
auto word2b = _mm256_lddqu_si256(reinterpret_cast<const __m256i *>(b2 + j + 1));
auto word2 = _mm256_or_si256(
_mm256_srli_epi64(word2a, (int)r2),
_mm256_slli_epi64(word2b, (int)ls2)
);
auto word1 = _mm256_lddqu_si256(reinterpret_cast<const __m256i *>(b1 + i));
auto cmpResult = _mm256_cmpeq_epi64(word1, word2);
if (_mm256_movemask_epi8(cmpResult) != 0xffffffffU) return false;
}
}
#endif
for (; i < last1; ++i, ++j) {
auto word2 = (b2[j] >> r2) | (b2[j+1] << ls2);
if (b1[i] != word2) return false;
}
if (b1[last1] != (b2[last2] >> r2)) return false;
}
return true;
}
// for debug only
std::ostream &operator<<(std::ostream &out, const FixedBitSet &fbs) {
std::stringstream ss;
for (int i = fbs.start; i < fbs.start + fbs.length; ++i) {
if (fbs.bs[i]) {
ss << '1';
} else {
ss << '0';
}
}
return out << ss.str();
}
#ifdef SAFETY_CHECKS
void FixedBitSet::check(bool value, const std::string &errorMsg) const {
if (!value) {
std::stringstream ss;
ss << errorMsg << "\n Details: "
<< "maxSize=" << maxSize
<< ", start=" << start
<< ", length: " << length;
// << ", cardinality: " << cardinality;
throw std::invalid_argument(ss.str());
}
}
#endif