util: Add util_widen_mask function.

Widens the given bit mask by a multiplier, meaning that it will
replicate each bit by that amount.
For example: 0b101 widened by 2 will become: 0b110011

This is typically used in shader I/O to transform a 64-bit
writemask to a 32-bit writemask.

Moving this function here because it is used in multiple places.

Signed-off-by: Timur Kristóf <timur.kristof@gmail.com>
Reviewed-by: Rhys Perry <pendingchaos02@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/14005>
This commit is contained in:
Timur Kristóf 2021-12-01 19:37:02 +01:00 committed by Marge Bot
parent 7e66da89f8
commit 6cde424945

View file

@ -349,6 +349,25 @@ util_bitcount64(uint64_t n)
#endif
}
/**
* Widens the given bit mask by a multiplier, meaning that it will
* replicate each bit by that amount.
*
* For example:
* 0b101 widened by 2 will become: 0b110011
*
* This is typically used in shader I/O to transform a 64-bit
* writemask to a 32-bit writemask.
*/
static inline uint32_t
util_widen_mask(uint32_t mask, unsigned multiplier)
{
uint32_t new_mask = 0;
u_foreach_bit(i, mask)
new_mask |= ((1u << multiplier) - 1u) << (i * multiplier);
return new_mask;
}
#ifdef __cplusplus
}