blob: 3b8265149815efbbc14e8281c22da7664241ac43 [file] [log] [blame]
Brian Carlstrom413e89f2013-10-21 23:53:49 -07001/*
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
19namespace art {
20
21// TODO: profile to make sure this is still a win relative to just using shifted masks.
22static 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 Carlstromba150c32013-08-27 17:31:03 -070031static inline uint32_t BitsToWords(uint32_t bits) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -070032 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 Carlstromba150c32013-08-27 17:31:03 -070038BitVector::BitVector(uint32_t start_bits,
Brian Carlstrom413e89f2013-10-21 23:53:49 -070039 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
54BitVector::~BitVector() {
55 allocator_->Free(storage_);
56}
57
58/*
59 * Determine whether or not the specified bit is set.
60 */
Brian Carlstromba150c32013-08-27 17:31:03 -070061bool BitVector::IsBitSet(uint32_t num) const {
Brian Carlstrom413e89f2013-10-21 23:53:49 -070062 DCHECK_LT(num, storage_size_ * sizeof(uint32_t) * 8);
63
Brian Carlstromba150c32013-08-27 17:31:03 -070064 uint32_t val = storage_[num >> 5] & check_masks[num & 0x1f];
Brian Carlstrom413e89f2013-10-21 23:53:49 -070065 return (val != 0);
66}
67
68// Mark all bits bit as "clear".
69void 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 Carlstromba150c32013-08-27 17:31:03 -070078void BitVector::SetBit(uint32_t num) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -070079 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 Carlstromba150c32013-08-27 17:31:03 -070083 uint32_t new_size = BitsToWords(num + 1);
Brian Carlstrom413e89f2013-10-21 23:53:49 -070084 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 Carlstromba150c32013-08-27 17:31:03 -070099void BitVector::ClearBit(uint32_t num) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700100 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.
105void BitVector::Intersect(const BitVector* src) {
106 DCHECK_EQ(storage_size_, src->GetStorageSize());
107 DCHECK_EQ(expandable_, src->IsExpandable());
Brian Carlstromba150c32013-08-27 17:31:03 -0700108 for (uint32_t idx = 0; idx < storage_size_; idx++) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700109 storage_[idx] &= src->GetRawStorageWord(idx);
110 }
111}
112
113/*
114 * Union with another bit vector. Sizes and expandability must be the same.
115 */
116void BitVector::Union(const BitVector* src) {
117 DCHECK_EQ(storage_size_, src->GetStorageSize());
118 DCHECK_EQ(expandable_, src->IsExpandable());
Brian Carlstromba150c32013-08-27 17:31:03 -0700119 for (uint32_t idx = 0; idx < storage_size_; idx++) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700120 storage_[idx] |= src->GetRawStorageWord(idx);
121 }
122}
123
124// Count the number of bits that are set.
Brian Carlstromba150c32013-08-27 17:31:03 -0700125uint32_t BitVector::NumSetBits() const {
126 uint32_t count = 0;
127 for (uint32_t word = 0; word < storage_size_; word++) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700128 count += __builtin_popcount(storage_[word]);
129 }
130 return count;
131}
132
Brian Carlstromba150c32013-08-27 17:31:03 -0700133// Count the number of bits that are set up through and including num.
134uint32_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
156BitVector::Iterator* BitVector::GetIterator() const {
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700157 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 Carlstromba150c32013-08-27 17:31:03 -0700165void BitVector::SetInitialBits(uint32_t num_bits) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700166 DCHECK_LE(BitsToWords(num_bits), storage_size_);
Brian Carlstromba150c32013-08-27 17:31:03 -0700167 uint32_t idx;
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700168 for (idx = 0; idx < (num_bits >> 5); idx++) {
169 storage_[idx] = -1;
170 }
Brian Carlstromba150c32013-08-27 17:31:03 -0700171 uint32_t rem_num_bits = num_bits & 0x1f;
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700172 if (rem_num_bits) {
173 storage_[idx] = (1 << rem_num_bits) - 1;
174 }
175}
176
177} // namespace art