From 37994f034c0c3153fd652f67a55c7ee68a665890 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Mon, 19 Nov 2012 12:11:07 +0100 Subject: [PATCH] MINOR: standard: add a simple popcount function This function returns the number of ones in a word. --- include/common/standard.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/include/common/standard.h b/include/common/standard.h index 481306574..221d8b880 100644 --- a/include/common/standard.h +++ b/include/common/standard.h @@ -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 characters from and always terminates with '\0' */ char *my_strndup(const char *src, int n);