Added flip_all function
[libfirm] / ir / adt / bitset.h
1 /**
2  * @file bitset.h
3  * @date 15.10.2004
4  * @author Sebastian Hack
5  * @brief A bitset implementation.
6  */
7
8 #ifndef __FIRM_BITSET_H
9 #define __FIRM_BITSET_H
10
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <assert.h>
14 #include <string.h>
15
16 #include "firm_config.h"
17 #include "bitfiddle.h"
18
19 typedef unsigned int bitset_pos_t;
20
21 #include "bitset_std.h"
22
23 /*
24 #if defined(__GNUC__) && defined(__i386__)
25 #include "bitset_ia32.h"
26 #endif
27 */
28
29 typedef struct _bitset_t {
30         bitset_pos_t units;
31   bitset_pos_t size;
32         bitset_unit_t *data;
33 } bitset_t;
34
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)
38
39 /**
40  * Initialize a bitset.
41  * This functions should not be called.
42  *
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
52  *   alignment.
53  *
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.
57  */
58 static INLINE bitset_t *_bitset_prepare(void *area, bitset_pos_t size)
59 {
60         bitset_t *ptr = area;
61         memset(area, 0, _bitset_overall_size(sizeof(bitset_t), size));
62         ptr->units = _bitset_units(size);
63   ptr->size = size;
64         ptr->data = _bitset_data_ptr(area, sizeof(bitset_t), size);
65         return ptr;
66 }
67
68 /**
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.
73  */
74 static INLINE bitset_t *_bitset_mask_highest(bitset_t *bs)
75 {
76         bitset_pos_t rest = bs->size & BS_UNIT_MASK;
77         if (rest)
78                 bs->data[bs->units - 1] &= (1 << rest) - 1;
79         return bs;
80 }
81
82 /**
83  * Get the capacity of the bitset in bits.
84  * @param bs The bitset.
85  * @return The capacity in bits of the bitset.
86  */
87 #define bitset_capacity(bs) ((bs)->units * BS_UNIT_SIZE_BITS)
88
89 /**
90  * Get the size of the bitset in bits.
91  * @note Note the difference between capacity and size.
92  * @param bs The bitset.
93  * @return The highest bit which can be set or cleared plus 1.
94  */
95 #define bitset_size(bs)  ((bs)->size)
96
97 /**
98  * Allocate a bitset on an obstack.
99  * @param obst The obstack.
100  * @param size The greatest bit that shall be stored in the set.
101  * @return A pointer to an empty initialized bitset.
102  */
103 #define bitset_obstack_alloc(obst,size) \
104   _bitset_prepare(obstack_alloc(obst, _bitset_overall_size(sizeof(bitset_t), size)), size)
105
106 /**
107  * Allocate a bitset via malloc.
108  * @param size The greatest bit that shall be stored in the set.
109  * @return A pointer to an empty initialized bitset.
110  */
111 #define bitset_malloc(size) \
112         _bitset_prepare(malloc(_bitset_overall_size(sizeof(bitset_t), size)), size)
113
114 /**
115  * Free a bitset allocated with bitset_malloc().
116  * @param bs The bitset.
117  */
118 #define bitset_free(bs) free(bs)
119
120 /**
121  * Allocate a bitset on the stack via alloca.
122  * @param size The greatest bit that shall be stored in the set.
123  * @return A pointer to an empty initialized bitset.
124  */
125 #define bitset_alloca(size) \
126         _bitset_prepare(alloca(_bitset_overall_size(sizeof(bitset_t), size)), size)
127
128
129 /**
130  * Get the unit which contains a specific bit.
131  * This function is internal.
132  * @param bs The bitset.
133  * @param bit The bit.
134  * @return A pointer to the unit containing the bit.
135  */
136 static INLINE bitset_unit_t *_bitset_get_unit(const bitset_t *bs, bitset_pos_t bit)
137 {
138         /* assert(bit < bs->units * BS_UNIT_SIZE_BITS && "Bit too large"); */
139   assert(bit <= bs->size && "Bit to large");
140         return bs->data + bit / BS_UNIT_SIZE_BITS;
141 }
142
143 /**
144  * Set a bit in the bitset.
145  * @param bs The bitset.
146  * @param bit The bit to set.
147  */
148 static INLINE void bitset_set(bitset_t *bs, bitset_pos_t bit)
149 {
150         bitset_unit_t *unit = _bitset_get_unit(bs, bit);
151         _bitset_inside_set(unit, bit & BS_UNIT_MASK);
152 }
153
154 /**
155  * Clear a bit in the bitset.
156  * @param bs The bitset.
157  * @param bit The bit to clear.
158  */
159 static INLINE void bitset_clear(bitset_t *bs, bitset_pos_t bit)
160 {
161         bitset_unit_t *unit = _bitset_get_unit(bs, bit);
162         _bitset_inside_clear(unit, bit & BS_UNIT_MASK);
163 }
164
165 /**
166  * Check, if a bit is set.
167  * @param bs The bitset.
168  * @param bit The bit to check for.
169  * @return 1, if the bit was set, 0 if not.
170  */
171 static INLINE int bitset_is_set(const bitset_t *bs, bitset_pos_t bit)
172 {
173         bitset_unit_t *unit = _bitset_get_unit(bs, bit);
174         return _bitset_inside_is_set(unit, bit & BS_UNIT_MASK);
175 }
176
177 /**
178  * Flip a bit in a bitset.
179  * @param bs The bitset.
180  * @param bit The bit to flip.
181  */
182 static INLINE void bitset_flip(bitset_t *bs, bitset_pos_t bit)
183 {
184         bitset_unit_t *unit = _bitset_get_unit(bs, bit);
185         _bitset_inside_flip(unit, bit & BS_UNIT_MASK);
186 }
187
188 /**
189  * Flip the whole bitset.
190  * @param bs The bitset.
191  */
192 static INLINE void bitset_flip_all(bitset_t *bs)
193 {
194         bitset_pos_t i;
195         for(i = 0; i < bs->units; i++)
196                 _bitset_inside_flip_unit(&bs->data[i]);
197         _bitset_mask_highest(bs);
198 }
199
200 /**
201  * Copy a bitset to another.
202  * @param tgt The target bitset.
203  * @param src The source bitset.
204  * @return The target bitset.
205  */
206 static INLINE bitset_t *bitset_copy(bitset_t *tgt, const bitset_t *src)
207 {
208         bitset_pos_t tu = tgt->units;
209         bitset_pos_t su = src->units;
210         bitset_pos_t min_units = tu < su ? tu : su;
211         memcpy(tgt->data, src->data, min_units * BS_UNIT_SIZE);
212         if(tu > min_units)
213                 memset(tgt->data + min_units, 0, BS_UNIT_SIZE * (tu - min_units));
214         return _bitset_mask_highest(tgt);
215 }
216
217 /**
218  * Find the smallest bit set in the bitset.
219  * @param bs The bitset.
220  * @return The smallest bit set in the bitset.
221  */
222 static INLINE bitset_pos_t bitset_min(const bitset_t *bs)
223 {
224         bitset_pos_t i, ofs = 0;
225
226         for(i = 0; i < bs->units; ++i) {
227                 bitset_unit_t *unit = &bs->data[i];
228                 bitset_pos_t pos = _bitset_inside_ntz(unit);
229                 if(pos > 0)
230                         return ofs + pos;
231                 ofs += BS_UNIT_SIZE_BITS;
232         }
233
234         return 0;
235 }
236
237 /**
238  * Find the greatest bit set in the bitset.
239  * @param bs The bitset.
240  * @return The greatest bit set in the bitset.
241  */
242 static INLINE bitset_pos_t bitset_max(const bitset_t *bs)
243 {
244         bitset_pos_t i, max = 0, ofs = 0;
245
246         for(i = 0; i < bs->units; ++i) {
247                 bitset_unit_t *unit = &bs->data[i];
248                 bitset_pos_t pos = _bitset_inside_nlz(unit);
249                 if(pos > 0)
250                         max = ofs + pos;
251                 ofs += BS_UNIT_SIZE_BITS;
252         }
253
254         return max;
255 }
256
257 /**
258  * Find the next set bit from a given bit.
259  * @note Note that if pos is set, pos is returned.
260  * @param bs The bitset.
261  * @param pos The bit from which to search for the next set bit.
262  * @return The next set bit from pos on, or -1, if no set bit was found
263  * after pos.
264  */
265 static INLINE bitset_pos_t _bitset_next(const bitset_t *bs,
266                 bitset_pos_t pos, int set)
267 {
268         bitset_pos_t unit_number = pos / BS_UNIT_SIZE_BITS;
269
270         if(unit_number >= bs->units)
271                 return -1;
272
273         {
274                 bitset_pos_t bit_in_unit = pos & BS_UNIT_MASK;
275                 bitset_pos_t in_unit_mask = (1 << bit_in_unit) - 1;
276
277                 /*
278                  * Mask out the bits smaller than pos in the current unit.
279                  * We are only interested in bits set higher than pos.
280                  */
281                 bitset_unit_t curr_unit = bs->data[unit_number];
282
283                 /*
284                  * Find the next bit set in the unit.
285                  * Mind that this function returns 0, if the unit is -1 and
286                  * counts the bits from 1 on.
287                  */
288                 bitset_pos_t next_in_this_unit =
289                         _bitset_inside_ntz_value((set ? curr_unit : ~curr_unit) & ~in_unit_mask);
290
291                 /* If there is a bit set in the current unit, exit. */
292                 if(next_in_this_unit < BS_UNIT_SIZE_BITS)
293                         return next_in_this_unit + unit_number * BS_UNIT_SIZE_BITS;
294
295                 /* Else search for set bits in the next units. */
296                 else {
297                         bitset_pos_t i;
298                         for(i = unit_number + 1; i < bs->units; ++i) {
299                                 bitset_unit_t data = bs->data[i];
300                                 bitset_pos_t first_set =
301                                         _bitset_inside_ntz_value(set ? data : ~data);
302
303                                 if(first_set < BS_UNIT_SIZE_BITS)
304                                         return first_set + i * BS_UNIT_SIZE_BITS;
305                         }
306                 }
307         }
308
309         return -1;
310 }
311
312 #define bitset_next_clear(bs,pos) _bitset_next((bs), (pos), 0)
313 #define bitset_next_set(bs,pos) _bitset_next((bs), (pos), 1)
314
315 /**
316  * Convenience macro for bitset iteration.
317  * @param bitset The bitset.
318  * @param elm A unsigned long variable.
319  */
320 #define bitset_foreach(bitset,elm) \
321   for(elm = bitset_next_set(bitset,0); elm != -1; elm = bitset_next_set(bitset,elm+1))
322
323
324 #define bitset_foreach_clear(bitset,elm) \
325   for(elm = bitset_next_clear(bitset,0); elm != -1; elm = bitset_next_clear(bitset,elm+1))
326
327 /**
328  * Count the bits set.
329  * This can also be seen as the cardinality of the set.
330  * @param bs The bitset.
331  * @return The number of bits set in the bitset.
332  */
333 static INLINE bitset_pos_t bitset_popcnt(const bitset_t *bs)
334 {
335         bitset_pos_t i, pop = 0;
336         bitset_unit_t *unit;
337
338         for(i = 0, unit = bs->data; i < bs->units; ++i, ++unit)
339                 pop += _bitset_inside_pop(unit);
340
341         return pop;
342 }
343
344 /**
345  * Clear the bitset.
346  * This sets all bits to zero.
347  * @param bs The bitset.
348  */
349 static INLINE bitset_t *bitset_clear_all(bitset_t *bs)
350 {
351         memset(bs->data, 0, BS_UNIT_SIZE * bs->units);
352   return bs;
353 }
354
355 /**
356  * Set the bitset.
357  * This sets all bits to one.
358  * @param bs The bitset.
359  */
360 static INLINE bitset_t *bitset_set_all(bitset_t *bs)
361 {
362         memset(bs->data, -1, bs->units * BS_UNIT_SIZE);
363   return _bitset_mask_highest(bs);
364 }
365
366 /**
367  * Check, if one bitset is contained by another.
368  * That is, each bit set in lhs is also set in rhs.
369  * @param lhs A bitset.
370  * @param rhs Another bitset.
371  * @return 1, if all bits in lhs are also set in rhs, 0 otherwise.
372  */
373 static INLINE int bitset_contains(const bitset_t *lhs, const bitset_t *rhs)
374 {
375         bitset_pos_t n = lhs->units < rhs->units ? lhs->units : rhs->units;
376         bitset_pos_t i;
377
378         for(i = 0; i < n; ++i) {
379                 bitset_unit_t lu = lhs->data[i];
380                 bitset_unit_t ru = rhs->data[i];
381
382                 if((lu | ru) & ~ru)
383                         return 0;
384         }
385
386         /*
387          * If the left hand sinde is a larger bitset than rhs,
388          * we have to check, that all extra bits in lhs are 0
389          */
390         if(lhs->units > n) {
391                 for(i = n; i < lhs->units; ++i) {
392                         if(lhs->data[i] != 0)
393                                 return 0;
394                 }
395         }
396
397         return 1;
398 }
399
400 /**
401  * Treat the bitset as a number and subtract 1.
402  * @param bs The bitset.
403  * @return The same bitset.
404  */
405 static INLINE void bitset_minus1(bitset_t *bs)
406 {
407 #define _SH (sizeof(bitset_unit_t) * 8 - 1)
408
409   bitset_pos_t i;
410
411   for(i = 0; i < bs->units; ++i) {
412     bitset_unit_t unit = bs->data[i];
413     bitset_unit_t um1  = unit - 1;
414
415     bs->data[i] = um1;
416
417     if(((unit >> _SH) ^ (um1 >> _SH)) == 0)
418       break;
419   }
420 #undef _SH
421 }
422
423 /**
424  * Print a bitset to a stream.
425  * The bitset is printed as a comma seperated list of bits set.
426  * @param file The stream.
427  * @param bs The bitset.
428  */
429 static INLINE void bitset_fprint(FILE *file, const bitset_t *bs)
430 {
431         const char *prefix = "";
432         int i;
433
434         putc('[', file);
435         for(i = bitset_next_set(bs, 0); i != -1; i = bitset_next_set(bs, i + 1)) {
436                 fprintf(file, "%s%u", prefix, i);
437                 prefix = ",";
438         }
439         putc(']', file);
440 }
441
442 static INLINE void bitset_debug_fprint(FILE *file, const bitset_t *bs)
443 {
444         bitset_pos_t i;
445
446         fprintf(file, "%u:", bs->units);
447         for(i = 0; i < bs->units; ++i)
448                 fprintf(file, " " BITSET_UNIT_FMT, bs->data[i]);
449 }
450
451 /*
452  * Here, the binary operations follow.
453  * And, Or, And Not, Xor are available.
454  */
455 #define BINARY_OP(op) \
456 static INLINE bitset_t *bitset_ ## op(bitset_t *tgt, const bitset_t *src) \
457 { \
458         bitset_pos_t i; \
459         bitset_pos_t n = tgt->units > src->units ? src->units : tgt->units; \
460         for(i = 0; i < n; i += _BITSET_BINOP_UNITS_INC) \
461                 _bitset_inside_binop_ ## op(&tgt->data[i], &src->data[i]); \
462         if(n < tgt->units) \
463                 _bitset_clear_rest(&tgt->data[i], tgt->units - i); \
464         return _bitset_mask_highest(tgt); \
465 }
466
467 /*
468  * Define the clear rest macro for the and, since it is the only case,
469  * were non existed (treated as 0) units in the src must be handled.
470  * For all other operations holds: x Op 0 = x for Op in { Andnot, Or, Xor }
471  *
472  * For and, each bitset implementer has to provide the macro
473  * _bitset_clear_units(data, n), which clears n units from the pointer
474  * data on.
475  */
476 #define _bitset_clear_rest(data,n) _bitset_inside_clear_units(data, n)
477 BINARY_OP(and)
478 #undef _bitset_clear_rest
479 #define _bitset_clear_rest(data,n)
480
481 BINARY_OP(andnot)
482 BINARY_OP(or)
483 BINARY_OP(xor)
484
485 #endif