#include "obst.h"
#define BITS_PER_ELEM (sizeof(unsigned) * 8)
-#define BITSET_SIZE_ELEMS(size_bits) ((size_bits)/BITS_PER_ELEM + 1)
+#define BITSET_SIZE_ELEMS(size_bits) ((size_bits+BITS_PER_ELEM-1)/BITS_PER_ELEM)
#define BITSET_SIZE_BYTES(size_bits) (BITSET_SIZE_ELEMS(size_bits) * sizeof(unsigned))
#define BITSET_ELEM(bitset,pos) bitset[pos / BITS_PER_ELEM]
unsigned i;
unsigned n = BITSET_SIZE_ELEMS(size);
+ if (n == 0)
+ return;
+
for (i = 0; i < n-1; ++i) {
bitset[i] = ~0u;
}
static inline void rbitset_flip_all(unsigned *bitset, unsigned size)
{
unsigned pos;
- unsigned n = BITSET_SIZE_ELEMS(size);
+ unsigned n = BITSET_SIZE_ELEMS(size);
+
+ if (n == 0)
+ return;
+
for (pos = 0; pos < n-1; ++pos) {
bitset[pos] ^= ~0u;
}
static inline void rbitset_set_range(unsigned *bitset, unsigned from,
unsigned to, bool val)
{
- assert(from < to);
-
- /*
- * A small example (for cleaning bits in the same unit).
- * from = 7
- * to = 19
- * do_set = 0
- * result: xxxxxxx000000000000xxxxxxxxxxxxx
- * from_unit_mask: 00000001111111111111111111111111
- * to_unit_mask: 11111111111111111110000000000000
- * scale: 01234567890123456789012345678901
- * 1 2 3
- */
+ /*
+ * A small example (for cleaning bits in the same unit).
+ * from = 7
+ * to = 19
+ * do_set = 0
+ * result: xxxxxxx000000000000xxxxxxxxxxxxx
+ * from_unit_mask: 00000001111111111111111111111111
+ * to_unit_mask: 11111111111111111110000000000000
+ * scale: 01234567890123456789012345678901
+ * 1 2 3
+ */
unsigned from_bit = from % BITS_PER_ELEM;
unsigned from_pos = from / BITS_PER_ELEM;
unsigned to_pos = to / BITS_PER_ELEM;
unsigned to_unit_mask = (1 << to_bit) - 1;
+ assert(from < to);
+
/* do we want to set the bits in the range? */
if (val) {
if (from_pos == to_pos) {
memcpy(dst, src, BITSET_SIZE_BYTES(size));
}
+static inline void rbitset_copy_into(unsigned *dst, const unsigned *src,
+ unsigned size)
+{
+ unsigned n = BITSET_SIZE_ELEMS(size);
+ unsigned last_mask = rbitset_last_mask_(size);
+
+ memcpy(dst, src, (n-1) * (BITS_PER_ELEM/8));
+ dst[n-1] = (src[n-1] & last_mask) | (dst[n-1] & ~last_mask);
+}
+
#endif