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