+static void
+test_ctz(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_ctz(1 << n, n);
+
+ /* Check maximum x such that f(x) == n. */
+ check_ctz(UINT32_MAX << n, n);
+
+ /* Check a random value in the middle. */
+ check_ctz((random_uint32() | 1) << n, n);
+ }
+
+ /* Check ctz(0). */
+ check_ctz(0, 32);
+}
+
+/* Returns a random number in the range 'min'...'max' inclusive. */
+static uint32_t
+random_in_range(uint32_t min, uint32_t max)
+{
+ return min == max ? min : min + random_range(max - min + 1);
+}
+
+static void
+check_rup2(uint32_t x, int n)
+{
+ uint32_t rup2 = ROUND_UP_POW2(x);
+ if (rup2 != n) {
+ fprintf(stderr, "ROUND_UP_POW2(%#"PRIx32") is %#"PRIx32" "
+ "but should be %#"PRIx32"\n", x, rup2, n);
+ abort();
+ }
+}
+
+static void
+test_round_up_pow2(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+{
+ int n;
+
+ for (n = 0; n < 32; n++) {
+ /* Min, max value for which ROUND_UP_POW2 should yield (1 << n). */
+ uint32_t min = ((1u << n) >> 1) + 1;
+ uint32_t max = 1u << n;
+
+ check_rup2(min, 1u << n);
+ check_rup2(max, 1u << n);
+ check_rup2(random_in_range(min, max), 1u << n);
+ }
+ check_rup2(0, 0);
+}
+
+static void
+check_rdp2(uint32_t x, int n)
+{
+ uint32_t rdp2 = ROUND_DOWN_POW2(x);
+ if (rdp2 != n) {
+ fprintf(stderr, "ROUND_DOWN_POW2(%#"PRIx32") is %#"PRIx32" "
+ "but should be %#"PRIx32"\n", x, rdp2, n);
+ abort();
+ }
+}
+
+static void
+test_round_down_pow2(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+{
+ int n;
+
+ for (n = 0; n < 32; n++) {
+ /* Min, max value for which ROUND_DOWN_POW2 should yield (1 << n). */
+ uint32_t min = 1u << n;
+ uint32_t max = ((1u << n) << 1) - 1;
+
+ check_rdp2(min, 1u << n);
+ check_rdp2(max, 1u << n);
+ check_rdp2(random_in_range(min, max), 1u << n);
+ }
+ check_rdp2(0, 0);
+}
+
+static void
+shuffle(unsigned int *p, size_t n)
+{
+ for (; n > 1; n--, p++) {
+ unsigned int *q = &p[random_range(n)];
+ unsigned int tmp = *p;
+ *p = *q;
+ *q = tmp;
+ }
+}
+
+static void
+check_popcount(uint32_t x, int n)
+{
+ if (popcount(x) != n) {
+ fprintf(stderr, "popcount(%#"PRIx32") is %d but should be %d\n",
+ x, popcount(x), n);
+ abort();
+ }
+}
+
+static void
+test_popcount(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+{
+ unsigned int bits[32];
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(bits); i++) {
+ bits[i] = 1u << i;
+ }
+
+ check_popcount(0, 0);
+
+ for (i = 0; i < 1000; i++) {
+ uint32_t x = 0;
+ int j;
+
+ shuffle(bits, ARRAY_SIZE(bits));
+ for (j = 0; j < 32; j++) {
+ x |= bits[j];
+ check_popcount(x, j + 1);
+ }
+ assert(x == UINT32_MAX);
+
+ shuffle(bits, ARRAY_SIZE(bits));
+ for (j = 31; j >= 0; j--) {
+ x &= ~bits[j];
+ check_popcount(x, j);
+ }
+ assert(x == 0);
+ }
+}
+