lib/util: Add clz32() and clz64().
[sliver-openvswitch.git] / tests / test-util.c
index 8dbc98b..9152562 100644 (file)
@@ -113,6 +113,58 @@ test_ctz(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
     check_ctz64(0, 64);
 }
 
+static void
+check_clz32(uint32_t x, int n)
+{
+    if (clz32(x) != n) {
+        fprintf(stderr, "clz32(%"PRIu32") is %d but should be %d\n",
+                x, clz32(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_clz32((1u << 31) >> n, n);
+
+        /* Check maximum x such that f(x) == n. */
+        check_clz32(UINT32_MAX >> n, n);
+
+        /* Check a random value in the middle. */
+        check_clz32((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_clz32(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)
 \f
 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},