be_lower_for_target is now a simple function in the public API
[libfirm] / ir / adt / raw_bitset.h
index e38cc51..5de07f1 100644 (file)
@@ -45,7 +45,7 @@
 #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]
 
  */
 static inline unsigned *rbitset_malloc(unsigned size)
 {
-       unsigned  size_bytes = BITSET_SIZE_BYTES(size);
-       unsigned *res        = xmalloc(size_bytes);
-       memset(res, 0, size_bytes);
-
-       return res;
+       return XMALLOCNZ(unsigned, BITSET_SIZE_ELEMS(size));
 }
 
 /**
@@ -74,7 +70,7 @@ static inline unsigned *rbitset_malloc(unsigned size)
 #define rbitset_alloca(res, size) \
 do { \
        unsigned size_bytes = BITSET_SIZE_BYTES(size); \
-       res = alloca(size_bytes); \
+       res = (unsigned*)alloca(size_bytes); \
        memset(res, 0, size_bytes); \
 } while(0)
 
@@ -89,37 +85,9 @@ do { \
 static inline unsigned *rbitset_obstack_alloc(struct obstack *obst,
                                               unsigned size)
 {
-       unsigned  size_bytes = BITSET_SIZE_BYTES(size);
-       unsigned *res        = obstack_alloc(obst, size_bytes);
-       memset(res, 0, size_bytes);
-
-       return res;
-}
-
-/**
- * Allocate an empty raw bitset including the size on an obstack.
- * The size of this bitset can be accessed by bitset[-1].
- *
- * @param obst  the obstack where the bitset is allocated on
- * @param size  number of bits in the bitset
- *
- * @return the new bitset
- */
-static inline unsigned *rbitset_w_size_obstack_alloc(struct obstack *obst,
-                                                     unsigned size)
-{
-       unsigned  size_bytes = BITSET_SIZE_BYTES(size);
-       unsigned *res        = obstack_alloc(obst, size_bytes + sizeof(unsigned));
-       *res = size;
-       ++res;
-       memset(res, 0, size_bytes);
-
-       return res;
+       return OALLOCNZ(obst, unsigned, BITSET_SIZE_ELEMS(size));
 }
 
-/** Return the size of a bitset allocated with a *_w_size_* function */
-#define rbitset_size(set)      (set)[-1]
-
 /**
  * Duplicate a raw bitset on an obstack.
  *
@@ -133,7 +101,7 @@ static inline unsigned *rbitset_duplicate_obstack_alloc(struct obstack *obst,
        const unsigned *old_bitset, unsigned size)
 {
        unsigned  size_bytes = BITSET_SIZE_BYTES(size);
-       unsigned *res        = obstack_alloc(obst, size_bytes);
+       unsigned *res        = OALLOCN(obst, unsigned, BITSET_SIZE_ELEMS(size));
        memcpy(res, old_bitset, size_bytes);
 
        return res;
@@ -197,6 +165,9 @@ static inline void rbitset_set_all(unsigned *bitset, unsigned size)
        unsigned i;
        unsigned n = BITSET_SIZE_ELEMS(size);
 
+       if (n == 0)
+               return;
+
        for (i = 0; i < n-1; ++i) {
                bitset[i] = ~0u;
        }
@@ -235,7 +206,11 @@ static inline void rbitset_clear_all(unsigned *bitset, unsigned size)
 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;
        }
@@ -250,7 +225,7 @@ static inline void rbitset_flip_all(unsigned *bitset, unsigned size)
  */
 static inline bool rbitset_is_set(const unsigned *bitset, unsigned pos)
 {
-       return BITSET_ELEM(bitset, pos) & (1 << (pos % BITS_PER_ELEM));
+       return (BITSET_ELEM(bitset, pos) & (1 << (pos % BITS_PER_ELEM))) != 0;
 }
 
 /**