#include "firm_config.h"
#include "bitfiddle.h"
+#ifdef _WIN32
+#include <malloc.h>
+#else
+#include <alloca.h>
+#endif
+
typedef unsigned int bitset_pos_t;
#include "bitset_std.h"
typedef struct _bitset_t {
bitset_pos_t units;
- bitset_pos_t highest_bit;
+ bitset_pos_t size;
bitset_unit_t *data;
} bitset_t;
*
* Note that this function needs three macros which must be provided by the
* bitfield implementor:
- * - _bitset_overall_size(highest_bit) The overall size that must be
+ * - _bitset_overall_size(size) The overall size that must be
* allocated for the bitfield in bytes.
- * - _bitset_units(highest_bit) The number of units that will be
+ * - _bitset_units(size) The number of units that will be
* present in the bitfield for a given highest bit.
- * - _bitset_data_ptr(data, highest_bit) This produces as pointer to the
+ * - _bitset_data_ptr(data, size) This produces as pointer to the
* first unit in the allocated memory area. The main reason for this
* macro is, that some bitset implementors want control over memory
* alignment.
*
* @param area A pointer to memory reserved for the bitset.
- * @param units The number of units that are allocated for the bitset.
+ * @param size The size of the bitset in bits.
* @return A pointer to the initialized bitset.
*/
-static INLINE bitset_t *_bitset_prepare(void *area, bitset_pos_t highest_bit)
+static INLINE bitset_t *_bitset_prepare(void *area, bitset_pos_t size)
{
bitset_t *ptr = area;
- memset(area, 0, _bitset_overall_size(sizeof(bitset_t), highest_bit));
- ptr->units = _bitset_units(highest_bit);
- ptr->highest_bit = highest_bit;
- ptr->data = _bitset_data_ptr(area, sizeof(bitset_t), highest_bit);
+ memset(area, 0, _bitset_overall_size(sizeof(bitset_t), size));
+ ptr->units = _bitset_units(size);
+ ptr->size = size;
+ ptr->data = _bitset_data_ptr(area, sizeof(bitset_t), size);
return ptr;
}
*/
static INLINE bitset_t *_bitset_mask_highest(bitset_t *bs)
{
- bs->data[bs->units - 1] &= (bs->highest_bit & BS_UNIT_MASK) - 1;
- return bs;
+ bitset_pos_t rest = bs->size & BS_UNIT_MASK;
+ if (rest)
+ bs->data[bs->units - 1] &= (1 << rest) - 1;
+ return bs;
}
/**
#define bitset_capacity(bs) ((bs)->units * BS_UNIT_SIZE_BITS)
/**
- * Get the highest bit which can be stored in the bitset.
+ * Get the size of the bitset in bits.
+ * @note Note the difference between capacity and size.
* @param bs The bitset.
- * @return The highest bit which can be set or cleared.
+ * @return The highest bit which can be set or cleared plus 1.
*/
-#define bistet_highest_bit(bs) ((bs)->highest_bit)
+#define bitset_size(bs) ((bs)->size)
/**
* Allocate a bitset on an obstack.
* @param obst The obstack.
- * @param highest_bit The greatest bit that shall be stored in the set.
+ * @param size The greatest bit that shall be stored in the set.
* @return A pointer to an empty initialized bitset.
*/
-#define bitset_obstack_alloc(obst,highest_bit) \
- _bitset_prepare(obstack_alloc(obst, _bitset_overall_size(sizeof(bitset_t), highest_bit)), highest_bit)
+#define bitset_obstack_alloc(obst,size) \
+ _bitset_prepare(obstack_alloc(obst, _bitset_overall_size(sizeof(bitset_t), size)), size)
/**
* Allocate a bitset via malloc.
- * @param highest_bit The greatest bit that shall be stored in the set.
+ * @param size The greatest bit that shall be stored in the set.
* @return A pointer to an empty initialized bitset.
*/
-#define bitset_malloc(highest_bit) \
- _bitset_prepare(malloc(_bitset_overall_size(sizeof(bitset_t), highest_bit)), highest_bit)
+#define bitset_malloc(size) \
+ _bitset_prepare(xmalloc(_bitset_overall_size(sizeof(bitset_t), size)), size)
/**
* Free a bitset allocated with bitset_malloc().
/**
* Allocate a bitset on the stack via alloca.
- * @param highest_bit The greatest bit that shall be stored in the set.
+ * @param size The greatest bit that shall be stored in the set.
* @return A pointer to an empty initialized bitset.
*/
-#define bitset_alloca(highest_bit) \
- _bitset_prepare(alloca(_bitset_overall_size(sizeof(bitset_t), highest_bit)), highest_bit)
+#define bitset_alloca(size) \
+ _bitset_prepare(alloca(_bitset_overall_size(sizeof(bitset_t), size)), size)
/**
static INLINE bitset_unit_t *_bitset_get_unit(const bitset_t *bs, bitset_pos_t bit)
{
/* assert(bit < bs->units * BS_UNIT_SIZE_BITS && "Bit too large"); */
- assert(bit <= bs->highest_bit && "Bit to large");
+ assert(bit <= bs->size && "Bit to large");
return bs->data + bit / BS_UNIT_SIZE_BITS;
}
_bitset_inside_flip(unit, bit & BS_UNIT_MASK);
}
+/**
+ * Flip the whole bitset.
+ * @param bs The bitset.
+ */
+static INLINE void bitset_flip_all(bitset_t *bs)
+{
+ bitset_pos_t i;
+ for(i = 0; i < bs->units; i++)
+ _bitset_inside_flip_unit(&bs->data[i]);
+ _bitset_mask_highest(bs);
+}
+
/**
* Copy a bitset to another.
* @param tgt The target bitset.
{
bitset_pos_t unit_number = pos / BS_UNIT_SIZE_BITS;
- if(unit_number >= bs->units)
+ if(pos >= bs->size)
return -1;
{
*/
static INLINE bitset_t *bitset_set_all(bitset_t *bs)
{
- memset(bs->data, -1, BS_UNIT_SIZE * bs->units);
- return bs;
+ memset(bs->data, -1, bs->units * BS_UNIT_SIZE);
+ return _bitset_mask_highest(bs);
}
/**
return 1;
}
+/**
+ * Treat the bitset as a number and subtract 1.
+ * @param bs The bitset.
+ * @return The same bitset.
+ */
+static INLINE void bitset_minus1(bitset_t *bs)
+{
+#define _SH (sizeof(bitset_unit_t) * 8 - 1)
+
+ bitset_pos_t i;
+
+ for(i = 0; i < bs->units; ++i) {
+ bitset_unit_t unit = bs->data[i];
+ bitset_unit_t um1 = unit - 1;
+
+ bs->data[i] = um1;
+
+ if(((unit >> _SH) ^ (um1 >> _SH)) == 0)
+ break;
+ }
+#undef _SH
+}
+
/**
* Print a bitset to a stream.
* The bitset is printed as a comma seperated list of bits set.