X-Git-Url: http://git.nguyen.vg/gitweb/?p=SXSI%2Flibbp.git;a=blobdiff_plain;f=bp-utils.h;fp=bp-utils.h;h=b54868876a27dd14637a586b1ea2e7dcd5fff7e2;hp=0000000000000000000000000000000000000000;hb=45ff7a2260f890f6ef6a7b56f654ffa1a057a7e7;hpb=b3b328570c63aea4d9d15854eec9f7434cc9fe0d diff --git a/bp-utils.h b/bp-utils.h new file mode 100644 index 0000000..b548688 --- /dev/null +++ b/bp-utils.h @@ -0,0 +1,90 @@ +#ifndef BP_UTILS_H_ +#define BP_UTILS_H_ + +#ifdef __cplusplus + +extern "C" { +#endif + +#define logD 5 +#define D (1< + + + +#ifdef HAS_NATIVE_POPCOUNT +static inline unsigned int popcount(unsigned int n){ + asm ("popcnt %1, %0" : "=r" (n) : "0" (n)); + return n; +} + +static inline unsigned int popcount8(unsigned int n) { + return popcount(n & 0xff); +} + +#else + +static unsigned int popcount8(unsigned int x) +{ + unsigned int r; + r = x; + r = ((r & 0xaa)>>1) + (r & 0x55); + r = ((r & 0xcc)>>2) + (r & 0x33); + r = ((r>>4) + r) & 0x0f; + return r; +} + +static inline unsigned int popcount(unsigned int x) +{ + unsigned int m1 = 0x55555555; + unsigned int m2 = 0xc30c30c3; + x -= (x >> 1) & m1; + x = (x & m2) + ((x >> 2) & m2) + ((x >> 4) & m2); + x += x >> 6; + return (x + (x >> 12) + (x >> 24)) & 0x3f; +} + +#endif + + +void * bp_malloc(size_t); + +#define bp_xmalloc(p, n) p = (__typeof__(p)) bp_malloc((n)*sizeof(*p)) + + + +void bp_free(void *); + +size_t bp_alloc_stats(void); + +void bp_reset_alloc_states(void); + +int bp_setbit(unsigned int *B, int i,int x); + +int bp_setbits(unsigned int *B, int i, int d, int x); + +static inline int bp_getbit(unsigned int *B, int i) +{ + int j = i >> logD; + int l = i & (D-1); + return (B[j] >> (D-1-l)) & 1; +} + +#ifdef __cplusplus + } +#endif + +#endif