2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief helper functions for working with raw bitsets
24 * @author Matthias Braun
27 * Raw bitsets are constructed from unsigned int arrays. Additional information
28 * like the size of the bitset or the used memory are not stored for
31 * These bitsets need less space than bitset_t and their representation
32 * as int arrays allows having constant bitsets in the ro data segment.
33 * They should for smaller bitset, whose length is known through other means
34 * (a typical usage case is a set of cpu registers)
36 * The bitset is built as an array of unsigned integers. It is assumed that
37 * exactly 32 bits may be put into each element of the array. If there are
38 * remaining bits, then they should be 0
40 #ifndef FIRM_ADT_RAW_BITSET_H
41 #define FIRM_ADT_RAW_BITSET_H
47 /** The base type for raw bitsets. */
48 typedef unsigned int rawbs_base_t;
50 #define BITS_PER_ELEM (sizeof(rawbs_base_t) * 8)
51 #define BITSET_SIZE_ELEMS(size_bits) ((size_bits)/BITS_PER_ELEM + 1)
52 #define BITSET_SIZE_BYTES(size_bits) (BITSET_SIZE_ELEMS(size_bits) * sizeof(rawbs_base_t))
53 #define BITSET_ELEM(bitset,pos) bitset[pos / BITS_PER_ELEM]
56 * Allocate an empty raw bitset on the heap.
58 * @param size number of bits in the bitset
60 * @return the new bitset
62 static inline unsigned *rbitset_malloc(unsigned size) {
63 unsigned size_bytes = BITSET_SIZE_BYTES(size);
64 unsigned *res = xmalloc(size_bytes);
65 memset(res, 0, size_bytes);
71 * Allocate an empty raw bitset on the stack.
73 * @param res will contain the newly allocated bitset
74 * @param size number of bits in the bitset
76 #define rbitset_alloca(res, size) \
78 unsigned size_bytes = BITSET_SIZE_BYTES(size); \
79 res = alloca(size_bytes); \
80 memset(res, 0, size_bytes); \
84 * Allocate an empty raw bitset on an obstack.
86 * @param obst the obstack where the bitset is allocated on
87 * @param size number of bits in the bitset
89 * @return the new bitset
91 static inline unsigned *rbitset_obstack_alloc(struct obstack *obst, unsigned size) {
92 unsigned size_bytes = BITSET_SIZE_BYTES(size);
93 unsigned *res = obstack_alloc(obst, size_bytes);
94 memset(res, 0, size_bytes);
100 * Allocate an empty raw bitset including the size on an obstack.
101 * The size of this bitset can be accessed by bitset[-1].
103 * @param obst the obstack where the bitset is allocated on
104 * @param size number of bits in the bitset
106 * @return the new bitset
108 static inline unsigned *rbitset_w_size_obstack_alloc(struct obstack *obst, unsigned size) {
109 unsigned size_bytes = BITSET_SIZE_BYTES(size);
110 unsigned *res = obstack_alloc(obst, size_bytes + sizeof(unsigned));
113 memset(res, 0, size_bytes);
118 /** Return the size of a bitset allocated with a *_w_size_* function */
119 #define rbitset_size(set) (set)[-1]
122 * Duplicate a raw bitset on an obstack.
124 * @param obst the obstack where the bitset is allocated on
125 * @param old_bitset the bitset to be duplicated
126 * @param size number of bits in the bitset
128 * @return the new bitset
131 unsigned *rbitset_duplicate_obstack_alloc(struct obstack *obst,
132 const unsigned *old_bitset,
135 unsigned size_bytes = BITSET_SIZE_BYTES(size);
136 unsigned *res = obstack_alloc(obst, size_bytes);
137 memcpy(res, old_bitset, size_bytes);
143 * Check if a bitset is empty, ie all bits cleared.
145 static inline int rbitset_is_empty(unsigned *bitset, unsigned size) {
146 unsigned i, size_bytes = BITSET_SIZE_BYTES(size);
147 for (i = 0; i < size_bytes; ++i)
148 if (bitset[i]) return 0;
153 * Set a bit at position pos.
155 * @param bitset the bitset
156 * @param pos the position of the bit to be set
158 static inline void rbitset_set(unsigned *bitset, unsigned pos) {
159 BITSET_ELEM(bitset,pos) |= 1 << (pos % BITS_PER_ELEM);
163 * Set all bits in a given bitset.
165 * @param bitset the bitset
166 * @param size number of bits in the bitset
168 static inline void rbitset_set_all(unsigned *bitset, unsigned size) {
169 unsigned size_bytes = BITSET_SIZE_BYTES(size);
170 memset(bitset, ~0, size_bytes);
175 * Clear a bit at position pos.
177 * @param bitset the bitset
178 * @param pos the position of the bit to be clear
180 static inline void rbitset_clear(unsigned *bitset, unsigned pos) {
181 BITSET_ELEM(bitset, pos) &= ~(1 << (pos % BITS_PER_ELEM));
185 * Clear all bits in a given bitset.
187 * @param bitset the bitset
188 * @param size number of bits in the bitset
190 static inline void rbitset_clear_all(unsigned *bitset, unsigned size) {
191 unsigned size_bytes = BITSET_SIZE_BYTES(size);
192 memset(bitset, 0, size_bytes);
196 * Check if a bit is set at position pos.
198 * @param bitset the bitset
199 * @param pos the position of the bit to check
201 static inline int rbitset_is_set(const unsigned *bitset, unsigned pos) {
202 return BITSET_ELEM(bitset, pos) & (1 << (pos % BITS_PER_ELEM));
206 * Calculate the number of set bits (number of elements).
208 * @param bitset the bitset
209 * @param size size of the bitset
211 static inline unsigned rbitset_popcnt(const unsigned *bitset, unsigned size) {
213 unsigned n = BITSET_SIZE_ELEMS(size);
215 const unsigned *elem = bitset;
217 for (pos = 0; pos < n; ++pos) {
218 res += _bitset_inside_pop(elem);
226 * Returns the position of the next bit starting from (and including)
229 * @param bitset a bitset
230 * @param pos the first position to check
231 * @param set if 0 search for unset bit, else for set bit
233 * @return the first position where a matched bit was found
235 * @note Does NOT check the size of the bitset, so ensure that a bit
236 * will be found or use a sentinel bit!
238 static inline unsigned rbitset_next(const unsigned *bitset, unsigned pos, int set) {
240 unsigned elem_pos = pos / BITS_PER_ELEM;
241 unsigned bit_pos = pos % BITS_PER_ELEM;
243 unsigned elem = bitset[elem_pos];
247 * Mask out the bits smaller than pos in the current unit.
248 * We are only interested in bits set higher than pos.
250 unsigned in_elem_mask = (1 << bit_pos) - 1;
255 p = _bitset_inside_ntz_value(elem & ~in_elem_mask);
257 /* If there is a bit set in the current elem, exit. */
258 if (p < BITS_PER_ELEM) {
259 return elem_pos * BITS_PER_ELEM + p;
262 /* Else search for set bits in the next units. */
265 elem = bitset[elem_pos] ^ mask;
267 p = _bitset_inside_ntz_value(elem);
268 if (p < BITS_PER_ELEM) {
269 return elem_pos * BITS_PER_ELEM + p;
275 * Inplace Intersection of two sets.
277 * @param dst the destination bitset and first operand
278 * @param src the second bitset
279 * @param size size of both bitsets
281 static inline void rbitset_and(unsigned *dst, const unsigned *src, unsigned size)
283 unsigned i, n = BITSET_SIZE_ELEMS(size);
285 for (i = 0; i < n; ++i) {
291 * Inplace Union of two sets.
293 * @param dst the destination bitset and first operand
294 * @param src the second bitset
295 * @param size size of both bitsets
297 static inline void rbitset_or(unsigned *dst, const unsigned *src, unsigned size)
299 unsigned i, n = BITSET_SIZE_ELEMS(size);
301 for (i = 0; i < n; ++i) {
307 * Remove all bits in src from dst.
309 * @param dst the destination bitset and first operand
310 * @param src the second bitset
311 * @param size size of both bitsets
313 static inline void rbitset_andnot(unsigned *dst, const unsigned *src, unsigned size)
315 unsigned i, n = BITSET_SIZE_ELEMS(size);
317 for (i = 0; i < n; ++i) {
323 * Xor of two bitsets.
325 * @param dst the destination bitset and first operand
326 * @param src the second bitset
327 * @param size size of both bitsets
329 static inline void rbitset_xor(unsigned *dst, const unsigned *src, unsigned size)
331 unsigned i, n = BITSET_SIZE_ELEMS(size);
333 for (i = 0; i < n; ++i) {
339 * Returns 1 of two bitsets are equal.
341 * @param bitset1 the first bitset
342 * @param bitset2 the second bitset
343 * @param size size of both bitsets
345 static inline int rbitset_equal(const unsigned *bitset1,
346 const unsigned *bitset2, size_t size)
348 return memcmp(bitset1, bitset2, BITSET_SIZE_BYTES(size)) == 0;
352 * Copy a raw bitset into another.
354 * @param dst the destination set
355 * @param src the source set
356 * @param size size of both bitsets
358 static inline void rbitset_cpy(unsigned *dst, const unsigned *src, size_t size) {
359 memcpy(dst, src, BITSET_SIZE_BYTES(size));
363 * Copy a raw bitset into an bitset.
367 static inline void rbitset_copy_to_bitset(const unsigned *rbitset, bitset_t *bitset) {
368 // TODO optimize me (or remove me)
369 unsigned i, n = bitset_size(bitset);
370 for (i = 0; i < n; ++i) {
371 if (rbitset_is_set(rbitset, i))
372 bitset_set(bitset, i);
376 #endif /* FIRM_ADT_RAW_BITSET_H */