annotate libsanitizer/sanitizer_common/sanitizer_bitvector.h @ 118:fd00160c1b76

ifdef TARGET_64BIT
author mir3636
date Tue, 27 Feb 2018 15:01:35 +0900
parents 04ced10e8804
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 //===-- sanitizer_bitvector.h -----------------------------------*- C++ -*-===//
kono
parents:
diff changeset
2 //
kono
parents:
diff changeset
3 // This file is distributed under the University of Illinois Open Source
kono
parents:
diff changeset
4 // License. See LICENSE.TXT for details.
kono
parents:
diff changeset
5 //
kono
parents:
diff changeset
6 //===----------------------------------------------------------------------===//
kono
parents:
diff changeset
7 //
kono
parents:
diff changeset
8 // Specializer BitVector implementation.
kono
parents:
diff changeset
9 //
kono
parents:
diff changeset
10 //===----------------------------------------------------------------------===//
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 #ifndef SANITIZER_BITVECTOR_H
kono
parents:
diff changeset
13 #define SANITIZER_BITVECTOR_H
kono
parents:
diff changeset
14
kono
parents:
diff changeset
15 #include "sanitizer_common.h"
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 namespace __sanitizer {
kono
parents:
diff changeset
18
kono
parents:
diff changeset
19 // Fixed size bit vector based on a single basic integer.
kono
parents:
diff changeset
20 template <class basic_int_t = uptr>
kono
parents:
diff changeset
21 class BasicBitVector {
kono
parents:
diff changeset
22 public:
kono
parents:
diff changeset
23 enum SizeEnum { kSize = sizeof(basic_int_t) * 8 };
kono
parents:
diff changeset
24
kono
parents:
diff changeset
25 uptr size() const { return kSize; }
kono
parents:
diff changeset
26 // No CTOR.
kono
parents:
diff changeset
27 void clear() { bits_ = 0; }
kono
parents:
diff changeset
28 void setAll() { bits_ = ~(basic_int_t)0; }
kono
parents:
diff changeset
29 bool empty() const { return bits_ == 0; }
kono
parents:
diff changeset
30
kono
parents:
diff changeset
31 // Returns true if the bit has changed from 0 to 1.
kono
parents:
diff changeset
32 bool setBit(uptr idx) {
kono
parents:
diff changeset
33 basic_int_t old = bits_;
kono
parents:
diff changeset
34 bits_ |= mask(idx);
kono
parents:
diff changeset
35 return bits_ != old;
kono
parents:
diff changeset
36 }
kono
parents:
diff changeset
37
kono
parents:
diff changeset
38 // Returns true if the bit has changed from 1 to 0.
kono
parents:
diff changeset
39 bool clearBit(uptr idx) {
kono
parents:
diff changeset
40 basic_int_t old = bits_;
kono
parents:
diff changeset
41 bits_ &= ~mask(idx);
kono
parents:
diff changeset
42 return bits_ != old;
kono
parents:
diff changeset
43 }
kono
parents:
diff changeset
44
kono
parents:
diff changeset
45 bool getBit(uptr idx) const { return (bits_ & mask(idx)) != 0; }
kono
parents:
diff changeset
46
kono
parents:
diff changeset
47 uptr getAndClearFirstOne() {
kono
parents:
diff changeset
48 CHECK(!empty());
kono
parents:
diff changeset
49 uptr idx = LeastSignificantSetBitIndex(bits_);
kono
parents:
diff changeset
50 clearBit(idx);
kono
parents:
diff changeset
51 return idx;
kono
parents:
diff changeset
52 }
kono
parents:
diff changeset
53
kono
parents:
diff changeset
54 // Do "this |= v" and return whether new bits have been added.
kono
parents:
diff changeset
55 bool setUnion(const BasicBitVector &v) {
kono
parents:
diff changeset
56 basic_int_t old = bits_;
kono
parents:
diff changeset
57 bits_ |= v.bits_;
kono
parents:
diff changeset
58 return bits_ != old;
kono
parents:
diff changeset
59 }
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61 // Do "this &= v" and return whether any bits have been removed.
kono
parents:
diff changeset
62 bool setIntersection(const BasicBitVector &v) {
kono
parents:
diff changeset
63 basic_int_t old = bits_;
kono
parents:
diff changeset
64 bits_ &= v.bits_;
kono
parents:
diff changeset
65 return bits_ != old;
kono
parents:
diff changeset
66 }
kono
parents:
diff changeset
67
kono
parents:
diff changeset
68 // Do "this &= ~v" and return whether any bits have been removed.
kono
parents:
diff changeset
69 bool setDifference(const BasicBitVector &v) {
kono
parents:
diff changeset
70 basic_int_t old = bits_;
kono
parents:
diff changeset
71 bits_ &= ~v.bits_;
kono
parents:
diff changeset
72 return bits_ != old;
kono
parents:
diff changeset
73 }
kono
parents:
diff changeset
74
kono
parents:
diff changeset
75 void copyFrom(const BasicBitVector &v) { bits_ = v.bits_; }
kono
parents:
diff changeset
76
kono
parents:
diff changeset
77 // Returns true if 'this' intersects with 'v'.
kono
parents:
diff changeset
78 bool intersectsWith(const BasicBitVector &v) const {
kono
parents:
diff changeset
79 return (bits_ & v.bits_) != 0;
kono
parents:
diff changeset
80 }
kono
parents:
diff changeset
81
kono
parents:
diff changeset
82 // for (BasicBitVector<>::Iterator it(bv); it.hasNext();) {
kono
parents:
diff changeset
83 // uptr idx = it.next();
kono
parents:
diff changeset
84 // use(idx);
kono
parents:
diff changeset
85 // }
kono
parents:
diff changeset
86 class Iterator {
kono
parents:
diff changeset
87 public:
kono
parents:
diff changeset
88 Iterator() { }
kono
parents:
diff changeset
89 explicit Iterator(const BasicBitVector &bv) : bv_(bv) {}
kono
parents:
diff changeset
90 bool hasNext() const { return !bv_.empty(); }
kono
parents:
diff changeset
91 uptr next() { return bv_.getAndClearFirstOne(); }
kono
parents:
diff changeset
92 void clear() { bv_.clear(); }
kono
parents:
diff changeset
93 private:
kono
parents:
diff changeset
94 BasicBitVector bv_;
kono
parents:
diff changeset
95 };
kono
parents:
diff changeset
96
kono
parents:
diff changeset
97 private:
kono
parents:
diff changeset
98 basic_int_t mask(uptr idx) const {
kono
parents:
diff changeset
99 CHECK_LT(idx, size());
kono
parents:
diff changeset
100 return (basic_int_t)1UL << idx;
kono
parents:
diff changeset
101 }
kono
parents:
diff changeset
102 basic_int_t bits_;
kono
parents:
diff changeset
103 };
kono
parents:
diff changeset
104
kono
parents:
diff changeset
105 // Fixed size bit vector of (kLevel1Size*BV::kSize**2) bits.
kono
parents:
diff changeset
106 // The implementation is optimized for better performance on
kono
parents:
diff changeset
107 // sparse bit vectors, i.e. the those with few set bits.
kono
parents:
diff changeset
108 template <uptr kLevel1Size = 1, class BV = BasicBitVector<> >
kono
parents:
diff changeset
109 class TwoLevelBitVector {
kono
parents:
diff changeset
110 // This is essentially a 2-level bit vector.
kono
parents:
diff changeset
111 // Set bit in the first level BV indicates that there are set bits
kono
parents:
diff changeset
112 // in the corresponding BV of the second level.
kono
parents:
diff changeset
113 // This structure allows O(kLevel1Size) time for clear() and empty(),
kono
parents:
diff changeset
114 // as well fast handling of sparse BVs.
kono
parents:
diff changeset
115 public:
kono
parents:
diff changeset
116 enum SizeEnum { kSize = BV::kSize * BV::kSize * kLevel1Size };
kono
parents:
diff changeset
117 // No CTOR.
kono
parents:
diff changeset
118
kono
parents:
diff changeset
119 uptr size() const { return kSize; }
kono
parents:
diff changeset
120
kono
parents:
diff changeset
121 void clear() {
kono
parents:
diff changeset
122 for (uptr i = 0; i < kLevel1Size; i++)
kono
parents:
diff changeset
123 l1_[i].clear();
kono
parents:
diff changeset
124 }
kono
parents:
diff changeset
125
kono
parents:
diff changeset
126 void setAll() {
kono
parents:
diff changeset
127 for (uptr i0 = 0; i0 < kLevel1Size; i0++) {
kono
parents:
diff changeset
128 l1_[i0].setAll();
kono
parents:
diff changeset
129 for (uptr i1 = 0; i1 < BV::kSize; i1++)
kono
parents:
diff changeset
130 l2_[i0][i1].setAll();
kono
parents:
diff changeset
131 }
kono
parents:
diff changeset
132 }
kono
parents:
diff changeset
133
kono
parents:
diff changeset
134 bool empty() const {
kono
parents:
diff changeset
135 for (uptr i = 0; i < kLevel1Size; i++)
kono
parents:
diff changeset
136 if (!l1_[i].empty())
kono
parents:
diff changeset
137 return false;
kono
parents:
diff changeset
138 return true;
kono
parents:
diff changeset
139 }
kono
parents:
diff changeset
140
kono
parents:
diff changeset
141 // Returns true if the bit has changed from 0 to 1.
kono
parents:
diff changeset
142 bool setBit(uptr idx) {
kono
parents:
diff changeset
143 check(idx);
kono
parents:
diff changeset
144 uptr i0 = idx0(idx);
kono
parents:
diff changeset
145 uptr i1 = idx1(idx);
kono
parents:
diff changeset
146 uptr i2 = idx2(idx);
kono
parents:
diff changeset
147 if (!l1_[i0].getBit(i1)) {
kono
parents:
diff changeset
148 l1_[i0].setBit(i1);
kono
parents:
diff changeset
149 l2_[i0][i1].clear();
kono
parents:
diff changeset
150 }
kono
parents:
diff changeset
151 bool res = l2_[i0][i1].setBit(i2);
kono
parents:
diff changeset
152 // Printf("%s: %zd => %zd %zd %zd; %d\n", __func__,
kono
parents:
diff changeset
153 // idx, i0, i1, i2, res);
kono
parents:
diff changeset
154 return res;
kono
parents:
diff changeset
155 }
kono
parents:
diff changeset
156
kono
parents:
diff changeset
157 bool clearBit(uptr idx) {
kono
parents:
diff changeset
158 check(idx);
kono
parents:
diff changeset
159 uptr i0 = idx0(idx);
kono
parents:
diff changeset
160 uptr i1 = idx1(idx);
kono
parents:
diff changeset
161 uptr i2 = idx2(idx);
kono
parents:
diff changeset
162 bool res = false;
kono
parents:
diff changeset
163 if (l1_[i0].getBit(i1)) {
kono
parents:
diff changeset
164 res = l2_[i0][i1].clearBit(i2);
kono
parents:
diff changeset
165 if (l2_[i0][i1].empty())
kono
parents:
diff changeset
166 l1_[i0].clearBit(i1);
kono
parents:
diff changeset
167 }
kono
parents:
diff changeset
168 return res;
kono
parents:
diff changeset
169 }
kono
parents:
diff changeset
170
kono
parents:
diff changeset
171 bool getBit(uptr idx) const {
kono
parents:
diff changeset
172 check(idx);
kono
parents:
diff changeset
173 uptr i0 = idx0(idx);
kono
parents:
diff changeset
174 uptr i1 = idx1(idx);
kono
parents:
diff changeset
175 uptr i2 = idx2(idx);
kono
parents:
diff changeset
176 // Printf("%s: %zd => %zd %zd %zd\n", __func__, idx, i0, i1, i2);
kono
parents:
diff changeset
177 return l1_[i0].getBit(i1) && l2_[i0][i1].getBit(i2);
kono
parents:
diff changeset
178 }
kono
parents:
diff changeset
179
kono
parents:
diff changeset
180 uptr getAndClearFirstOne() {
kono
parents:
diff changeset
181 for (uptr i0 = 0; i0 < kLevel1Size; i0++) {
kono
parents:
diff changeset
182 if (l1_[i0].empty()) continue;
kono
parents:
diff changeset
183 uptr i1 = l1_[i0].getAndClearFirstOne();
kono
parents:
diff changeset
184 uptr i2 = l2_[i0][i1].getAndClearFirstOne();
kono
parents:
diff changeset
185 if (!l2_[i0][i1].empty())
kono
parents:
diff changeset
186 l1_[i0].setBit(i1);
kono
parents:
diff changeset
187 uptr res = i0 * BV::kSize * BV::kSize + i1 * BV::kSize + i2;
kono
parents:
diff changeset
188 // Printf("getAndClearFirstOne: %zd %zd %zd => %zd\n", i0, i1, i2, res);
kono
parents:
diff changeset
189 return res;
kono
parents:
diff changeset
190 }
kono
parents:
diff changeset
191 CHECK(0);
kono
parents:
diff changeset
192 return 0;
kono
parents:
diff changeset
193 }
kono
parents:
diff changeset
194
kono
parents:
diff changeset
195 // Do "this |= v" and return whether new bits have been added.
kono
parents:
diff changeset
196 bool setUnion(const TwoLevelBitVector &v) {
kono
parents:
diff changeset
197 bool res = false;
kono
parents:
diff changeset
198 for (uptr i0 = 0; i0 < kLevel1Size; i0++) {
kono
parents:
diff changeset
199 BV t = v.l1_[i0];
kono
parents:
diff changeset
200 while (!t.empty()) {
kono
parents:
diff changeset
201 uptr i1 = t.getAndClearFirstOne();
kono
parents:
diff changeset
202 if (l1_[i0].setBit(i1))
kono
parents:
diff changeset
203 l2_[i0][i1].clear();
kono
parents:
diff changeset
204 if (l2_[i0][i1].setUnion(v.l2_[i0][i1]))
kono
parents:
diff changeset
205 res = true;
kono
parents:
diff changeset
206 }
kono
parents:
diff changeset
207 }
kono
parents:
diff changeset
208 return res;
kono
parents:
diff changeset
209 }
kono
parents:
diff changeset
210
kono
parents:
diff changeset
211 // Do "this &= v" and return whether any bits have been removed.
kono
parents:
diff changeset
212 bool setIntersection(const TwoLevelBitVector &v) {
kono
parents:
diff changeset
213 bool res = false;
kono
parents:
diff changeset
214 for (uptr i0 = 0; i0 < kLevel1Size; i0++) {
kono
parents:
diff changeset
215 if (l1_[i0].setIntersection(v.l1_[i0]))
kono
parents:
diff changeset
216 res = true;
kono
parents:
diff changeset
217 if (!l1_[i0].empty()) {
kono
parents:
diff changeset
218 BV t = l1_[i0];
kono
parents:
diff changeset
219 while (!t.empty()) {
kono
parents:
diff changeset
220 uptr i1 = t.getAndClearFirstOne();
kono
parents:
diff changeset
221 if (l2_[i0][i1].setIntersection(v.l2_[i0][i1]))
kono
parents:
diff changeset
222 res = true;
kono
parents:
diff changeset
223 if (l2_[i0][i1].empty())
kono
parents:
diff changeset
224 l1_[i0].clearBit(i1);
kono
parents:
diff changeset
225 }
kono
parents:
diff changeset
226 }
kono
parents:
diff changeset
227 }
kono
parents:
diff changeset
228 return res;
kono
parents:
diff changeset
229 }
kono
parents:
diff changeset
230
kono
parents:
diff changeset
231 // Do "this &= ~v" and return whether any bits have been removed.
kono
parents:
diff changeset
232 bool setDifference(const TwoLevelBitVector &v) {
kono
parents:
diff changeset
233 bool res = false;
kono
parents:
diff changeset
234 for (uptr i0 = 0; i0 < kLevel1Size; i0++) {
kono
parents:
diff changeset
235 BV t = l1_[i0];
kono
parents:
diff changeset
236 t.setIntersection(v.l1_[i0]);
kono
parents:
diff changeset
237 while (!t.empty()) {
kono
parents:
diff changeset
238 uptr i1 = t.getAndClearFirstOne();
kono
parents:
diff changeset
239 if (l2_[i0][i1].setDifference(v.l2_[i0][i1]))
kono
parents:
diff changeset
240 res = true;
kono
parents:
diff changeset
241 if (l2_[i0][i1].empty())
kono
parents:
diff changeset
242 l1_[i0].clearBit(i1);
kono
parents:
diff changeset
243 }
kono
parents:
diff changeset
244 }
kono
parents:
diff changeset
245 return res;
kono
parents:
diff changeset
246 }
kono
parents:
diff changeset
247
kono
parents:
diff changeset
248 void copyFrom(const TwoLevelBitVector &v) {
kono
parents:
diff changeset
249 clear();
kono
parents:
diff changeset
250 setUnion(v);
kono
parents:
diff changeset
251 }
kono
parents:
diff changeset
252
kono
parents:
diff changeset
253 // Returns true if 'this' intersects with 'v'.
kono
parents:
diff changeset
254 bool intersectsWith(const TwoLevelBitVector &v) const {
kono
parents:
diff changeset
255 for (uptr i0 = 0; i0 < kLevel1Size; i0++) {
kono
parents:
diff changeset
256 BV t = l1_[i0];
kono
parents:
diff changeset
257 t.setIntersection(v.l1_[i0]);
kono
parents:
diff changeset
258 while (!t.empty()) {
kono
parents:
diff changeset
259 uptr i1 = t.getAndClearFirstOne();
kono
parents:
diff changeset
260 if (!v.l1_[i0].getBit(i1)) continue;
kono
parents:
diff changeset
261 if (l2_[i0][i1].intersectsWith(v.l2_[i0][i1]))
kono
parents:
diff changeset
262 return true;
kono
parents:
diff changeset
263 }
kono
parents:
diff changeset
264 }
kono
parents:
diff changeset
265 return false;
kono
parents:
diff changeset
266 }
kono
parents:
diff changeset
267
kono
parents:
diff changeset
268 // for (TwoLevelBitVector<>::Iterator it(bv); it.hasNext();) {
kono
parents:
diff changeset
269 // uptr idx = it.next();
kono
parents:
diff changeset
270 // use(idx);
kono
parents:
diff changeset
271 // }
kono
parents:
diff changeset
272 class Iterator {
kono
parents:
diff changeset
273 public:
kono
parents:
diff changeset
274 Iterator() { }
kono
parents:
diff changeset
275 explicit Iterator(const TwoLevelBitVector &bv) : bv_(bv), i0_(0), i1_(0) {
kono
parents:
diff changeset
276 it1_.clear();
kono
parents:
diff changeset
277 it2_.clear();
kono
parents:
diff changeset
278 }
kono
parents:
diff changeset
279
kono
parents:
diff changeset
280 bool hasNext() const {
kono
parents:
diff changeset
281 if (it1_.hasNext()) return true;
kono
parents:
diff changeset
282 for (uptr i = i0_; i < kLevel1Size; i++)
kono
parents:
diff changeset
283 if (!bv_.l1_[i].empty()) return true;
kono
parents:
diff changeset
284 return false;
kono
parents:
diff changeset
285 }
kono
parents:
diff changeset
286
kono
parents:
diff changeset
287 uptr next() {
kono
parents:
diff changeset
288 // Printf("++++: %zd %zd; %d %d; size %zd\n", i0_, i1_, it1_.hasNext(),
kono
parents:
diff changeset
289 // it2_.hasNext(), kSize);
kono
parents:
diff changeset
290 if (!it1_.hasNext() && !it2_.hasNext()) {
kono
parents:
diff changeset
291 for (; i0_ < kLevel1Size; i0_++) {
kono
parents:
diff changeset
292 if (bv_.l1_[i0_].empty()) continue;
kono
parents:
diff changeset
293 it1_ = typename BV::Iterator(bv_.l1_[i0_]);
kono
parents:
diff changeset
294 // Printf("+i0: %zd %zd; %d %d; size %zd\n", i0_, i1_, it1_.hasNext(),
kono
parents:
diff changeset
295 // it2_.hasNext(), kSize);
kono
parents:
diff changeset
296 break;
kono
parents:
diff changeset
297 }
kono
parents:
diff changeset
298 }
kono
parents:
diff changeset
299 if (!it2_.hasNext()) {
kono
parents:
diff changeset
300 CHECK(it1_.hasNext());
kono
parents:
diff changeset
301 i1_ = it1_.next();
kono
parents:
diff changeset
302 it2_ = typename BV::Iterator(bv_.l2_[i0_][i1_]);
kono
parents:
diff changeset
303 // Printf("++i1: %zd %zd; %d %d; size %zd\n", i0_, i1_, it1_.hasNext(),
kono
parents:
diff changeset
304 // it2_.hasNext(), kSize);
kono
parents:
diff changeset
305 }
kono
parents:
diff changeset
306 CHECK(it2_.hasNext());
kono
parents:
diff changeset
307 uptr i2 = it2_.next();
kono
parents:
diff changeset
308 uptr res = i0_ * BV::kSize * BV::kSize + i1_ * BV::kSize + i2;
kono
parents:
diff changeset
309 // Printf("+ret: %zd %zd; %d %d; size %zd; res: %zd\n", i0_, i1_,
kono
parents:
diff changeset
310 // it1_.hasNext(), it2_.hasNext(), kSize, res);
kono
parents:
diff changeset
311 if (!it1_.hasNext() && !it2_.hasNext())
kono
parents:
diff changeset
312 i0_++;
kono
parents:
diff changeset
313 return res;
kono
parents:
diff changeset
314 }
kono
parents:
diff changeset
315
kono
parents:
diff changeset
316 private:
kono
parents:
diff changeset
317 const TwoLevelBitVector &bv_;
kono
parents:
diff changeset
318 uptr i0_, i1_;
kono
parents:
diff changeset
319 typename BV::Iterator it1_, it2_;
kono
parents:
diff changeset
320 };
kono
parents:
diff changeset
321
kono
parents:
diff changeset
322 private:
kono
parents:
diff changeset
323 void check(uptr idx) const { CHECK_LE(idx, size()); }
kono
parents:
diff changeset
324
kono
parents:
diff changeset
325 uptr idx0(uptr idx) const {
kono
parents:
diff changeset
326 uptr res = idx / (BV::kSize * BV::kSize);
kono
parents:
diff changeset
327 CHECK_LE(res, kLevel1Size);
kono
parents:
diff changeset
328 return res;
kono
parents:
diff changeset
329 }
kono
parents:
diff changeset
330
kono
parents:
diff changeset
331 uptr idx1(uptr idx) const {
kono
parents:
diff changeset
332 uptr res = (idx / BV::kSize) % BV::kSize;
kono
parents:
diff changeset
333 CHECK_LE(res, BV::kSize);
kono
parents:
diff changeset
334 return res;
kono
parents:
diff changeset
335 }
kono
parents:
diff changeset
336
kono
parents:
diff changeset
337 uptr idx2(uptr idx) const {
kono
parents:
diff changeset
338 uptr res = idx % BV::kSize;
kono
parents:
diff changeset
339 CHECK_LE(res, BV::kSize);
kono
parents:
diff changeset
340 return res;
kono
parents:
diff changeset
341 }
kono
parents:
diff changeset
342
kono
parents:
diff changeset
343 BV l1_[kLevel1Size];
kono
parents:
diff changeset
344 BV l2_[kLevel1Size][BV::kSize];
kono
parents:
diff changeset
345 };
kono
parents:
diff changeset
346
kono
parents:
diff changeset
347 } // namespace __sanitizer
kono
parents:
diff changeset
348
kono
parents:
diff changeset
349 #endif // SANITIZER_BITVECTOR_H