Mesa (main): util: add util_popcnt_inline_asm

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Fri Oct 1 15:45:22 UTC 2021


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

Author: Marek Olšák <marek.olsak at amd.com>
Date:   Thu Sep 16 04:15:34 2021 -0400

util: add util_popcnt_inline_asm

Reviewed-By: Mike Blumenkrantz <michael.blumenkrantz at gmail.com>
Reviewed-by: Pierre-Eric Pelloux-Prayer <pierre-eric.pelloux-prayer at amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/13050>

---

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

diff --git a/src/util/bitscan.h b/src/util/bitscan.h
index 1ea7b07359f..105b7ba3122 100644
--- a/src/util/bitscan.h
+++ b/src/util/bitscan.h
@@ -319,6 +319,26 @@ util_bitcount(unsigned n)
 #endif
 }
 
+/**
+ * Return the number of bits set in n using the native popcnt instruction.
+ * The caller is responsible for ensuring that popcnt is supported by the CPU.
+ *
+ * gcc doesn't use it if -mpopcnt or -march= that has popcnt is missing.
+ *
+ */
+static inline unsigned
+util_popcnt_inline_asm(unsigned n)
+{
+#if defined(USE_X86_64_ASM) || defined(USE_X86_ASM)
+   uint32_t out;
+   __asm volatile("popcnt %1, %0" : "=r"(out) : "r"(n));
+   return out;
+#else
+   /* We should never get here by accident, but I'm sure it'll happen. */
+   return util_bitcount(n);
+#endif
+}
+
 static inline unsigned
 util_bitcount64(uint64_t n)
 {



More information about the mesa-commit mailing list