1 /* CC0 license (public domain) - see LICENSE file for details */
2 #include <ccan/bitops/bitops.h>
5 /* We do naive replacement versions: good for testing, and really your
6 * compiler should do better. */
8 int bitops_ffs32(uint32_t u)
11 for (i = 0; i < 32; i++)
12 if (u & ((uint32_t)1 << i))
17 int bitops_ffs64(uint64_t u)
20 for (i = 0; i < 64; i++)
21 if (u & ((uint64_t)1 << i))
27 #ifdef BITOPS_NEED_CLZ
28 int bitops_clz32(uint32_t u)
31 for (i = 0; i < 32; i++)
32 if (u & ((uint32_t)1 << (31 - i)))
37 int bitops_clz64(uint64_t u)
40 for (i = 0; i < 64; i++)
41 if (u & ((uint64_t)1 << (63 - i)))
47 #ifdef BITOPS_NEED_CTZ
48 int bitops_ctz32(uint32_t u)
50 BITOPS_ASSERT_NONZERO(u);
51 return bitops_ffs32(u) - 1;
54 int bitops_ctz64(uint64_t u)
56 BITOPS_ASSERT_NONZERO(u);
57 return bitops_ffs64(u) - 1;
61 #ifdef BITOPS_NEED_WEIGHT
62 int bitops_weight32(uint32_t u)
65 for (i = 0; i < 32; i++)
66 if (u & ((uint32_t)1 << i))
71 int bitops_weight64(uint64_t u)
74 for (i = 0; i < 64; i++)
75 if (u & ((uint64_t)1 << i))