[ovs-dev] [PATCH] util: Rename popcount to count_1bits

YAMAMOTO Takashi yamamoto at valinux.co.jp
Tue Nov 19 08:13:00 UTC 2013


This avoids a conflict with NetBSD's strings.h/libc.
(http://netbsd.gw.com/cgi-bin/man-cgi?popcount++NetBSD-current)

The new name is suggested by Ben Pfaff.

Signed-off-by: YAMAMOTO Takashi <yamamoto at valinux.co.jp>
---
 lib/flow.c        |  5 +++--
 lib/util.c        | 16 ++++++++--------
 lib/util.h        |  2 +-
 tests/library.at  |  2 +-
 tests/test-util.c | 18 +++++++++---------
 5 files changed, 22 insertions(+), 21 deletions(-)

diff --git a/lib/flow.c b/lib/flow.c
index 63c6ef8..be1c309 100644
--- a/lib/flow.c
+++ b/lib/flow.c
@@ -1101,7 +1101,7 @@ flow_compose(struct ofpbuf *b, const struct flow *flow)
 static int
 miniflow_n_values(const struct miniflow *flow)
 {
-    return popcount(flow->map);
+    return count_1bits(flow->map);
 }
 
 static uint32_t *
@@ -1221,7 +1221,8 @@ miniflow_get__(const struct miniflow *flow, unsigned int u32_ofs)
         static const uint32_t zero = 0;
         return &zero;
     }
-    return flow->values + popcount(flow->map & ((UINT64_C(1) << u32_ofs) - 1));
+    return flow->values +
+           count_1bits(flow->map & ((UINT64_C(1) << u32_ofs) - 1));
 }
 
 /* Returns the uint32_t that would be at byte offset '4 * u32_ofs' if 'flow'
diff --git a/lib/util.c b/lib/util.c
index eb8beca..c2ffab2 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -918,7 +918,7 @@ raw_ctz(uint64_t n)
 
 /* Returns the number of 1-bits in 'x', between 0 and 32 inclusive. */
 static unsigned int
-popcount32(uint32_t x)
+count_1bits_32(uint32_t x)
 {
     /* In my testing, this implementation is over twice as fast as any other
      * portable implementation that I tried, including GCC 4.4
@@ -940,21 +940,21 @@ popcount32(uint32_t x)
 #define INIT32(X) INIT16(X), INIT16((X) + 16)
 #define INIT64(X) INIT32(X), INIT32((X) + 32)
 
-    static const uint8_t popcount8[256] = {
+    static const uint8_t count_1bits_8[256] = {
         INIT64(0), INIT64(64), INIT64(128), INIT64(192)
     };
 
-    return (popcount8[x & 0xff] +
-            popcount8[(x >> 8) & 0xff] +
-            popcount8[(x >> 16) & 0xff] +
-            popcount8[x >> 24]);
+    return (count_1bits_8[x & 0xff] +
+            count_1bits_8[(x >> 8) & 0xff] +
+            count_1bits_8[(x >> 16) & 0xff] +
+            count_1bits_8[x >> 24]);
 }
 
 /* Returns the number of 1-bits in 'x', between 0 and 64 inclusive. */
 unsigned int
-popcount(uint64_t x)
+count_1bits(uint64_t x)
 {
-    return popcount32(x) + popcount32(x >> 32);
+    return count_1bits_32(x) + count_1bits_32(x >> 32);
 }
 
 /* Returns true if the 'n' bytes starting at 'p' are zeros. */
diff --git a/lib/util.h b/lib/util.h
index 4e2b2a7..acb2d8f 100644
--- a/lib/util.h
+++ b/lib/util.h
@@ -287,7 +287,7 @@ void ignore(bool x OVS_UNUSED);
 
 int log_2_floor(uint32_t);
 int log_2_ceil(uint32_t);
-unsigned int popcount(uint64_t);
+unsigned int count_1bits(uint64_t);
 
 /* Returns the number of trailing 0-bits in 'n'.  Undefined if 'n' == 0. */
 #if __GNUC__ >= 4
diff --git a/tests/library.at b/tests/library.at
index 5cd6c4e..6e28573 100644
--- a/tests/library.at
+++ b/tests/library.at
@@ -114,7 +114,7 @@ m4_foreach(
   [[ctz],
    [round_up_pow2],
    [round_down_pow2],
-   [popcount],
+   [count_1bits],
    [log_2_floor],
    [bitwise_copy],
    [bitwise_zero],
diff --git a/tests/test-util.c b/tests/test-util.c
index e66987d..7183f46 100644
--- a/tests/test-util.c
+++ b/tests/test-util.c
@@ -188,17 +188,17 @@ shuffle(uint64_t *p, size_t n)
 }
 
 static void
-check_popcount(uint64_t x, int n)
+check_count_1bits(uint64_t x, int n)
 {
-    if (popcount(x) != n) {
-        fprintf(stderr, "popcount(%#"PRIx64") is %d but should be %d\n",
-                x, popcount(x), n);
+    if (count_1bits(x) != n) {
+        fprintf(stderr, "count_1bits(%#"PRIx64") is %d but should be %d\n",
+                x, count_1bits(x), n);
         abort();
     }
 }
 
 static void
-test_popcount(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+test_count_1bits(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 {
     uint64_t bits[64];
     int i;
@@ -207,7 +207,7 @@ test_popcount(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
         bits[i] = UINT64_C(1) << i;
     }
 
-    check_popcount(0, 0);
+    check_count_1bits(0, 0);
 
     for (i = 0; i < 1000; i++) {
         uint64_t x = 0;
@@ -216,14 +216,14 @@ test_popcount(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
         shuffle(bits, ARRAY_SIZE(bits));
         for (j = 0; j < 64; j++) {
             x |= bits[j];
-            check_popcount(x, j + 1);
+            check_count_1bits(x, j + 1);
         }
         assert(x == UINT64_MAX);
 
         shuffle(bits, ARRAY_SIZE(bits));
         for (j = 63; j >= 0; j--) {
             x &= ~bits[j];
-            check_popcount(x, j);
+            check_count_1bits(x, j);
         }
         assert(x == 0);
     }
@@ -966,7 +966,7 @@ static const struct command commands[] = {
     {"ctz", 0, 0, test_ctz},
     {"round_up_pow2", 0, 0, test_round_up_pow2},
     {"round_down_pow2", 0, 0, test_round_down_pow2},
-    {"popcount", 0, 0, test_popcount},
+    {"count_1bits", 0, 0, test_count_1bits},
     {"log_2_floor", 0, 0, test_log_2_floor},
     {"bitwise_copy", 0, 0, test_bitwise_copy},
     {"bitwise_zero", 0, 0, test_bitwise_zero},
-- 
1.8.3.1




More information about the dev mailing list