6 /** Use ordinary ints as unit types. */
7 typedef unsigned int bitset_unit_t;
9 #define BITSET_UNIT_FMT "%0x"
10 #define BITSET_UNIT_ALL_ONE ((unsigned int) -1)
13 * Units needed for a given highest bit.
14 * This implementation always allocates units in a multiple of 16 bytes.
15 * @param size The size of the bitset in bits.
16 * @return The number of units needed.
18 #define _bitset_units(size) (round_up2(size, BS_UNIT_SIZE_BITS) / BS_UNIT_SIZE_BITS)
21 * Compute the size in bytes needed for a bitseti, overall.
22 * This also include the size for the bitset data structure.
23 * This implementation computes the size in wat, that the bitset units
24 * can be aligned to 16 bytes.
25 * @param size The size of the bitset in bits.
26 * @return The overall amount of bytes needed for that bitset.
28 #define _bitset_overall_size(bitset_base_size,size) \
29 (bitset_base_size + _bitset_units(size) * BS_UNIT_SIZE)
32 * calculate the pointer to the data space of the bitset.
33 * @param data The base address of the allocated memory
34 * @param bitset_base_size The size of the basical bitset data structure
35 * which has to be taken into account.
36 * @param size The size of the bitset in bits.
38 #define _bitset_data_ptr(data,bitset_base_size,size) \
39 ((bitset_unit_t *) ((char *) data + bitset_base_size))
43 * Clear some units from a certain address on.
44 * @param addr The address from where to clear.
45 * @param n The number of units to set to 0.
47 #define _bitset_inside_clear_units(addr,n) \
48 memset(addr, 0, n * BS_UNIT_SIZE)
51 * Set a bit in a unit.
52 * @param unit A pointer to the unit.
53 * @param bit which bit to set.
55 #define _bitset_inside_set(unit_ptr,bit) (*unit_ptr) |= (1 << (bit))
58 * Clear a bit in a unit.
59 * @param unit A pointer to the unit.
60 * @param bit which bit to set.
62 #define _bitset_inside_clear(unit_ptr,bit) (*unit_ptr) &= ~(1 << (bit))
65 * Flip a bit in a unit.
66 * @param unit A pointer to the unit.
67 * @param bit which bit to set.
69 #define _bitset_inside_flip(unit_ptr,bit) (*unit_ptr) ^= (1 << (bit))
73 * @param unit_ptr The pointer to the unit.
75 #define _bitset_inside_flip_unit(unit_ptr) (*unit_ptr) = ~(*unit_ptr)
78 * Count the number of leading zeroes in a unit.
79 * @param unit A pointer to the unit.
80 * @return The Number of leading zeroes.
82 #define _bitset_inside_nlz(unit_ptr) (nlz(*unit_ptr))
86 * Count the number of trailing zeroes in a unit.
87 * @param unit A pointer to the unit.
88 * @return The Number of leading zeroes.
90 #define _bitset_inside_ntz(unit_ptr) _bitset_std_inside_ntz(unit_ptr)
91 static INLINE unsigned _bitset_std_inside_ntz(bitset_unit_t *unit_ptr)
93 unsigned long data = *unit_ptr;
94 return 32 - (unsigned) nlz(~data & (data - 1));
98 * Count the number of trailing zeroes in a unit (whereas the unit is no
99 * pointer but a value).
100 * @param unit A unit.
101 * @return The Number of leading zeroes.
103 #define _bitset_inside_ntz_value(unit) (32 - nlz(~(unit) & ((unit) - 1)))
106 * test if a bit is set in a unit.
107 * @param unit_ptr The pointer to the unit.
108 * @param bit The bit to check.
109 * @return 1, if the bit is set, 0 otherise.
111 #define _bitset_inside_is_set(unit_ptr,bit) \
112 (((*unit_ptr) & (1 << (bit))) != 0)
115 * count the number of bits set in a unit.
116 * @param unit_ptr The pointer to a unit.
117 * @return The number of bits set in the unit.
119 #define _bitset_inside_pop(unit_ptr) (popcnt(*unit_ptr))
121 #define _BITSET_BINOP_UNITS_INC 1
123 #define _bitset_inside_binop_and(tgt,src) ((*tgt) &= (*src))
124 #define _bitset_inside_binop_andnot(tgt,src) ((*tgt) &= ~(*src))
125 #define _bitset_inside_binop_or(tgt,src) ((*tgt) |= (*src))
126 #define _bitset_inside_binop_xor(tgt,src) ((*tgt) ^= (*src))