Add table version of popcount and popcount8
[SXSI/libbp.git] / bp-utils.h
index b548688..6ead4fa 100644 (file)
@@ -36,7 +36,25 @@ static inline unsigned int popcount8(unsigned int n) {
 }
 
 #else
+#ifdef HAS_POPCOUNT_TABLE
+
+extern unsigned char popCount[256];
 
+static unsigned int popcount8(unsigned int x)
+{
+  return (unsigned int) popCount[x & 0xff];
+}
+
+static unsigned int popcount(unsigned int x)
+{
+  return popcount8(x) +
+    popcount8((x >> 8)) +
+    popcount8((x >> 16)) +
+    popcount8((x >> 24));
+}
+
+
+#else
 static unsigned int popcount8(unsigned int x)
 {
   unsigned int r;
@@ -58,6 +76,7 @@ static inline unsigned int popcount(unsigned int x)
 }
 
 #endif
+#endif
 
 
 void * bp_malloc(size_t);