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 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 68 | uint32_t val = storage_[num >> 5] & check_masks[num & 0x1f]; |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 69 | return (val != 0); |
| 70 | } |
| 71 | |
| 72 | // Mark all bits bit as "clear". |
| 73 | void BitVector::ClearAllBits() { |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame^] | 74 | memset(storage_, 0, storage_size_ * sizeof(*storage_)); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 75 | } |
| 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 Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 82 | void BitVector::SetBit(uint32_t num) { |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame^] | 83 | if (num >= storage_size_ * sizeof(*storage_) * 8) { |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 84 | DCHECK(expandable_) << "Attempted to expand a non-expandable bitmap to position " << num; |
| 85 | |
| 86 | /* Round up to word boundaries for "num+1" bits */ |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 87 | uint32_t new_size = BitsToWords(num + 1); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 88 | DCHECK_GT(new_size, storage_size_); |
| 89 | uint32_t *new_storage = |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame^] | 90 | static_cast<uint32_t*>(allocator_->Alloc(new_size * sizeof(*storage_))); |
| 91 | memcpy(new_storage, storage_, storage_size_ * sizeof(*storage_)); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 92 | // Zero out the new storage words. |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame^] | 93 | memset(&new_storage[storage_size_], 0, (new_size - storage_size_) * sizeof(*storage_)); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 94 | // 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 Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 103 | void BitVector::ClearBit(uint32_t num) { |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame^] | 104 | // 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 Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame^] | 111 | bool 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 Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 137 | void BitVector::Intersect(const BitVector* src) { |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame^] | 138 | 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 Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 145 | storage_[idx] &= src->GetRawStorageWord(idx); |
| 146 | } |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame^] | 147 | |
| 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 Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /* |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame^] | 158 | * Union with another bit vector. |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 159 | */ |
| 160 | void BitVector::Union(const BitVector* src) { |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame^] | 161 | 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 Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 188 | storage_[idx] |= src->GetRawStorageWord(idx); |
| 189 | } |
| 190 | } |
| 191 | |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame^] | 192 | void 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 Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 207 | // Count the number of bits that are set. |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 208 | uint32_t BitVector::NumSetBits() const { |
| 209 | uint32_t count = 0; |
| 210 | for (uint32_t word = 0; word < storage_size_; word++) { |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 211 | count += __builtin_popcount(storage_[word]); |
| 212 | } |
| 213 | return count; |
| 214 | } |
| 215 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 216 | // Count the number of bits that are set up through and including num. |
| 217 | uint32_t BitVector::NumSetBits(uint32_t num) const { |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame^] | 218 | DCHECK_LT(num, storage_size_ * sizeof(*storage_) * 8); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 219 | 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 | |
| 239 | BitVector::Iterator* BitVector::GetIterator() const { |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 240 | 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 Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 248 | void BitVector::SetInitialBits(uint32_t num_bits) { |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame^] | 249 | // 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 Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 258 | uint32_t idx; |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame^] | 259 | // We can set every storage element with -1. |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 260 | for (idx = 0; idx < (num_bits >> 5); idx++) { |
| 261 | storage_[idx] = -1; |
| 262 | } |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame^] | 263 | |
| 264 | // Handle the potentially last few bits. |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 265 | uint32_t rem_num_bits = num_bits & 0x1f; |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame^] | 266 | if (rem_num_bits != 0) { |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 267 | storage_[idx] = (1 << rem_num_bits) - 1; |
| 268 | } |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame^] | 269 | |
| 270 | // Now set the upper ones to 0. |
| 271 | for (; idx < storage_size_; idx++) { |
| 272 | storage_[idx] = 0; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | int BitVector::GetHighestBitSet() const { |
| 277 | unsigned int max = storage_size_; |
| 278 | for (int idx = max - 1; idx >= 0; idx--) { |
| 279 | // If not 0, we have more work: check the bits. |
| 280 | uint32_t value = storage_[idx]; |
| 281 | |
| 282 | if (value != 0) { |
| 283 | // Shift right for the counting. |
| 284 | value /= 2; |
| 285 | |
| 286 | int cnt = 0; |
| 287 | |
| 288 | // Count the bits. |
| 289 | while (value > 0) { |
| 290 | value /= 2; |
| 291 | cnt++; |
| 292 | } |
| 293 | |
| 294 | // Return cnt + how many storage units still remain * the number of bits per unit. |
| 295 | int res = cnt + (idx * (sizeof(*storage_) * 8)); |
| 296 | return res; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | // All zero, therefore return -1. |
| 301 | return -1; |
| 302 | } |
| 303 | |
| 304 | void BitVector::Copy(const BitVector *src) { |
| 305 | // Get highest bit set, we only need to copy till then. |
| 306 | int highest_bit = src->GetHighestBitSet(); |
| 307 | |
| 308 | // If nothing is set, clear everything. |
| 309 | if (highest_bit == -1) { |
| 310 | ClearAllBits(); |
| 311 | return; |
| 312 | } |
| 313 | |
| 314 | // Set upper bit to ensure right size before copy. |
| 315 | SetBit(highest_bit); |
| 316 | |
| 317 | // Now set until highest bit's storage. |
| 318 | uint32_t size = 1 + (highest_bit / (sizeof(*storage_) * 8)); |
| 319 | memcpy(storage_, src->GetRawStorage(), sizeof(*storage_) * size); |
| 320 | |
| 321 | // Set upper bits to 0. |
| 322 | uint32_t left = storage_size_ - size; |
| 323 | |
| 324 | if (left > 0) { |
| 325 | memset(storage_ + size, 0, sizeof(*storage_) * left); |
| 326 | } |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | } // namespace art |