X-Git-Url: http://git.nguyen.vg/gitweb/?p=SXSI%2Flibbp.git;a=blobdiff_plain;f=utils.h;fp=utils.h;h=0d5eed8d51511425abc394b38e9b303781d6376f;hp=0000000000000000000000000000000000000000;hb=b94f8d72735df125b191bf5a49cba0c037278787;hpb=657b1bc3dc283b45b9cceab5f1825a06496146cd diff --git a/utils.h b/utils.h new file mode 100644 index 0000000..0d5eed8 --- /dev/null +++ b/utils.h @@ -0,0 +1,49 @@ +#ifndef UTILS_H_ +#define UTILS_H_ + +#ifdef __cplusplus +extern "C" { +#endif + + +#ifdef HAS_NATIVE_POPCOUNT +static inline unsigned int popcount(pb n){ + asm ("popcnt %1, %0" : "=r" (n) : "0" (n)); + return n; +} + +static inline unsigned int popcount8(pb n) { + return popcount(n & 0xff); +} + +#else + +static unsigned int popcount8(pb x) +{ + dword 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(pb x) +{ + uint m1 = 0x55555555; + uint 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 + +#ifdef __cplusplus + } +#endif + + +#endif