]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: standard: add a simple popcount function
authorWilly Tarreau <w@1wt.eu>
Mon, 19 Nov 2012 11:11:07 +0000 (12:11 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 19 Nov 2012 11:12:24 +0000 (12:12 +0100)
This function returns the number of ones in a word.

include/common/standard.h

index 4813065742de3ba66ccd7cf3e85dcdfca94f4d89..221d8b880d20e1175f7462ebf8c1abd1ab9c4a02 100644 (file)
@@ -519,6 +519,17 @@ static inline unsigned int div64_32(unsigned long long o1, unsigned int o2)
        return result;
 }
 
+/* Simple popcount implementation. It returns the number of ones in a word */
+static inline unsigned int popcount(unsigned int a)
+{
+       unsigned int cnt;
+       for (cnt = 0; a; a >>= 1) {
+               if (a & 1)
+                       cnt++;
+       }
+       return cnt;
+}
+
 /* copies at most <n> characters from <src> and always terminates with '\0' */
 char *my_strndup(const char *src, int n);