[ovs-dev] [PATCH v3 1/2] lib/util: Add clz() and clz64().

Jarno Rajahalme jrajahalme at nicira.com
Thu Nov 21 22:25:30 UTC 2013


Count leading zeroes using builtins if available.

Signed-off-by: Jarno Rajahalme <jrajahalme at nicira.com>
---
 lib/util.c        |   25 +++++++++++++++++++++++++
 lib/util.h        |   26 ++++++++++++++++++++++++++
 tests/library.at  |    1 +
 tests/test-util.c |   53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 105 insertions(+)

diff --git a/lib/util.c b/lib/util.c
index c2ffab2..118ffb1 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -892,6 +892,7 @@ log_2_ceil(uint32_t n)
 #if __GNUC__ >= 4
 /* Defined inline in util.h. */
 #else
+/* Returns the number of trailing 0-bits in 'n'.  Undefined if 'n' == 0. */
 int
 raw_ctz(uint64_t n)
 {
@@ -914,6 +915,30 @@ raw_ctz(uint64_t n)
 
     return count;
 }
+
+/* Returns the number of leading 0-bits in 'n'.  Undefined if 'n' == 0. */
+int
+raw_clz(uint64_t n)
+{
+    uint64_t k;
+    int count = 63;
+
+#define CLZ_STEP(X)                             \
+    k = n >> (X);                               \
+    if (k) {                                    \
+        count -= X;                             \
+        n = k;                                  \
+    }
+    CLZ_STEP(32);
+    CLZ_STEP(16);
+    CLZ_STEP(8);
+    CLZ_STEP(4);
+    CLZ_STEP(2);
+    CLZ_STEP(1);
+#undef CLZ_STEP
+
+    return count;
+}
 #endif
 
 /* Returns the number of 1-bits in 'x', between 0 and 32 inclusive. */
diff --git a/lib/util.h b/lib/util.h
index acb2d8f..1ee532a 100644
--- a/lib/util.h
+++ b/lib/util.h
@@ -301,9 +301,21 @@ raw_ctz(uint64_t n)
             ? __builtin_ctz(n)
             : __builtin_ctzll(n));
 }
+
+static inline int
+raw_clz(uint64_t n)
+{
+    /* With GCC 4.7 on 32-bit x86, if a 32-bit integer is passed as 'n', using
+     * a plain __builtin_ctzll() here always generates an out-of-line function
+     * call.  The test below helps it to emit a single 'bsf' instruction. */
+    return (__builtin_constant_p(n <= UINT32_MAX) && n <= UINT32_MAX
+            ? __builtin_clz(n) + 32
+            : __builtin_clzll(n));
+}
 #else
 /* Defined in util.c. */
 int raw_ctz(uint64_t n);
+int raw_clz(uint64_t n);
 #endif
 
 /* Returns the number of trailing 0-bits in 'n', or 32 if 'n' is 0. */
@@ -320,6 +332,20 @@ ctz64(uint64_t n)
     return n ? raw_ctz(n) : 64;
 }
 
+/* Returns the number of leading 0-bits in 'n', or 32 if 'n' is 0. */
+static inline int
+clz(uint32_t n)
+{
+    return n ? raw_clz(n) - 32 : 32;
+}
+
+/* Returns the number of leading 0-bits in 'n', or 64 if 'n' is 0. */
+static inline int
+clz64(uint64_t n)
+{
+    return n ? raw_clz(n) : 64;
+}
+
 /* Returns the rightmost 1-bit in 'x' (e.g. 01011000 => 00001000), or 0 if 'x'
  * is 0. */
 static inline uintmax_t
diff --git a/tests/library.at b/tests/library.at
index 6e28573..57cdd6c 100644
--- a/tests/library.at
+++ b/tests/library.at
@@ -112,6 +112,7 @@ AT_CLEANUP
 m4_foreach(
   [testname],
   [[ctz],
+   [clz],
    [round_up_pow2],
    [round_down_pow2],
    [count_1bits],
diff --git a/tests/test-util.c b/tests/test-util.c
index 7183f46..3caf712 100644
--- a/tests/test-util.c
+++ b/tests/test-util.c
@@ -113,6 +113,58 @@ test_ctz(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
     check_ctz64(0, 64);
 }
 
+static void
+check_clz(uint32_t x, int n)
+{
+    if (clz(x) != n) {
+        fprintf(stderr, "clz(%"PRIu32") is %d but should be %d\n",
+                x, clz(x), n);
+        abort();
+    }
+}
+
+static void
+check_clz64(uint64_t x, int n)
+{
+    if (clz64(x) != n) {
+        fprintf(stderr, "clz64(%"PRIu64") is %d but should be %d\n",
+                x, clz64(x), n);
+        abort();
+    }
+}
+
+static void
+test_clz(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+{
+    int n;
+
+    for (n = 0; n < 32; n++) {
+        /* Check minimum x such that f(x) == n. */
+        check_clz((1u << 31) >> n, n);
+
+        /* Check maximum x such that f(x) == n. */
+        check_clz(UINT32_MAX >> n, n);
+
+        /* Check a random value in the middle. */
+        check_clz((random_uint32() | 1u << 31) >> n, n);
+    }
+
+    for (n = 0; n < 64; n++) {
+        /* Check minimum x such that f(x) == n. */
+        check_clz64((UINT64_C(1) << 63) >> n, n);
+
+        /* Check maximum x such that f(x) == n. */
+        check_clz64(UINT64_MAX >> n, n);
+
+        /* Check a random value in the middle. */
+        check_clz64((random_uint64() | UINT64_C(1) << 63) >> n, n);
+    }
+
+    /* Check clz(0). */
+    check_clz(0, 32);
+    check_clz64(0, 64);
+}
+
 /* Returns a random number in the range 'min'...'max' inclusive. */
 static uint32_t
 random_in_range(uint32_t min, uint32_t max)
@@ -964,6 +1016,7 @@ test_ovs_scan(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 
 static const struct command commands[] = {
     {"ctz", 0, 0, test_ctz},
+    {"clz", 0, 0, test_clz},
     {"round_up_pow2", 0, 0, test_round_up_pow2},
     {"round_down_pow2", 0, 0, test_round_down_pow2},
     {"count_1bits", 0, 0, test_count_1bits},
-- 
1.7.10.4




More information about the dev mailing list