#ifndef __FIRM_BITSET_H
#define __FIRM_BITSET_H
+#include "firm_config.h"
+
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <alloca.h>
#endif
-#include "firm_config.h"
+#include "xmalloc.h"
#include "bitfiddle.h"
typedef unsigned int bitset_pos_t;
typedef struct _bitset_t {
bitset_pos_t units;
- bitset_pos_t size;
+ bitset_pos_t size;
bitset_unit_t *data;
} bitset_t;
bitset_t *ptr = area;
memset(area, 0, _bitset_overall_size(sizeof(bitset_t), size));
ptr->units = _bitset_units(size);
- ptr->size = size;
+ ptr->size = size;
ptr->data = _bitset_data_ptr(area, sizeof(bitset_t), size);
return ptr;
}
* @return A pointer to an empty initialized bitset.
*/
#define bitset_obstack_alloc(obst,size) \
- _bitset_prepare(obstack_alloc(obst, _bitset_overall_size(sizeof(bitset_t), size)), size)
+ _bitset_prepare(obstack_alloc(obst, _bitset_overall_size(sizeof(bitset_t), size)), size)
/**
* Allocate a bitset via malloc.
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->size && "Bit to large");
+ assert(bit <= bs->size && "Bit to large");
return bs->data + bit / BS_UNIT_SIZE_BITS;
}
static INLINE bitset_pos_t _bitset_next(const bitset_t *bs,
bitset_pos_t pos, int set)
{
+ bitset_pos_t unit_number = pos / BS_UNIT_SIZE_BITS;
+ bitset_pos_t res;
+
if(pos >= bs->size)
return -1;
{
- lc_bitset_pos_t unit_number = pos / LC_BS_UNIT_SIZE_BITS;
- lc_bitset_unit_t set_mask = -set;
- lc_bitset_pos_t bit_in_unit = pos & LC_BS_UNIT_MASK;
- lc_bitset_pos_t in_unit_mask = (1 << bit_in_unit) - 1;
+ bitset_pos_t bit_in_unit = pos & BS_UNIT_MASK;
+ bitset_pos_t in_unit_mask = (1 << bit_in_unit) - 1;
/*
* Mask out the bits smaller than pos in the current unit.
* We are only interested in bits set higher than pos.
*/
- lc_bitset_unit_t curr_unit = bs->data[unit_number];
+ bitset_unit_t curr_unit = bs->data[unit_number];
/*
* Find the next bit set in the unit.
* Mind that this function returns 0, if the unit is -1 and
* counts the bits from 1 on.
*/
- lc_bitset_pos_t next_in_this_unit =
- _lc_bitset_inside_ntz_value((set_mask ^ curr_unit) & ~in_unit_mask);
+ bitset_pos_t next_in_this_unit =
+ _bitset_inside_ntz_value((set ? curr_unit : ~curr_unit) & ~in_unit_mask);
/* If there is a bit set in the current unit, exit. */
- if(next_in_this_unit < LC_BS_UNIT_SIZE_BITS)
- return next_in_this_unit + unit_number * LC_BS_UNIT_SIZE_BITS;
+ if (next_in_this_unit < BS_UNIT_SIZE_BITS) {
+ res = next_in_this_unit + unit_number * BS_UNIT_SIZE_BITS;
+ return res < bs->size ? res : -1;
+ }
/* Else search for set bits in the next units. */
else {
- lc_bitset_pos_t i;
+ bitset_pos_t i;
for(i = unit_number + 1; i < bs->units; ++i) {
- lc_bitset_unit_t data = bs->data[i];
- lc_bitset_pos_t first_set = _lc_bitset_inside_ntz_value(set_mask ^ data);
-
- if(first_set < LC_BS_UNIT_SIZE_BITS)
- return first_set + i * LC_BS_UNIT_SIZE_BITS;
+ bitset_unit_t data = bs->data[i];
+ bitset_pos_t first_set =
+ _bitset_inside_ntz_value(set ? data : ~data);
+
+ if (first_set < BS_UNIT_SIZE_BITS) {
+ res = first_set + i * BS_UNIT_SIZE_BITS;
+ return res < bs->size ? res : -1;
+ }
}
}
}
* @param elm A unsigned long variable.
*/
#define bitset_foreach(bitset,elm) \
- for(elm = bitset_next_set(bitset,0); elm != -1; elm = bitset_next_set(bitset,elm+1))
+ for(elm = bitset_next_set(bitset,0); elm != -1; elm = bitset_next_set(bitset,elm+1))
#define bitset_foreach_clear(bitset,elm) \
- for(elm = bitset_next_clear(bitset,0); elm != -1; elm = bitset_next_clear(bitset,elm+1))
+ for(elm = bitset_next_clear(bitset,0); elm != -1; elm = bitset_next_clear(bitset,elm+1))
/**
* Count the bits set.
static INLINE bitset_t *bitset_clear_all(bitset_t *bs)
{
memset(bs->data, 0, BS_UNIT_SIZE * bs->units);
- return bs;
+ return bs;
}
/**
static INLINE bitset_t *bitset_set_all(bitset_t *bs)
{
memset(bs->data, -1, bs->units * BS_UNIT_SIZE);
- return _bitset_mask_highest(bs);
+ return _bitset_mask_highest(bs);
}
/**
{
#define _SH (sizeof(bitset_unit_t) * 8 - 1)
- bitset_pos_t i;
+ bitset_pos_t i;
- for(i = 0; i < bs->units; ++i) {
- bitset_unit_t unit = bs->data[i];
- bitset_unit_t um1 = unit - 1;
+ for(i = 0; i < bs->units; ++i) {
+ bitset_unit_t unit = bs->data[i];
+ bitset_unit_t um1 = unit - 1;
- bs->data[i] = um1;
+ bs->data[i] = um1;
- if(((unit >> _SH) ^ (um1 >> _SH)) == 0)
- break;
- }
+ 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.
+ * The bitset is printed as a comma separated list of bits set.
* @param file The stream.
* @param bs The bitset.
*/
const char *prefix = "";
int i;
- putc('[', file);
+ putc('{', file);
for(i = bitset_next_set(bs, 0); i != -1; i = bitset_next_set(bs, i + 1)) {
fprintf(file, "%s%u", prefix, i);
prefix = ",";
}
- putc(']', file);
+ putc('}', file);
}
static INLINE void bitset_debug_fprint(FILE *file, const bitset_t *bs)