blob: 7dad55ec46b6569c71d164a0eeaa3c3a63fb8120 [file] [log] [blame]
Brian Carlstrom413e89f2013-10-21 23:53:49 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "bit_vector.h"
18
19namespace art {
20
21// TODO: profile to make sure this is still a win relative to just using shifted masks.
22static uint32_t check_masks[32] = {
23 0x00000001, 0x00000002, 0x00000004, 0x00000008, 0x00000010,
24 0x00000020, 0x00000040, 0x00000080, 0x00000100, 0x00000200,
25 0x00000400, 0x00000800, 0x00001000, 0x00002000, 0x00004000,
26 0x00008000, 0x00010000, 0x00020000, 0x00040000, 0x00080000,
27 0x00100000, 0x00200000, 0x00400000, 0x00800000, 0x01000000,
28 0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x20000000,
29 0x40000000, 0x80000000 };
30
Brian Carlstromba150c32013-08-27 17:31:03 -070031static inline uint32_t BitsToWords(uint32_t bits) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -070032 return (bits + 31) >> 5;
33}
34
35// TODO: replace excessive argument defaulting when we are at gcc 4.7
36// or later on host with delegating constructor support. Specifically,
37// starts_bits and storage_size/storage are mutually exclusive.
Brian Carlstromba150c32013-08-27 17:31:03 -070038BitVector::BitVector(uint32_t start_bits,
Brian Carlstrom413e89f2013-10-21 23:53:49 -070039 bool expandable,
40 Allocator* allocator,
41 uint32_t storage_size,
42 uint32_t* storage)
43 : allocator_(allocator),
44 expandable_(expandable),
45 storage_size_(storage_size),
Nicolas Geoffray804d0932014-05-02 08:46:00 +010046 storage_(storage),
47 number_of_bits_(start_bits) {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +010048 COMPILE_ASSERT(sizeof(*storage_) == kWordBytes, check_word_bytes);
49 COMPILE_ASSERT(sizeof(*storage_) * 8u == kWordBits, check_word_bits);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080050 if (storage_ == nullptr) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -070051 storage_size_ = BitsToWords(start_bits);
Vladimir Markoa5b8fde2014-05-23 15:16:44 +010052 storage_ = static_cast<uint32_t*>(allocator_->Alloc(storage_size_ * kWordBytes));
Brian Carlstrom413e89f2013-10-21 23:53:49 -070053 }
54}
55
56BitVector::~BitVector() {
57 allocator_->Free(storage_);
58}
59
60/*
61 * Determine whether or not the specified bit is set.
62 */
Brian Carlstromba150c32013-08-27 17:31:03 -070063bool BitVector::IsBitSet(uint32_t num) const {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080064 // If the index is over the size:
Vladimir Markoa5b8fde2014-05-23 15:16:44 +010065 if (num >= storage_size_ * kWordBits) {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080066 // Whether it is expandable or not, this bit does not exist: thus it is not set.
67 return false;
68 }
Brian Carlstrom413e89f2013-10-21 23:53:49 -070069
Vladimir Markod3c5beb2014-04-11 16:32:51 +010070 return IsBitSet(storage_, num);
Brian Carlstrom413e89f2013-10-21 23:53:49 -070071}
72
73// Mark all bits bit as "clear".
74void BitVector::ClearAllBits() {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +010075 memset(storage_, 0, storage_size_ * kWordBytes);
Brian Carlstrom413e89f2013-10-21 23:53:49 -070076}
77
78// Mark the specified bit as "set".
79/*
80 * TUNING: this could have pathologically bad growth/expand behavior. Make sure we're
81 * not using it badly or change resize mechanism.
82 */
Brian Carlstromba150c32013-08-27 17:31:03 -070083void BitVector::SetBit(uint32_t num) {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +010084 if (num >= storage_size_ * kWordBits) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -070085 DCHECK(expandable_) << "Attempted to expand a non-expandable bitmap to position " << num;
86
87 /* Round up to word boundaries for "num+1" bits */
Brian Carlstromba150c32013-08-27 17:31:03 -070088 uint32_t new_size = BitsToWords(num + 1);
Brian Carlstrom413e89f2013-10-21 23:53:49 -070089 DCHECK_GT(new_size, storage_size_);
90 uint32_t *new_storage =
Vladimir Markoa5b8fde2014-05-23 15:16:44 +010091 static_cast<uint32_t*>(allocator_->Alloc(new_size * kWordBytes));
92 memcpy(new_storage, storage_, storage_size_ * kWordBytes);
Brian Carlstrom413e89f2013-10-21 23:53:49 -070093 // Zero out the new storage words.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +010094 memset(&new_storage[storage_size_], 0, (new_size - storage_size_) * kWordBytes);
Brian Carlstrom413e89f2013-10-21 23:53:49 -070095 // TOTO: collect stats on space wasted because of resize.
96 storage_ = new_storage;
97 storage_size_ = new_size;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010098 number_of_bits_ = num;
Brian Carlstrom413e89f2013-10-21 23:53:49 -070099 }
100
101 storage_[num >> 5] |= check_masks[num & 0x1f];
102}
103
104// Mark the specified bit as "unset".
Brian Carlstromba150c32013-08-27 17:31:03 -0700105void BitVector::ClearBit(uint32_t num) {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800106 // If the index is over the size, we don't have to do anything, it is cleared.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100107 if (num < storage_size_ * kWordBits) {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800108 // Otherwise, go ahead and clear it.
109 storage_[num >> 5] &= ~check_masks[num & 0x1f];
110 }
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700111}
112
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800113bool BitVector::SameBitsSet(const BitVector *src) {
114 int our_highest = GetHighestBitSet();
115 int src_highest = src->GetHighestBitSet();
116
117 // If the highest bit set is different, we are different.
118 if (our_highest != src_highest) {
Jean Christophe Beylerb5c9b402014-04-30 14:52:00 -0700119 return false;
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800120 }
121
122 // If the highest bit set is -1, both are cleared, we are the same.
123 // If the highest bit set is 0, both have a unique bit set, we are the same.
Jean Christophe Beylerb5c9b402014-04-30 14:52:00 -0700124 if (our_highest <= 0) {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800125 return true;
126 }
127
Jean Christophe Beylerb5c9b402014-04-30 14:52:00 -0700128 // Get the highest bit set's cell's index
129 // No need of highest + 1 here because it can't be 0 so BitsToWords will work here.
130 int our_highest_index = BitsToWords(our_highest);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800131
132 // This memcmp is enough: we know that the highest bit set is the same for both:
133 // - Therefore, min_size goes up to at least that, we are thus comparing at least what we need to, but not less.
134 // ie. we are comparing all storage cells that could have difference, if both vectors have cells above our_highest_index,
135 // they are automatically at 0.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100136 return (memcmp(storage_, src->GetRawStorage(), our_highest_index * kWordBytes) == 0);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800137}
138
139// Intersect with another bit vector.
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700140void BitVector::Intersect(const BitVector* src) {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800141 uint32_t src_storage_size = src->storage_size_;
142
143 // Get the minimum size between us and source.
144 uint32_t min_size = (storage_size_ < src_storage_size) ? storage_size_ : src_storage_size;
145
146 uint32_t idx;
147 for (idx = 0; idx < min_size; idx++) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700148 storage_[idx] &= src->GetRawStorageWord(idx);
149 }
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800150
151 // Now, due to this being an intersection, there are two possibilities:
152 // - Either src was larger than us: we don't care, all upper bits would thus be 0.
153 // - Either we are larger than src: we don't care, all upper bits would have been 0 too.
154 // So all we need to do is set all remaining bits to 0.
155 for (; idx < storage_size_; idx++) {
156 storage_[idx] = 0;
157 }
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700158}
159
160/*
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800161 * Union with another bit vector.
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700162 */
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100163bool BitVector::Union(const BitVector* src) {
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700164 // Get the highest bit to determine how much we need to expand.
165 int highest_bit = src->GetHighestBitSet();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100166 bool changed = false;
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800167
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700168 // If src has no bit set, we are done: there is no need for a union with src.
169 if (highest_bit == -1) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100170 return changed;
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700171 }
172
173 // Update src_size to how many cells we actually care about: where the bit is + 1.
174 uint32_t src_size = BitsToWords(highest_bit + 1);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800175
176 // Is the storage size smaller than src's?
177 if (storage_size_ < src_size) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100178 changed = true;
179
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800180 // Set it to reallocate.
181 SetBit(highest_bit);
182
183 // Paranoid: storage size should be big enough to hold this bit now.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100184 DCHECK_LT(static_cast<uint32_t> (highest_bit), storage_size_ * kWordBits);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800185 }
186
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700187 for (uint32_t idx = 0; idx < src_size; idx++) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100188 uint32_t existing = storage_[idx];
189 uint32_t update = existing | src->GetRawStorageWord(idx);
190 if (existing != update) {
191 changed = true;
192 storage_[idx] = update;
193 }
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700194 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100195 return changed;
196}
197
198bool BitVector::UnionIfNotIn(const BitVector* union_with, const BitVector* not_in) {
199 // Get the highest bit to determine how much we need to expand.
200 int highest_bit = union_with->GetHighestBitSet();
201 bool changed = false;
202
203 // If src has no bit set, we are done: there is no need for a union with src.
204 if (highest_bit == -1) {
205 return changed;
206 }
207
208 // Update union_with_size to how many cells we actually care about: where the bit is + 1.
209 uint32_t union_with_size = BitsToWords(highest_bit + 1);
210
211 // Is the storage size smaller than src's?
212 if (storage_size_ < union_with_size) {
213 changed = true;
214
215 // Set it to reallocate.
216 SetBit(highest_bit);
217
218 // Paranoid: storage size should be big enough to hold this bit now.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100219 DCHECK_LT(static_cast<uint32_t> (highest_bit), storage_size_ * kWordBits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100220 }
221
222 uint32_t not_in_size = not_in->GetStorageSize();
223
224 uint32_t idx = 0;
225 for (; idx < std::min(not_in_size, union_with_size); idx++) {
226 uint32_t existing = storage_[idx];
227 uint32_t update = existing |
228 (union_with->GetRawStorageWord(idx) & ~not_in->GetRawStorageWord(idx));
229 if (existing != update) {
230 changed = true;
231 storage_[idx] = update;
232 }
233 }
234
235 for (; idx < union_with_size; idx++) {
236 uint32_t existing = storage_[idx];
237 uint32_t update = existing | union_with->GetRawStorageWord(idx);
238 if (existing != update) {
239 changed = true;
240 storage_[idx] = update;
241 }
242 }
243 return changed;
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700244}
245
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800246void BitVector::Subtract(const BitVector *src) {
247 uint32_t src_size = src->storage_size_;
248
249 // We only need to operate on bytes up to the smaller of the sizes of the two operands.
250 unsigned int min_size = (storage_size_ > src_size) ? src_size : storage_size_;
251
252 // Difference until max, we know both accept it:
253 // There is no need to do more:
254 // If we are bigger than src, the upper bits are unchanged.
255 // If we are smaller than src, the non-existant upper bits are 0 and thus can't get subtracted.
256 for (uint32_t idx = 0; idx < min_size; idx++) {
257 storage_[idx] &= (~(src->GetRawStorageWord(idx)));
258 }
259}
260
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700261// Count the number of bits that are set.
Brian Carlstromba150c32013-08-27 17:31:03 -0700262uint32_t BitVector::NumSetBits() const {
263 uint32_t count = 0;
264 for (uint32_t word = 0; word < storage_size_; word++) {
Vladimir Marko81949632014-05-02 11:53:22 +0100265 count += POPCOUNT(storage_[word]);
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700266 }
267 return count;
268}
269
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100270// Count the number of bits that are set in range [0, end).
271uint32_t BitVector::NumSetBits(uint32_t end) const {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100272 DCHECK_LE(end, storage_size_ * kWordBits);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100273 return NumSetBits(storage_, end);
Brian Carlstromba150c32013-08-27 17:31:03 -0700274}
275
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700276/*
277 * Mark specified number of bits as "set". Cannot set all bits like ClearAll
278 * since there might be unused bits - setting those to one will confuse the
279 * iterator.
280 */
Brian Carlstromba150c32013-08-27 17:31:03 -0700281void BitVector::SetInitialBits(uint32_t num_bits) {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800282 // If num_bits is 0, clear everything.
283 if (num_bits == 0) {
284 ClearAllBits();
285 return;
286 }
287
288 // Set the highest bit we want to set to get the BitVector allocated if need be.
289 SetBit(num_bits - 1);
290
Brian Carlstromba150c32013-08-27 17:31:03 -0700291 uint32_t idx;
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800292 // We can set every storage element with -1.
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700293 for (idx = 0; idx < (num_bits >> 5); idx++) {
294 storage_[idx] = -1;
295 }
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800296
297 // Handle the potentially last few bits.
Brian Carlstromba150c32013-08-27 17:31:03 -0700298 uint32_t rem_num_bits = num_bits & 0x1f;
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800299 if (rem_num_bits != 0) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700300 storage_[idx] = (1 << rem_num_bits) - 1;
Vladimir Marko4812d432014-03-11 12:42:25 +0000301 ++idx;
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700302 }
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800303
304 // Now set the upper ones to 0.
305 for (; idx < storage_size_; idx++) {
306 storage_[idx] = 0;
307 }
308}
309
310int BitVector::GetHighestBitSet() const {
311 unsigned int max = storage_size_;
312 for (int idx = max - 1; idx >= 0; idx--) {
313 // If not 0, we have more work: check the bits.
314 uint32_t value = storage_[idx];
315
316 if (value != 0) {
317 // Shift right for the counting.
318 value /= 2;
319
320 int cnt = 0;
321
322 // Count the bits.
323 while (value > 0) {
324 value /= 2;
325 cnt++;
326 }
327
328 // Return cnt + how many storage units still remain * the number of bits per unit.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100329 int res = cnt + (idx * kWordBits);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800330 return res;
331 }
332 }
333
334 // All zero, therefore return -1.
335 return -1;
336}
337
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700338bool BitVector::EnsureSizeAndClear(unsigned int num) {
339 // Check if the bitvector is expandable.
340 if (IsExpandable() == false) {
341 return false;
342 }
343
344 if (num > 0) {
345 // Now try to expand by setting the last bit.
346 SetBit(num - 1);
347 }
348
349 // We must clear all bits as per our specification.
350 ClearAllBits();
351
352 return true;
353}
354
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800355void BitVector::Copy(const BitVector *src) {
356 // Get highest bit set, we only need to copy till then.
357 int highest_bit = src->GetHighestBitSet();
358
359 // If nothing is set, clear everything.
360 if (highest_bit == -1) {
361 ClearAllBits();
362 return;
363 }
364
365 // Set upper bit to ensure right size before copy.
366 SetBit(highest_bit);
367
368 // Now set until highest bit's storage.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100369 uint32_t size = 1 + (highest_bit / kWordBits);
370 memcpy(storage_, src->GetRawStorage(), kWordBytes * size);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800371
372 // Set upper bits to 0.
373 uint32_t left = storage_size_ - size;
374
375 if (left > 0) {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100376 memset(storage_ + size, 0, kWordBytes * left);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800377 }
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700378}
379
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100380bool BitVector::IsBitSet(const uint32_t* storage, uint32_t num) {
381 uint32_t val = storage[num >> 5] & check_masks[num & 0x1f];
382 return (val != 0);
383}
384
385uint32_t BitVector::NumSetBits(const uint32_t* storage, uint32_t end) {
386 uint32_t word_end = end >> 5;
387 uint32_t partial_word_bits = end & 0x1f;
388
389 uint32_t count = 0u;
390 for (uint32_t word = 0u; word < word_end; word++) {
Vladimir Marko81949632014-05-02 11:53:22 +0100391 count += POPCOUNT(storage[word]);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100392 }
393 if (partial_word_bits != 0u) {
Vladimir Marko81949632014-05-02 11:53:22 +0100394 count += POPCOUNT(storage[word_end] & ~(0xffffffffu << partial_word_bits));
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100395 }
396 return count;
397}
398
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100399void BitVector::Dump(std::ostream& os, const char *prefix) const {
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700400 std::ostringstream buffer;
401 DumpHelper(buffer, prefix);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100402 os << buffer.str() << std::endl;
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700403}
404
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100405void BitVector::DumpDot(FILE* file, const char* prefix, bool last_entry) const {
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700406 std::ostringstream buffer;
407 Dump(buffer, prefix);
408
409 // Now print it to the file.
410 fprintf(file, " {%s}", buffer.str().c_str());
411
412 // If it isn't the last entry, add a |.
413 if (last_entry == false) {
414 fprintf(file, "|");
415 }
416
417 // Add the \n.
418 fprintf(file, "\\\n");
419}
420
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100421void BitVector::DumpHelper(std::ostringstream& buffer, const char* prefix) const {
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700422 // Initialize it.
423 if (prefix != nullptr) {
424 buffer << prefix;
425 }
426
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100427 buffer << '(';
428 for (size_t i = 0; i < number_of_bits_; i++) {
429 buffer << IsBitSet(i);
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700430 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100431 buffer << ')';
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700432}
433
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700434} // namespace art