[Mesa-dev] [PATCH 01/14] gallium/u_math: add util_logbase2_ceil

Nicolai Hähnle nhaehnle at gmail.com
Tue Sep 13 09:56:12 UTC 2016


From: Nicolai Hähnle <nicolai.haehnle at amd.com>

For finding the exponent of the next power of two.
---
 src/gallium/auxiliary/util/u_math.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/src/gallium/auxiliary/util/u_math.h b/src/gallium/auxiliary/util/u_math.h
index a923271..2ab5f03 100644
--- a/src/gallium/auxiliary/util/u_math.h
+++ b/src/gallium/auxiliary/util/u_math.h
@@ -422,20 +422,32 @@ util_logbase2(unsigned n)
    unsigned pos = 0;
    if (n >= 1<<16) { n >>= 16; pos += 16; }
    if (n >= 1<< 8) { n >>=  8; pos +=  8; }
    if (n >= 1<< 4) { n >>=  4; pos +=  4; }
    if (n >= 1<< 2) { n >>=  2; pos +=  2; }
    if (n >= 1<< 1) {           pos +=  1; }
    return pos;
 #endif
 }
 
+/**
+ * Returns the ceiling of log n base 2, and 0 when n == 0. Equivalently,
+ * returns the smallest x such that n <= 2**x.
+ */
+static inline unsigned
+util_logbase2_ceil(unsigned n)
+{
+   if (n <= 1)
+      return 0;
+
+   return 1 + util_logbase2(n - 1);
+}
 
 /**
  * Returns the smallest power of two >= x
  */
 static inline unsigned
 util_next_power_of_two(unsigned x)
 {
 #if defined(HAVE___BUILTIN_CLZ)
    if (x <= 1)
        return 1;
-- 
2.7.4



More information about the mesa-dev mailing list