2 /** Use ordinary ints as unit types. */
3 typedef unsigned int bitset_unit_t;
5 #define BITSET_UNIT_FMT "%0x"
6 #define BITSET_UNIT_ALL_ONE ((unsigned int) -1)
9 * Units needed for a given highest bit.
10 * This implementation always allocates units in a multiple of 16 bytes.
11 * @param size The size of the bitset in bits.
12 * @return The number of units needed.
14 #define _bitset_units(size) (round_up2(size, BS_UNIT_SIZE_BITS) / BS_UNIT_SIZE_BITS)
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 size The size of the bitset in bits.
22 * @return The overall amount of bytes needed for that bitset.
24 #define _bitset_overall_size(bitset_base_size,size) \
25 (bitset_base_size + _bitset_units(size) * BS_UNIT_SIZE)
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 has to be taken into account.
32 * @param size The size of the bitset in bits.
34 #define _bitset_data_ptr(data,bitset_base_size,size) \
35 ((bitset_unit_t *) ((char *) data + bitset_base_size))
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.
43 #define _bitset_inside_clear_units(addr,n) \
44 memset(addr, 0, n * BS_UNIT_SIZE)
47 * Set a bit in a unit.
48 * @param unit A pointer to the unit.
49 * @param bit which bit to set.
51 #define _bitset_inside_set(unit_ptr,bit) (*unit_ptr) |= (1 << (bit))
54 * Clear a bit in a unit.
55 * @param unit A pointer to the unit.
56 * @param bit which bit to set.
58 #define _bitset_inside_clear(unit_ptr,bit) (*unit_ptr) &= ~(1 << (bit))
61 * Flip a bit in a unit.
62 * @param unit A pointer to the unit.
63 * @param bit which bit to set.
65 #define _bitset_inside_flip(unit_ptr,bit) (*unit_ptr) ^= ~(1 << (bit))
69 * @param unit_ptr The pointer to the unit.
71 #define _bitset_inside_flip_unit(unit_ptr) (*unit_ptr) = ~(*unit_ptr)
74 * Count the number of leading zeroes in a unit.
75 * @param unit A pointer to the unit.
76 * @return The Number of leading zeroes.
78 #define _bitset_inside_nlz(unit_ptr) (nlz(*unit_ptr))
82 * Count the number of trailing zeroes in a unit.
83 * @param unit A pointer to the unit.
84 * @return The Number of leading zeroes.
86 #define _bitset_inside_ntz(unit_ptr) _bitset_std_inside_ntz(unit_ptr)
87 static INLINE bitset_pos_t _bitset_std_inside_ntz(bitset_unit_t *unit_ptr)
89 unsigned long data = *unit_ptr;
90 return 32 - (bitset_pos_t) nlz(~data & (data - 1));
94 * Count the number of trailing zeroes in a unit (whereas the unit is no
95 * pointer but a value).
97 * @return The Number of leading zeroes.
99 #define _bitset_inside_ntz_value(unit) (32 - nlz(~(unit) & ((unit) - 1)))
102 * test if a bit is set in a unit.
103 * @param unit_ptr The pointer to the unit.
104 * @param bit The bit to check.
105 * @return 1, if the bit is set, 0 otherise.
107 #define _bitset_inside_is_set(unit_ptr,bit) \
108 (((*unit_ptr) & (1 << (bit))) != 0)
111 * count the number of bits set in a unit.
112 * @param unit_ptr The pointer to a unit.
113 * @return The number of bits set in the unit.
115 #define _bitset_inside_pop(unit_ptr) (popcnt(*unit_ptr))
117 #define _BITSET_BINOP_UNITS_INC 1
119 #define _bitset_inside_binop_and(tgt,src) ((*tgt) &= (*src))
120 #define _bitset_inside_binop_andnot(tgt,src) ((*tgt) &= ~(*src))
121 #define _bitset_inside_binop_or(tgt,src) ((*tgt) |= (*src))
122 #define _bitset_inside_binop_xor(tgt,src) ((*tgt) ^= (*src))