7183e877e8f3e41a0710650293795fbaa0d77728
[libfirm] / ir / adt / bitset_std.h
1
2 /** Use ordinary ints as unit types. */
3 typedef unsigned int bitset_unit_t;
4
5 #define BITSET_UNIT_FMT "%0x"
6 #define BITSET_UNIT_ALL_ONE ((unsigned int) -1)
7
8 /**
9  * Units needed for a given highest bit.
10  * This implementation always allocates units in a multiple of 16 bytes.
11  * @param highest_bit The highest bit that should be storable.
12  * @return The number of units needed.
13  */
14 #define _bitset_units(highest_bit) (round_up2(highest_bit, BS_UNIT_SIZE_BITS) / BS_UNIT_SIZE_BITS)
15
16 /**
17  * Compute the size in bytes needed for a bitseti, overall.
18  * This also include the size for the bitset data structure.
19  * This implementation computes the size in wat, that the bitset units
20  * can be aligned to 16 bytes.
21  * @param highest_bit The highest bit that shall be storable.
22  * @return The overall amount of bytes needed for that bitset.
23  */
24 #define _bitset_overall_size(bitset_base_size,highest_bit) \
25         (bitset_base_size + _bitset_units(highest_bit) * BS_UNIT_SIZE)
26
27 /**
28  * calculate the pointer to the data space of the bitset.
29  * @param data The base address of the allocated memory
30  * @param bitset_base_size The size of the basical bitset data structure
31  * which hast to be taken into account.
32  * @param hightest_bit The highest bit that will occur in the bitset.
33  */
34 #define _bitset_data_ptr(data,bitset_base_size,highest_bit) \
35         ((bitset_unit_t *) ((char *) data + bitset_base_size))
36
37
38 /**
39  * Clear some units from a certain address on.
40  * @param addr The address from where to clear.
41  * @param n The number of units to set to 0.
42  */
43 #define _bitset_inside_clear_units(addr,n) \
44         memset(addr, 0, n * BS_UNIT_SIZE)
45
46 /**
47  * Set a bit in a unit.
48  * @param unit A pointer to the unit.
49  * @param bit which bit to set.
50  */
51 #define _bitset_inside_set(unit_ptr,bit) (*unit_ptr) |= (1 << (bit))
52
53 /**
54  * Clear a bit in a unit.
55  * @param unit A pointer to the unit.
56  * @param bit which bit to set.
57  */
58 #define _bitset_inside_clear(unit_ptr,bit) (*unit_ptr) &= ~(1 << (bit))
59
60 /**
61  * Flip a bit in a unit.
62  * @param unit A pointer to the unit.
63  * @param bit which bit to set.
64  */
65 #define _bitset_inside_flip(unit_ptr,bit) (*unit_ptr) ^= ~(1 << (bit))
66
67 /**
68  * Count the number of leading zeroes in a unit.
69  * @param unit A pointer to the unit.
70  * @return The Number of leading zeroes.
71  */
72 #define _bitset_inside_nlz(unit_ptr) (nlz(*unit_ptr))
73
74
75 /**
76  * Count the number of trailing zeroes in a unit.
77  * @param unit A pointer to the unit.
78  * @return The Number of leading zeroes.
79  */
80 #define _bitset_inside_ntz(unit_ptr) _bitset_std_inside_ntz(unit_ptr)
81 static INLINE bitset_pos_t _bitset_std_inside_ntz(bitset_unit_t *unit_ptr)
82 {
83         unsigned long data = *unit_ptr;
84         return 32 - (bitset_pos_t) nlz(~data & (data - 1));
85 }
86
87 /**
88  * Count the number of trailing zeroes in a unit (whereas the unit is no
89  * pointer but a value).
90  * @param unit A unit.
91  * @return The Number of leading zeroes.
92  */
93 #define _bitset_inside_ntz_value(unit) (32 - nlz(~(unit) & ((unit) - 1)))
94
95 /**
96  * test if a bit is set in a unit.
97  * @param unit_ptr The pointer to the unit.
98  * @param bit The bit to check.
99  * @return 1, if the bit is set, 0 otherise.
100  */
101 #define _bitset_inside_is_set(unit_ptr,bit) \
102         (((*unit_ptr) & (1 << (bit))) != 0)
103
104 /**
105  * count the number of bits set in a unit.
106  * @param unit_ptr The pointer to a unit.
107  * @return The number of bits set in the unit.
108  */
109 #define _bitset_inside_pop(unit_ptr) (popcnt(*unit_ptr))
110
111 #define _BITSET_BINOP_UNITS_INC 1
112
113 #define _bitset_inside_binop_and(tgt,src) ((*tgt) &= (*src))
114 #define _bitset_inside_binop_andnot(tgt,src) ((*tgt) &= ~(*src))
115 #define _bitset_inside_binop_or(tgt,src) ((*tgt) |= (*src))
116 #define _bitset_inside_binop_xor(tgt,src) ((*tgt) ^= (*src))