diff options
| author | Alyssa Rosenzweig <alyssa.rosenzweig@intel.com> | 2025-09-05 11:47:50 -0400 |
|---|---|---|
| committer | Marge Bot <marge-bot@fdo.invalid> | 2025-09-16 21:48:37 +0000 |
| commit | 86a5dd10ace4a630d21190177d4295507b66766b (patch) | |
| tree | ddd49ecdc06cf098e38ce14f1ea4d105821653e1 | |
| parent | 055f8ebf96091f2ae989570f12bce8f07ad4ca4c (diff) | |
util: add util_bit_swap macro
We will use this to manipulate lookup tables, but it's a common algorithm.
Signed-off-by: Alyssa Rosenzweig <alyssa.rosenzweig@intel.com>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/37200>
| -rw-r--r-- | src/util/macros.h | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/src/util/macros.h b/src/util/macros.h index 829031c88ca..e914fa2c6a7 100644 --- a/src/util/macros.h +++ b/src/util/macros.h @@ -564,4 +564,15 @@ typedef int lock_cap_t; } \ } while (0) +/* + * Swap bits a and b. From Bithacks + * https://graphics.stanford.edu/~seander/bithacks.html#SwappingBitsXOR + */ +static inline uint32_t +util_bit_swap(uint32_t v, unsigned a, unsigned b) +{ + uint32_t x = ((v >> a) ^ (v >> b)) & 1; + return v ^ ((x << a) | (x << b)); +} + #endif /* UTIL_MACROS_H */ |