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)
93 unsigned size_bytes = BITSET_SIZE_BYTES(size);
94 unsigned *res = obstack_alloc(obst, size_bytes);
95 memset(res, 0, size_bytes);
101 * Allocate an empty raw bitset including the size on an obstack.
102 * The size of this bitset can be accessed by bitset[-1].
104 * @param obst the obstack where the bitset is allocated on
105 * @param size number of bits in the bitset
107 * @return the new bitset
109 static inline unsigned *rbitset_w_size_obstack_alloc(struct obstack *obst, unsigned size)
111 unsigned size_bytes = BITSET_SIZE_BYTES(size);
112 unsigned *res = obstack_alloc(obst, size_bytes + sizeof(unsigned));
115 memset(res, 0, size_bytes);
120 /** Return the size of a bitset allocated with a *_w_size_* function */
121 #define rbitset_size(set) (set)[-1]
124 * Duplicate a raw bitset on an obstack.
126 * @param obst the obstack where the bitset is allocated on
127 * @param old_bitset the bitset to be duplicated
128 * @param size number of bits in the bitset
130 * @return the new bitset
133 unsigned *rbitset_duplicate_obstack_alloc(struct obstack *obst,
134 const unsigned *old_bitset,
137 unsigned size_bytes = BITSET_SIZE_BYTES(size);
138 unsigned *res = obstack_alloc(obst, size_bytes);
139 memcpy(res, old_bitset, size_bytes);
145 * Check if a bitset is empty, ie all bits cleared.
147 static inline int rbitset_is_empty(unsigned *bitset, unsigned size)
149 unsigned n = BITSET_SIZE_ELEMS(size);
151 for (i = 0; i < n; ++i) {
160 * Set a bit at position pos.
162 * @param bitset the bitset
163 * @param pos the position of the bit to be set
165 static inline void rbitset_set(unsigned *bitset, unsigned pos)
167 BITSET_ELEM(bitset,pos) |= 1 << (pos % BITS_PER_ELEM);
171 * Set all bits in a given bitset.
173 * @param bitset the bitset
174 * @param size number of bits in the bitset
176 static inline void rbitset_set_all(unsigned *bitset, unsigned size)
178 unsigned size_bytes = BITSET_SIZE_BYTES(size);
179 memset(bitset, ~0, size_bytes);
184 * Clear a bit at position pos.
186 * @param bitset the bitset
187 * @param pos the position of the bit to be clear
189 static inline void rbitset_clear(unsigned *bitset, unsigned pos)
191 BITSET_ELEM(bitset, pos) &= ~(1 << (pos % BITS_PER_ELEM));
195 * Clear all bits in a given bitset.
197 * @param bitset the bitset
198 * @param size number of bits in the bitset
200 static inline void rbitset_clear_all(unsigned *bitset, unsigned size)
202 unsigned size_bytes = BITSET_SIZE_BYTES(size);
203 memset(bitset, 0, size_bytes);
207 * Check if a bit is set at position pos.
209 * @param bitset the bitset
210 * @param pos the position of the bit to check
212 static inline int rbitset_is_set(const unsigned *bitset, unsigned pos)
214 return BITSET_ELEM(bitset, pos) & (1 << (pos % BITS_PER_ELEM));
218 * Calculate the number of set bits (number of elements).
220 * @param bitset the bitset
221 * @param size size of the bitset
223 static inline unsigned rbitset_popcnt(const unsigned *bitset, unsigned size)
226 unsigned n = BITSET_SIZE_ELEMS(size);
228 const unsigned *elem = bitset;
230 for (pos = 0; pos < n; ++pos) {
231 res += _bitset_inside_pop(elem);
239 * Returns the position of the next bit starting from (and including)
242 * @param bitset a bitset
243 * @param pos the first position to check
244 * @param set if 0 search for unset bit, else for set bit
246 * @return the first position where a matched bit was found
248 * @note Does NOT check the size of the bitset, so ensure that a bit
249 * will be found or use a sentinel bit!
251 static inline unsigned rbitset_next(const unsigned *bitset, unsigned pos, int set)
254 unsigned elem_pos = pos / BITS_PER_ELEM;
255 unsigned bit_pos = pos % BITS_PER_ELEM;
257 unsigned elem = bitset[elem_pos];
261 * Mask out the bits smaller than pos in the current unit.
262 * We are only interested in bits set higher than pos.
264 unsigned in_elem_mask = (1 << bit_pos) - 1;
269 p = _bitset_inside_ntz_value(elem & ~in_elem_mask);
271 /* If there is a bit set in the current elem, exit. */
272 if (p < BITS_PER_ELEM) {
273 return elem_pos * BITS_PER_ELEM + p;
276 /* Else search for set bits in the next units. */
279 elem = bitset[elem_pos] ^ mask;
281 p = _bitset_inside_ntz_value(elem);
282 if (p < BITS_PER_ELEM) {
283 return elem_pos * BITS_PER_ELEM + p;
289 * Inplace Intersection of two sets.
291 * @param dst the destination bitset and first operand
292 * @param src the second bitset
293 * @param size size of both bitsets
295 static inline void rbitset_and(unsigned *dst, const unsigned *src, unsigned size)
297 unsigned i, n = BITSET_SIZE_ELEMS(size);
299 for (i = 0; i < n; ++i) {
305 * Inplace Union of two sets.
307 * @param dst the destination bitset and first operand
308 * @param src the second bitset
309 * @param size size of both bitsets
311 static inline void rbitset_or(unsigned *dst, const unsigned *src, unsigned size)
313 unsigned i, n = BITSET_SIZE_ELEMS(size);
315 for (i = 0; i < n; ++i) {
321 * Remove all bits in src from dst.
323 * @param dst the destination bitset and first operand
324 * @param src the second bitset
325 * @param size size of both bitsets
327 static inline void rbitset_andnot(unsigned *dst, const unsigned *src, unsigned size)
329 unsigned i, n = BITSET_SIZE_ELEMS(size);
331 for (i = 0; i < n; ++i) {
337 * Xor of two bitsets.
339 * @param dst the destination bitset and first operand
340 * @param src the second bitset
341 * @param size size of both bitsets
343 static inline void rbitset_xor(unsigned *dst, const unsigned *src, unsigned size)
345 unsigned i, n = BITSET_SIZE_ELEMS(size);
347 for (i = 0; i < n; ++i) {
353 * Returns 1 of two bitsets are equal.
355 * @param bitset1 the first bitset
356 * @param bitset2 the second bitset
357 * @param size size of both bitsets
359 static inline int rbitset_equal(const unsigned *bitset1,
360 const unsigned *bitset2, size_t size)
362 return memcmp(bitset1, bitset2, BITSET_SIZE_BYTES(size)) == 0;
366 * Tests wether 2 bitsets wether at least 1 bit is set in both.
368 * @param bitset1 the first bitset
369 * @param bitset2 the second bitset
370 * @param size size of both bitsets
372 static inline int rbitsets_have_common(const unsigned *bitset1,
373 const unsigned *bitset2, size_t size)
376 unsigned n = BITSET_SIZE_ELEMS(size);
378 for (i = 0; i < n; ++i) {
379 if ((bitset1[i] & bitset2[i]) != 0)
386 * Copy a raw bitset into another.
388 * @param dst the destination set
389 * @param src the source set
390 * @param size size of both bitsets
392 static inline void rbitset_copy(unsigned *dst, const unsigned *src, size_t size)
394 memcpy(dst, src, BITSET_SIZE_BYTES(size));
398 * Copy a raw bitset into an bitset.
402 static inline void rbitset_copy_to_bitset(const unsigned *rbitset, bitset_t *bitset)
404 // TODO optimize me (or remove me)
405 unsigned i, n = bitset_size(bitset);
406 for (i = 0; i < n; ++i) {
407 if (rbitset_is_set(rbitset, i))
408 bitset_set(bitset, i);
412 #endif /* FIRM_ADT_RAW_BITSET_H */