Mesa (9.0): mesa: Add a _mesa_fls() function to find the last bit set in a word.

Kenneth Graunke kwg at kemper.freedesktop.org
Fri Sep 14 09:30:01 UTC 2012


Module: Mesa
Branch: 9.0
Commit: 66e8f863d34fc8b8f7602c45bb3230fe663d4eb0
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=66e8f863d34fc8b8f7602c45bb3230fe663d4eb0

Author: Kenneth Graunke <kenneth at whitecape.org>
Date:   Tue Sep 11 22:14:58 2012 -0700

mesa: Add a _mesa_fls() function to find the last bit set in a word.

ffs() finds the least significant bit set; _mesa_fls() finds the /most/
significant bit.

v2: Make it an inline function in imports.h, per Brian's suggestion.

Signed-off-by: Kenneth Graunke <kenneth at whitecape.org>
Reviewed-by: Brian Paul <brianp at vmware.com>
Reviewed-by: Matt Turner <mattst88 at gmail.com>
(cherry picked from commit 0fc163408e6b9521d545daba19f70631011d5752)

---

 src/mesa/main/imports.h |   22 ++++++++++++++++++++++
 1 files changed, 22 insertions(+), 0 deletions(-)

diff --git a/src/mesa/main/imports.h b/src/mesa/main/imports.h
index 73913b5..551aea7 100644
--- a/src/mesa/main/imports.h
+++ b/src/mesa/main/imports.h
@@ -587,6 +587,28 @@ extern unsigned int
 _mesa_bitcount_64(uint64_t n);
 #endif
 
+/**
+ * Find the last (most significant) bit set in a word.
+ *
+ * Essentially ffs() in the reverse direction.
+ */
+static inline unsigned int
+_mesa_fls(unsigned int n)
+{
+#if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304)
+   return n == 0 ? 0 : 32 - __builtin_clz(n);
+#else
+   unsigned int v = 1;
+
+   if (n == 0)
+      return 0;
+
+   while (n >>= 1)
+       v++;
+
+   return v;
+#endif
+}
 
 extern GLhalfARB
 _mesa_float_to_half(float f);




More information about the mesa-commit mailing list