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