blob: 65cb02839a91121531cd4e549f3c8a88c07daf1d [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
Ian Rogers5a2e4cc2014-10-30 15:13:22 -070019#include <limits>
Ian Rogersc7dd2952014-10-21 23:31:19 -070020#include <sstream>
21
Ian Rogerse77493c2014-08-20 15:08:45 -070022#include "allocator.h"
23#include "bit_vector-inl.h"
24
Brian Carlstrom413e89f2013-10-21 23:53:49 -070025namespace art {
26
Ian Rogerse77493c2014-08-20 15:08:45 -070027// The number of words necessary to encode bits.
28static constexpr uint32_t BitsToWords(uint32_t bits) {
29 return RoundUp(bits, 32) / 32;
Brian Carlstrom413e89f2013-10-21 23:53:49 -070030}
31
32// TODO: replace excessive argument defaulting when we are at gcc 4.7
33// or later on host with delegating constructor support. Specifically,
34// starts_bits and storage_size/storage are mutually exclusive.
Brian Carlstromba150c32013-08-27 17:31:03 -070035BitVector::BitVector(uint32_t start_bits,
Brian Carlstrom413e89f2013-10-21 23:53:49 -070036 bool expandable,
37 Allocator* allocator,
38 uint32_t storage_size,
39 uint32_t* storage)
Ian Rogerse77493c2014-08-20 15:08:45 -070040 : storage_(storage),
Brian Carlstrom413e89f2013-10-21 23:53:49 -070041 storage_size_(storage_size),
Ian Rogerse77493c2014-08-20 15:08:45 -070042 allocator_(allocator),
43 expandable_(expandable) {
Andreas Gampe575e78c2014-11-03 23:41:03 -080044 static_assert(sizeof(*storage_) == kWordBytes, "word bytes");
45 static_assert(sizeof(*storage_) * 8u == kWordBits, "word bits");
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080046 if (storage_ == nullptr) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -070047 storage_size_ = BitsToWords(start_bits);
Vladimir Markoa5b8fde2014-05-23 15:16:44 +010048 storage_ = static_cast<uint32_t*>(allocator_->Alloc(storage_size_ * kWordBytes));
Brian Carlstrom413e89f2013-10-21 23:53:49 -070049 }
50}
51
52BitVector::~BitVector() {
53 allocator_->Free(storage_);
54}
55
Ian Rogerse77493c2014-08-20 15:08:45 -070056bool BitVector::SameBitsSet(const BitVector *src) const {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080057 int our_highest = GetHighestBitSet();
58 int src_highest = src->GetHighestBitSet();
59
60 // If the highest bit set is different, we are different.
61 if (our_highest != src_highest) {
Jean Christophe Beylerb5c9b402014-04-30 14:52:00 -070062 return false;
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080063 }
64
65 // If the highest bit set is -1, both are cleared, we are the same.
66 // 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 -070067 if (our_highest <= 0) {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080068 return true;
69 }
70
Jean Christophe Beylerb5c9b402014-04-30 14:52:00 -070071 // Get the highest bit set's cell's index
72 // No need of highest + 1 here because it can't be 0 so BitsToWords will work here.
73 int our_highest_index = BitsToWords(our_highest);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080074
75 // This memcmp is enough: we know that the highest bit set is the same for both:
76 // - Therefore, min_size goes up to at least that, we are thus comparing at least what we need to, but not less.
77 // ie. we are comparing all storage cells that could have difference, if both vectors have cells above our_highest_index,
78 // they are automatically at 0.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +010079 return (memcmp(storage_, src->GetRawStorage(), our_highest_index * kWordBytes) == 0);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080080}
81
David Brazdil7d275372015-04-21 16:36:35 +010082bool BitVector::IsSubsetOf(const BitVector *other) const {
83 int this_highest = GetHighestBitSet();
84 int other_highest = other->GetHighestBitSet();
85
86 // If the highest bit set is -1, this is empty and a trivial subset.
87 if (this_highest < 0) {
88 return true;
89 }
90
91 // If the highest bit set is higher, this cannot be a subset.
92 if (this_highest > other_highest) {
93 return false;
94 }
95
96 // Compare each 32-bit word.
97 size_t this_highest_index = BitsToWords(this_highest + 1);
98 for (size_t i = 0; i < this_highest_index; ++i) {
99 uint32_t this_storage = storage_[i];
100 uint32_t other_storage = other->storage_[i];
101 if ((this_storage | other_storage) != other_storage) {
102 return false;
103 }
104 }
105 return true;
106}
107
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700108void BitVector::Intersect(const BitVector* src) {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800109 uint32_t src_storage_size = src->storage_size_;
110
111 // Get the minimum size between us and source.
112 uint32_t min_size = (storage_size_ < src_storage_size) ? storage_size_ : src_storage_size;
113
114 uint32_t idx;
115 for (idx = 0; idx < min_size; idx++) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700116 storage_[idx] &= src->GetRawStorageWord(idx);
117 }
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800118
119 // Now, due to this being an intersection, there are two possibilities:
120 // - Either src was larger than us: we don't care, all upper bits would thus be 0.
121 // - Either we are larger than src: we don't care, all upper bits would have been 0 too.
122 // So all we need to do is set all remaining bits to 0.
123 for (; idx < storage_size_; idx++) {
124 storage_[idx] = 0;
125 }
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700126}
127
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100128bool BitVector::Union(const BitVector* src) {
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700129 // Get the highest bit to determine how much we need to expand.
130 int highest_bit = src->GetHighestBitSet();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100131 bool changed = false;
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800132
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700133 // If src has no bit set, we are done: there is no need for a union with src.
134 if (highest_bit == -1) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100135 return changed;
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700136 }
137
138 // Update src_size to how many cells we actually care about: where the bit is + 1.
139 uint32_t src_size = BitsToWords(highest_bit + 1);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800140
141 // Is the storage size smaller than src's?
142 if (storage_size_ < src_size) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100143 changed = true;
144
Ian Rogerse77493c2014-08-20 15:08:45 -0700145 EnsureSize(highest_bit);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800146
147 // Paranoid: storage size should be big enough to hold this bit now.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100148 DCHECK_LT(static_cast<uint32_t> (highest_bit), storage_size_ * kWordBits);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800149 }
150
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700151 for (uint32_t idx = 0; idx < src_size; idx++) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100152 uint32_t existing = storage_[idx];
153 uint32_t update = existing | src->GetRawStorageWord(idx);
154 if (existing != update) {
155 changed = true;
156 storage_[idx] = update;
157 }
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700158 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100159 return changed;
160}
161
162bool BitVector::UnionIfNotIn(const BitVector* union_with, const BitVector* not_in) {
163 // Get the highest bit to determine how much we need to expand.
164 int highest_bit = union_with->GetHighestBitSet();
165 bool changed = false;
166
167 // If src has no bit set, we are done: there is no need for a union with src.
168 if (highest_bit == -1) {
169 return changed;
170 }
171
172 // Update union_with_size to how many cells we actually care about: where the bit is + 1.
173 uint32_t union_with_size = BitsToWords(highest_bit + 1);
174
175 // Is the storage size smaller than src's?
176 if (storage_size_ < union_with_size) {
Nicolas Geoffrayb5567612014-10-22 10:25:24 +0100177 EnsureSize(highest_bit);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100178
179 // Paranoid: storage size should be big enough to hold this bit now.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100180 DCHECK_LT(static_cast<uint32_t> (highest_bit), storage_size_ * kWordBits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100181 }
182
183 uint32_t not_in_size = not_in->GetStorageSize();
184
185 uint32_t idx = 0;
186 for (; idx < std::min(not_in_size, union_with_size); idx++) {
187 uint32_t existing = storage_[idx];
188 uint32_t update = existing |
189 (union_with->GetRawStorageWord(idx) & ~not_in->GetRawStorageWord(idx));
190 if (existing != update) {
191 changed = true;
192 storage_[idx] = update;
193 }
194 }
195
196 for (; idx < union_with_size; idx++) {
197 uint32_t existing = storage_[idx];
198 uint32_t update = existing | union_with->GetRawStorageWord(idx);
199 if (existing != update) {
200 changed = true;
201 storage_[idx] = update;
202 }
203 }
204 return changed;
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700205}
206
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800207void BitVector::Subtract(const BitVector *src) {
Ian Rogerse77493c2014-08-20 15:08:45 -0700208 uint32_t src_size = src->storage_size_;
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800209
Ian Rogerse77493c2014-08-20 15:08:45 -0700210 // We only need to operate on bytes up to the smaller of the sizes of the two operands.
211 unsigned int min_size = (storage_size_ > src_size) ? src_size : storage_size_;
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800212
Ian Rogerse77493c2014-08-20 15:08:45 -0700213 // Difference until max, we know both accept it:
214 // There is no need to do more:
215 // If we are bigger than src, the upper bits are unchanged.
216 // If we are smaller than src, the non-existant upper bits are 0 and thus can't get subtracted.
217 for (uint32_t idx = 0; idx < min_size; idx++) {
218 storage_[idx] &= (~(src->GetRawStorageWord(idx)));
219 }
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800220}
221
Brian Carlstromba150c32013-08-27 17:31:03 -0700222uint32_t BitVector::NumSetBits() const {
223 uint32_t count = 0;
224 for (uint32_t word = 0; word < storage_size_; word++) {
Vladimir Marko81949632014-05-02 11:53:22 +0100225 count += POPCOUNT(storage_[word]);
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700226 }
227 return count;
228}
229
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100230uint32_t BitVector::NumSetBits(uint32_t end) const {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100231 DCHECK_LE(end, storage_size_ * kWordBits);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100232 return NumSetBits(storage_, end);
Brian Carlstromba150c32013-08-27 17:31:03 -0700233}
234
Brian Carlstromba150c32013-08-27 17:31:03 -0700235void BitVector::SetInitialBits(uint32_t num_bits) {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800236 // If num_bits is 0, clear everything.
237 if (num_bits == 0) {
238 ClearAllBits();
239 return;
240 }
241
242 // Set the highest bit we want to set to get the BitVector allocated if need be.
243 SetBit(num_bits - 1);
244
Brian Carlstromba150c32013-08-27 17:31:03 -0700245 uint32_t idx;
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800246 // We can set every storage element with -1.
Ian Rogerse77493c2014-08-20 15:08:45 -0700247 for (idx = 0; idx < WordIndex(num_bits); idx++) {
Ian Rogers5a2e4cc2014-10-30 15:13:22 -0700248 storage_[idx] = std::numeric_limits<uint32_t>::max();
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700249 }
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800250
251 // Handle the potentially last few bits.
Brian Carlstromba150c32013-08-27 17:31:03 -0700252 uint32_t rem_num_bits = num_bits & 0x1f;
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800253 if (rem_num_bits != 0) {
Ian Rogers5a2e4cc2014-10-30 15:13:22 -0700254 storage_[idx] = (1U << rem_num_bits) - 1;
Vladimir Marko4812d432014-03-11 12:42:25 +0000255 ++idx;
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700256 }
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800257
258 // Now set the upper ones to 0.
259 for (; idx < storage_size_; idx++) {
260 storage_[idx] = 0;
261 }
262}
263
264int BitVector::GetHighestBitSet() const {
265 unsigned int max = storage_size_;
266 for (int idx = max - 1; idx >= 0; idx--) {
267 // If not 0, we have more work: check the bits.
268 uint32_t value = storage_[idx];
269
270 if (value != 0) {
Ian Rogerse77493c2014-08-20 15:08:45 -0700271 // Return highest bit set in value plus bits from previous storage indexes.
272 return 31 - CLZ(value) + (idx * kWordBits);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800273 }
274 }
275
276 // All zero, therefore return -1.
277 return -1;
278}
279
280void BitVector::Copy(const BitVector *src) {
281 // Get highest bit set, we only need to copy till then.
282 int highest_bit = src->GetHighestBitSet();
283
284 // If nothing is set, clear everything.
285 if (highest_bit == -1) {
286 ClearAllBits();
287 return;
288 }
289
290 // Set upper bit to ensure right size before copy.
291 SetBit(highest_bit);
292
293 // Now set until highest bit's storage.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100294 uint32_t size = 1 + (highest_bit / kWordBits);
295 memcpy(storage_, src->GetRawStorage(), kWordBytes * size);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800296
297 // Set upper bits to 0.
298 uint32_t left = storage_size_ - size;
299
300 if (left > 0) {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100301 memset(storage_ + size, 0, kWordBytes * left);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800302 }
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700303}
304
Chih-Hung Hsieh1675f2c2015-01-30 15:37:34 -0800305#if defined(__clang__) && defined(__ARM_64BIT_STATE)
306// b/19180814 When POPCOUNT is inlined, boot up failed on arm64 devices.
307__attribute__((optnone))
308#endif
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100309uint32_t BitVector::NumSetBits(const uint32_t* storage, uint32_t end) {
Ian Rogerse77493c2014-08-20 15:08:45 -0700310 uint32_t word_end = WordIndex(end);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100311 uint32_t partial_word_bits = end & 0x1f;
312
313 uint32_t count = 0u;
314 for (uint32_t word = 0u; word < word_end; word++) {
Vladimir Marko81949632014-05-02 11:53:22 +0100315 count += POPCOUNT(storage[word]);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100316 }
317 if (partial_word_bits != 0u) {
Vladimir Marko81949632014-05-02 11:53:22 +0100318 count += POPCOUNT(storage[word_end] & ~(0xffffffffu << partial_word_bits));
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100319 }
320 return count;
321}
322
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100323void BitVector::Dump(std::ostream& os, const char *prefix) const {
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700324 std::ostringstream buffer;
Jean Christophe Beyler520f37b2014-05-22 15:43:50 -0700325 DumpHelper(prefix, buffer);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100326 os << buffer.str() << std::endl;
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700327}
328
Jean Christophe Beyler520f37b2014-05-22 15:43:50 -0700329void BitVector::DumpHelper(const char* prefix, std::ostringstream& buffer) const {
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700330 // Initialize it.
331 if (prefix != nullptr) {
332 buffer << prefix;
333 }
334
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100335 buffer << '(';
Jean Christophe Beyler014d77a2014-06-02 11:21:21 -0700336 for (size_t i = 0; i < storage_size_ * kWordBits; i++) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100337 buffer << IsBitSet(i);
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700338 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100339 buffer << ')';
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700340}
341
Ian Rogerse77493c2014-08-20 15:08:45 -0700342void BitVector::EnsureSize(uint32_t idx) {
343 if (idx >= storage_size_ * kWordBits) {
344 DCHECK(expandable_) << "Attempted to expand a non-expandable bitmap to position " << idx;
345
346 /* Round up to word boundaries for "idx+1" bits */
347 uint32_t new_size = BitsToWords(idx + 1);
348 DCHECK_GT(new_size, storage_size_);
349 uint32_t *new_storage =
350 static_cast<uint32_t*>(allocator_->Alloc(new_size * kWordBytes));
351 memcpy(new_storage, storage_, storage_size_ * kWordBytes);
352 // Zero out the new storage words.
353 memset(&new_storage[storage_size_], 0, (new_size - storage_size_) * kWordBytes);
Andreas Gampedc8aa692014-10-24 21:32:07 -0700354 // TODO: collect stats on space wasted because of resize.
355
356 // Free old storage.
357 allocator_->Free(storage_);
358
359 // Set fields.
Ian Rogerse77493c2014-08-20 15:08:45 -0700360 storage_ = new_storage;
361 storage_size_ = new_size;
362 }
363}
364
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700365} // namespace art