blob: f1d68a08bed69478955fed2726883e3fcee9c30b [file] [log] [blame]
Jeff Brown66db6892010-04-22 18:58:52 -07001/*
2 * Copyright (C) 2010 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#ifndef UTILS_BITSET_H
18#define UTILS_BITSET_H
19
20#include <stdint.h>
Jeff Brown9a0a76d2012-03-16 14:45:49 -070021#include <utils/TypeHelpers.h>
Jeff Brown66db6892010-04-22 18:58:52 -070022
23/*
24 * Contains some bit manipulation helpers.
25 */
26
27namespace android {
28
29// A simple set of 32 bits that can be individually marked or cleared.
30struct BitSet32 {
31 uint32_t value;
32
Michael Wrightdf33e8b2014-03-18 17:25:20 -070033 inline BitSet32() : value(0UL) { }
Jeff Brown66db6892010-04-22 18:58:52 -070034 explicit inline BitSet32(uint32_t value) : value(value) { }
35
36 // Gets the value associated with a particular bit index.
Michael Wrightdf33e8b2014-03-18 17:25:20 -070037 static inline uint32_t valueForBit(uint32_t n) { return 0x80000000UL >> n; }
Jeff Brown66db6892010-04-22 18:58:52 -070038
39 // Clears the bit set.
Michael Wrightdf33e8b2014-03-18 17:25:20 -070040 inline void clear() { clear(value); }
41
42 static inline void clear(uint32_t& value) { value = 0UL; }
Jeff Brown66db6892010-04-22 18:58:52 -070043
Jeff Brown7d90df82010-09-26 22:20:12 -070044 // Returns the number of marked bits in the set.
Michael Wrightdf33e8b2014-03-18 17:25:20 -070045 inline uint32_t count() const { return count(value); }
46
47 static inline uint32_t count(uint32_t value) { return __builtin_popcountl(value); }
Jeff Brown7d90df82010-09-26 22:20:12 -070048
Jeff Brown66db6892010-04-22 18:58:52 -070049 // Returns true if the bit set does not contain any marked bits.
Michael Wrightdf33e8b2014-03-18 17:25:20 -070050 inline bool isEmpty() const { return isEmpty(value); }
51
52 static inline bool isEmpty(uint32_t value) { return ! value; }
Jeff Brown66db6892010-04-22 18:58:52 -070053
Jeff Brown82e14f62011-07-01 17:59:27 -070054 // Returns true if the bit set does not contain any unmarked bits.
Michael Wrightdf33e8b2014-03-18 17:25:20 -070055 inline bool isFull() const { return isFull(value); }
56
57 static inline bool isFull(uint32_t value) { return value == 0xffffffffUL; }
Jeff Brown82e14f62011-07-01 17:59:27 -070058
Jeff Brown66db6892010-04-22 18:58:52 -070059 // Returns true if the specified bit is marked.
Michael Wrightdf33e8b2014-03-18 17:25:20 -070060 inline bool hasBit(uint32_t n) const { return hasBit(value, n); }
61
62 static inline bool hasBit(uint32_t value, uint32_t n) { return value & valueForBit(n); }
Jeff Brown66db6892010-04-22 18:58:52 -070063
64 // Marks the specified bit.
Michael Wrightdf33e8b2014-03-18 17:25:20 -070065 inline void markBit(uint32_t n) { markBit(value, n); }
66
67 static inline void markBit (uint32_t& value, uint32_t n) { value |= valueForBit(n); }
Jeff Brown66db6892010-04-22 18:58:52 -070068
69 // Clears the specified bit.
Michael Wrightdf33e8b2014-03-18 17:25:20 -070070 inline void clearBit(uint32_t n) { clearBit(value, n); }
71
72 static inline void clearBit(uint32_t& value, uint32_t n) { value &= ~ valueForBit(n); }
Jeff Brown66db6892010-04-22 18:58:52 -070073
74 // Finds the first marked bit in the set.
75 // Result is undefined if all bits are unmarked.
Michael Wrightdf33e8b2014-03-18 17:25:20 -070076 inline uint32_t firstMarkedBit() const { return firstMarkedBit(value); }
77
78 static uint32_t firstMarkedBit(uint32_t value) { return __builtin_clzl(value); }
Jeff Brown66db6892010-04-22 18:58:52 -070079
80 // Finds the first unmarked bit in the set.
81 // Result is undefined if all bits are marked.
Michael Wrightdf33e8b2014-03-18 17:25:20 -070082 inline uint32_t firstUnmarkedBit() const { return firstUnmarkedBit(value); }
83
84 static inline uint32_t firstUnmarkedBit(uint32_t value) { return __builtin_clzl(~ value); }
Jeff Brown66db6892010-04-22 18:58:52 -070085
Jeff Brown5e353702011-03-14 19:39:54 -070086 // Finds the last marked bit in the set.
87 // Result is undefined if all bits are unmarked.
Michael Wrightdf33e8b2014-03-18 17:25:20 -070088 inline uint32_t lastMarkedBit() const { return lastMarkedBit(value); }
89
90 static inline uint32_t lastMarkedBit(uint32_t value) { return 31 - __builtin_ctzl(value); }
Jeff Brown5e353702011-03-14 19:39:54 -070091
Jeff Brown4ccb2fc2011-07-27 16:04:54 -070092 // Finds the first marked bit in the set and clears it. Returns the bit index.
93 // Result is undefined if all bits are unmarked.
Michael Wrightdf33e8b2014-03-18 17:25:20 -070094 inline uint32_t clearFirstMarkedBit() { return clearFirstMarkedBit(value); }
95
96 static inline uint32_t clearFirstMarkedBit(uint32_t& value) {
97 uint32_t n = firstMarkedBit(value);
98 clearBit(value, n);
Jeff Brown4ccb2fc2011-07-27 16:04:54 -070099 return n;
100 }
101
102 // Finds the first unmarked bit in the set and marks it. Returns the bit index.
103 // Result is undefined if all bits are marked.
Michael Wrightdf33e8b2014-03-18 17:25:20 -0700104 inline uint32_t markFirstUnmarkedBit() { return markFirstUnmarkedBit(value); }
105
106 static inline uint32_t markFirstUnmarkedBit(uint32_t& value) {
107 uint32_t n = firstUnmarkedBit(value);
108 markBit(value, n);
Jeff Brown4ccb2fc2011-07-27 16:04:54 -0700109 return n;
110 }
111
112 // Finds the last marked bit in the set and clears it. Returns the bit index.
113 // Result is undefined if all bits are unmarked.
Michael Wrightdf33e8b2014-03-18 17:25:20 -0700114 inline uint32_t clearLastMarkedBit() { return clearLastMarkedBit(value); }
115
116 static inline uint32_t clearLastMarkedBit(uint32_t& value) {
117 uint32_t n = lastMarkedBit(value);
118 clearBit(value, n);
Jeff Brown4ccb2fc2011-07-27 16:04:54 -0700119 return n;
120 }
121
Jeff Brown9ae794d2011-03-09 17:39:48 -0800122 // Gets the index of the specified bit in the set, which is the number of
123 // marked bits that appear before the specified bit.
124 inline uint32_t getIndexOfBit(uint32_t n) const {
Michael Wrightdf33e8b2014-03-18 17:25:20 -0700125 return getIndexOfBit(value, n);
126 }
127
128 static inline uint32_t getIndexOfBit(uint32_t value, uint32_t n) {
129 return __builtin_popcountl(value & ~(0xffffffffUL >> n));
Jeff Brown9ae794d2011-03-09 17:39:48 -0800130 }
131
Jeff Brown66db6892010-04-22 18:58:52 -0700132 inline bool operator== (const BitSet32& other) const { return value == other.value; }
133 inline bool operator!= (const BitSet32& other) const { return value != other.value; }
Michael Wrightd614ee42013-05-21 14:11:34 -0700134 inline BitSet32 operator& (const BitSet32& other) const {
135 return BitSet32(value & other.value);
136 }
137 inline BitSet32& operator&= (const BitSet32& other) {
138 value &= other.value;
139 return *this;
140 }
141 inline BitSet32 operator| (const BitSet32& other) const {
142 return BitSet32(value | other.value);
143 }
144 inline BitSet32& operator|= (const BitSet32& other) {
145 value |= other.value;
146 return *this;
147 }
Jeff Brown66db6892010-04-22 18:58:52 -0700148};
149
Jeff Brown9a0a76d2012-03-16 14:45:49 -0700150ANDROID_BASIC_TYPES_TRAITS(BitSet32)
151
Michael Wrightdf33e8b2014-03-18 17:25:20 -0700152// A simple set of 64 bits that can be individually marked or cleared.
153struct BitSet64 {
154 uint64_t value;
155
156 inline BitSet64() : value(0ULL) { }
157 explicit inline BitSet64(uint64_t value) : value(value) { }
158
159 // Gets the value associated with a particular bit index.
160 static inline uint64_t valueForBit(uint32_t n) { return 0x8000000000000000ULL >> n; }
161
162 // Clears the bit set.
163 inline void clear() { clear(value); }
164
165 static inline void clear(uint64_t& value) { value = 0ULL; }
166
167 // Returns the number of marked bits in the set.
168 inline uint32_t count() const { return count(value); }
169
170 static inline uint32_t count(uint64_t value) { return __builtin_popcountll(value); }
171
172 // Returns true if the bit set does not contain any marked bits.
173 inline bool isEmpty() const { return isEmpty(value); }
174
175 static inline bool isEmpty(uint64_t value) { return ! value; }
176
177 // Returns true if the bit set does not contain any unmarked bits.
178 inline bool isFull() const { return isFull(value); }
179
180 static inline bool isFull(uint64_t value) { return value == 0xffffffffffffffffULL; }
181
182 // Returns true if the specified bit is marked.
183 inline bool hasBit(uint32_t n) const { return hasBit(value, n); }
184
185 static inline bool hasBit(uint64_t value, uint32_t n) { return value & valueForBit(n); }
186
187 // Marks the specified bit.
188 inline void markBit(uint32_t n) { markBit(value, n); }
189
190 static inline void markBit(uint64_t& value, uint32_t n) { value |= valueForBit(n); }
191
192 // Clears the specified bit.
193 inline void clearBit(uint32_t n) { clearBit(value, n); }
194
195 static inline void clearBit(uint64_t& value, uint32_t n) { value &= ~ valueForBit(n); }
196
197 // Finds the first marked bit in the set.
198 // Result is undefined if all bits are unmarked.
199 inline uint32_t firstMarkedBit() const { return firstMarkedBit(value); }
200
201 static inline uint32_t firstMarkedBit(uint64_t value) { return __builtin_clzll(value); }
202
203 // Finds the first unmarked bit in the set.
204 // Result is undefined if all bits are marked.
205 inline uint32_t firstUnmarkedBit() const { return firstUnmarkedBit(value); }
206
207 static inline uint32_t firstUnmarkedBit(uint64_t value) { return __builtin_clzll(~ value); }
208
209 // Finds the last marked bit in the set.
210 // Result is undefined if all bits are unmarked.
211 inline uint32_t lastMarkedBit() const { return lastMarkedBit(value); }
212
213 static inline uint32_t lastMarkedBit(uint64_t value) { return 63 - __builtin_ctzll(value); }
214
215 // Finds the first marked bit in the set and clears it. Returns the bit index.
216 // Result is undefined if all bits are unmarked.
217 inline uint32_t clearFirstMarkedBit() { return clearFirstMarkedBit(value); }
218
219 static inline uint32_t clearFirstMarkedBit(uint64_t& value) {
220 uint64_t n = firstMarkedBit(value);
221 clearBit(value, n);
222 return n;
223 }
224
225 // Finds the first unmarked bit in the set and marks it. Returns the bit index.
226 // Result is undefined if all bits are marked.
227 inline uint32_t markFirstUnmarkedBit() { return markFirstUnmarkedBit(value); }
228
229 static inline uint32_t markFirstUnmarkedBit(uint64_t& value) {
230 uint64_t n = firstUnmarkedBit(value);
231 markBit(value, n);
232 return n;
233 }
234
235 // Finds the last marked bit in the set and clears it. Returns the bit index.
236 // Result is undefined if all bits are unmarked.
237 inline uint32_t clearLastMarkedBit() { return clearLastMarkedBit(value); }
238
239 static inline uint32_t clearLastMarkedBit(uint64_t& value) {
240 uint64_t n = lastMarkedBit(value);
241 clearBit(value, n);
242 return n;
243 }
244
245 // Gets the index of the specified bit in the set, which is the number of
246 // marked bits that appear before the specified bit.
247 inline uint32_t getIndexOfBit(uint32_t n) const { return getIndexOfBit(value, n); }
248
249 static inline uint32_t getIndexOfBit(uint64_t value, uint32_t n) {
250 return __builtin_popcountll(value & ~(0xffffffffffffffffULL >> n));
251 }
252
253 inline bool operator== (const BitSet64& other) const { return value == other.value; }
254 inline bool operator!= (const BitSet64& other) const { return value != other.value; }
255 inline BitSet64 operator& (const BitSet64& other) const {
256 return BitSet64(value & other.value);
257 }
258 inline BitSet64& operator&= (const BitSet64& other) {
259 value &= other.value;
260 return *this;
261 }
262 inline BitSet64 operator| (const BitSet64& other) const {
263 return BitSet64(value | other.value);
264 }
265 inline BitSet64& operator|= (const BitSet64& other) {
266 value |= other.value;
267 return *this;
268 }
269};
270
271ANDROID_BASIC_TYPES_TRAITS(BitSet64)
272
Jeff Brown66db6892010-04-22 18:58:52 -0700273} // namespace android
274
275#endif // UTILS_BITSET_H