[Mesa-dev] [PATCH v2 4/5] util: Add u_bit_count64 and u_next_power_of_two
Rhys Perry
pendingchaos02 at gmail.com
Wed Jul 18 17:05:42 UTC 2018
Signed-off-by: Rhys Perry <pendingchaos02 at gmail.com>
---
src/util/bitscan.h | 28 ++++++++++++++++++++++++++++
1 file changed, 28 insertions(+)
diff --git a/src/util/bitscan.h b/src/util/bitscan.h
index dc89ac93f2..cae61d3f71 100644
--- a/src/util/bitscan.h
+++ b/src/util/bitscan.h
@@ -286,6 +286,34 @@ u_bit_consecutive64(unsigned start, unsigned count)
return (((uint64_t)1 << count) - 1) << start;
}
+/* Returns the number of bits set.
+ *
+ * based on
+ * http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
+ */
+static inline unsigned
+u_bit_count64(uint64_t val)
+{
+#ifdef __POPCNT__
+ return _mm_popcnt_u64(v);
+#else
+ unsigned result;
+ for (result = 0; val; result++)
+ val &= val - 1; /* clear the least significant bit set */
+ return result;
+#endif
+}
+
+/* Round the input to the next power of two.
+ * Zero is rounded to one.
+ */
+static inline uint64_t
+u_next_power_of_two(unsigned val)
+{
+ bool power_of_two_nonzero = util_is_power_of_two_or_zero64(val) && val;
+ return power_of_two_nonzero ? val : ((uint64_t)1 << util_last_bit64(val));
+}
+
#ifdef __cplusplus
}
--
2.14.4
More information about the mesa-dev
mailing list