Big renaming. Uses the bp namespace everywhere
[SXSI/libbp.git] / bp-utils.h
1 #ifndef BP_UTILS_H_
2 #define BP_UTILS_H_
3
4 #ifdef __cplusplus
5
6 extern "C" {
7 #endif
8
9 #define logD 5
10 #define D (1<<logD)
11
12 #define logR 16
13 #define R1 (1<<logR)
14 #define logRR 10
15 //#define logRR 8
16 #define RR (1<<logRR)
17 #define logRRR 7
18 #define RRR (1<<logRRR)
19
20
21
22
23
24 #include <stdlib.h>
25
26
27
28 #ifdef HAS_NATIVE_POPCOUNT
29 static inline unsigned int popcount(unsigned int n){
30   asm ("popcnt %1, %0" : "=r" (n) : "0" (n));
31   return n;
32 }
33
34 static inline unsigned int popcount8(unsigned int n) {
35   return popcount(n & 0xff);
36 }
37
38 #else
39
40 static unsigned int popcount8(unsigned int x)
41 {
42   unsigned int r;
43   r = x;
44   r = ((r & 0xaa)>>1) + (r & 0x55);
45   r = ((r & 0xcc)>>2) + (r & 0x33);
46   r = ((r>>4) + r) & 0x0f;
47   return r;
48 }
49
50 static inline unsigned int popcount(unsigned int x)
51 {
52   unsigned int m1 = 0x55555555;
53   unsigned int m2 = 0xc30c30c3;
54   x -= (x >> 1) & m1;
55   x = (x & m2) + ((x >> 2) & m2) + ((x >> 4) & m2);
56   x += x >> 6;
57   return  (x + (x >> 12) + (x >> 24)) & 0x3f;
58 }
59
60 #endif
61
62
63 void * bp_malloc(size_t);
64
65 #define bp_xmalloc(p, n) p = (__typeof__(p)) bp_malloc((n)*sizeof(*p))
66
67
68
69 void bp_free(void *);
70
71 size_t bp_alloc_stats(void);
72
73 void bp_reset_alloc_states(void);
74
75 int bp_setbit(unsigned int *B, int i,int x);
76
77 int bp_setbits(unsigned int *B, int i, int d, int x);
78
79 static inline int bp_getbit(unsigned int *B, int i)
80 {
81   int j = i >> logD;
82   int l = i & (D-1);
83   return (B[j] >> (D-1-l)) & 1;
84 }
85
86 #ifdef __cplusplus
87  }
88 #endif
89
90 #endif