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