Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 19 | namespace art { |
| 20 | |
| 21 | // TODO: profile to make sure this is still a win relative to just using shifted masks. |
| 22 | static 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 Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 31 | static inline uint32_t BitsToWords(uint32_t bits) { |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 32 | 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 Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 38 | BitVector::BitVector(uint32_t start_bits, |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 39 | 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 Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 47 | DCHECK_EQ(sizeof(*storage_), 4U); // Assuming 32-bit units. |
| 48 | if (storage_ == nullptr) { |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 49 | storage_size_ = BitsToWords(start_bits); |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 50 | storage_ = static_cast<uint32_t*>(allocator_->Alloc(storage_size_ * sizeof(*storage_))); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | BitVector::~BitVector() { |
| 55 | allocator_->Free(storage_); |
| 56 | } |
| 57 | |
| 58 | /* |
| 59 | * Determine whether or not the specified bit is set. |
| 60 | */ |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 61 | bool BitVector::IsBitSet(uint32_t num) const { |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 62 | // 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 Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 67 | |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 68 | return IsBitSet(storage_, num); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // Mark all bits bit as "clear". |
| 72 | void BitVector::ClearAllBits() { |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 73 | memset(storage_, 0, storage_size_ * sizeof(*storage_)); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 74 | } |
| 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 Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 81 | void BitVector::SetBit(uint32_t num) { |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 82 | if (num >= storage_size_ * sizeof(*storage_) * 8) { |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 83 | DCHECK(expandable_) << "Attempted to expand a non-expandable bitmap to position " << num; |
| 84 | |
| 85 | /* Round up to word boundaries for "num+1" bits */ |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 86 | uint32_t new_size = BitsToWords(num + 1); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 87 | DCHECK_GT(new_size, storage_size_); |
| 88 | uint32_t *new_storage = |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 89 | static_cast<uint32_t*>(allocator_->Alloc(new_size * sizeof(*storage_))); |
| 90 | memcpy(new_storage, storage_, storage_size_ * sizeof(*storage_)); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 91 | // Zero out the new storage words. |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 92 | memset(&new_storage[storage_size_], 0, (new_size - storage_size_) * sizeof(*storage_)); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 93 | // 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 Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 102 | void BitVector::ClearBit(uint32_t num) { |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 103 | // 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 Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 110 | bool 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 Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 136 | void BitVector::Intersect(const BitVector* src) { |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 137 | 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 Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 144 | storage_[idx] &= src->GetRawStorageWord(idx); |
| 145 | } |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 146 | |
| 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 Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /* |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 157 | * Union with another bit vector. |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 158 | */ |
| 159 | void BitVector::Union(const BitVector* src) { |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame^] | 160 | // Get the highest bit to determine how much we need to expand. |
| 161 | int highest_bit = src->GetHighestBitSet(); |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 162 | |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame^] | 163 | // 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 Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 170 | |
| 171 | // Is the storage size smaller than src's? |
| 172 | if (storage_size_ < src_size) { |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 173 | // 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 Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame^] | 180 | for (uint32_t idx = 0; idx < src_size; idx++) { |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 181 | storage_[idx] |= src->GetRawStorageWord(idx); |
| 182 | } |
| 183 | } |
| 184 | |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 185 | void 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 Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 200 | // Count the number of bits that are set. |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 201 | uint32_t BitVector::NumSetBits() const { |
| 202 | uint32_t count = 0; |
| 203 | for (uint32_t word = 0; word < storage_size_; word++) { |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 204 | count += __builtin_popcount(storage_[word]); |
| 205 | } |
| 206 | return count; |
| 207 | } |
| 208 | |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 209 | // Count the number of bits that are set in range [0, end). |
| 210 | uint32_t BitVector::NumSetBits(uint32_t end) const { |
| 211 | DCHECK_LE(end, storage_size_ * sizeof(*storage_) * 8); |
| 212 | return NumSetBits(storage_, end); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | BitVector::Iterator* BitVector::GetIterator() const { |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 216 | 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 Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 224 | void BitVector::SetInitialBits(uint32_t num_bits) { |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 225 | // 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 Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 234 | uint32_t idx; |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 235 | // We can set every storage element with -1. |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 236 | for (idx = 0; idx < (num_bits >> 5); idx++) { |
| 237 | storage_[idx] = -1; |
| 238 | } |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 239 | |
| 240 | // Handle the potentially last few bits. |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 241 | uint32_t rem_num_bits = num_bits & 0x1f; |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 242 | if (rem_num_bits != 0) { |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 243 | storage_[idx] = (1 << rem_num_bits) - 1; |
Vladimir Marko | 4812d43 | 2014-03-11 12:42:25 +0000 | [diff] [blame] | 244 | ++idx; |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 245 | } |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 246 | |
| 247 | // Now set the upper ones to 0. |
| 248 | for (; idx < storage_size_; idx++) { |
| 249 | storage_[idx] = 0; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | int 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 Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame^] | 281 | bool 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 Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 298 | void 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 Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 323 | bool 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 | |
| 328 | uint32_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++) { |
| 334 | count += __builtin_popcount(storage[word]); |
| 335 | } |
| 336 | if (partial_word_bits != 0u) { |
| 337 | count += __builtin_popcount(storage[word_end] & ~(0xffffffffu << partial_word_bits)); |
| 338 | } |
| 339 | return count; |
| 340 | } |
| 341 | |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame^] | 342 | void BitVector::Dump(std::ostream& os, const char *prefix) { |
| 343 | std::ostringstream buffer; |
| 344 | DumpHelper(buffer, prefix); |
| 345 | os << buffer << std::endl; |
| 346 | } |
| 347 | |
| 348 | void 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 | |
| 364 | void 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 Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 379 | } // namespace art |