blob: 1e0be3a43306622e66cedc018c0f29bb33fd617e [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 Rogersc7dd2952014-10-21 23:31:19 -070019#include <sstream>
20
Ian Rogerse77493c2014-08-20 15:08:45 -070021#include "allocator.h"
22#include "bit_vector-inl.h"
23
Brian Carlstrom413e89f2013-10-21 23:53:49 -070024namespace art {
25
Ian Rogerse77493c2014-08-20 15:08:45 -070026// The number of words necessary to encode bits.
27static constexpr uint32_t BitsToWords(uint32_t bits) {
28 return RoundUp(bits, 32) / 32;
Brian Carlstrom413e89f2013-10-21 23:53:49 -070029}
30
31// TODO: replace excessive argument defaulting when we are at gcc 4.7
32// or later on host with delegating constructor support. Specifically,
33// starts_bits and storage_size/storage are mutually exclusive.
Brian Carlstromba150c32013-08-27 17:31:03 -070034BitVector::BitVector(uint32_t start_bits,
Brian Carlstrom413e89f2013-10-21 23:53:49 -070035 bool expandable,
36 Allocator* allocator,
37 uint32_t storage_size,
38 uint32_t* storage)
Ian Rogerse77493c2014-08-20 15:08:45 -070039 : storage_(storage),
Brian Carlstrom413e89f2013-10-21 23:53:49 -070040 storage_size_(storage_size),
Ian Rogerse77493c2014-08-20 15:08:45 -070041 allocator_(allocator),
42 expandable_(expandable) {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +010043 COMPILE_ASSERT(sizeof(*storage_) == kWordBytes, check_word_bytes);
44 COMPILE_ASSERT(sizeof(*storage_) * 8u == kWordBits, check_word_bits);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080045 if (storage_ == nullptr) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -070046 storage_size_ = BitsToWords(start_bits);
Vladimir Markoa5b8fde2014-05-23 15:16:44 +010047 storage_ = static_cast<uint32_t*>(allocator_->Alloc(storage_size_ * kWordBytes));
Brian Carlstrom413e89f2013-10-21 23:53:49 -070048 }
49}
50
51BitVector::~BitVector() {
52 allocator_->Free(storage_);
53}
54
Ian Rogerse77493c2014-08-20 15:08:45 -070055bool BitVector::SameBitsSet(const BitVector *src) const {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080056 int our_highest = GetHighestBitSet();
57 int src_highest = src->GetHighestBitSet();
58
59 // If the highest bit set is different, we are different.
60 if (our_highest != src_highest) {
Jean Christophe Beylerb5c9b402014-04-30 14:52:00 -070061 return false;
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080062 }
63
64 // If the highest bit set is -1, both are cleared, we are the same.
65 // 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 -070066 if (our_highest <= 0) {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080067 return true;
68 }
69
Jean Christophe Beylerb5c9b402014-04-30 14:52:00 -070070 // Get the highest bit set's cell's index
71 // No need of highest + 1 here because it can't be 0 so BitsToWords will work here.
72 int our_highest_index = BitsToWords(our_highest);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080073
74 // This memcmp is enough: we know that the highest bit set is the same for both:
75 // - Therefore, min_size goes up to at least that, we are thus comparing at least what we need to, but not less.
76 // ie. we are comparing all storage cells that could have difference, if both vectors have cells above our_highest_index,
77 // they are automatically at 0.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +010078 return (memcmp(storage_, src->GetRawStorage(), our_highest_index * kWordBytes) == 0);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080079}
80
Brian Carlstrom413e89f2013-10-21 23:53:49 -070081void BitVector::Intersect(const BitVector* src) {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080082 uint32_t src_storage_size = src->storage_size_;
83
84 // Get the minimum size between us and source.
85 uint32_t min_size = (storage_size_ < src_storage_size) ? storage_size_ : src_storage_size;
86
87 uint32_t idx;
88 for (idx = 0; idx < min_size; idx++) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -070089 storage_[idx] &= src->GetRawStorageWord(idx);
90 }
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080091
92 // Now, due to this being an intersection, there are two possibilities:
93 // - Either src was larger than us: we don't care, all upper bits would thus be 0.
94 // - Either we are larger than src: we don't care, all upper bits would have been 0 too.
95 // So all we need to do is set all remaining bits to 0.
96 for (; idx < storage_size_; idx++) {
97 storage_[idx] = 0;
98 }
Brian Carlstrom413e89f2013-10-21 23:53:49 -070099}
100
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100101bool BitVector::Union(const BitVector* src) {
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700102 // Get the highest bit to determine how much we need to expand.
103 int highest_bit = src->GetHighestBitSet();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100104 bool changed = false;
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800105
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700106 // If src has no bit set, we are done: there is no need for a union with src.
107 if (highest_bit == -1) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100108 return changed;
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700109 }
110
111 // Update src_size to how many cells we actually care about: where the bit is + 1.
112 uint32_t src_size = BitsToWords(highest_bit + 1);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800113
114 // Is the storage size smaller than src's?
115 if (storage_size_ < src_size) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100116 changed = true;
117
Ian Rogerse77493c2014-08-20 15:08:45 -0700118 EnsureSize(highest_bit);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800119
120 // Paranoid: storage size should be big enough to hold this bit now.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100121 DCHECK_LT(static_cast<uint32_t> (highest_bit), storage_size_ * kWordBits);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800122 }
123
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700124 for (uint32_t idx = 0; idx < src_size; idx++) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100125 uint32_t existing = storage_[idx];
126 uint32_t update = existing | src->GetRawStorageWord(idx);
127 if (existing != update) {
128 changed = true;
129 storage_[idx] = update;
130 }
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700131 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100132 return changed;
133}
134
135bool BitVector::UnionIfNotIn(const BitVector* union_with, const BitVector* not_in) {
136 // Get the highest bit to determine how much we need to expand.
137 int highest_bit = union_with->GetHighestBitSet();
138 bool changed = false;
139
140 // If src has no bit set, we are done: there is no need for a union with src.
141 if (highest_bit == -1) {
142 return changed;
143 }
144
145 // Update union_with_size to how many cells we actually care about: where the bit is + 1.
146 uint32_t union_with_size = BitsToWords(highest_bit + 1);
147
148 // Is the storage size smaller than src's?
149 if (storage_size_ < union_with_size) {
150 changed = true;
151
152 // Set it to reallocate.
153 SetBit(highest_bit);
154
155 // Paranoid: storage size should be big enough to hold this bit now.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100156 DCHECK_LT(static_cast<uint32_t> (highest_bit), storage_size_ * kWordBits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100157 }
158
159 uint32_t not_in_size = not_in->GetStorageSize();
160
161 uint32_t idx = 0;
162 for (; idx < std::min(not_in_size, union_with_size); idx++) {
163 uint32_t existing = storage_[idx];
164 uint32_t update = existing |
165 (union_with->GetRawStorageWord(idx) & ~not_in->GetRawStorageWord(idx));
166 if (existing != update) {
167 changed = true;
168 storage_[idx] = update;
169 }
170 }
171
172 for (; idx < union_with_size; idx++) {
173 uint32_t existing = storage_[idx];
174 uint32_t update = existing | union_with->GetRawStorageWord(idx);
175 if (existing != update) {
176 changed = true;
177 storage_[idx] = update;
178 }
179 }
180 return changed;
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700181}
182
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800183void BitVector::Subtract(const BitVector *src) {
Ian Rogerse77493c2014-08-20 15:08:45 -0700184 uint32_t src_size = src->storage_size_;
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800185
Ian Rogerse77493c2014-08-20 15:08:45 -0700186 // We only need to operate on bytes up to the smaller of the sizes of the two operands.
187 unsigned int min_size = (storage_size_ > src_size) ? src_size : storage_size_;
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800188
Ian Rogerse77493c2014-08-20 15:08:45 -0700189 // Difference until max, we know both accept it:
190 // There is no need to do more:
191 // If we are bigger than src, the upper bits are unchanged.
192 // If we are smaller than src, the non-existant upper bits are 0 and thus can't get subtracted.
193 for (uint32_t idx = 0; idx < min_size; idx++) {
194 storage_[idx] &= (~(src->GetRawStorageWord(idx)));
195 }
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800196}
197
Brian Carlstromba150c32013-08-27 17:31:03 -0700198uint32_t BitVector::NumSetBits() const {
199 uint32_t count = 0;
200 for (uint32_t word = 0; word < storage_size_; word++) {
Vladimir Marko81949632014-05-02 11:53:22 +0100201 count += POPCOUNT(storage_[word]);
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700202 }
203 return count;
204}
205
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100206uint32_t BitVector::NumSetBits(uint32_t end) const {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100207 DCHECK_LE(end, storage_size_ * kWordBits);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100208 return NumSetBits(storage_, end);
Brian Carlstromba150c32013-08-27 17:31:03 -0700209}
210
Brian Carlstromba150c32013-08-27 17:31:03 -0700211void BitVector::SetInitialBits(uint32_t num_bits) {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800212 // If num_bits is 0, clear everything.
213 if (num_bits == 0) {
214 ClearAllBits();
215 return;
216 }
217
218 // Set the highest bit we want to set to get the BitVector allocated if need be.
219 SetBit(num_bits - 1);
220
Brian Carlstromba150c32013-08-27 17:31:03 -0700221 uint32_t idx;
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800222 // We can set every storage element with -1.
Ian Rogerse77493c2014-08-20 15:08:45 -0700223 for (idx = 0; idx < WordIndex(num_bits); idx++) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700224 storage_[idx] = -1;
225 }
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800226
227 // Handle the potentially last few bits.
Brian Carlstromba150c32013-08-27 17:31:03 -0700228 uint32_t rem_num_bits = num_bits & 0x1f;
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800229 if (rem_num_bits != 0) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700230 storage_[idx] = (1 << rem_num_bits) - 1;
Vladimir Marko4812d432014-03-11 12:42:25 +0000231 ++idx;
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700232 }
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800233
234 // Now set the upper ones to 0.
235 for (; idx < storage_size_; idx++) {
236 storage_[idx] = 0;
237 }
238}
239
240int BitVector::GetHighestBitSet() const {
241 unsigned int max = storage_size_;
242 for (int idx = max - 1; idx >= 0; idx--) {
243 // If not 0, we have more work: check the bits.
244 uint32_t value = storage_[idx];
245
246 if (value != 0) {
Ian Rogerse77493c2014-08-20 15:08:45 -0700247 // Return highest bit set in value plus bits from previous storage indexes.
248 return 31 - CLZ(value) + (idx * kWordBits);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800249 }
250 }
251
252 // All zero, therefore return -1.
253 return -1;
254}
255
256void BitVector::Copy(const BitVector *src) {
257 // Get highest bit set, we only need to copy till then.
258 int highest_bit = src->GetHighestBitSet();
259
260 // If nothing is set, clear everything.
261 if (highest_bit == -1) {
262 ClearAllBits();
263 return;
264 }
265
266 // Set upper bit to ensure right size before copy.
267 SetBit(highest_bit);
268
269 // Now set until highest bit's storage.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100270 uint32_t size = 1 + (highest_bit / kWordBits);
271 memcpy(storage_, src->GetRawStorage(), kWordBytes * size);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800272
273 // Set upper bits to 0.
274 uint32_t left = storage_size_ - size;
275
276 if (left > 0) {
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100277 memset(storage_ + size, 0, kWordBytes * left);
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800278 }
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700279}
280
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100281uint32_t BitVector::NumSetBits(const uint32_t* storage, uint32_t end) {
Ian Rogerse77493c2014-08-20 15:08:45 -0700282 uint32_t word_end = WordIndex(end);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100283 uint32_t partial_word_bits = end & 0x1f;
284
285 uint32_t count = 0u;
286 for (uint32_t word = 0u; word < word_end; word++) {
Vladimir Marko81949632014-05-02 11:53:22 +0100287 count += POPCOUNT(storage[word]);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100288 }
289 if (partial_word_bits != 0u) {
Vladimir Marko81949632014-05-02 11:53:22 +0100290 count += POPCOUNT(storage[word_end] & ~(0xffffffffu << partial_word_bits));
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100291 }
292 return count;
293}
294
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100295void BitVector::Dump(std::ostream& os, const char *prefix) const {
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700296 std::ostringstream buffer;
Jean Christophe Beyler520f37b2014-05-22 15:43:50 -0700297 DumpHelper(prefix, buffer);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100298 os << buffer.str() << std::endl;
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700299}
300
Jean Christophe Beyler520f37b2014-05-22 15:43:50 -0700301void BitVector::DumpHelper(const char* prefix, std::ostringstream& buffer) const {
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700302 // Initialize it.
303 if (prefix != nullptr) {
304 buffer << prefix;
305 }
306
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100307 buffer << '(';
Jean Christophe Beyler014d77a2014-06-02 11:21:21 -0700308 for (size_t i = 0; i < storage_size_ * kWordBits; i++) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100309 buffer << IsBitSet(i);
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700310 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100311 buffer << ')';
Jean Christophe Beyler5afa08f2014-04-15 15:54:35 -0700312}
313
Ian Rogerse77493c2014-08-20 15:08:45 -0700314void BitVector::EnsureSize(uint32_t idx) {
315 if (idx >= storage_size_ * kWordBits) {
316 DCHECK(expandable_) << "Attempted to expand a non-expandable bitmap to position " << idx;
317
318 /* Round up to word boundaries for "idx+1" bits */
319 uint32_t new_size = BitsToWords(idx + 1);
320 DCHECK_GT(new_size, storage_size_);
321 uint32_t *new_storage =
322 static_cast<uint32_t*>(allocator_->Alloc(new_size * kWordBytes));
323 memcpy(new_storage, storage_, storage_size_ * kWordBytes);
324 // Zero out the new storage words.
325 memset(&new_storage[storage_size_], 0, (new_size - storage_size_) * kWordBytes);
326 // TOTO: collect stats on space wasted because of resize.
327 storage_ = new_storage;
328 storage_size_ = new_size;
329 }
330}
331
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700332} // namespace art