Mesa (main): util: Add util_widen_mask function.

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Fri Dec 3 19:02:12 UTC 2021


Module: Mesa
Branch: main
Commit: 6cde424945181d352c494f6511105000c3e5b805
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=6cde424945181d352c494f6511105000c3e5b805

Author: Timur Kristóf <timur.kristof at gmail.com>
Date:   Wed Dec  1 19:37:02 2021 +0100

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 at gmail.com>
Reviewed-by: Rhys Perry <pendingchaos02 at gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/14005>

---

 src/util/bitscan.h | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/src/util/bitscan.h b/src/util/bitscan.h
index 82b1bb5a1dd..726d2d2c385 100644
--- a/src/util/bitscan.h
+++ b/src/util/bitscan.h
@@ -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
 }
 



More information about the mesa-commit mailing list