|
| 1 | +// Copyright (c) Facebook, Inc. and its affiliates. |
| 2 | +// SPDX-License-Identifier: GPL-3.0+ |
| 3 | + |
| 4 | +/** |
| 5 | + * @file |
| 6 | + * |
| 7 | + * Bitwise operations. |
| 8 | + * |
| 9 | + * See @ref BitwiseOperations. |
| 10 | + */ |
| 11 | + |
| 12 | +#ifndef DRGN_BITOPS_H |
| 13 | +#define DRGN_BITOPS_H |
| 14 | + |
| 15 | +#include "pp.h" |
| 16 | + |
| 17 | +/** |
| 18 | + * @ingroup Internals |
| 19 | + * |
| 20 | + * @defgroup BitwiseOperations Bitwise operations |
| 21 | + * |
| 22 | + * Generic bitwise operations. |
| 23 | + * |
| 24 | + * @{ |
| 25 | + */ |
| 26 | + |
| 27 | +/** |
| 28 | + * Count Trailing Zero bits. |
| 29 | + * |
| 30 | + * Return the number of trailing least significant 0-bits in @p x. This is |
| 31 | + * undefined if @p x is zero. |
| 32 | + * |
| 33 | + * ``` |
| 34 | + * ctz(1) == ctz(0b1) == 0 |
| 35 | + * ctz(2) == ctz(0b10) == 1 |
| 36 | + * ctz(12) == ctz(0b1100) == 2 |
| 37 | + * ``` |
| 38 | + * |
| 39 | + * @param[in] x Integer. |
| 40 | + */ |
| 41 | +#define ctz(x) generic_bitop(x, PP_UNIQUE(_x), __builtin_ctz) |
| 42 | + |
| 43 | +/** |
| 44 | + * Find Last Set bit. |
| 45 | + * |
| 46 | + * Return the one-based index of the most significant 1-bit of @p x or 0 if @p x |
| 47 | + * is 0. |
| 48 | + * |
| 49 | + * ``` |
| 50 | + * fls(0) == fls(0b0) == 0 |
| 51 | + * fls(1) == fls(0b1) == 1 |
| 52 | + * fls(13) == fls(0b1101) == 4 |
| 53 | + * ``` |
| 54 | + * |
| 55 | + * For unsigned integers, |
| 56 | + * ``` |
| 57 | + * fls(x) = floor(log2(x)) + 1, if x > 0 |
| 58 | + * 0, if x == 0 |
| 59 | + * ``` |
| 60 | + * |
| 61 | + * @param[in] x Integer. |
| 62 | + */ |
| 63 | +#define fls(x) generic_bitop(x, PP_UNIQUE(_x), fls_) |
| 64 | +/** @cond */ |
| 65 | +/* |
| 66 | + * The straightfoward implementation is bits - clz. However, as noted by the |
| 67 | + * folly implementation: "If X is a power of two, X - Y = 1 + ((X - 1) ^ Y). |
| 68 | + * Doing this transformation allows GCC to remove its own xor that it adds to |
| 69 | + * implement clz using bsr." |
| 70 | + * |
| 71 | + * This doesn't do the normal macro argument safety stuff because it should only |
| 72 | + * be used via generic_bitop() which already does it. |
| 73 | + */ |
| 74 | +#define fls_impl(x, type, suffix) \ |
| 75 | + (x ? 1 + ((8 * sizeof(type) - 1) ^ __builtin_clz##suffix(x)) : 0) |
| 76 | +#define fls_(x) fls_impl(x, unsigned int,) |
| 77 | +#define fls_l(x) fls_impl(x, unsigned long, l) |
| 78 | +#define fls_ll(x) fls_impl(x, unsigned long long, ll) |
| 79 | + |
| 80 | +#define generic_bitop(x, unique_x, op) ({ \ |
| 81 | + __auto_type unique_x = (x); \ |
| 82 | + _Static_assert(sizeof(unique_x) <= sizeof(unsigned long long), \ |
| 83 | + "type is too large"); \ |
| 84 | + (unsigned int)(sizeof(unique_x) <= sizeof(unsigned int) ? \ |
| 85 | + op(unique_x) : \ |
| 86 | + sizeof(unique_x) <= sizeof(unsigned long) ? \ |
| 87 | + op##l(unique_x) : \ |
| 88 | + op##ll(unique_x)); \ |
| 89 | +}) |
| 90 | +/** @endcond */ |
| 91 | + |
| 92 | +/** |
| 93 | + * Return the smallest power of two greater than or equal to @p x. |
| 94 | + * |
| 95 | + * ``` |
| 96 | + * next_power_of_two(0) == 1 // Zero is not a power of two |
| 97 | + * next_power_of_two(1) == 1 |
| 98 | + * next_power_of_two(13) == 16 |
| 99 | + * ``` |
| 100 | + * |
| 101 | + * @param[in] x Integer. |
| 102 | + */ |
| 103 | +#define next_power_of_two(x) next_power_of_two_impl(x, PP_UNIQUE(_x)) |
| 104 | +/** @cond */ |
| 105 | +#define next_power_of_two_impl(x, unique_x) ({ \ |
| 106 | + __auto_type unique_x = (x); \ |
| 107 | + unique_x ? (typeof(unique_x))1 << fls(unique_x - 1) : \ |
| 108 | + (typeof(unique_x))1; \ |
| 109 | +}) |
| 110 | +/** @endcond */ |
| 111 | + |
| 112 | +/** |
| 113 | + * Iterate over each 1-bit in @p mask. |
| 114 | + * |
| 115 | + * On each iteration, this sets @p i to the zero-based index of the least |
| 116 | + * significant 1-bit in @p mask and clears that bit in @p mask. It stops |
| 117 | + * iterating when @p mask is zero. |
| 118 | + * |
| 119 | + * ``` |
| 120 | + * // Outputs 0 2 3 |
| 121 | + * unsigned int mask = 13, i; |
| 122 | + * for_each_bit(i, mask) |
| 123 | + * printf("%u ", i); |
| 124 | + * ``` |
| 125 | + * |
| 126 | + * @param[out] i Iteration variable name. |
| 127 | + * @param[in,out] mask Integer to iterate over. This is modified. |
| 128 | + */ |
| 129 | +#define for_each_bit(i, mask) \ |
| 130 | + while (mask && (i = ctz(mask), mask &= mask - 1, 1)) |
| 131 | + |
| 132 | +/** @} */ |
| 133 | + |
| 134 | +#endif /* DRGN_BITOPS_H */ |
0 commit comments