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