blob: 590835e059b054ed16d9d9985eeab3bf02bb9124 [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
Brian Carlstromba150c32013-08-27 17:31:03 -070068 uint32_t val = storage_[num >> 5] & check_masks[num & 0x1f];
Brian Carlstrom413e89f2013-10-21 23:53:49 -070069 return (val != 0);
70}
71
72// Mark all bits bit as "clear".
73void BitVector::ClearAllBits() {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080074 memset(storage_, 0, storage_size_ * sizeof(*storage_));
Brian Carlstrom413e89f2013-10-21 23:53:49 -070075}
76
77// Mark the specified bit as "set".
78/*
79 * TUNING: this could have pathologically bad growth/expand behavior. Make sure we're
80 * not using it badly or change resize mechanism.
81 */
Brian Carlstromba150c32013-08-27 17:31:03 -070082void BitVector::SetBit(uint32_t num) {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080083 if (num >= storage_size_ * sizeof(*storage_) * 8) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -070084 DCHECK(expandable_) << "Attempted to expand a non-expandable bitmap to position " << num;
85
86 /* Round up to word boundaries for "num+1" bits */
Brian Carlstromba150c32013-08-27 17:31:03 -070087 uint32_t new_size = BitsToWords(num + 1);
Brian Carlstrom413e89f2013-10-21 23:53:49 -070088 DCHECK_GT(new_size, storage_size_);
89 uint32_t *new_storage =
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080090 static_cast<uint32_t*>(allocator_->Alloc(new_size * sizeof(*storage_)));
91 memcpy(new_storage, storage_, storage_size_ * sizeof(*storage_));
Brian Carlstrom413e89f2013-10-21 23:53:49 -070092 // Zero out the new storage words.
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -080093 memset(&new_storage[storage_size_], 0, (new_size - storage_size_) * sizeof(*storage_));
Brian Carlstrom413e89f2013-10-21 23:53:49 -070094 // TOTO: collect stats on space wasted because of resize.
95 storage_ = new_storage;
96 storage_size_ = new_size;
97 }
98
99 storage_[num >> 5] |= check_masks[num & 0x1f];
100}
101
102// Mark the specified bit as "unset".
Brian Carlstromba150c32013-08-27 17:31:03 -0700103void BitVector::ClearBit(uint32_t num) {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800104 // If the index is over the size, we don't have to do anything, it is cleared.
105 if (num < storage_size_ * sizeof(*storage_) * 8) {
106 // Otherwise, go ahead and clear it.
107 storage_[num >> 5] &= ~check_masks[num & 0x1f];
108 }
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700109}
110
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800111bool BitVector::SameBitsSet(const BitVector *src) {
112 int our_highest = GetHighestBitSet();
113 int src_highest = src->GetHighestBitSet();
114
115 // If the highest bit set is different, we are different.
116 if (our_highest != src_highest) {
117 return true;
118 }
119
120 // If the highest bit set is -1, both are cleared, we are the same.
121 // If the highest bit set is 0, both have a unique bit set, we are the same.
122 if (our_highest >= 0) {
123 return true;
124 }
125
126 // Get the highest bit set's cell's index.
127 int our_highest_index = (our_highest >> 5);
128
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.
133 return (memcmp(storage_, src->GetRawStorage(), our_highest_index * sizeof(*storage_)) != 0);
134}
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 Beylerad0d30a2014-01-16 09:00:18 -0800161 uint32_t src_size = src->storage_size_;
162
163 // Get our size, we use this variable for the last loop of the method:
164 // - It can change in the if block if src is of a different size.
165 uint32_t size = storage_size_;
166
167 // Is the storage size smaller than src's?
168 if (storage_size_ < src_size) {
169 // Get the highest bit to determine how much we need to expand.
170 int highest_bit = src->GetHighestBitSet();
171
172 // If src has no bit set, we are done: there is no need for a union with src.
173 if (highest_bit == -1) {
174 return;
175 }
176
177 // Set it to reallocate.
178 SetBit(highest_bit);
179
180 // Paranoid: storage size should be big enough to hold this bit now.
181 DCHECK_LT(static_cast<uint32_t> (highest_bit), storage_size_ * sizeof(*(storage_)) * 8);
182
183 // Update the size, our size can now not be bigger than the src size
184 size = storage_size_;
185 }
186
187 for (uint32_t idx = 0; idx < size; idx++) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700188 storage_[idx] |= src->GetRawStorageWord(idx);
189 }
190}
191
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800192void BitVector::Subtract(const BitVector *src) {
193 uint32_t src_size = src->storage_size_;
194
195 // We only need to operate on bytes up to the smaller of the sizes of the two operands.
196 unsigned int min_size = (storage_size_ > src_size) ? src_size : storage_size_;
197
198 // Difference until max, we know both accept it:
199 // There is no need to do more:
200 // If we are bigger than src, the upper bits are unchanged.
201 // If we are smaller than src, the non-existant upper bits are 0 and thus can't get subtracted.
202 for (uint32_t idx = 0; idx < min_size; idx++) {
203 storage_[idx] &= (~(src->GetRawStorageWord(idx)));
204 }
205}
206
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700207// Count the number of bits that are set.
Brian Carlstromba150c32013-08-27 17:31:03 -0700208uint32_t BitVector::NumSetBits() const {
209 uint32_t count = 0;
210 for (uint32_t word = 0; word < storage_size_; word++) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700211 count += __builtin_popcount(storage_[word]);
212 }
213 return count;
214}
215
Brian Carlstromba150c32013-08-27 17:31:03 -0700216// Count the number of bits that are set up through and including num.
217uint32_t BitVector::NumSetBits(uint32_t num) const {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800218 DCHECK_LT(num, storage_size_ * sizeof(*storage_) * 8);
Brian Carlstromba150c32013-08-27 17:31:03 -0700219 uint32_t last_word = num >> 5;
220 uint32_t partial_word_bits = num & 0x1f;
221
222 // partial_word_bits | # | | | partial_word_mask
223 // 00000 | 0 | 0xffffffff >> (31 - 0) | (1 << (0 + 1)) - 1 | 0x00000001
224 // 00001 | 1 | 0xffffffff >> (31 - 1) | (1 << (1 + 1)) - 1 | 0x00000003
225 // 00010 | 2 | 0xffffffff >> (31 - 2) | (1 << (2 + 1)) - 1 | 0x00000007
226 // ..... |
227 // 11110 | 30 | 0xffffffff >> (31 - 30) | (1 << (30 + 1)) - 1 | 0x7fffffff
228 // 11111 | 31 | 0xffffffff >> (31 - 31) | last_full_word++ | 0xffffffff
229 uint32_t partial_word_mask = 0xffffffff >> (0x1f - partial_word_bits);
230
231 uint32_t count = 0;
232 for (uint32_t word = 0; word < last_word; word++) {
233 count += __builtin_popcount(storage_[word]);
234 }
235 count += __builtin_popcount(storage_[last_word] & partial_word_mask);
236 return count;
237}
238
239BitVector::Iterator* BitVector::GetIterator() const {
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700240 return new (allocator_) Iterator(this);
241}
242
243/*
244 * Mark specified number of bits as "set". Cannot set all bits like ClearAll
245 * since there might be unused bits - setting those to one will confuse the
246 * iterator.
247 */
Brian Carlstromba150c32013-08-27 17:31:03 -0700248void BitVector::SetInitialBits(uint32_t num_bits) {
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800249 // If num_bits is 0, clear everything.
250 if (num_bits == 0) {
251 ClearAllBits();
252 return;
253 }
254
255 // Set the highest bit we want to set to get the BitVector allocated if need be.
256 SetBit(num_bits - 1);
257
Brian Carlstromba150c32013-08-27 17:31:03 -0700258 uint32_t idx;
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800259 // We can set every storage element with -1.
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700260 for (idx = 0; idx < (num_bits >> 5); idx++) {
261 storage_[idx] = -1;
262 }
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800263
264 // Handle the potentially last few bits.
Brian Carlstromba150c32013-08-27 17:31:03 -0700265 uint32_t rem_num_bits = num_bits & 0x1f;
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800266 if (rem_num_bits != 0) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700267 storage_[idx] = (1 << rem_num_bits) - 1;
Vladimir Marko4812d432014-03-11 12:42:25 +0000268 ++idx;
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700269 }
Jean Christophe Beylerad0d30a2014-01-16 09:00:18 -0800270
271 // Now set the upper ones to 0.
272 for (; idx < storage_size_; idx++) {
273 storage_[idx] = 0;
274 }
275}
276
277int BitVector::GetHighestBitSet() const {
278 unsigned int max = storage_size_;
279 for (int idx = max - 1; idx >= 0; idx--) {
280 // If not 0, we have more work: check the bits.
281 uint32_t value = storage_[idx];
282
283 if (value != 0) {
284 // Shift right for the counting.
285 value /= 2;
286
287 int cnt = 0;
288
289 // Count the bits.
290 while (value > 0) {
291 value /= 2;
292 cnt++;
293 }
294
295 // Return cnt + how many storage units still remain * the number of bits per unit.
296 int res = cnt + (idx * (sizeof(*storage_) * 8));
297 return res;
298 }
299 }
300
301 // All zero, therefore return -1.
302 return -1;
303}
304
305void BitVector::Copy(const BitVector *src) {
306 // Get highest bit set, we only need to copy till then.
307 int highest_bit = src->GetHighestBitSet();
308
309 // If nothing is set, clear everything.
310 if (highest_bit == -1) {
311 ClearAllBits();
312 return;
313 }
314
315 // Set upper bit to ensure right size before copy.
316 SetBit(highest_bit);
317
318 // Now set until highest bit's storage.
319 uint32_t size = 1 + (highest_bit / (sizeof(*storage_) * 8));
320 memcpy(storage_, src->GetRawStorage(), sizeof(*storage_) * size);
321
322 // Set upper bits to 0.
323 uint32_t left = storage_size_ - size;
324
325 if (left > 0) {
326 memset(storage_ + size, 0, sizeof(*storage_) * left);
327 }
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700328}
329
330} // namespace art