028c54fed81ba3bbd61073111e5f76fcbf67b275
[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 "config.h"
17 #include "bitfiddle.h"
18
19 #include "bitset_std.h"
20
21 #if defined(__GNUC__) && defined(__i386__)
22 #include "bitset_ia32.h"
23 #endif
24
25 typedef struct _bitset_t {
26         unsigned long units;
27         unsigned long *data;
28 } bitset_t;
29
30 #define BS_UNIT_SIZE sizeof(unsigned long)
31 #define BS_UNIT_SIZE_BITS (BS_UNIT_SIZE * 8)
32 #define BS_UNIT_MASK (BS_UNIT_SIZE_BITS - 1)
33
34 /**
35  * Initialize a bitset.
36  * This functions should not be called.
37  *
38  * Note that this function needs three macros which must be provided by the
39  * bitfield implementor:
40  * - _bitset_overall_size(highest_bit) The overall size that must be
41  *   allocated for the bitfield in bytes.
42  * - _bitset_units(highest_bit) The number of units that will be
43  *   present in the bitfield for a given highest bit.
44  * - _bitset_data_ptr(data, highest_bit) This produces as pointer to the
45  *   first unit in the allocated memory area. The main reason for this
46  *   macro is, that some bitset implementors want control over memory
47  *   alignment.
48  *
49  * @param area A pointer to memory reserved for the bitset.
50  * @param units The number of units that are allocated for the bitset.
51  * @return A pointer to the initialized bitset.
52  */
53 static INLINE bitset_t *_bitset_prepare(void *area, unsigned long highest_bit)
54 {
55         bitset_t *ptr = area;
56         memset(area, 0, _bitset_overall_size(sizeof(bitset_t), highest_bit));
57         ptr->units = _bitset_units(highest_bit);
58         ptr->data = _bitset_data_ptr(area, sizeof(bitset_t), highest_bit);
59         return ptr;
60 }
61
62 /**
63  * Get the capacity of the bitset in bits.
64  * @param bs The bitset.
65  * @return The capacity in bits of the bitset.
66  */
67 #define bitset_capacity(bs) ((bs)->units * BS_UNIT_SIZE_BITS)
68
69 /**
70  * Allocate a bitset on an obstack.
71  * @param obst The obstack.
72  * @param highest_bit The greatest bit that shall be stored in the set.
73  * @return A pointer to an empty initialized bitset.
74  */
75 #define bitset_obstack_alloc(obst,highest_bit) \
76   _bitset_prepare(obstack_alloc(obst, _bitset_overall_size(sizeof(bitset_t), highest_bit)), highest_bit)
77
78 /**
79  * Allocate a bitset via malloc.
80  * @param highest_bit The greatest bit that shall be stored in the set.
81  * @return A pointer to an empty initialized bitset.
82  */
83 #define bitset_malloc(highest_bit) \
84         _bitset_prepare(malloc(_bitset_overall_size(sizeof(bitset_t), highest_bit)), highest_bit)
85
86 /**
87  * Free a bitset allocated with bitset_malloc().
88  * @param bs The bitset.
89  */
90 #define bitset_free(bs) free(bs)
91
92 /**
93  * Allocate a bitset on the stack via alloca.
94  * @param highest_bit The greatest bit that shall be stored in the set.
95  * @return A pointer to an empty initialized bitset.
96  */
97 #define bitset_alloca(highest_bit) \
98         _bitset_prepare(alloca(_bitset_overall_size(sizeof(bitset_t), highest_bit)), highest_bit)
99
100
101 /**
102  * Get the unit which contains a specific bit.
103  * This function is internal.
104  * @param bs The bitset.
105  * @param bit The bit.
106  * @return A pointer to the unit containing the bit.
107  */
108 static INLINE unsigned long *_bitset_get_unit(const bitset_t *bs, unsigned long bit)
109 {
110         assert(bit < bs->units * BS_UNIT_SIZE_BITS && "Bit too large");
111         return bs->data + bit / BS_UNIT_SIZE_BITS;
112 }
113
114 /**
115  * Set a bit in the bitset.
116  * @param bs The bitset.
117  * @param bit The bit to set.
118  */
119 static INLINE void bitset_set(bitset_t *bs, unsigned long bit)
120 {
121         unsigned long *unit = _bitset_get_unit(bs, bit);
122         _bitset_inside_set(*unit, bit & BS_UNIT_MASK);
123 }
124
125 /**
126  * Clear a bit in the bitset.
127  * @param bs The bitset.
128  * @param bit The bit to clear.
129  */
130 static INLINE void bitset_clear(bitset_t *bs, unsigned long bit)
131 {
132         unsigned long *unit = _bitset_get_unit(bs, bit);
133         _bitset_inside_clear(unit, bit & BS_UNIT_MASK);
134 }
135
136 static INLINE int bitset_is_set(const bitset_t *bs, unsigned long bit)
137 {
138         unsigned long *unit = _bitset_get_unit(bs, bit);
139         return _bitset_inside_is_set(unit, bit & BS_UNIT_MASK);
140 }
141
142 /**
143  * Flip a bit in a bitset.
144  * @param bs The bitset.
145  * @param bit The bit to flip.
146  */
147 static INLINE void bitset_flip(bitset_t *bs, unsigned long bit)
148 {
149         unsigned long *unit = _bitset_get_unit(bs, bit);
150         _bitset_inside_flip(unit, bit & BS_UNIT_MASK);
151 }
152
153 /**
154  * Copy a bitset to another.
155  * @param tgt The target bitset.
156  * @param src The source bitset.
157  * @return The target bitset.
158  */
159 static INLINE bitset_t *bitset_copy(bitset_t *tgt, const bitset_t *src)
160 {
161         unsigned long tu = tgt->units;
162         unsigned long su = src->units;
163         unsigned long min_units = tu < su ? tu : su;
164         memcpy(tgt->data, src->data, min_units * BS_UNIT_SIZE);
165         if(tu > min_units)
166                 memset(tgt->data + min_units, 0, BS_UNIT_SIZE * (tu - min_units));
167         return tgt;
168 }
169
170 /**
171  * Find the smallest bit set in the bitset.
172  * @param bs The bitset.
173  * @return The smallest bit set in the bitset.
174  */
175 static INLINE unsigned long bitset_min(const bitset_t *bs)
176 {
177         unsigned long i, ofs = 0;
178
179         for(i = 0; i < bs->units; ++i) {
180                 unsigned long *unit = &bs->data[i];
181                 unsigned long pos = _bitset_inside_ntz(unit);
182                 if(pos > 0)
183                         return ofs + pos;
184                 ofs += BS_UNIT_SIZE_BITS;
185         }
186
187         return 0;
188 }
189
190 /**
191  * Find the greatest bit set in the bitset.
192  * @param bs The bitset.
193  * @return The greatest bit set in the bitset.
194  */
195 static INLINE unsigned long bitset_max(const bitset_t *bs)
196 {
197         unsigned long i, max = 0, ofs = 0;
198
199         for(i = 0; i < bs->units; ++i) {
200                 unsigned long *unit = &bs->data[i];
201                 unsigned long pos = _bitset_inside_nlz(unit);
202                 if(pos > 0)
203                         max = ofs + pos;
204                 ofs += BS_UNIT_SIZE_BITS;
205         }
206
207         return max;
208 }
209
210 /**
211  * Find the next set bit from a given bit.
212  * @note Note that if pos is set, pos is returned.
213  * @param bs The bitset.
214  * @param pos The bit from which to search for the next set bit.
215  * @return The next set bit from pos on, or -1, if no set bit was found
216  * after pos.
217  */
218 static INLINE unsigned long _bitset_next(const bitset_t *bs,
219                 unsigned long pos, int set)
220 {
221         unsigned long unit_number = pos / BS_UNIT_SIZE_BITS;
222         unsigned long bit_in_unit = pos & BS_UNIT_MASK;
223         unsigned long in_unit_mask = (1 << bit_in_unit) - 1;
224
225         /*
226          * Mask out the bits smaller than pos in the current unit.
227          * We are only interested in bits set higher than pos.
228          */
229         unsigned long curr_unit = bs->data[unit_number] & ~in_unit_mask;
230
231         /* Find the next bit set in the unit. */
232         unsigned long next_in_this_unit
233                 = _bitset_inside_ntz_value(set ? curr_unit : ~curr_unit);
234
235         /* If there is a bit set in the current unit, exit. */
236         if(next_in_this_unit < BS_UNIT_SIZE_BITS)
237                 return next_in_this_unit + unit_number * BS_UNIT_SIZE_BITS;
238
239         /* Else search for set bits in the next units. */
240         else {
241                 unsigned long i;
242                 for(i = unit_number + 1; i < bs->units; ++i) {
243                         unsigned long data = bs->data[i];
244                         unsigned long first_set = _bitset_inside_ntz_value(set ? data : ~data);
245                         if(first_set < BS_UNIT_SIZE_BITS)
246                                 return first_set + i * BS_UNIT_SIZE_BITS;
247                 }
248         }
249
250         return -1;
251 }
252
253 #define bitset_next_clear(bs,pos) _bitset_next((bs), (pos), 0)
254 #define bitset_next_set(bs,pos) _bitset_next((bs), (pos), 1)
255
256 /**
257  * Convenience macro for bitset iteration.
258  * @param bitset The bitset.
259  * @param elm A unsigned long variable.
260  */
261 #define bitset_foreach(bitset,elm) \
262   for(elm = bitset_next_set(bitset,0); elm != -1; elm = bitset_next_set(bitset,elm+1))
263
264 /**
265  * Count the bits set.
266  * This can also be seen as the cardinality of the set.
267  * @param bs The bitset.
268  * @return The number of bits set in the bitset.
269  */
270 static INLINE unsigned long bitset_popcnt(const bitset_t *bs)
271 {
272         unsigned long i, pop = 0;
273         unsigned long *unit;
274
275         for(i = 0, unit = bs->data; i < bs->units; ++i, ++unit)
276                 pop += _bitset_inside_pop(unit);
277
278         return pop;
279 }
280
281 /**
282  * Clear the bitset.
283  * This sets all bits to zero.
284  * @param bs The bitset.
285  */
286 static INLINE void bitset_clear_all(bitset_t *bs)
287 {
288         memset(bs->data, 0, BS_UNIT_SIZE * bs->units);
289 }
290
291 /**
292  * Check, if one bitset is contained by another.
293  * That is, each bit set in lhs is also set in rhs.
294  * @param lhs A bitset.
295  * @param rhs Another bitset.
296  * @return 1, if all bits in lhs are also set in rhs, 0 otherwise.
297  */
298 static INLINE int bitset_contains(const bitset_t *lhs, const bitset_t *rhs)
299 {
300         unsigned long n = lhs->units < rhs->units ? lhs->units : rhs->units;
301         unsigned long i;
302
303         for(i = 0; i < n; ++i) {
304                 unsigned long lu = lhs->data[i];
305                 unsigned long ru = rhs->data[i];
306
307                 if((lu | ru) & ~ru)
308                         return 0;
309         }
310
311         /*
312          * If the left hand sinde is a larger bitset than rhs,
313          * we have to check, that all extra bits in lhs are 0
314          */
315         if(lhs->units > n) {
316                 for(i = n; i < lhs->units; ++i) {
317                         if(lhs->data[i] != 0)
318                                 return 0;
319                 }
320         }
321
322         return 1;
323 }
324
325 /**
326  * Print a bitset to a stream.
327  * The bitset is printed as a comma seperated list of bits set.
328  * @param file The stream.
329  * @param bs The bitset.
330  */
331 static INLINE void bitset_fprint(FILE *file, bitset_t *bs)
332 {
333         const char *prefix = "";
334         int i;
335
336         putc('[', file);
337         for(i = bitset_next_set(bs, 0); i != -1; i = bitset_next_set(bs, i + 1)) {
338                 fprintf(file, "%s%u", prefix, i);
339                 prefix = ",";
340         }
341         putc(']', file);
342 }
343
344 /*
345  * Here, the binary operations follow.
346  * And, Or, And Not, Xor are available.
347  */
348
349 #define BINARY_OP(op) \
350 static INLINE bitset_t *bitset_ ## op(bitset_t *tgt, const bitset_t *src) \
351 { \
352         int i; \
353         int n = tgt->units > src->units ? src->units : tgt->units; \
354         for(i = 0; i < n; i += _BITSET_BINOP_UNITS_INC) \
355                 _bitset_inside_binop_ ## op(&tgt->data[i], &src->data[i]); \
356         for(i = n; i < tgt->units; i += _BITSET_BINOP_UNITS_INC) \
357                 _bitset_inside_binop_with_zero_ ## op(&tgt->data[i]); \
358         return tgt; \
359 }
360
361 BINARY_OP(and)
362 BINARY_OP(andnot)
363 BINARY_OP(or)
364 BINARY_OP(xor)
365
366
367 #endif