Adds utils.h
authorkim <kim@3cdefd35-fc62-479d-8e8d-bae585ffb9ca>
Thu, 8 Sep 2011 19:34:28 +0000 (19:34 +0000)
committerkim <kim@3cdefd35-fc62-479d-8e8d-bae585ffb9ca>
Thu, 8 Sep 2011 19:34:28 +0000 (19:34 +0000)
git-svn-id: svn+ssh://idea.nguyen.vg/svn/sxsi/trunk/XMLTree@1103 3cdefd35-fc62-479d-8e8d-bae585ffb9ca

utils.h [new file with mode: 0644]

diff --git a/utils.h b/utils.h
new file mode 100644 (file)
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