aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--include/grpc/support/useful.h13
-rw-r--r--test/core/support/useful_test.c6
2 files changed, 9 insertions, 10 deletions
diff --git a/include/grpc/support/useful.h b/include/grpc/support/useful.h
index 38072dfc1e..5b70447123 100644
--- a/include/grpc/support/useful.h
+++ b/include/grpc/support/useful.h
@@ -34,8 +34,6 @@
#ifndef GRPC_SUPPORT_USEFUL_H
#define GRPC_SUPPORT_USEFUL_H
-#include <grpc/support/port_platform.h>
-
/* useful macros that don't belong anywhere else */
#define GPR_MIN(a, b) ((a) < (b) ? (a) : (b))
@@ -63,11 +61,12 @@
/** Get the \a n-th bit of \a i */
#define GPR_BITGET(i, n) (((i) & (1u << n)) != 0)
+#define HEXDIGIT_BITCOUNT_(x) \
+ ((x) - (((x) >> 1) & 0x77777777) - (((x) >> 2) & 0x33333333) - \
+ (((x) >> 3) & 0x11111111))
+
/** Returns number of bits set in bitset \a i */
-static int gpr_bitcount(gpr_uint32 i) {
- i = i - ((i >> 1) & 0x55555555);
- i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
- return (((i + (i >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
-}
+#define GPR_BITCOUNT(x) \
+ (((HEXDIGIT_BITCOUNT_(x) + (HEXDIGIT_BITCOUNT_(x) >> 4)) & 0x0F0F0F0F) % 255)
#endif /* GRPC_SUPPORT_USEFUL_H */
diff --git a/test/core/support/useful_test.c b/test/core/support/useful_test.c
index 7129dce632..34f4ce6870 100644
--- a/test/core/support/useful_test.c
+++ b/test/core/support/useful_test.c
@@ -57,12 +57,12 @@ int main(int argc, char **argv) {
GPR_ASSERT(GPR_ARRAY_SIZE(five) == 5);
GPR_ASSERT(GPR_BITSET(&bitset, 3) == 8);
- GPR_ASSERT(gpr_bitcount(bitset) == 1);
+ GPR_ASSERT(GPR_BITCOUNT(bitset) == 1);
GPR_ASSERT(GPR_BITGET(bitset, 3) == 1);
GPR_ASSERT(GPR_BITSET(&bitset, 1) == 10);
- GPR_ASSERT(gpr_bitcount(bitset) == 2);
+ GPR_ASSERT(GPR_BITCOUNT(bitset) == 2);
GPR_ASSERT(GPR_BITCLEAR(&bitset, 3) == 2);
- GPR_ASSERT(gpr_bitcount(bitset) == 1);
+ GPR_ASSERT(GPR_BITCOUNT(bitset) == 1);
GPR_ASSERT(GPR_BITGET(bitset, 3) == 0);
return 0;