Initial import of libbp
[SXSI/libbp.git] / utils.h
1 #ifndef UTILS_H_
2 #define UTILS_H_
3
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7
8
9 #ifdef HAS_NATIVE_POPCOUNT
10 static inline unsigned int popcount(pb n){
11   asm ("popcnt %1, %0" : "=r" (n) : "0" (n));
12   return n;
13 }
14
15 static inline unsigned int popcount8(pb n) {
16   return popcount(n & 0xff);
17 }
18
19 #else
20
21 static unsigned int popcount8(pb x)
22 {
23   dword r;
24   r = x;
25   r = ((r & 0xaa)>>1) + (r & 0x55);
26   r = ((r & 0xcc)>>2) + (r & 0x33);
27   r = ((r>>4) + r) & 0x0f;
28   return r;
29 }
30
31 static inline unsigned int
32 popcount(pb x)
33 {
34   uint m1 = 0x55555555;
35   uint m2 = 0xc30c30c3;
36   x -= (x >> 1) & m1;
37   x = (x & m2) + ((x >> 2) & m2) + ((x >> 4) & m2);
38   x += x >> 6;
39   return  (x + (x >> 12) + (x >> 24)) & 0x3f;
40 }
41
42 #endif
43
44 #ifdef __cplusplus
45  }
46 #endif
47
48
49 #endif