blob: d8ef962524b4b092d2796c62632a7e2ba84824fe [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 Beylerad0d30a2014-01-16 09:00:18 -0800160 uint32_t src_size = src->storage_size_;
161
162 // Get our size, we use this variable for the last loop of the method:
163 // - It can change in the if block if src is of a different size.
164 uint32_t size = storage_size_;
165
166 // Is the storage size smaller than src's?
167 if (storage_size_ < src_size) {
168 // Get the highest bit to determine how much we need to expand.
169 int highest_bit = src->GetHighestBitSet();
170
171 // If src has no bit set, we are done: there is no need for a union with src.
172 if (highest_bit == -1) {
173 return;
174 }
175
176 // Set it to reallocate.
177 SetBit(highest_bit);
178
179 // Paranoid: storage size should be big enough to hold this bit now.
180 DCHECK_LT(static_cast<uint32_t> (highest_bit), storage_size_ * sizeof(*(storage_)) * 8);
181
182 // Update the size, our size can now not be bigger than the src size
183 size = storage_size_;
184 }
185
186 for (uint32_t idx = 0; idx < size; idx++) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700187 storage_[idx] |= src->GetRawStorageWord(idx);
188 }
189}
190
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800191void BitVector::Subtract(const BitVector *src) {
192 uint32_t src_size = src->storage_size_;
193
194 // We only need to operate on bytes up to the smaller of the sizes of the two operands.
195 unsigned int min_size = (storage_size_ > src_size) ? src_size : storage_size_;
196
197 // Difference until max, we know both accept it:
198 // There is no need to do more:
199 // If we are bigger than src, the upper bits are unchanged.
200 // If we are smaller than src, the non-existant upper bits are 0 and thus can't get subtracted.
201 for (uint32_t idx = 0; idx < min_size; idx++) {
202 storage_[idx] &= (~(src->GetRawStorageWord(idx)));
203 }
204}
205
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700206// Count the number of bits that are set.
Brian Carlstromba150c32013-08-27 17:31:03 -0700207uint32_t BitVector::NumSetBits() const {
208 uint32_t count = 0;
209 for (uint32_t word = 0; word < storage_size_; word++) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700210 count += __builtin_popcount(storage_[word]);
211 }
212 return count;
213}
214
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100215// Count the number of bits that are set in range [0, end).
216uint32_t BitVector::NumSetBits(uint32_t end) const {
217 DCHECK_LE(end, storage_size_ * sizeof(*storage_) * 8);
218 return NumSetBits(storage_, end);
Brian Carlstromba150c32013-08-27 17:31:03 -0700219}
220
221BitVector::Iterator* BitVector::GetIterator() const {
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700222 return new (allocator_) Iterator(this);
223}
224
225/*
226 * Mark specified number of bits as "set". Cannot set all bits like ClearAll
227 * since there might be unused bits - setting those to one will confuse the
228 * iterator.
229 */
Brian Carlstromba150c32013-08-27 17:31:03 -0700230void BitVector::SetInitialBits(uint32_t num_bits) {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800231 // If num_bits is 0, clear everything.
232 if (num_bits == 0) {
233 ClearAllBits();
234 return;
235 }
236
237 // Set the highest bit we want to set to get the BitVector allocated if need be.
238 SetBit(num_bits - 1);
239
Brian Carlstromba150c32013-08-27 17:31:03 -0700240 uint32_t idx;
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800241 // We can set every storage element with -1.
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700242 for (idx = 0; idx < (num_bits >> 5); idx++) {
243 storage_[idx] = -1;
244 }
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800245
246 // Handle the potentially last few bits.
Brian Carlstromba150c32013-08-27 17:31:03 -0700247 uint32_t rem_num_bits = num_bits & 0x1f;
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800248 if (rem_num_bits != 0) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700249 storage_[idx] = (1 << rem_num_bits) - 1;
Vladimir Marko4812d432014-03-11 12:42:25 +0000250 ++idx;
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700251 }
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800252
253 // Now set the upper ones to 0.
254 for (; idx < storage_size_; idx++) {
255 storage_[idx] = 0;
256 }
257}
258
259int BitVector::GetHighestBitSet() const {
260 unsigned int max = storage_size_;
261 for (int idx = max - 1; idx >= 0; idx--) {
262 // If not 0, we have more work: check the bits.
263 uint32_t value = storage_[idx];
264
265 if (value != 0) {
266 // Shift right for the counting.
267 value /= 2;
268
269 int cnt = 0;
270
271 // Count the bits.
272 while (value > 0) {
273 value /= 2;
274 cnt++;
275 }
276
277 // Return cnt + how many storage units still remain * the number of bits per unit.
278 int res = cnt + (idx * (sizeof(*storage_) * 8));
279 return res;
280 }
281 }
282
283 // All zero, therefore return -1.
284 return -1;
285}
286
287void BitVector::Copy(const BitVector *src) {
288 // Get highest bit set, we only need to copy till then.
289 int highest_bit = src->GetHighestBitSet();
290
291 // If nothing is set, clear everything.
292 if (highest_bit == -1) {
293 ClearAllBits();
294 return;
295 }
296
297 // Set upper bit to ensure right size before copy.
298 SetBit(highest_bit);
299
300 // Now set until highest bit's storage.
301 uint32_t size = 1 + (highest_bit / (sizeof(*storage_) * 8));
302 memcpy(storage_, src->GetRawStorage(), sizeof(*storage_) * size);
303
304 // Set upper bits to 0.
305 uint32_t left = storage_size_ - size;
306
307 if (left > 0) {
308 memset(storage_ + size, 0, sizeof(*storage_) * left);
309 }
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700310}
311
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100312bool BitVector::IsBitSet(const uint32_t* storage, uint32_t num) {
313 uint32_t val = storage[num >> 5] & check_masks[num & 0x1f];
314 return (val != 0);
315}
316
317uint32_t BitVector::NumSetBits(const uint32_t* storage, uint32_t end) {
318 uint32_t word_end = end >> 5;
319 uint32_t partial_word_bits = end & 0x1f;
320
321 uint32_t count = 0u;
322 for (uint32_t word = 0u; word < word_end; word++) {
323 count += __builtin_popcount(storage[word]);
324 }
325 if (partial_word_bits != 0u) {
326 count += __builtin_popcount(storage[word_end] & ~(0xffffffffu << partial_word_bits));
327 }
328 return count;
329}
330
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700331} // namespace art