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))
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.
72 #define _bitset_inside_nlz(unit_ptr) (nlz(*unit_ptr))
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.
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)
83 unsigned long data = *unit_ptr;
84 return 32 - (bitset_pos_t) nlz(~data & (data - 1));
88 * Count the number of trailing zeroes in a unit (whereas the unit is no
89 * pointer but a value).
91 * @return The Number of leading zeroes.
93 #define _bitset_inside_ntz_value(unit) (32 - nlz(~(unit) & ((unit) - 1)))
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.
101 #define _bitset_inside_is_set(unit_ptr,bit) \
102 (((*unit_ptr) & (1 << (bit))) != 0)
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.
109 #define _bitset_inside_pop(unit_ptr) (popcnt(*unit_ptr))
111 #define _BITSET_BINOP_UNITS_INC 1
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))