Big renaming. Uses the bp namespace everywhere
[SXSI/libbp.git] / bp-utils.h
diff --git a/bp-utils.h b/bp-utils.h
new file mode 100644 (file)
index 0000000..b548688
--- /dev/null
@@ -0,0 +1,90 @@
+#ifndef BP_UTILS_H_
+#define BP_UTILS_H_
+
+#ifdef __cplusplus
+
+extern "C" {
+#endif
+
+#define logD 5
+#define D (1<<logD)
+
+#define logR 16
+#define R1 (1<<logR)
+#define logRR 10
+//#define logRR 8
+#define RR (1<<logRR)
+#define logRRR 7
+#define RRR (1<<logRRR)
+
+
+
+
+
+#include <stdlib.h>
+
+
+
+#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