Clean up turn_back_am(), panic on unknown arity.
[libfirm] / include / libfirm / adt / bitset.h
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   A bitset implementation.
23  * @author  Sebastian Hack
24  * @date    15.10.2004
25  * @version $Id$
26  */
27 #ifndef FIRM_ADT_BITSET_H
28 #define FIRM_ADT_BITSET_H
29
30 #include "firm_config.h"
31
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <assert.h>
35 #include <string.h>
36
37 #include "xmalloc.h"
38 #include "bitfiddle.h"
39
40 typedef unsigned int bitset_pos_t;
41
42 #include "bitset_std.h"
43
44 #if defined(__GNUC__) && defined(__i386__)
45 #include "bitset_ia32.h"
46 #endif
47
48 typedef struct _bitset_t {
49         bitset_pos_t units;
50         bitset_pos_t size;
51 } bitset_t;
52
53 #define BS_UNIT_SIZE         sizeof(bitset_unit_t)
54 #define BS_UNIT_SIZE_BITS    (BS_UNIT_SIZE * 8)
55 #define BS_UNIT_MASK         (BS_UNIT_SIZE_BITS - 1)
56
57 #define BS_DATA(bs)          ((bitset_unit_t *) ((char *) (bs) + sizeof(bitset_t)))
58 #define BS_UNITS(bits)       (round_up2(bits, BS_UNIT_SIZE_BITS) / BS_UNIT_SIZE_BITS)
59 #define BS_TOTAL_SIZE(bits)  (sizeof(bitset_t) + BS_UNITS(bits) * BS_UNIT_SIZE)
60
61 /**
62  * Initialize a bitset.
63  * This functions should not be called.
64  *
65  * Note that this function needs three macros which must be provided by the
66  * bitfield implementor:
67  * - _bitset_overall_size(size) The overall size that must be
68  *   allocated for the bitfield in bytes.
69  * - _bitset_units(size) The number of units that will be
70  *   present in the bitfield for a given highest bit.
71  * - _bitset_data_ptr(data, size) This produces as pointer to the
72  *   first unit in the allocated memory area. The main reason for this
73  *   macro is, that some bitset implementors want control over memory
74  *   alignment.
75  *
76  * @param area A pointer to memory reserved for the bitset.
77  * @param size The size of the bitset in bits.
78  * @return A pointer to the initialized bitset.
79  */
80 static INLINE bitset_t *_bitset_prepare(void *area, bitset_pos_t size)
81 {
82         bitset_t *ptr = area;
83         memset(ptr, 0, BS_TOTAL_SIZE(size));
84         ptr->units = BS_UNITS(size);
85         ptr->size  = size;
86         return ptr;
87 }
88
89 /**
90  * Mask out all bits, which are only there, because the number
91  * of bits in the set didn't match a unit size boundary.
92  * @param bs The bitset.
93  * @return The masked bitset.
94  */
95 static INLINE bitset_t *_bitset_mask_highest(bitset_t *bs)
96 {
97         bitset_pos_t rest = bs->size & BS_UNIT_MASK;
98         if (rest)
99                 BS_DATA(bs)[bs->units - 1] &= (1 << rest) - 1;
100         return bs;
101 }
102
103 /**
104  * Get the capacity of the bitset in bits.
105  * @param bs The bitset.
106  * @return The capacity in bits of the bitset.
107  */
108 #define bitset_capacity(bs) ((bs)->units * BS_UNIT_SIZE_BITS)
109
110 /**
111  * Get the size of the bitset in bits.
112  * @note Note the difference between capacity and size.
113  * @param bs The bitset.
114  * @return The highest bit which can be set or cleared plus 1.
115  */
116 #define bitset_size(bs)  ((bs)->size)
117
118 /**
119  * Allocate a bitset on an obstack.
120  * @param obst The obstack.
121  * @param size The greatest bit that shall be stored in the set.
122  * @return A pointer to an empty initialized bitset.
123  */
124 #define bitset_obstack_alloc(obst,size) \
125         _bitset_prepare(obstack_alloc(obst, BS_TOTAL_SIZE(size)), size)
126
127 /**
128  * Allocate a bitset via malloc.
129  * @param size The greatest bit that shall be stored in the set.
130  * @return A pointer to an empty initialized bitset.
131  */
132 #define bitset_malloc(size) \
133         _bitset_prepare(xmalloc(BS_TOTAL_SIZE(size)), size)
134
135 /**
136  * Free a bitset allocated with bitset_malloc().
137  * @param bs The bitset.
138  */
139 #define bitset_free(bs) free(bs)
140
141 /**
142  * Allocate a bitset on the stack via alloca.
143  * @param size The greatest bit that shall be stored in the set.
144  * @return A pointer to an empty initialized bitset.
145  */
146 #define bitset_alloca(size) \
147         _bitset_prepare(alloca(BS_TOTAL_SIZE(size)), size)
148
149
150 /**
151  * Get the unit which contains a specific bit.
152  * This function is internal.
153  * @param bs The bitset.
154  * @param bit The bit.
155  * @return A pointer to the unit containing the bit.
156  */
157 static INLINE bitset_unit_t *_bitset_get_unit(const bitset_t *bs, bitset_pos_t bit)
158 {
159         assert(bit <= bs->size && "Bit to large");
160         return BS_DATA(bs) + bit / BS_UNIT_SIZE_BITS;
161 }
162
163 /**
164  * Set a bit in the bitset.
165  * @param bs The bitset.
166  * @param bit The bit to set.
167  */
168 static INLINE void bitset_set(bitset_t *bs, bitset_pos_t bit)
169 {
170         bitset_unit_t *unit = _bitset_get_unit(bs, bit);
171         _bitset_inside_set(unit, bit & BS_UNIT_MASK);
172 }
173
174 /**
175  * Clear a bit in the bitset.
176  * @param bs The bitset.
177  * @param bit The bit to clear.
178  */
179 static INLINE void bitset_clear(bitset_t *bs, bitset_pos_t bit)
180 {
181         bitset_unit_t *unit = _bitset_get_unit(bs, bit);
182         _bitset_inside_clear(unit, bit & BS_UNIT_MASK);
183 }
184
185 /**
186  * Check, if a bit is set.
187  * @param bs The bitset.
188  * @param bit The bit to check for.
189  * @return 1, if the bit was set, 0 if not.
190  */
191 static INLINE int bitset_is_set(const bitset_t *bs, bitset_pos_t bit)
192 {
193         bitset_unit_t *unit = _bitset_get_unit(bs, bit);
194         return _bitset_inside_is_set(unit, bit & BS_UNIT_MASK);
195 }
196
197 /**
198  * Flip a bit in a bitset.
199  * @param bs The bitset.
200  * @param bit The bit to flip.
201  */
202 static INLINE void bitset_flip(bitset_t *bs, bitset_pos_t bit)
203 {
204         bitset_unit_t *unit = _bitset_get_unit(bs, bit);
205         _bitset_inside_flip(unit, bit & BS_UNIT_MASK);
206 }
207
208 /**
209  * Flip the whole bitset.
210  * @param bs The bitset.
211  */
212 static INLINE void bitset_flip_all(bitset_t *bs)
213 {
214         bitset_pos_t i;
215         for(i = 0; i < bs->units; i++)
216                 _bitset_inside_flip_unit(&BS_DATA(bs)[i]);
217         _bitset_mask_highest(bs);
218 }
219
220 /**
221  * Copy a bitset to another.
222  * @param tgt The target bitset.
223  * @param src The source bitset.
224  * @return The target bitset.
225  */
226 static INLINE bitset_t *bitset_copy(bitset_t *tgt, const bitset_t *src)
227 {
228         bitset_pos_t tu = tgt->units;
229         bitset_pos_t su = src->units;
230         bitset_pos_t min_units = tu < su ? tu : su;
231         memcpy(BS_DATA(tgt), BS_DATA(src), min_units * BS_UNIT_SIZE);
232         if(tu > min_units)
233                 memset(BS_DATA(tgt) + min_units, 0, BS_UNIT_SIZE * (tu - min_units));
234         return _bitset_mask_highest(tgt);
235 }
236
237 /**
238  * Find the next set bit from a given bit.
239  * @note Note that if pos is set, pos is returned.
240  * @param bs The bitset.
241  * @param pos The bit from which to search for the next set bit.
242  * @return The next set bit from pos on, or -1, if no set bit was found
243  * after pos.
244  */
245 static INLINE bitset_pos_t _bitset_next(const bitset_t *bs,
246                 bitset_pos_t pos, int set)
247 {
248         bitset_pos_t unit_number = pos / BS_UNIT_SIZE_BITS;
249         bitset_pos_t res;
250
251         if(pos >= bs->size)
252                 return -1;
253
254         {
255                 bitset_pos_t bit_in_unit = pos & BS_UNIT_MASK;
256                 bitset_pos_t in_unit_mask = (1 << bit_in_unit) - 1;
257
258                 /*
259                  * Mask out the bits smaller than pos in the current unit.
260                  * We are only interested in bits set higher than pos.
261                  */
262                 bitset_unit_t curr_unit = BS_DATA(bs)[unit_number];
263
264                 /*
265                  * Find the next bit set in the unit.
266                  * Mind that this function returns 0, if the unit is -1 and
267                  * counts the bits from 1 on.
268                  */
269                 bitset_pos_t next_in_this_unit =
270                         _bitset_inside_ntz_value((set ? curr_unit : ~curr_unit) & ~in_unit_mask);
271
272                 /* If there is a bit set in the current unit, exit. */
273                 if (next_in_this_unit < BS_UNIT_SIZE_BITS) {
274                         res = next_in_this_unit + unit_number * BS_UNIT_SIZE_BITS;
275                         return res < bs->size ? res : (bitset_pos_t) -1;
276                 }
277
278                 /* Else search for set bits in the next units. */
279                 else {
280                         bitset_pos_t i;
281                         for(i = unit_number + 1; i < bs->units; ++i) {
282                                 bitset_unit_t data = BS_DATA(bs)[i];
283                                 bitset_pos_t first_set =
284                                         _bitset_inside_ntz_value(set ? data : ~data);
285
286                                 if (first_set < BS_UNIT_SIZE_BITS) {
287                                         res = first_set + i * BS_UNIT_SIZE_BITS;
288                                         return res < bs->size ? res : (bitset_pos_t) -1;
289                                 }
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 != (bitset_pos_t) -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 != (bitset_pos_t) -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 unsigned bitset_popcnt(const bitset_t *bs)
319 {
320         bitset_pos_t  i;
321         bitset_unit_t *unit;
322         unsigned      pop = 0;
323
324         for (i = 0, unit = BS_DATA(bs); i < bs->units; ++i, ++unit)
325                 pop += _bitset_inside_pop(unit);
326
327         return pop;
328 }
329
330 /**
331  * Clear the bitset.
332  * This sets all bits to zero.
333  * @param bs The bitset.
334  */
335 static INLINE bitset_t *bitset_clear_all(bitset_t *bs)
336 {
337         memset(BS_DATA(bs), 0, BS_UNIT_SIZE * bs->units);
338         return bs;
339 }
340
341 /**
342  * Set the bitset.
343  * This sets all bits to one.
344  * @param bs The bitset.
345  */
346 static INLINE bitset_t *bitset_set_all(bitset_t *bs)
347 {
348         memset(BS_DATA(bs), -1, bs->units * BS_UNIT_SIZE);
349         return _bitset_mask_highest(bs);
350 }
351
352 /**
353  * Check, if one bitset is contained by another.
354  * That is, each bit set in lhs is also set in rhs.
355  * @param lhs A bitset.
356  * @param rhs Another bitset.
357  * @return 1, if all bits in lhs are also set in rhs, 0 otherwise.
358  */
359 static INLINE int bitset_contains(const bitset_t *lhs, const bitset_t *rhs)
360 {
361         bitset_pos_t n = lhs->units < rhs->units ? lhs->units : rhs->units;
362         bitset_pos_t i;
363
364         for(i = 0; i < n; ++i) {
365                 bitset_unit_t lu = BS_DATA(lhs)[i];
366                 bitset_unit_t ru = BS_DATA(rhs)[i];
367
368                 if((lu | ru) & ~ru)
369                         return 0;
370         }
371
372         /*
373          * If the left hand sinde is a larger bitset than rhs,
374          * we have to check, that all extra bits in lhs are 0
375          */
376         if(lhs->units > n) {
377                 for(i = n; i < lhs->units; ++i) {
378                         if(BS_DATA(lhs)[i] != 0)
379                                 return 0;
380                 }
381         }
382
383         return 1;
384 }
385
386 /**
387  * Treat the bitset as a number and subtract 1.
388  * @param bs The bitset.
389  * @return The same bitset.
390  */
391 static INLINE void bitset_minus1(bitset_t *bs)
392 {
393 #define _SH (sizeof(bitset_unit_t) * 8 - 1)
394
395         bitset_pos_t i;
396
397         for(i = 0; i < bs->units; ++i) {
398                 bitset_unit_t unit = BS_DATA(bs)[i];
399                 bitset_unit_t um1  = unit - 1;
400
401                 BS_DATA(bs)[i] = um1;
402
403                 if(((unit >> _SH) ^ (um1 >> _SH)) == 0)
404                         break;
405         }
406 #undef _SH
407 }
408
409 /**
410  * Check if two bitsets intersect.
411  * @param a The first bitset.
412  * @param b The second bitset.
413  * @return 1 if they have a bit in common, 0 if not.
414  */
415 static INLINE int bitset_intersect(const bitset_t *a, const bitset_t *b)
416 {
417         bitset_pos_t n = a->units < b->units ? a->units : b->units;
418         bitset_pos_t i;
419
420         for (i = 0; i < n; ++i)
421                 if (BS_DATA(a)[i] & BS_DATA(b)[i])
422                         return 1;
423
424         return 0;
425 }
426
427 /**
428  * set or clear all bits in the range [from;to[.
429  * @param a      The bitset.
430  * @param from   The first index to set to one.
431  * @param to     The last index plus one to set to one.
432  * @param do_set If 1 the bits are set, if 0, they are cleared.
433  */
434 static INLINE void bitset_mod_range(bitset_t *a, bitset_pos_t from, bitset_pos_t to, int do_set)
435 {
436         bitset_pos_t from_unit, to_unit, i;
437         bitset_unit_t from_unit_mask, to_unit_mask;
438
439         if (from == to)
440             return;
441
442         if (to < from) {
443                 bitset_pos_t tmp = from;
444                 from = to;
445                 to = tmp;
446         }
447
448         if (to > a->size)
449                 to = a->size;
450
451         /*
452          * A small example (for cleaning bits in the same unit).
453          * from   = 7
454          * to     = 19
455          * do_set = 0
456          * result:         xxxxxxx000000000000xxxxxxxxxxxxx
457          * from_unit_mask: 00000001111111111111111111111111
458          * to_unit_mask:   11111111111111111110000000000000
459          * scale:          01234567890123456789012345678901
460          *                           1         2         3
461          */
462
463         from_unit      = from / BS_UNIT_SIZE_BITS;
464         from_unit_mask = ~((1 << from) - 1);
465         from           = from & BS_UNIT_MASK;
466
467         to_unit        = to / BS_UNIT_SIZE_BITS;
468         to_unit_mask   = (1 << to) - 1;
469         to             = to & BS_UNIT_MASK;
470
471         /* do we want to set the bits in the range? */
472         if (do_set) {
473                 /* If from and to are in the same unit: */
474                 if (from_unit == to_unit)
475                         BS_DATA(a)[from_unit] |= from_unit_mask & to_unit_mask;
476
477                 /* Else, we have to treat the from and to units seperately */
478                 else {
479                         BS_DATA(a)[from_unit] |= from_unit_mask;
480                         BS_DATA(a)[to_unit]   |= to_unit_mask;
481                         for (i = from_unit + 1; i < to_unit; i++)
482                                 BS_DATA(a)[i] = BITSET_UNIT_ALL_ONE;
483                 }
484         }
485
486         /* ... or clear them? */
487         else {
488                 if (from_unit == to_unit)
489                         BS_DATA(a)[from_unit] &= ~(from_unit_mask & to_unit_mask);
490
491                 else {
492                         BS_DATA(a)[from_unit] &= ~from_unit_mask;
493                         BS_DATA(a)[to_unit]   &= ~to_unit_mask;
494                         for (i = from_unit + 1; i < to_unit; i++)
495                                 BS_DATA(a)[i] = 0;
496                 }
497         }
498 }
499
500 #define bitset_set_range(bs, from, to)   bitset_mod_range((bs), (from), (to), 1)
501 #define bitset_clear_range(bs, from, to) bitset_mod_range((bs), (from), (to), 0)
502
503 /**
504  * Check, if a bitset is empty.
505  * @param a The bitset.
506  * @return 1, if the bitset is empty, 0 if not.
507  */
508 static INLINE int bitset_is_empty(const bitset_t *a)
509 {
510         bitset_pos_t i;
511         for (i = 0; i < a->units; ++i)
512                 if (BS_DATA(a)[i] != 0)
513                         return 0;
514         return 1;
515 }
516
517 /**
518  * Print a bitset to a stream.
519  * The bitset is printed as a comma separated list of bits set.
520  * @param file The stream.
521  * @param bs The bitset.
522  */
523 static INLINE void bitset_fprint(FILE *file, const bitset_t *bs)
524 {
525         const char *prefix = "";
526         int i;
527
528         putc('{', file);
529         for(i = bitset_next_set(bs, 0); i != -1; i = bitset_next_set(bs, i + 1)) {
530                 fprintf(file, "%s%u", prefix, i);
531                 prefix = ",";
532         }
533         putc('}', file);
534 }
535
536 static INLINE void bitset_debug_fprint(FILE *file, const bitset_t *bs)
537 {
538         bitset_pos_t i;
539
540         fprintf(file, "%u:", bs->units);
541         for(i = 0; i < bs->units; ++i)
542                 fprintf(file, " " BITSET_UNIT_FMT, BS_DATA(bs)[i]);
543 }
544
545 /**
546  * Perform tgt = tgt \ src operation.
547  * @param tgt  The target bitset.
548  * @param src  The source bitset.
549  * @return the tgt set.
550  */
551 static INLINE bitset_t *bitset_andnot(bitset_t *tgt, const bitset_t *src);
552
553 /**
554  * Perform Union, tgt = tgt u src operation.
555  * @param tgt  The target bitset.
556  * @param src  The source bitset.
557  * @return the tgt set.
558  */
559 static INLINE bitset_t *bitset_or(bitset_t *tgt, const bitset_t *src);
560
561 /**
562  * Perform tgt = tgt ^ ~src operation.
563  * @param tgt  The target bitset.
564  * @param src  The source bitset.
565  * @return the tgt set.
566  */
567 static INLINE bitset_t *bitset_xor(bitset_t *tgt, const bitset_t *src);
568
569 /*
570  * Here, the binary operations follow.
571  * And, Or, And Not, Xor are available.
572  */
573 #define BINARY_OP(op) \
574 static INLINE bitset_t *bitset_ ## op(bitset_t *tgt, const bitset_t *src) \
575 { \
576         bitset_pos_t i; \
577         bitset_pos_t n = tgt->units > src->units ? src->units : tgt->units; \
578         for(i = 0; i < n; i += _BITSET_BINOP_UNITS_INC) \
579                 _bitset_inside_binop_ ## op(&BS_DATA(tgt)[i], &BS_DATA(src)[i]); \
580         if(n < tgt->units) \
581                 _bitset_clear_rest(&BS_DATA(tgt)[i], tgt->units - i); \
582         return _bitset_mask_highest(tgt); \
583 }
584
585 /*
586  * Define the clear rest macro for the and, since it is the only case,
587  * were non existed (treated as 0) units in the src must be handled.
588  * For all other operations holds: x Op 0 = x for Op in { Andnot, Or, Xor }
589  *
590  * For and, each bitset implementer has to provide the macro
591  * _bitset_clear_units(data, n), which clears n units from the pointer
592  * data on.
593  */
594 #define _bitset_clear_rest(data,n) _bitset_inside_clear_units(data, n)
595 BINARY_OP(and)
596 #undef _bitset_clear_rest
597 #define _bitset_clear_rest(data,n) do { } while(0)
598
599 BINARY_OP(andnot)
600 BINARY_OP(or)
601 BINARY_OP(xor)
602
603 #endif