--- /dev/null
+#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