| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #ifndef UTIL_BIT_H | ||
| 2 | #define UTIL_BIT_H | ||
| 3 | |||
| 4 | #include <stddef.h> | ||
| 5 | #include <stdint.h> | ||
| 6 | #include "debug.h" | ||
| 7 | #include "macros.h" | ||
| 8 | |||
| 9 | /* | ||
| 10 | * The default C compiler on FreeBSD 14.1 (Clang 18.1.5) accepts the | ||
| 11 | * -std=gnu23 command-line option and then defines __STDC_VERSION__ as | ||
| 12 | * 202311L, while not actually providing a C23 conforming libc (the | ||
| 13 | * <stdbit.h> header is missing). This guard condition originally used | ||
| 14 | * `||` instead of `&&`, but it was changed so as to work around this | ||
| 15 | * disregard for standards. Fortunately this doesn't really have any | ||
| 16 | * downsides for other platforms, since __has_include() is also part | ||
| 17 | * of C23. | ||
| 18 | */ | ||
| 19 | #if __STDC_VERSION__ >= 202311L && HAS_INCLUDE(<stdbit.h>) | ||
| 20 | #include <stdbit.h> | ||
| 21 | #define USE_STDBIT(fn, arg) return fn(arg) | ||
| 22 | #else | ||
| 23 | #define USE_STDBIT(fn, arg) | ||
| 24 | #endif | ||
| 25 | |||
| 26 | #define U64(x) (UINT64_C(x)) | ||
| 27 | #define U32(x) (UINT32_C(x)) | ||
| 28 | |||
| 29 | #ifdef HAS_BUILTIN_TYPES_COMPATIBLE_P | ||
| 30 | #define HAS_COMPATIBLE_BUILTIN(arg, type, fn) \ | ||
| 31 | (GNUC_AT_LEAST(3, 4) || HAS_BUILTIN(__builtin_ ## fn)) \ | ||
| 32 | && __builtin_types_compatible_p(__typeof__(arg), type) | ||
| 33 | |||
| 34 | // If there's an appropriate built-in function for `arg`, emit | ||
| 35 | // a call to it (and eliminate everything below it as dead code) | ||
| 36 | #define USE_BUILTIN(fn, arg) \ | ||
| 37 | if (HAS_COMPATIBLE_BUILTIN(arg, unsigned long long, fn ## ll)) { \ | ||
| 38 | return __builtin_ ## fn ## ll(arg); \ | ||
| 39 | } else if (HAS_COMPATIBLE_BUILTIN(arg, unsigned long, fn ## l)) { \ | ||
| 40 | return __builtin_ ## fn ## l(arg); \ | ||
| 41 | } else if (HAS_COMPATIBLE_BUILTIN(arg, unsigned int, fn)) { \ | ||
| 42 | return __builtin_ ## fn(arg); \ | ||
| 43 | } | ||
| 44 | #else | ||
| 45 | #define USE_BUILTIN(fn, arg) | ||
| 46 | #endif | ||
| 47 | |||
| 48 | // Population count (cardinality) of set bits | ||
| 49 | 212 | static inline unsigned int u64_popcount(uint64_t n) | |
| 50 | { | ||
| 51 | 212 | USE_STDBIT(stdc_count_ones, n); | |
| 52 | USE_BUILTIN(popcount, n); | ||
| 53 | n -= ((n >> 1) & U64(0x5555555555555555)); | ||
| 54 | n = (n & U64(0x3333333333333333)) + ((n >> 2) & U64(0x3333333333333333)); | ||
| 55 | n = (n + (n >> 4)) & U64(0x0F0F0F0F0F0F0F0F); | ||
| 56 | return (n * U64(0x0101010101010101)) >> 56; | ||
| 57 | } | ||
| 58 | |||
| 59 | 104 | static inline unsigned int u32_popcount(uint32_t n) | |
| 60 | { | ||
| 61 | 104 | USE_STDBIT(stdc_count_ones, n); | |
| 62 | USE_BUILTIN(popcount, n); | ||
| 63 | n -= ((n >> 1) & U32(0x55555555)); | ||
| 64 | n = (n & U32(0x33333333)) + ((n >> 2) & U32(0x33333333)); | ||
| 65 | n = (n + (n >> 4)) & U32(0x0F0F0F0F); | ||
| 66 | return (n * U32(0x01010101)) >> 24; | ||
| 67 | } | ||
| 68 | |||
| 69 | // Count trailing zeros | ||
| 70 | 168 | static inline unsigned int u32_ctz(uint32_t n) | |
| 71 | { | ||
| 72 | 168 | BUG_ON(n == 0); | |
| 73 | 168 | USE_STDBIT(stdc_trailing_zeros, n); | |
| 74 | USE_BUILTIN(ctz, n); | ||
| 75 | return u32_popcount(~n & (n - 1)); | ||
| 76 | } | ||
| 77 | |||
| 78 | 30 | static inline unsigned int umax_ctz(uintmax_t n) | |
| 79 | { | ||
| 80 | 30 | BUG_ON(n == 0); | |
| 81 | 30 | USE_STDBIT(stdc_trailing_zeros, n); | |
| 82 | USE_BUILTIN(ctz, n); | ||
| 83 | |||
| 84 | unsigned int count = 0; | ||
| 85 | while (n) { | ||
| 86 | uint32_t part = (uint32_t)(n & 0xFFFFFFFFUL); | ||
| 87 | count += part ? u32_ctz(part) : 32; | ||
| 88 | n >>= 32; | ||
| 89 | } | ||
| 90 | |||
| 91 | return count; | ||
| 92 | } | ||
| 93 | |||
| 94 | // Find position of first (least significant) set bit (starting at 1) | ||
| 95 | 12 | static inline unsigned int u32_ffs(uint32_t n) | |
| 96 | { | ||
| 97 | 12 | USE_BUILTIN(ffs, n); | |
| 98 | return n ? u32_ctz(n) + 1 : 0; | ||
| 99 | } | ||
| 100 | |||
| 101 | // Extract (isolate) least significant set bit | ||
| 102 | 83 | static inline uint32_t u32_lsbit(uint32_t x) | |
| 103 | { | ||
| 104 | 83 | return x & -x; | |
| 105 | } | ||
| 106 | |||
| 107 | // Extract (isolate) most significant set bit (logâ‚‚ x) | ||
| 108 | 557 | static inline size_t size_msbit(size_t x) | |
| 109 | { | ||
| 110 | 557 | const size_t nbits = BITSIZE(x); | |
| 111 | 557 | size_t i = 1; | |
| 112 | |||
| 113 | #if HAS_BUILTIN(__builtin_clzg) | ||
| 114 |
2/2✓ Branch 2 → 3 taken 555 times.
✓ Branch 2 → 4 taken 2 times.
|
557 | return x ? i << ((nbits - __builtin_clzg(x)) - 1) : 0; |
| 115 | #endif | ||
| 116 | |||
| 117 | UNROLL_LOOP(8) | ||
| 118 | while (i < nbits) { | ||
| 119 | x |= x >> i; | ||
| 120 | i <<= 1; | ||
| 121 | } | ||
| 122 | |||
| 123 | return x ^ (x >> 1); | ||
| 124 | } | ||
| 125 | |||
| 126 | // Return the smallest power of 2 greater than or equal to `x`, or 0 if `x` | ||
| 127 | // is greater than the largest size_t power of 2. This is mostly the same | ||
| 128 | // as C23 stdc_bit_ceil(), except for the `x > max_pow2` case noted above. | ||
| 129 | 472 | static inline size_t next_pow2(size_t x) | |
| 130 | { | ||
| 131 | 472 | size_t msbit = size_msbit(x); | |
| 132 |
4/4✓ Branch 2 → 3 taken 471 times.
✓ Branch 2 → 5 taken 1 time.
✓ Branch 3 → 4 taken 271 times.
✓ Branch 3 → 5 taken 200 times.
|
472 | size_t shift = !IS_POWER_OF_2(x); |
| 133 | 472 | return (msbit << shift) + !x; | |
| 134 | } | ||
| 135 | |||
| 136 | // Count leading zeros | ||
| 137 | 9 | static inline unsigned int u64_clz(uint64_t x) | |
| 138 | { | ||
| 139 | 9 | BUG_ON(x == 0); | |
| 140 | 9 | USE_STDBIT(stdc_leading_zeros, x); | |
| 141 | USE_BUILTIN(clz, x); | ||
| 142 | x |= x >> 1; | ||
| 143 | x |= x >> 2; | ||
| 144 | x |= x >> 4; | ||
| 145 | x |= x >> 8; | ||
| 146 | x |= x >> 16; | ||
| 147 | x |= x >> 32; | ||
| 148 | return u64_popcount(~x); | ||
| 149 | } | ||
| 150 | |||
| 151 | // Round x up to a multiple of r (which *must* be a power of 2) | ||
| 152 | 33886 | static inline size_t next_multiple(size_t x, size_t r) | |
| 153 | DIAGNOSE_IF(!IS_POWER_OF_2(r)) | ||
| 154 | { | ||
| 155 | 33886 | r--; | |
| 156 | 33886 | return (x + r) & ~r; | |
| 157 | } | ||
| 158 | |||
| 159 | // Calculate the number of significant bits in `x` (the minimum number | ||
| 160 | // of base 2 digits needed to represent it). Note that this returns 0 | ||
| 161 | // for x=0, but see umax_count_base16_digits() (below) for an example | ||
| 162 | // of how that can be special cased. | ||
| 163 | 114 | static inline unsigned int umax_bitwidth(uintmax_t x) | |
| 164 | { | ||
| 165 |
2/2✓ Branch 2 → 3 taken 107 times.
✓ Branch 2 → 4 taken 7 times.
|
114 | USE_STDBIT(stdc_bit_width, x); |
| 166 | |||
| 167 | #if HAS_BUILTIN(__builtin_stdc_bit_width) | ||
| 168 | return __builtin_stdc_bit_width(x); | ||
| 169 | #endif | ||
| 170 | |||
| 171 | if (BITSIZE(x) == 64) { | ||
| 172 | return x ? 64u - u64_clz(x) : 0; | ||
| 173 | } | ||
| 174 | |||
| 175 | unsigned int width = 0; | ||
| 176 | while (x) { | ||
| 177 | x >>= 1; | ||
| 178 | width++; | ||
| 179 | } | ||
| 180 | return width; | ||
| 181 | } | ||
| 182 | |||
| 183 | // Calculate the number of hexadecimal digits (0-F) needed to represent | ||
| 184 | // `x` as a string (which is 1 in the case of x=0) | ||
| 185 | 37 | static inline size_t umax_count_base16_digits(uintmax_t x) | |
| 186 | { | ||
| 187 | 37 | unsigned int bit_width = umax_bitwidth(x); | |
| 188 | |||
| 189 | // `bit_width` can only be 0 when `x` is 0, so this simply ensures | ||
| 190 | // 1 is returned in that case | ||
| 191 | 37 | BUG_ON(!bit_width != !x); // Improves code gen | |
| 192 | 37 | unsigned int base2_digits = bit_width + !x; | |
| 193 | |||
| 194 | // This is equivalent to `(base2_digits >> 2) + !!(base2_digits & 3)`, | ||
| 195 | // but GCC seems to generate slightly better code when done this way | ||
| 196 | 37 | return next_multiple(base2_digits, 4) / 4; | |
| 197 | } | ||
| 198 | |||
| 199 | #endif | ||
| 200 |