Tweaks to bitset.h, established a C test framework.

This commit is contained in:
Matthew Bloch
2012-05-20 14:38:46 +01:00
parent 8a38cf48eb
commit c94b6f365c
4 changed files with 162 additions and 5 deletions

View File

@@ -3,6 +3,9 @@
#include <inttypes.h>
#include <string.h>
#include <pthread.h>
#include "util.h"
static inline char char_with_bit_set(int num) { return 1<<(num%8); }
@@ -20,9 +23,11 @@ static inline int bit_has_value(char* b, int idx, int value) {
}
static inline void bit_set(char* b, int idx) {
b[idx/8] |= char_with_bit_set(idx);
//__sync_fetch_and_or(b+(idx/8), char_with_bit_set(idx));
}
static inline void bit_clear(char* b, int idx) {
b[idx/8] &= ~char_with_bit_set(idx);
//__sync_fetch_and_nand(b+(idx/8), char_with_bit_set(idx));
}
static inline void bit_set_range(char* b, int from, int len) {
for (; from%8 != 0 && len > 0; len--)
@@ -67,5 +72,74 @@ static inline int bit_run_count(char* b, int from, int len) {
return count;
}
struct bitset_mapping {
uint64_t size;
int resolution;
char bits[];
};
static inline struct bitset_mapping* bitset_alloc(
uint64_t size,
int resolution
)
{
struct bitset_mapping *bitset = xmalloc(
sizeof(struct bitset_mapping)+
(size+resolution-1)/resolution
);
bitset->size = size;
bitset->resolution = resolution;
return bitset;
}
#define INT_FIRST_AND_LAST \
int first = from/set->resolution, \
last = (from+len-1)/set->resolution, \
bitlen = last-first+1
static inline void bitset_set_range(
struct bitset_mapping* set,
uint64_t from,
uint64_t len)
{
INT_FIRST_AND_LAST;
bit_set_range(set->bits, first, bitlen);
}
static inline void bitset_clear_range(
struct bitset_mapping* set,
uint64_t from,
uint64_t len)
{
INT_FIRST_AND_LAST;
bit_clear_range(set->bits, first, bitlen);
}
static inline int bitset_run_count(
struct bitset_mapping* set,
uint64_t from,
uint64_t len)
{
INT_FIRST_AND_LAST;
return bit_run_count(set->bits, first, bitlen) * set->resolution;
}
static inline int bitset_is_clear_at(
struct bitset_mapping* set,
uint64_t at
)
{
return bit_is_clear(set->bits, at/set->resolution);
}
static inline int bitset_is_set_at(
struct bitset_mapping* set,
uint64_t at
)
{
return bit_is_set(set->bits, at/set->resolution);
}
#endif