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>
This commit is contained in:
Alyssa Rosenzweig 2025-09-05 11:47:50 -04:00 committed by Marge Bot
parent 055f8ebf96
commit 86a5dd10ac

View file

@ -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 */