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