[Mesa-dev] [PATCH 1/2] mesa: Add a _mesa_fls() function to find the last bit set in a word.
Kenneth Graunke
kenneth at whitecape.org
Tue Sep 11 22:14:58 PDT 2012
ffs() finds the least significant bit set; _mesa_fls() finds the /most/
significant bit.
Signed-off-by: Kenneth Graunke <kenneth at whitecape.org>
---
src/mesa/main/imports.c | 22 ++++++++++++++++++++++
src/mesa/main/imports.h | 2 ++
2 files changed, 24 insertions(+)
Wow. NAK my last patch which used ffs(). Totally backwards and just
happened to work for the one test because it only used limited values.
diff --git a/src/mesa/main/imports.c b/src/mesa/main/imports.c
index 934a2d0..eac8e21 100644
--- a/src/mesa/main/imports.c
+++ b/src/mesa/main/imports.c
@@ -316,6 +316,28 @@ _mesa_bitcount_64(uint64_t n)
}
#endif
+/**
+ * Find the last (most significant) bit set in a word.
+ *
+ * Essentially ffs() in the reverse direction.
+ */
+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
+}
/**
* Convert a 4-byte float to a 2-byte half float.
diff --git a/src/mesa/main/imports.h b/src/mesa/main/imports.h
index abf216c..ebe128e 100644
--- a/src/mesa/main/imports.h
+++ b/src/mesa/main/imports.h
@@ -520,6 +520,8 @@ extern unsigned int
_mesa_bitcount_64(uint64_t n);
#endif
+extern unsigned int
+_mesa_fls(unsigned int n);
extern GLhalfARB
_mesa_float_to_half(float f);
--
1.7.11.4
More information about the mesa-dev
mailing list