Silence a printf warning for %lu on 32bits archs.
[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 #include <stdlib.h>
22
23 #ifdef __GNUC__
24 #define UNUSED __attribute__((unused))
25 #else
26 #define UNUSED
27 #endif
28
29 #ifdef HAS_NATIVE_POPCOUNT
30 static inline UNUSED unsigned int popcount(unsigned int n){
31   return __builtin_popcount(n);
32 }
33
34 static inline UNUSED unsigned int popcount8(unsigned int n) {
35   return popcount(n & 0xff);
36 }
37
38 #else
39 #ifdef HAS_POPCOUNT_TABLE
40
41 extern unsigned char popCount[256];
42
43 static UNUSED unsigned int popcount8(unsigned int x)
44 {
45   return (unsigned int) popCount[x & 0xff];
46 }
47
48 static UNUSED unsigned int popcount(unsigned int x)
49 {
50   return popcount8(x) +
51     popcount8((x >> 8)) +
52     popcount8((x >> 16)) +
53     popcount8((x >> 24));
54 }
55
56
57 #else
58 static UNUSED unsigned int popcount8(unsigned int x)
59 {
60   unsigned int r;
61   r = x;
62   r = ((r & 0xaa)>>1) + (r & 0x55);
63   r = ((r & 0xcc)>>2) + (r & 0x33);
64   r = ((r>>4) + r) & 0x0f;
65   return r;
66 }
67
68 static inline UNUSED unsigned int popcount(unsigned int x)
69 {
70   unsigned int m1 = 0x55555555;
71   unsigned int m2 = 0xc30c30c3;
72   x -= (x >> 1) & m1;
73   x = (x & m2) + ((x >> 2) & m2) + ((x >> 4) & m2);
74   x += x >> 6;
75   return  (x + (x >> 12) + (x >> 24)) & 0x3f;
76 }
77
78 #endif
79 #endif
80
81
82 void * bp_malloc(size_t);
83
84 #define bp_xmalloc(p, n) p = (__typeof__(p)) bp_malloc((n)*sizeof(*p))
85
86
87
88 void bp_free(void *);
89
90 size_t bp_get_alloc_stats(void);
91
92 void bp_reset_alloc_states(void);
93
94 int bp_setbit(unsigned int *B, int i,int x);
95
96 int bp_setbits(unsigned int *B, int i, int d, int x);
97
98 static inline int bp_getbit(unsigned int *B, int i)
99 {
100   int j = i >> logD;
101   int l = i & (D-1);
102   return (B[j] >> (D-1-l)) & 1;
103 }
104
105 #ifdef __cplusplus
106  }
107 #endif
108
109 #endif