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), |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 46 | storage_(storage), |
| 47 | number_of_bits_(start_bits) { |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame^] | 48 | COMPILE_ASSERT(sizeof(*storage_) == kWordBytes, check_word_bytes); |
| 49 | COMPILE_ASSERT(sizeof(*storage_) * 8u == kWordBits, check_word_bits); |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 50 | if (storage_ == nullptr) { |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 51 | storage_size_ = BitsToWords(start_bits); |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame^] | 52 | storage_ = static_cast<uint32_t*>(allocator_->Alloc(storage_size_ * kWordBytes)); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
| 56 | BitVector::~BitVector() { |
| 57 | allocator_->Free(storage_); |
| 58 | } |
| 59 | |
| 60 | /* |
| 61 | * Determine whether or not the specified bit is set. |
| 62 | */ |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 63 | bool BitVector::IsBitSet(uint32_t num) const { |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 64 | // If the index is over the size: |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame^] | 65 | if (num >= storage_size_ * kWordBits) { |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 66 | // Whether it is expandable or not, this bit does not exist: thus it is not set. |
| 67 | return false; |
| 68 | } |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 69 | |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 70 | return IsBitSet(storage_, num); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // Mark all bits bit as "clear". |
| 74 | void BitVector::ClearAllBits() { |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame^] | 75 | memset(storage_, 0, storage_size_ * kWordBytes); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | // Mark the specified bit as "set". |
| 79 | /* |
| 80 | * TUNING: this could have pathologically bad growth/expand behavior. Make sure we're |
| 81 | * not using it badly or change resize mechanism. |
| 82 | */ |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 83 | void BitVector::SetBit(uint32_t num) { |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame^] | 84 | if (num >= storage_size_ * kWordBits) { |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 85 | DCHECK(expandable_) << "Attempted to expand a non-expandable bitmap to position " << num; |
| 86 | |
| 87 | /* Round up to word boundaries for "num+1" bits */ |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 88 | uint32_t new_size = BitsToWords(num + 1); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 89 | DCHECK_GT(new_size, storage_size_); |
| 90 | uint32_t *new_storage = |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame^] | 91 | static_cast<uint32_t*>(allocator_->Alloc(new_size * kWordBytes)); |
| 92 | memcpy(new_storage, storage_, storage_size_ * kWordBytes); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 93 | // Zero out the new storage words. |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame^] | 94 | memset(&new_storage[storage_size_], 0, (new_size - storage_size_) * kWordBytes); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 95 | // TOTO: collect stats on space wasted because of resize. |
| 96 | storage_ = new_storage; |
| 97 | storage_size_ = new_size; |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 98 | number_of_bits_ = num; |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | storage_[num >> 5] |= check_masks[num & 0x1f]; |
| 102 | } |
| 103 | |
| 104 | // Mark the specified bit as "unset". |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 105 | void BitVector::ClearBit(uint32_t num) { |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 106 | // If the index is over the size, we don't have to do anything, it is cleared. |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame^] | 107 | if (num < storage_size_ * kWordBits) { |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 108 | // Otherwise, go ahead and clear it. |
| 109 | storage_[num >> 5] &= ~check_masks[num & 0x1f]; |
| 110 | } |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 113 | bool BitVector::SameBitsSet(const BitVector *src) { |
| 114 | int our_highest = GetHighestBitSet(); |
| 115 | int src_highest = src->GetHighestBitSet(); |
| 116 | |
| 117 | // If the highest bit set is different, we are different. |
| 118 | if (our_highest != src_highest) { |
Jean Christophe Beyler | b5c9b40 | 2014-04-30 14:52:00 -0700 | [diff] [blame] | 119 | return false; |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // If the highest bit set is -1, both are cleared, we are the same. |
| 123 | // If the highest bit set is 0, both have a unique bit set, we are the same. |
Jean Christophe Beyler | b5c9b40 | 2014-04-30 14:52:00 -0700 | [diff] [blame] | 124 | if (our_highest <= 0) { |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 125 | return true; |
| 126 | } |
| 127 | |
Jean Christophe Beyler | b5c9b40 | 2014-04-30 14:52:00 -0700 | [diff] [blame] | 128 | // Get the highest bit set's cell's index |
| 129 | // No need of highest + 1 here because it can't be 0 so BitsToWords will work here. |
| 130 | int our_highest_index = BitsToWords(our_highest); |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 131 | |
| 132 | // This memcmp is enough: we know that the highest bit set is the same for both: |
| 133 | // - Therefore, min_size goes up to at least that, we are thus comparing at least what we need to, but not less. |
| 134 | // ie. we are comparing all storage cells that could have difference, if both vectors have cells above our_highest_index, |
| 135 | // they are automatically at 0. |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame^] | 136 | return (memcmp(storage_, src->GetRawStorage(), our_highest_index * kWordBytes) == 0); |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | // Intersect with another bit vector. |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 140 | void BitVector::Intersect(const BitVector* src) { |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 141 | uint32_t src_storage_size = src->storage_size_; |
| 142 | |
| 143 | // Get the minimum size between us and source. |
| 144 | uint32_t min_size = (storage_size_ < src_storage_size) ? storage_size_ : src_storage_size; |
| 145 | |
| 146 | uint32_t idx; |
| 147 | for (idx = 0; idx < min_size; idx++) { |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 148 | storage_[idx] &= src->GetRawStorageWord(idx); |
| 149 | } |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 150 | |
| 151 | // Now, due to this being an intersection, there are two possibilities: |
| 152 | // - Either src was larger than us: we don't care, all upper bits would thus be 0. |
| 153 | // - Either we are larger than src: we don't care, all upper bits would have been 0 too. |
| 154 | // So all we need to do is set all remaining bits to 0. |
| 155 | for (; idx < storage_size_; idx++) { |
| 156 | storage_[idx] = 0; |
| 157 | } |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /* |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 161 | * Union with another bit vector. |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 162 | */ |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 163 | bool BitVector::Union(const BitVector* src) { |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame] | 164 | // Get the highest bit to determine how much we need to expand. |
| 165 | int highest_bit = src->GetHighestBitSet(); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 166 | bool changed = false; |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 167 | |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame] | 168 | // If src has no bit set, we are done: there is no need for a union with src. |
| 169 | if (highest_bit == -1) { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 170 | return changed; |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | // Update src_size to how many cells we actually care about: where the bit is + 1. |
| 174 | uint32_t src_size = BitsToWords(highest_bit + 1); |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 175 | |
| 176 | // Is the storage size smaller than src's? |
| 177 | if (storage_size_ < src_size) { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 178 | changed = true; |
| 179 | |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 180 | // Set it to reallocate. |
| 181 | SetBit(highest_bit); |
| 182 | |
| 183 | // Paranoid: storage size should be big enough to hold this bit now. |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame^] | 184 | DCHECK_LT(static_cast<uint32_t> (highest_bit), storage_size_ * kWordBits); |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame] | 187 | for (uint32_t idx = 0; idx < src_size; idx++) { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 188 | uint32_t existing = storage_[idx]; |
| 189 | uint32_t update = existing | src->GetRawStorageWord(idx); |
| 190 | if (existing != update) { |
| 191 | changed = true; |
| 192 | storage_[idx] = update; |
| 193 | } |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 194 | } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 195 | return changed; |
| 196 | } |
| 197 | |
| 198 | bool BitVector::UnionIfNotIn(const BitVector* union_with, const BitVector* not_in) { |
| 199 | // Get the highest bit to determine how much we need to expand. |
| 200 | int highest_bit = union_with->GetHighestBitSet(); |
| 201 | bool changed = false; |
| 202 | |
| 203 | // If src has no bit set, we are done: there is no need for a union with src. |
| 204 | if (highest_bit == -1) { |
| 205 | return changed; |
| 206 | } |
| 207 | |
| 208 | // Update union_with_size to how many cells we actually care about: where the bit is + 1. |
| 209 | uint32_t union_with_size = BitsToWords(highest_bit + 1); |
| 210 | |
| 211 | // Is the storage size smaller than src's? |
| 212 | if (storage_size_ < union_with_size) { |
| 213 | changed = true; |
| 214 | |
| 215 | // Set it to reallocate. |
| 216 | SetBit(highest_bit); |
| 217 | |
| 218 | // Paranoid: storage size should be big enough to hold this bit now. |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame^] | 219 | DCHECK_LT(static_cast<uint32_t> (highest_bit), storage_size_ * kWordBits); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | uint32_t not_in_size = not_in->GetStorageSize(); |
| 223 | |
| 224 | uint32_t idx = 0; |
| 225 | for (; idx < std::min(not_in_size, union_with_size); idx++) { |
| 226 | uint32_t existing = storage_[idx]; |
| 227 | uint32_t update = existing | |
| 228 | (union_with->GetRawStorageWord(idx) & ~not_in->GetRawStorageWord(idx)); |
| 229 | if (existing != update) { |
| 230 | changed = true; |
| 231 | storage_[idx] = update; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | for (; idx < union_with_size; idx++) { |
| 236 | uint32_t existing = storage_[idx]; |
| 237 | uint32_t update = existing | union_with->GetRawStorageWord(idx); |
| 238 | if (existing != update) { |
| 239 | changed = true; |
| 240 | storage_[idx] = update; |
| 241 | } |
| 242 | } |
| 243 | return changed; |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 246 | void BitVector::Subtract(const BitVector *src) { |
| 247 | uint32_t src_size = src->storage_size_; |
| 248 | |
| 249 | // We only need to operate on bytes up to the smaller of the sizes of the two operands. |
| 250 | unsigned int min_size = (storage_size_ > src_size) ? src_size : storage_size_; |
| 251 | |
| 252 | // Difference until max, we know both accept it: |
| 253 | // There is no need to do more: |
| 254 | // If we are bigger than src, the upper bits are unchanged. |
| 255 | // If we are smaller than src, the non-existant upper bits are 0 and thus can't get subtracted. |
| 256 | for (uint32_t idx = 0; idx < min_size; idx++) { |
| 257 | storage_[idx] &= (~(src->GetRawStorageWord(idx))); |
| 258 | } |
| 259 | } |
| 260 | |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 261 | // Count the number of bits that are set. |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 262 | uint32_t BitVector::NumSetBits() const { |
| 263 | uint32_t count = 0; |
| 264 | for (uint32_t word = 0; word < storage_size_; word++) { |
Vladimir Marko | 8194963 | 2014-05-02 11:53:22 +0100 | [diff] [blame] | 265 | count += POPCOUNT(storage_[word]); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 266 | } |
| 267 | return count; |
| 268 | } |
| 269 | |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 270 | // Count the number of bits that are set in range [0, end). |
| 271 | uint32_t BitVector::NumSetBits(uint32_t end) const { |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame^] | 272 | DCHECK_LE(end, storage_size_ * kWordBits); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 273 | return NumSetBits(storage_, end); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 276 | /* |
| 277 | * Mark specified number of bits as "set". Cannot set all bits like ClearAll |
| 278 | * since there might be unused bits - setting those to one will confuse the |
| 279 | * iterator. |
| 280 | */ |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 281 | void BitVector::SetInitialBits(uint32_t num_bits) { |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 282 | // If num_bits is 0, clear everything. |
| 283 | if (num_bits == 0) { |
| 284 | ClearAllBits(); |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | // Set the highest bit we want to set to get the BitVector allocated if need be. |
| 289 | SetBit(num_bits - 1); |
| 290 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 291 | uint32_t idx; |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 292 | // We can set every storage element with -1. |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 293 | for (idx = 0; idx < (num_bits >> 5); idx++) { |
| 294 | storage_[idx] = -1; |
| 295 | } |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 296 | |
| 297 | // Handle the potentially last few bits. |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 298 | uint32_t rem_num_bits = num_bits & 0x1f; |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 299 | if (rem_num_bits != 0) { |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 300 | storage_[idx] = (1 << rem_num_bits) - 1; |
Vladimir Marko | 4812d43 | 2014-03-11 12:42:25 +0000 | [diff] [blame] | 301 | ++idx; |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 302 | } |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 303 | |
| 304 | // Now set the upper ones to 0. |
| 305 | for (; idx < storage_size_; idx++) { |
| 306 | storage_[idx] = 0; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | int BitVector::GetHighestBitSet() const { |
| 311 | unsigned int max = storage_size_; |
| 312 | for (int idx = max - 1; idx >= 0; idx--) { |
| 313 | // If not 0, we have more work: check the bits. |
| 314 | uint32_t value = storage_[idx]; |
| 315 | |
| 316 | if (value != 0) { |
| 317 | // Shift right for the counting. |
| 318 | value /= 2; |
| 319 | |
| 320 | int cnt = 0; |
| 321 | |
| 322 | // Count the bits. |
| 323 | while (value > 0) { |
| 324 | value /= 2; |
| 325 | cnt++; |
| 326 | } |
| 327 | |
| 328 | // Return cnt + how many storage units still remain * the number of bits per unit. |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame^] | 329 | int res = cnt + (idx * kWordBits); |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 330 | return res; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | // All zero, therefore return -1. |
| 335 | return -1; |
| 336 | } |
| 337 | |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame] | 338 | bool BitVector::EnsureSizeAndClear(unsigned int num) { |
| 339 | // Check if the bitvector is expandable. |
| 340 | if (IsExpandable() == false) { |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | if (num > 0) { |
| 345 | // Now try to expand by setting the last bit. |
| 346 | SetBit(num - 1); |
| 347 | } |
| 348 | |
| 349 | // We must clear all bits as per our specification. |
| 350 | ClearAllBits(); |
| 351 | |
| 352 | return true; |
| 353 | } |
| 354 | |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 355 | void BitVector::Copy(const BitVector *src) { |
| 356 | // Get highest bit set, we only need to copy till then. |
| 357 | int highest_bit = src->GetHighestBitSet(); |
| 358 | |
| 359 | // If nothing is set, clear everything. |
| 360 | if (highest_bit == -1) { |
| 361 | ClearAllBits(); |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | // Set upper bit to ensure right size before copy. |
| 366 | SetBit(highest_bit); |
| 367 | |
| 368 | // Now set until highest bit's storage. |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame^] | 369 | uint32_t size = 1 + (highest_bit / kWordBits); |
| 370 | memcpy(storage_, src->GetRawStorage(), kWordBytes * size); |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 371 | |
| 372 | // Set upper bits to 0. |
| 373 | uint32_t left = storage_size_ - size; |
| 374 | |
| 375 | if (left > 0) { |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame^] | 376 | memset(storage_ + size, 0, kWordBytes * left); |
Jean Christophe Beyler | ad0d30a | 2014-01-16 09:00:18 -0800 | [diff] [blame] | 377 | } |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 380 | bool BitVector::IsBitSet(const uint32_t* storage, uint32_t num) { |
| 381 | uint32_t val = storage[num >> 5] & check_masks[num & 0x1f]; |
| 382 | return (val != 0); |
| 383 | } |
| 384 | |
| 385 | uint32_t BitVector::NumSetBits(const uint32_t* storage, uint32_t end) { |
| 386 | uint32_t word_end = end >> 5; |
| 387 | uint32_t partial_word_bits = end & 0x1f; |
| 388 | |
| 389 | uint32_t count = 0u; |
| 390 | for (uint32_t word = 0u; word < word_end; word++) { |
Vladimir Marko | 8194963 | 2014-05-02 11:53:22 +0100 | [diff] [blame] | 391 | count += POPCOUNT(storage[word]); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 392 | } |
| 393 | if (partial_word_bits != 0u) { |
Vladimir Marko | 8194963 | 2014-05-02 11:53:22 +0100 | [diff] [blame] | 394 | count += POPCOUNT(storage[word_end] & ~(0xffffffffu << partial_word_bits)); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 395 | } |
| 396 | return count; |
| 397 | } |
| 398 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 399 | void BitVector::Dump(std::ostream& os, const char *prefix) const { |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame] | 400 | std::ostringstream buffer; |
| 401 | DumpHelper(buffer, prefix); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 402 | os << buffer.str() << std::endl; |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 405 | void BitVector::DumpDot(FILE* file, const char* prefix, bool last_entry) const { |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame] | 406 | std::ostringstream buffer; |
| 407 | Dump(buffer, prefix); |
| 408 | |
| 409 | // Now print it to the file. |
| 410 | fprintf(file, " {%s}", buffer.str().c_str()); |
| 411 | |
| 412 | // If it isn't the last entry, add a |. |
| 413 | if (last_entry == false) { |
| 414 | fprintf(file, "|"); |
| 415 | } |
| 416 | |
| 417 | // Add the \n. |
| 418 | fprintf(file, "\\\n"); |
| 419 | } |
| 420 | |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 421 | void BitVector::DumpHelper(std::ostringstream& buffer, const char* prefix) const { |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame] | 422 | // Initialize it. |
| 423 | if (prefix != nullptr) { |
| 424 | buffer << prefix; |
| 425 | } |
| 426 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 427 | buffer << '('; |
| 428 | for (size_t i = 0; i < number_of_bits_; i++) { |
| 429 | buffer << IsBitSet(i); |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame] | 430 | } |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 431 | buffer << ')'; |
Jean Christophe Beyler | 5afa08f | 2014-04-15 15:54:35 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 434 | } // namespace art |