4 * @author Sebastian Hack
5 * @brief A bitset implementation.
8 #ifndef __FIRM_BITSET_H
9 #define __FIRM_BITSET_H
16 #include "firm_config.h"
17 #include "bitfiddle.h"
19 typedef unsigned int bitset_pos_t;
21 #include "bitset_std.h"
24 #if defined(__GNUC__) && defined(__i386__)
25 #include "bitset_ia32.h"
29 typedef struct _bitset_t {
35 #define BS_UNIT_SIZE sizeof(bitset_unit_t)
36 #define BS_UNIT_SIZE_BITS (BS_UNIT_SIZE * 8)
37 #define BS_UNIT_MASK (BS_UNIT_SIZE_BITS - 1)
40 * Initialize a bitset.
41 * This functions should not be called.
43 * Note that this function needs three macros which must be provided by the
44 * bitfield implementor:
45 * - _bitset_overall_size(size) The overall size that must be
46 * allocated for the bitfield in bytes.
47 * - _bitset_units(size) The number of units that will be
48 * present in the bitfield for a given highest bit.
49 * - _bitset_data_ptr(data, size) This produces as pointer to the
50 * first unit in the allocated memory area. The main reason for this
51 * macro is, that some bitset implementors want control over memory
54 * @param area A pointer to memory reserved for the bitset.
55 * @param size The size of the bitset in bits.
56 * @return A pointer to the initialized bitset.
58 static INLINE bitset_t *_bitset_prepare(void *area, bitset_pos_t size)
61 memset(area, 0, _bitset_overall_size(sizeof(bitset_t), size));
62 ptr->units = _bitset_units(size);
64 ptr->data = _bitset_data_ptr(area, sizeof(bitset_t), size);
69 * Mask out all bits, which are only there, because the number
70 * of bits in the set didn't match a unit size boundary.
71 * @param bs The bitset.
72 * @return The masked bitset.
74 static INLINE bitset_t *_bitset_mask_highest(bitset_t *bs)
76 bs->data[bs->units - 1] &= (bs->size & BS_UNIT_MASK) - 1;
81 * Get the capacity of the bitset in bits.
82 * @param bs The bitset.
83 * @return The capacity in bits of the bitset.
85 #define bitset_capacity(bs) ((bs)->units * BS_UNIT_SIZE_BITS)
88 * Get the size of the bitset in bits.
89 * @note Note the difference between capacity and size.
90 * @param bs The bitset.
91 * @return The highest bit which can be set or cleared plus 1.
93 #define bistet_size(bs) ((bs)->size)
96 * Allocate a bitset on an obstack.
97 * @param obst The obstack.
98 * @param size The greatest bit that shall be stored in the set.
99 * @return A pointer to an empty initialized bitset.
101 #define bitset_obstack_alloc(obst,size) \
102 _bitset_prepare(obstack_alloc(obst, _bitset_overall_size(sizeof(bitset_t), size)), size)
105 * Allocate a bitset via malloc.
106 * @param size The greatest bit that shall be stored in the set.
107 * @return A pointer to an empty initialized bitset.
109 #define bitset_malloc(size) \
110 _bitset_prepare(malloc(_bitset_overall_size(sizeof(bitset_t), size)), size)
113 * Free a bitset allocated with bitset_malloc().
114 * @param bs The bitset.
116 #define bitset_free(bs) free(bs)
119 * Allocate a bitset on the stack via alloca.
120 * @param size The greatest bit that shall be stored in the set.
121 * @return A pointer to an empty initialized bitset.
123 #define bitset_alloca(size) \
124 _bitset_prepare(alloca(_bitset_overall_size(sizeof(bitset_t), size)), size)
128 * Get the unit which contains a specific bit.
129 * This function is internal.
130 * @param bs The bitset.
131 * @param bit The bit.
132 * @return A pointer to the unit containing the bit.
134 static INLINE bitset_unit_t *_bitset_get_unit(const bitset_t *bs, bitset_pos_t bit)
136 /* assert(bit < bs->units * BS_UNIT_SIZE_BITS && "Bit too large"); */
137 assert(bit <= bs->size && "Bit to large");
138 return bs->data + bit / BS_UNIT_SIZE_BITS;
142 * Set a bit in the bitset.
143 * @param bs The bitset.
144 * @param bit The bit to set.
146 static INLINE void bitset_set(bitset_t *bs, bitset_pos_t bit)
148 bitset_unit_t *unit = _bitset_get_unit(bs, bit);
149 _bitset_inside_set(unit, bit & BS_UNIT_MASK);
153 * Clear a bit in the bitset.
154 * @param bs The bitset.
155 * @param bit The bit to clear.
157 static INLINE void bitset_clear(bitset_t *bs, bitset_pos_t bit)
159 bitset_unit_t *unit = _bitset_get_unit(bs, bit);
160 _bitset_inside_clear(unit, bit & BS_UNIT_MASK);
164 * Check, if a bit is set.
165 * @param bs The bitset.
166 * @param bit The bit to check for.
167 * @return 1, if the bit was set, 0 if not.
169 static INLINE int bitset_is_set(const bitset_t *bs, bitset_pos_t bit)
171 bitset_unit_t *unit = _bitset_get_unit(bs, bit);
172 return _bitset_inside_is_set(unit, bit & BS_UNIT_MASK);
176 * Flip a bit in a bitset.
177 * @param bs The bitset.
178 * @param bit The bit to flip.
180 static INLINE void bitset_flip(bitset_t *bs, bitset_pos_t bit)
182 bitset_unit_t *unit = _bitset_get_unit(bs, bit);
183 _bitset_inside_flip(unit, bit & BS_UNIT_MASK);
187 * Copy a bitset to another.
188 * @param tgt The target bitset.
189 * @param src The source bitset.
190 * @return The target bitset.
192 static INLINE bitset_t *bitset_copy(bitset_t *tgt, const bitset_t *src)
194 bitset_pos_t tu = tgt->units;
195 bitset_pos_t su = src->units;
196 bitset_pos_t min_units = tu < su ? tu : su;
197 memcpy(tgt->data, src->data, min_units * BS_UNIT_SIZE);
199 memset(tgt->data + min_units, 0, BS_UNIT_SIZE * (tu - min_units));
200 return _bitset_mask_highest(tgt);
204 * Find the smallest bit set in the bitset.
205 * @param bs The bitset.
206 * @return The smallest bit set in the bitset.
208 static INLINE bitset_pos_t bitset_min(const bitset_t *bs)
210 bitset_pos_t i, ofs = 0;
212 for(i = 0; i < bs->units; ++i) {
213 bitset_unit_t *unit = &bs->data[i];
214 bitset_pos_t pos = _bitset_inside_ntz(unit);
217 ofs += BS_UNIT_SIZE_BITS;
224 * Find the greatest bit set in the bitset.
225 * @param bs The bitset.
226 * @return The greatest bit set in the bitset.
228 static INLINE bitset_pos_t bitset_max(const bitset_t *bs)
230 bitset_pos_t i, max = 0, ofs = 0;
232 for(i = 0; i < bs->units; ++i) {
233 bitset_unit_t *unit = &bs->data[i];
234 bitset_pos_t pos = _bitset_inside_nlz(unit);
237 ofs += BS_UNIT_SIZE_BITS;
244 * Find the next set bit from a given bit.
245 * @note Note that if pos is set, pos is returned.
246 * @param bs The bitset.
247 * @param pos The bit from which to search for the next set bit.
248 * @return The next set bit from pos on, or -1, if no set bit was found
251 static INLINE bitset_pos_t _bitset_next(const bitset_t *bs,
252 bitset_pos_t pos, int set)
254 bitset_pos_t unit_number = pos / BS_UNIT_SIZE_BITS;
256 if(unit_number >= bs->units)
260 bitset_pos_t bit_in_unit = pos & BS_UNIT_MASK;
261 bitset_pos_t in_unit_mask = (1 << bit_in_unit) - 1;
264 * Mask out the bits smaller than pos in the current unit.
265 * We are only interested in bits set higher than pos.
267 bitset_unit_t curr_unit = bs->data[unit_number];
270 * Find the next bit set in the unit.
271 * Mind that this function returns 0, if the unit is -1 and
272 * counts the bits from 1 on.
274 bitset_pos_t next_in_this_unit =
275 _bitset_inside_ntz_value((set ? curr_unit : ~curr_unit) & ~in_unit_mask);
277 /* If there is a bit set in the current unit, exit. */
278 if(next_in_this_unit < BS_UNIT_SIZE_BITS)
279 return next_in_this_unit + unit_number * BS_UNIT_SIZE_BITS;
281 /* Else search for set bits in the next units. */
284 for(i = unit_number + 1; i < bs->units; ++i) {
285 bitset_unit_t data = bs->data[i];
286 bitset_pos_t first_set =
287 _bitset_inside_ntz_value(set ? data : ~data);
289 if(first_set < BS_UNIT_SIZE_BITS)
290 return first_set + i * BS_UNIT_SIZE_BITS;
298 #define bitset_next_clear(bs,pos) _bitset_next((bs), (pos), 0)
299 #define bitset_next_set(bs,pos) _bitset_next((bs), (pos), 1)
302 * Convenience macro for bitset iteration.
303 * @param bitset The bitset.
304 * @param elm A unsigned long variable.
306 #define bitset_foreach(bitset,elm) \
307 for(elm = bitset_next_set(bitset,0); elm != -1; elm = bitset_next_set(bitset,elm+1))
310 #define bitset_foreach_clear(bitset,elm) \
311 for(elm = bitset_next_clear(bitset,0); elm != -1; elm = bitset_next_clear(bitset,elm+1))
314 * Count the bits set.
315 * This can also be seen as the cardinality of the set.
316 * @param bs The bitset.
317 * @return The number of bits set in the bitset.
319 static INLINE bitset_pos_t bitset_popcnt(const bitset_t *bs)
321 bitset_pos_t i, pop = 0;
324 for(i = 0, unit = bs->data; i < bs->units; ++i, ++unit)
325 pop += _bitset_inside_pop(unit);
332 * This sets all bits to zero.
333 * @param bs The bitset.
335 static INLINE bitset_t *bitset_clear_all(bitset_t *bs)
337 memset(bs->data, 0, BS_UNIT_SIZE * bs->units);
343 * This sets all bits to one.
344 * @param bs The bitset.
346 static INLINE bitset_t *bitset_set_all(bitset_t *bs)
348 memset(bs->data, -1, BS_UNIT_SIZE * bs->units);
353 * Check, if one bitset is contained by another.
354 * That is, each bit set in lhs is also set in rhs.
355 * @param lhs A bitset.
356 * @param rhs Another bitset.
357 * @return 1, if all bits in lhs are also set in rhs, 0 otherwise.
359 static INLINE int bitset_contains(const bitset_t *lhs, const bitset_t *rhs)
361 bitset_pos_t n = lhs->units < rhs->units ? lhs->units : rhs->units;
364 for(i = 0; i < n; ++i) {
365 bitset_unit_t lu = lhs->data[i];
366 bitset_unit_t ru = rhs->data[i];
373 * If the left hand sinde is a larger bitset than rhs,
374 * we have to check, that all extra bits in lhs are 0
377 for(i = n; i < lhs->units; ++i) {
378 if(lhs->data[i] != 0)
387 * Treat the bitset as a number and subtract 1.
388 * @param bs The bitset.
389 * @return The same bitset.
391 static INLINE void bitset_minus1(bitset_t *bs)
393 #define _SH (sizeof(bitset_unit_t) * 8 - 1)
395 for(i = 0; i < bs->units; ++i) {
396 bitset_unit_t unit = bs->data[i];
397 bitset_unit_t um1 = unit - 1;
401 if(((unit >> _SH) ^ (um1 >> _SH)) == 0)
408 * Print a bitset to a stream.
409 * The bitset is printed as a comma seperated list of bits set.
410 * @param file The stream.
411 * @param bs The bitset.
413 static INLINE void bitset_fprint(FILE *file, const bitset_t *bs)
415 const char *prefix = "";
419 for(i = bitset_next_set(bs, 0); i != -1; i = bitset_next_set(bs, i + 1)) {
420 fprintf(file, "%s%u", prefix, i);
426 static INLINE void bitset_debug_fprint(FILE *file, const bitset_t *bs)
430 fprintf(file, "%u:", bs->units);
431 for(i = 0; i < bs->units; ++i)
432 fprintf(file, " " BITSET_UNIT_FMT, bs->data[i]);
436 * Here, the binary operations follow.
437 * And, Or, And Not, Xor are available.
439 #define BINARY_OP(op) \
440 static INLINE bitset_t *bitset_ ## op(bitset_t *tgt, const bitset_t *src) \
443 bitset_pos_t n = tgt->units > src->units ? src->units : tgt->units; \
444 for(i = 0; i < n; i += _BITSET_BINOP_UNITS_INC) \
445 _bitset_inside_binop_ ## op(&tgt->data[i], &src->data[i]); \
447 _bitset_clear_rest(&tgt->data[i], tgt->units - i); \
448 return _bitset_mask_highest(tgt); \
452 * Define the clear rest macro for the and, since it is the only case,
453 * were non existed (treated as 0) units in the src must be handled.
454 * For all other operations holds: x Op 0 = x for Op in { Andnot, Or, Xor }
456 * For and, each bitset implementer has to provide the macro
457 * _bitset_clear_units(data, n), which clears n units from the pointer
460 #define _bitset_clear_rest(data,n) _bitset_inside_clear_units(data, n)
462 #undef _bitset_clear_rest
463 #define _bitset_clear_rest(data,n)