blob: 980b1a67ed7e6c543c6fa7d60a3d435613b33a8d [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),
46 storage_(storage) {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080047 DCHECK_EQ(sizeof(*storage_), 4U); // Assuming 32-bit units.
48 if (storage_ == nullptr) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -070049 storage_size_ = BitsToWords(start_bits);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080050 storage_ = static_cast<uint32_t*>(allocator_->Alloc(storage_size_ * sizeof(*storage_)));
Brian Carlstrom413e89f2013-10-21 23:53:49 -070051 }
52}
53
54BitVector::~BitVector() {
55 allocator_->Free(storage_);
56}
57
58/*
59 * Determine whether or not the specified bit is set.
60 */
Brian Carlstromba150c32013-08-27 17:31:03 -070061bool BitVector::IsBitSet(uint32_t num) const {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080062 // If the index is over the size:
63 if (num >= storage_size_ * sizeof(*storage_) * 8) {
64 // Whether it is expandable or not, this bit does not exist: thus it is not set.
65 return false;
66 }
Brian Carlstrom413e89f2013-10-21 23:53:49 -070067
Vladimir Markod3c5beb2014-04-11 16:32:51 +010068 return IsBitSet(storage_, num);
Brian Carlstrom413e89f2013-10-21 23:53:49 -070069}
70
71// Mark all bits bit as "clear".
72void BitVector::ClearAllBits() {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080073 memset(storage_, 0, storage_size_ * sizeof(*storage_));
Brian Carlstrom413e89f2013-10-21 23:53:49 -070074}
75
76// Mark the specified bit as "set".
77/*
78 * TUNING: this could have pathologically bad growth/expand behavior. Make sure we're
79 * not using it badly or change resize mechanism.
80 */
Brian Carlstromba150c32013-08-27 17:31:03 -070081void BitVector::SetBit(uint32_t num) {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080082 if (num >= storage_size_ * sizeof(*storage_) * 8) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -070083 DCHECK(expandable_) << "Attempted to expand a non-expandable bitmap to position " << num;
84
85 /* Round up to word boundaries for "num+1" bits */
Brian Carlstromba150c32013-08-27 17:31:03 -070086 uint32_t new_size = BitsToWords(num + 1);
Brian Carlstrom413e89f2013-10-21 23:53:49 -070087 DCHECK_GT(new_size, storage_size_);
88 uint32_t *new_storage =
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080089 static_cast<uint32_t*>(allocator_->Alloc(new_size * sizeof(*storage_)));
90 memcpy(new_storage, storage_, storage_size_ * sizeof(*storage_));
Brian Carlstrom413e89f2013-10-21 23:53:49 -070091 // Zero out the new storage words.
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080092 memset(&new_storage[storage_size_], 0, (new_size - storage_size_) * sizeof(*storage_));
Brian Carlstrom413e89f2013-10-21 23:53:49 -070093 // TOTO: collect stats on space wasted because of resize.
94 storage_ = new_storage;
95 storage_size_ = new_size;
96 }
97
98 storage_[num >> 5] |= check_masks[num & 0x1f];
99}
100
101// Mark the specified bit as "unset".
Brian Carlstromba150c32013-08-27 17:31:03 -0700102void BitVector::ClearBit(uint32_t num) {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800103 // If the index is over the size, we don't have to do anything, it is cleared.
104 if (num < storage_size_ * sizeof(*storage_) * 8) {
105 // Otherwise, go ahead and clear it.
106 storage_[num >> 5] &= ~check_masks[num & 0x1f];
107 }
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700108}
109
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800110bool BitVector::SameBitsSet(const BitVector *src) {
111 int our_highest = GetHighestBitSet();
112 int src_highest = src->GetHighestBitSet();
113
114 // If the highest bit set is different, we are different.
115 if (our_highest != src_highest) {
Jean Christophe Beylerb5c9b402014-04-30 14:52:00 -0700116 return false;
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800117 }
118
119 // If the highest bit set is -1, both are cleared, we are the same.
120 // 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 -0700121 if (our_highest <= 0) {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800122 return true;
123 }
124
Jean Christophe Beylerb5c9b402014-04-30 14:52:00 -0700125 // Get the highest bit set's cell's index
126 // No need of highest + 1 here because it can't be 0 so BitsToWords will work here.
127 int our_highest_index = BitsToWords(our_highest);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800128
129 // This memcmp is enough: we know that the highest bit set is the same for both:
130 // - Therefore, min_size goes up to at least that, we are thus comparing at least what we need to, but not less.
131 // ie. we are comparing all storage cells that could have difference, if both vectors have cells above our_highest_index,
132 // they are automatically at 0.
Jean Christophe Beylerb5c9b402014-04-30 14:52:00 -0700133 return (memcmp(storage_, src->GetRawStorage(), our_highest_index * sizeof(*storage_)) == 0);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800134}
135
136// Intersect with another bit vector.
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700137void BitVector::Intersect(const BitVector* src) {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800138 uint32_t src_storage_size = src->storage_size_;
139
140 // Get the minimum size between us and source.
141 uint32_t min_size = (storage_size_ < src_storage_size) ? storage_size_ : src_storage_size;
142
143 uint32_t idx;
144 for (idx = 0; idx < min_size; idx++) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700145 storage_[idx] &= src->GetRawStorageWord(idx);
146 }
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800147
148 // Now, due to this being an intersection, there are two possibilities:
149 // - Either src was larger than us: we don't care, all upper bits would thus be 0.
150 // - Either we are larger than src: we don't care, all upper bits would have been 0 too.
151 // So all we need to do is set all remaining bits to 0.
152 for (; idx < storage_size_; idx++) {
153 storage_[idx] = 0;
154 }
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700155}
156
157/*
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800158 * Union with another bit vector.
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700159 */
160void BitVector::Union(const BitVector* src) {
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700161 // Get the highest bit to determine how much we need to expand.
162 int highest_bit = src->GetHighestBitSet();
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800163
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700164 // If src has no bit set, we are done: there is no need for a union with src.
165 if (highest_bit == -1) {
166 return;
167 }
168
169 // Update src_size to how many cells we actually care about: where the bit is + 1.
170 uint32_t src_size = BitsToWords(highest_bit + 1);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800171
172 // Is the storage size smaller than src's?
173 if (storage_size_ < src_size) {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800174 // Set it to reallocate.
175 SetBit(highest_bit);
176
177 // Paranoid: storage size should be big enough to hold this bit now.
178 DCHECK_LT(static_cast<uint32_t> (highest_bit), storage_size_ * sizeof(*(storage_)) * 8);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800179 }
180
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700181 for (uint32_t idx = 0; idx < src_size; idx++) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700182 storage_[idx] |= src->GetRawStorageWord(idx);
183 }
184}
185
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800186void BitVector::Subtract(const BitVector *src) {
187 uint32_t src_size = src->storage_size_;
188
189 // We only need to operate on bytes up to the smaller of the sizes of the two operands.
190 unsigned int min_size = (storage_size_ > src_size) ? src_size : storage_size_;
191
192 // Difference until max, we know both accept it:
193 // There is no need to do more:
194 // If we are bigger than src, the upper bits are unchanged.
195 // If we are smaller than src, the non-existant upper bits are 0 and thus can't get subtracted.
196 for (uint32_t idx = 0; idx < min_size; idx++) {
197 storage_[idx] &= (~(src->GetRawStorageWord(idx)));
198 }
199}
200
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700201// Count the number of bits that are set.
Brian Carlstromba150c32013-08-27 17:31:03 -0700202uint32_t BitVector::NumSetBits() const {
203 uint32_t count = 0;
204 for (uint32_t word = 0; word < storage_size_; word++) {
Vladimir Marko81949632014-05-02 11:53:22 +0100205 count += POPCOUNT(storage_[word]);
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700206 }
207 return count;
208}
209
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100210// Count the number of bits that are set in range [0, end).
211uint32_t BitVector::NumSetBits(uint32_t end) const {
212 DCHECK_LE(end, storage_size_ * sizeof(*storage_) * 8);
213 return NumSetBits(storage_, end);
Brian Carlstromba150c32013-08-27 17:31:03 -0700214}
215
216BitVector::Iterator* BitVector::GetIterator() const {
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700217 return new (allocator_) Iterator(this);
218}
219
220/*
221 * Mark specified number of bits as "set". Cannot set all bits like ClearAll
222 * since there might be unused bits - setting those to one will confuse the
223 * iterator.
224 */
Brian Carlstromba150c32013-08-27 17:31:03 -0700225void BitVector::SetInitialBits(uint32_t num_bits) {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800226 // If num_bits is 0, clear everything.
227 if (num_bits == 0) {
228 ClearAllBits();
229 return;
230 }
231
232 // Set the highest bit we want to set to get the BitVector allocated if need be.
233 SetBit(num_bits - 1);
234
Brian Carlstromba150c32013-08-27 17:31:03 -0700235 uint32_t idx;
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800236 // We can set every storage element with -1.
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700237 for (idx = 0; idx < (num_bits >> 5); idx++) {
238 storage_[idx] = -1;
239 }
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800240
241 // Handle the potentially last few bits.
Brian Carlstromba150c32013-08-27 17:31:03 -0700242 uint32_t rem_num_bits = num_bits & 0x1f;
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800243 if (rem_num_bits != 0) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700244 storage_[idx] = (1 << rem_num_bits) - 1;
Vladimir Marko4812d432014-03-11 12:42:25 +0000245 ++idx;
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700246 }
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800247
248 // Now set the upper ones to 0.
249 for (; idx < storage_size_; idx++) {
250 storage_[idx] = 0;
251 }
252}
253
254int BitVector::GetHighestBitSet() const {
255 unsigned int max = storage_size_;
256 for (int idx = max - 1; idx >= 0; idx--) {
257 // If not 0, we have more work: check the bits.
258 uint32_t value = storage_[idx];
259
260 if (value != 0) {
261 // Shift right for the counting.
262 value /= 2;
263
264 int cnt = 0;
265
266 // Count the bits.
267 while (value > 0) {
268 value /= 2;
269 cnt++;
270 }
271
272 // Return cnt + how many storage units still remain * the number of bits per unit.
273 int res = cnt + (idx * (sizeof(*storage_) * 8));
274 return res;
275 }
276 }
277
278 // All zero, therefore return -1.
279 return -1;
280}
281
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700282bool BitVector::EnsureSizeAndClear(unsigned int num) {
283 // Check if the bitvector is expandable.
284 if (IsExpandable() == false) {
285 return false;
286 }
287
288 if (num > 0) {
289 // Now try to expand by setting the last bit.
290 SetBit(num - 1);
291 }
292
293 // We must clear all bits as per our specification.
294 ClearAllBits();
295
296 return true;
297}
298
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800299void BitVector::Copy(const BitVector *src) {
300 // Get highest bit set, we only need to copy till then.
301 int highest_bit = src->GetHighestBitSet();
302
303 // If nothing is set, clear everything.
304 if (highest_bit == -1) {
305 ClearAllBits();
306 return;
307 }
308
309 // Set upper bit to ensure right size before copy.
310 SetBit(highest_bit);
311
312 // Now set until highest bit's storage.
313 uint32_t size = 1 + (highest_bit / (sizeof(*storage_) * 8));
314 memcpy(storage_, src->GetRawStorage(), sizeof(*storage_) * size);
315
316 // Set upper bits to 0.
317 uint32_t left = storage_size_ - size;
318
319 if (left > 0) {
320 memset(storage_ + size, 0, sizeof(*storage_) * left);
321 }
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700322}
323
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100324bool BitVector::IsBitSet(const uint32_t* storage, uint32_t num) {
325 uint32_t val = storage[num >> 5] & check_masks[num & 0x1f];
326 return (val != 0);
327}
328
329uint32_t BitVector::NumSetBits(const uint32_t* storage, uint32_t end) {
330 uint32_t word_end = end >> 5;
331 uint32_t partial_word_bits = end & 0x1f;
332
333 uint32_t count = 0u;
334 for (uint32_t word = 0u; word < word_end; word++) {
Vladimir Marko81949632014-05-02 11:53:22 +0100335 count += POPCOUNT(storage[word]);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100336 }
337 if (partial_word_bits != 0u) {
Vladimir Marko81949632014-05-02 11:53:22 +0100338 count += POPCOUNT(storage[word_end] & ~(0xffffffffu << partial_word_bits));
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100339 }
340 return count;
341}
342
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700343void BitVector::Dump(std::ostream& os, const char *prefix) {
344 std::ostringstream buffer;
345 DumpHelper(buffer, prefix);
346 os << buffer << std::endl;
347}
348
349void BitVector::DumpDot(FILE* file, const char* prefix, bool last_entry) {
350 std::ostringstream buffer;
351 Dump(buffer, prefix);
352
353 // Now print it to the file.
354 fprintf(file, " {%s}", buffer.str().c_str());
355
356 // If it isn't the last entry, add a |.
357 if (last_entry == false) {
358 fprintf(file, "|");
359 }
360
361 // Add the \n.
362 fprintf(file, "\\\n");
363}
364
365void BitVector::DumpHelper(std::ostringstream& buffer, const char* prefix) {
366 // Initialize it.
367 if (prefix != nullptr) {
368 buffer << prefix;
369 }
370
371 int max = GetHighestBitSet();
372
373 for (int i = 0; i <= max; i++) {
374 if (IsBitSet(i)) {
375 buffer << i << " ";
376 }
377 }
378}
379
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700380} // namespace art