util: Add util_next_power_of_two() for rounding a uint up to a POT.
authorYounes Manton <younes.m@gmail.com>
Sun, 27 Sep 2009 18:40:14 +0000 (14:40 -0400)
committerYounes Manton <younes.m@gmail.com>
Sun, 27 Sep 2009 23:25:57 +0000 (19:25 -0400)
src/gallium/auxiliary/util/u_math.h

index cd6a9fcc091ed9048ee119682f7a89105e6fae9b..75b075f160d922c14c0277a57dd9c5b7b60ec680 100644 (file)
@@ -470,6 +470,26 @@ util_logbase2(unsigned n)
 }
 
 
+/**
+ * Returns the smallest power of two >= x
+ */
+static INLINE unsigned
+util_next_power_of_two(unsigned x)
+{
+   unsigned i;
+
+   if (x == 0)
+      return 1;
+
+   --x;
+
+   for (i = 1; i < sizeof(unsigned) * 8; i <<= 1)
+      x |= x >> i;
+
+   return x + 1;
+}
+
+
 /**
  * Clamp X to [MIN, MAX].
  * This is a macro to allow float, int, uint, etc. types.