From: kim Date: Thu, 8 Sep 2011 19:34:28 +0000 (+0000) Subject: Adds utils.h X-Git-Url: http://git.nguyen.vg/gitweb/?p=SXSI%2FXMLTree.git;a=commitdiff_plain;h=249d0e6a99770acbd28d27d13c8a116788143850 Adds utils.h git-svn-id: svn+ssh://idea.nguyen.vg/svn/sxsi/trunk/XMLTree@1103 3cdefd35-fc62-479d-8e8d-bae585ffb9ca --- diff --git a/utils.h b/utils.h new file mode 100644 index 0000000..13a942c --- /dev/null +++ b/utils.h @@ -0,0 +1,39 @@ +#ifndef UTILS_H_ +#define UTILS_H_ + +#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 + +#endif