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