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) { |
| 47 | DCHECK_EQ(sizeof(storage_[0]), 4U); // Assuming 32-bit units. |
| 48 | if (storage_ == NULL) { |
| 49 | storage_size_ = BitsToWords(start_bits); |
| 50 | storage_ = static_cast<uint32_t*>(allocator_->Alloc(storage_size_ * sizeof(uint32_t))); |
| 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 { |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 62 | DCHECK_LT(num, storage_size_ * sizeof(uint32_t) * 8); |
| 63 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame^] | 64 | uint32_t val = storage_[num >> 5] & check_masks[num & 0x1f]; |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 65 | return (val != 0); |
| 66 | } |
| 67 | |
| 68 | // Mark all bits bit as "clear". |
| 69 | void BitVector::ClearAllBits() { |
| 70 | memset(storage_, 0, storage_size_ * sizeof(uint32_t)); |
| 71 | } |
| 72 | |
| 73 | // Mark the specified bit as "set". |
| 74 | /* |
| 75 | * TUNING: this could have pathologically bad growth/expand behavior. Make sure we're |
| 76 | * not using it badly or change resize mechanism. |
| 77 | */ |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame^] | 78 | void BitVector::SetBit(uint32_t num) { |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 79 | if (num >= storage_size_ * sizeof(uint32_t) * 8) { |
| 80 | DCHECK(expandable_) << "Attempted to expand a non-expandable bitmap to position " << num; |
| 81 | |
| 82 | /* Round up to word boundaries for "num+1" bits */ |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame^] | 83 | uint32_t new_size = BitsToWords(num + 1); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 84 | DCHECK_GT(new_size, storage_size_); |
| 85 | uint32_t *new_storage = |
| 86 | static_cast<uint32_t*>(allocator_->Alloc(new_size * sizeof(uint32_t))); |
| 87 | memcpy(new_storage, storage_, storage_size_ * sizeof(uint32_t)); |
| 88 | // Zero out the new storage words. |
| 89 | memset(&new_storage[storage_size_], 0, (new_size - storage_size_) * sizeof(uint32_t)); |
| 90 | // TOTO: collect stats on space wasted because of resize. |
| 91 | storage_ = new_storage; |
| 92 | storage_size_ = new_size; |
| 93 | } |
| 94 | |
| 95 | storage_[num >> 5] |= check_masks[num & 0x1f]; |
| 96 | } |
| 97 | |
| 98 | // Mark the specified bit as "unset". |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame^] | 99 | void BitVector::ClearBit(uint32_t num) { |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 100 | DCHECK_LT(num, storage_size_ * sizeof(uint32_t) * 8); |
| 101 | storage_[num >> 5] &= ~check_masks[num & 0x1f]; |
| 102 | } |
| 103 | |
| 104 | // Intersect with another bit vector. Sizes and expandability must be the same. |
| 105 | void BitVector::Intersect(const BitVector* src) { |
| 106 | DCHECK_EQ(storage_size_, src->GetStorageSize()); |
| 107 | DCHECK_EQ(expandable_, src->IsExpandable()); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame^] | 108 | for (uint32_t idx = 0; idx < storage_size_; idx++) { |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 109 | storage_[idx] &= src->GetRawStorageWord(idx); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * Union with another bit vector. Sizes and expandability must be the same. |
| 115 | */ |
| 116 | void BitVector::Union(const BitVector* src) { |
| 117 | DCHECK_EQ(storage_size_, src->GetStorageSize()); |
| 118 | DCHECK_EQ(expandable_, src->IsExpandable()); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame^] | 119 | for (uint32_t idx = 0; idx < storage_size_; idx++) { |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 120 | storage_[idx] |= src->GetRawStorageWord(idx); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | // Count the number of bits that are set. |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame^] | 125 | uint32_t BitVector::NumSetBits() const { |
| 126 | uint32_t count = 0; |
| 127 | for (uint32_t word = 0; word < storage_size_; word++) { |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 128 | count += __builtin_popcount(storage_[word]); |
| 129 | } |
| 130 | return count; |
| 131 | } |
| 132 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame^] | 133 | // Count the number of bits that are set up through and including num. |
| 134 | uint32_t BitVector::NumSetBits(uint32_t num) const { |
| 135 | DCHECK_LT(num, storage_size_ * sizeof(uint32_t) * 8); |
| 136 | uint32_t last_word = num >> 5; |
| 137 | uint32_t partial_word_bits = num & 0x1f; |
| 138 | |
| 139 | // partial_word_bits | # | | | partial_word_mask |
| 140 | // 00000 | 0 | 0xffffffff >> (31 - 0) | (1 << (0 + 1)) - 1 | 0x00000001 |
| 141 | // 00001 | 1 | 0xffffffff >> (31 - 1) | (1 << (1 + 1)) - 1 | 0x00000003 |
| 142 | // 00010 | 2 | 0xffffffff >> (31 - 2) | (1 << (2 + 1)) - 1 | 0x00000007 |
| 143 | // ..... | |
| 144 | // 11110 | 30 | 0xffffffff >> (31 - 30) | (1 << (30 + 1)) - 1 | 0x7fffffff |
| 145 | // 11111 | 31 | 0xffffffff >> (31 - 31) | last_full_word++ | 0xffffffff |
| 146 | uint32_t partial_word_mask = 0xffffffff >> (0x1f - partial_word_bits); |
| 147 | |
| 148 | uint32_t count = 0; |
| 149 | for (uint32_t word = 0; word < last_word; word++) { |
| 150 | count += __builtin_popcount(storage_[word]); |
| 151 | } |
| 152 | count += __builtin_popcount(storage_[last_word] & partial_word_mask); |
| 153 | return count; |
| 154 | } |
| 155 | |
| 156 | BitVector::Iterator* BitVector::GetIterator() const { |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 157 | return new (allocator_) Iterator(this); |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Mark specified number of bits as "set". Cannot set all bits like ClearAll |
| 162 | * since there might be unused bits - setting those to one will confuse the |
| 163 | * iterator. |
| 164 | */ |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame^] | 165 | void BitVector::SetInitialBits(uint32_t num_bits) { |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 166 | DCHECK_LE(BitsToWords(num_bits), storage_size_); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame^] | 167 | uint32_t idx; |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 168 | for (idx = 0; idx < (num_bits >> 5); idx++) { |
| 169 | storage_[idx] = -1; |
| 170 | } |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame^] | 171 | uint32_t rem_num_bits = num_bits & 0x1f; |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 172 | if (rem_num_bits) { |
| 173 | storage_[idx] = (1 << rem_num_bits) - 1; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | } // namespace art |