1 #include <ccan/bitops/bitops.c>
2 #include <ccan/tap/tap.h>
4 /* Get naive versions */
5 #ifndef BITOPS_NEED_FFS
6 #define BITOPS_NEED_FFS
9 #ifndef BITOPS_NEED_CLZ
10 #define BITOPS_NEED_CLZ
13 #ifndef BITOPS_NEED_CTZ
14 #define BITOPS_NEED_CTZ
17 #ifndef BITOPS_NEED_WEIGHT
18 #define BITOPS_NEED_WEIGHT
21 int naive_bitops_ffs32(uint32_t u);
22 int naive_bitops_ffs64(uint64_t u);
23 int naive_bitops_clz32(uint32_t u);
24 int naive_bitops_clz64(uint64_t u);
25 int naive_bitops_ctz32(uint32_t u);
26 int naive_bitops_ctz64(uint64_t u);
27 int naive_bitops_weight32(uint32_t u);
28 int naive_bitops_weight64(uint64_t u);
30 #define bitops_ffs32 naive_bitops_ffs32
31 #define bitops_ffs64 naive_bitops_ffs64
32 #define bitops_clz32 naive_bitops_clz32
33 #define bitops_clz64 naive_bitops_clz64
34 #define bitops_ctz32 naive_bitops_ctz32
35 #define bitops_ctz64 naive_bitops_ctz64
36 #define bitops_weight32 naive_bitops_weight32
37 #define bitops_weight64 naive_bitops_weight64
38 #include <ccan/bitops/bitops.c>
40 static void test_against_naive32(uint32_t v)
42 ok1(bitops_ffs32(v) == naive_bitops_ffs32(v));
43 ok1(bitops_clz32(v) == naive_bitops_clz32(v));
44 ok1(bitops_ctz32(v) == naive_bitops_ctz32(v));
45 ok1(bitops_weight32(v) == naive_bitops_weight32(v));
48 static void test_against_naive64(uint64_t v)
50 ok1(bitops_ffs64(v) == naive_bitops_ffs64(v));
51 ok1(bitops_clz64(v) == naive_bitops_clz64(v));
52 ok1(bitops_ctz64(v) == naive_bitops_ctz64(v));
53 ok1(bitops_weight64(v) == naive_bitops_weight64(v));
61 /* This is how many tests you plan to run */
62 plan_tests(32 * 32 * 8 + (64 * 64) * 8 + 4 + 4);
64 /* Various comparisons with any one or two bits set */
65 for (i = 0; i < 32; i++) {
66 for (j = 0; j < 32; j++) {
67 v = ((uint64_t)1 << i) | ((uint64_t)1 << j);
68 test_against_naive32(v);
69 test_against_naive32(~v);
73 for (i = 0; i < 64; i++) {
74 for (j = 0; j < 64; j++) {
75 v = ((uint64_t)1 << i) | ((uint64_t)1 << j);
76 test_against_naive64(v);
77 test_against_naive64(~v);
81 test_against_naive64(0xFFFFFFFFFFFFFFFFULL);
82 ok1(bitops_ffs32(0) == naive_bitops_ffs32(0));
83 ok1(bitops_ffs64(0) == naive_bitops_ffs64(0));
84 ok1(bitops_weight32(0) == naive_bitops_weight32(0));
85 ok1(bitops_weight64(0) == naive_bitops_weight64(0));
87 /* This exits depending on whether all tests passed */