blob: 3df5101fa3891cbd58ccfa239893bba36e33ba9d [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) {
116 return true;
117 }
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.
121 if (our_highest >= 0) {
122 return true;
123 }
124
125 // Get the highest bit set's cell's index.
126 int our_highest_index = (our_highest >> 5);
127
128 // This memcmp is enough: we know that the highest bit set is the same for both:
129 // - Therefore, min_size goes up to at least that, we are thus comparing at least what we need to, but not less.
130 // ie. we are comparing all storage cells that could have difference, if both vectors have cells above our_highest_index,
131 // they are automatically at 0.
132 return (memcmp(storage_, src->GetRawStorage(), our_highest_index * sizeof(*storage_)) != 0);
133}
134
135// Intersect with another bit vector.
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700136void BitVector::Intersect(const BitVector* src) {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800137 uint32_t src_storage_size = src->storage_size_;
138
139 // Get the minimum size between us and source.
140 uint32_t min_size = (storage_size_ < src_storage_size) ? storage_size_ : src_storage_size;
141
142 uint32_t idx;
143 for (idx = 0; idx < min_size; idx++) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700144 storage_[idx] &= src->GetRawStorageWord(idx);
145 }
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800146
147 // Now, due to this being an intersection, there are two possibilities:
148 // - Either src was larger than us: we don't care, all upper bits would thus be 0.
149 // - Either we are larger than src: we don't care, all upper bits would have been 0 too.
150 // So all we need to do is set all remaining bits to 0.
151 for (; idx < storage_size_; idx++) {
152 storage_[idx] = 0;
153 }
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700154}
155
156/*
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800157 * Union with another bit vector.
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700158 */
159void BitVector::Union(const BitVector* src) {
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700160 // Get the highest bit to determine how much we need to expand.
161 int highest_bit = src->GetHighestBitSet();
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800162
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700163 // If src has no bit set, we are done: there is no need for a union with src.
164 if (highest_bit == -1) {
165 return;
166 }
167
168 // Update src_size to how many cells we actually care about: where the bit is + 1.
169 uint32_t src_size = BitsToWords(highest_bit + 1);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800170
171 // Is the storage size smaller than src's?
172 if (storage_size_ < src_size) {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800173 // Set it to reallocate.
174 SetBit(highest_bit);
175
176 // Paranoid: storage size should be big enough to hold this bit now.
177 DCHECK_LT(static_cast<uint32_t> (highest_bit), storage_size_ * sizeof(*(storage_)) * 8);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800178 }
179
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700180 for (uint32_t idx = 0; idx < src_size; idx++) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700181 storage_[idx] |= src->GetRawStorageWord(idx);
182 }
183}
184
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800185void BitVector::Subtract(const BitVector *src) {
186 uint32_t src_size = src->storage_size_;
187
188 // We only need to operate on bytes up to the smaller of the sizes of the two operands.
189 unsigned int min_size = (storage_size_ > src_size) ? src_size : storage_size_;
190
191 // Difference until max, we know both accept it:
192 // There is no need to do more:
193 // If we are bigger than src, the upper bits are unchanged.
194 // If we are smaller than src, the non-existant upper bits are 0 and thus can't get subtracted.
195 for (uint32_t idx = 0; idx < min_size; idx++) {
196 storage_[idx] &= (~(src->GetRawStorageWord(idx)));
197 }
198}
199
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700200// Count the number of bits that are set.
Brian Carlstromba150c32013-08-27 17:31:03 -0700201uint32_t BitVector::NumSetBits() const {
202 uint32_t count = 0;
203 for (uint32_t word = 0; word < storage_size_; word++) {
Vladimir Marko81949632014-05-02 11:53:22 +0100204 count += POPCOUNT(storage_[word]);
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700205 }
206 return count;
207}
208
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100209// Count the number of bits that are set in range [0, end).
210uint32_t BitVector::NumSetBits(uint32_t end) const {
211 DCHECK_LE(end, storage_size_ * sizeof(*storage_) * 8);
212 return NumSetBits(storage_, end);
Brian Carlstromba150c32013-08-27 17:31:03 -0700213}
214
215BitVector::Iterator* BitVector::GetIterator() const {
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700216 return new (allocator_) Iterator(this);
217}
218
219/*
220 * Mark specified number of bits as "set". Cannot set all bits like ClearAll
221 * since there might be unused bits - setting those to one will confuse the
222 * iterator.
223 */
Brian Carlstromba150c32013-08-27 17:31:03 -0700224void BitVector::SetInitialBits(uint32_t num_bits) {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800225 // If num_bits is 0, clear everything.
226 if (num_bits == 0) {
227 ClearAllBits();
228 return;
229 }
230
231 // Set the highest bit we want to set to get the BitVector allocated if need be.
232 SetBit(num_bits - 1);
233
Brian Carlstromba150c32013-08-27 17:31:03 -0700234 uint32_t idx;
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800235 // We can set every storage element with -1.
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700236 for (idx = 0; idx < (num_bits >> 5); idx++) {
237 storage_[idx] = -1;
238 }
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800239
240 // Handle the potentially last few bits.
Brian Carlstromba150c32013-08-27 17:31:03 -0700241 uint32_t rem_num_bits = num_bits & 0x1f;
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800242 if (rem_num_bits != 0) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700243 storage_[idx] = (1 << rem_num_bits) - 1;
Vladimir Marko4812d432014-03-11 12:42:25 +0000244 ++idx;
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700245 }
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800246
247 // Now set the upper ones to 0.
248 for (; idx < storage_size_; idx++) {
249 storage_[idx] = 0;
250 }
251}
252
253int BitVector::GetHighestBitSet() const {
254 unsigned int max = storage_size_;
255 for (int idx = max - 1; idx >= 0; idx--) {
256 // If not 0, we have more work: check the bits.
257 uint32_t value = storage_[idx];
258
259 if (value != 0) {
260 // Shift right for the counting.
261 value /= 2;
262
263 int cnt = 0;
264
265 // Count the bits.
266 while (value > 0) {
267 value /= 2;
268 cnt++;
269 }
270
271 // Return cnt + how many storage units still remain * the number of bits per unit.
272 int res = cnt + (idx * (sizeof(*storage_) * 8));
273 return res;
274 }
275 }
276
277 // All zero, therefore return -1.
278 return -1;
279}
280
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700281bool BitVector::EnsureSizeAndClear(unsigned int num) {
282 // Check if the bitvector is expandable.
283 if (IsExpandable() == false) {
284 return false;
285 }
286
287 if (num > 0) {
288 // Now try to expand by setting the last bit.
289 SetBit(num - 1);
290 }
291
292 // We must clear all bits as per our specification.
293 ClearAllBits();
294
295 return true;
296}
297
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800298void BitVector::Copy(const BitVector *src) {
299 // Get highest bit set, we only need to copy till then.
300 int highest_bit = src->GetHighestBitSet();
301
302 // If nothing is set, clear everything.
303 if (highest_bit == -1) {
304 ClearAllBits();
305 return;
306 }
307
308 // Set upper bit to ensure right size before copy.
309 SetBit(highest_bit);
310
311 // Now set until highest bit's storage.
312 uint32_t size = 1 + (highest_bit / (sizeof(*storage_) * 8));
313 memcpy(storage_, src->GetRawStorage(), sizeof(*storage_) * size);
314
315 // Set upper bits to 0.
316 uint32_t left = storage_size_ - size;
317
318 if (left > 0) {
319 memset(storage_ + size, 0, sizeof(*storage_) * left);
320 }
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700321}
322
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100323bool BitVector::IsBitSet(const uint32_t* storage, uint32_t num) {
324 uint32_t val = storage[num >> 5] & check_masks[num & 0x1f];
325 return (val != 0);
326}
327
328uint32_t BitVector::NumSetBits(const uint32_t* storage, uint32_t end) {
329 uint32_t word_end = end >> 5;
330 uint32_t partial_word_bits = end & 0x1f;
331
332 uint32_t count = 0u;
333 for (uint32_t word = 0u; word < word_end; word++) {
Vladimir Marko81949632014-05-02 11:53:22 +0100334 count += POPCOUNT(storage[word]);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100335 }
336 if (partial_word_bits != 0u) {
Vladimir Marko81949632014-05-02 11:53:22 +0100337 count += POPCOUNT(storage[word_end] & ~(0xffffffffu << partial_word_bits));
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100338 }
339 return count;
340}
341
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700342void BitVector::Dump(std::ostream& os, const char *prefix) {
343 std::ostringstream buffer;
344 DumpHelper(buffer, prefix);
345 os << buffer << std::endl;
346}
347
348void BitVector::DumpDot(FILE* file, const char* prefix, bool last_entry) {
349 std::ostringstream buffer;
350 Dump(buffer, prefix);
351
352 // Now print it to the file.
353 fprintf(file, " {%s}", buffer.str().c_str());
354
355 // If it isn't the last entry, add a |.
356 if (last_entry == false) {
357 fprintf(file, "|");
358 }
359
360 // Add the \n.
361 fprintf(file, "\\\n");
362}
363
364void BitVector::DumpHelper(std::ostringstream& buffer, const char* prefix) {
365 // Initialize it.
366 if (prefix != nullptr) {
367 buffer << prefix;
368 }
369
370 int max = GetHighestBitSet();
371
372 for (int i = 0; i <= max; i++) {
373 if (IsBitSet(i)) {
374 buffer << i << " ";
375 }
376 }
377}
378
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700379} // namespace art