aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorKenneth Graunke <[email protected]>2012-09-11 22:14:58 -0700
committerKenneth Graunke <[email protected]>2012-09-12 22:13:05 -0700
commit0fc163408e6b9521d545daba19f70631011d5752 (patch)
tree0a3423252f2e3be95c90cff3bb364eead7f7545d /src
parent1a5d4f7cb2367c7863b28efbd78e9169114baf42 (diff)
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 <[email protected]> Reviewed-by: Brian Paul <[email protected]> Reviewed-by: Matt Turner <[email protected]>
Diffstat (limited to 'src')
-rw-r--r--src/mesa/main/imports.h22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/mesa/main/imports.h b/src/mesa/main/imports.h
index abf216c99ac..81da5104717 100644
--- a/src/mesa/main/imports.h
+++ b/src/mesa/main/imports.h
@@ -520,6 +520,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);