Le 03/06/2011 01:40, Brian Paul a écrit :
> I'd like to avoid the warning if at all possible.  If you replace (val
>>> 32) with (val >> (sizeof(unsigned) * 4)) does that silence it?

Yes it fix, but as Matt Turner said, it's not necessary to check this.
Btw we don't check that in the other functions.


Le 03/06/2011 06:09, Matt Turner a écrit :
> Also, if you want to check if the value is already a power-of-two,
> instead of a case statement for every POT (including 0), just do the
> standard is-power-of-two check:
>
> (x & (x - 1)) == 0

My own tests (on a Core2) shows that it's less efficient to do that, at
least with -O2 optimization enabled. With -O0, it's equal.

So here is a v2 patch with a builtin GCC optimization which is the
fastest (thx Matt to point me to this solution).

Regards.

Benjamin Bellec
diff --git a/src/gallium/auxiliary/util/u_math.h b/src/gallium/auxiliary/util/u_math.h
index 65a99fc..1b984b6 100644
--- a/src/gallium/auxiliary/util/u_math.h
+++ b/src/gallium/auxiliary/util/u_math.h
@@ -42,7 +42,6 @@
 #include "pipe/p_compiler.h"
 #include "util/u_debug.h"
 
-
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -486,24 +485,49 @@ util_logbase2(unsigned n)
    return pos;
 }
 
-
 /**
  * Returns the smallest power of two >= x
  */
 static INLINE unsigned
 util_next_power_of_two(unsigned x)
 {
-   unsigned i;
-
+#if defined(PIPE_CC_GCC)
    if (x == 0)
       return 1;
-
-   --x;
-
-   for (i = 1; i < sizeof(unsigned) * 8; i <<= 1)
-      x |= x >> i;
-
-   return x + 1;
+   else
+      return (1 << (32 - __builtin_clz(x - 1)));
+#else
+   unsigned val = x;
+
+   switch (x)
+   {
+      case 1:
+      case 2:
+      case 4:
+      case 8:
+      case 16:
+      case 32:
+      case 64:
+      case 128:
+      case 256:
+      case 512:
+      case 1024:
+      case 2048:
+      case 4096:
+         return x; /* this is the most commonly used values */
+      case 0:
+         return 1;
+      default:
+         val--;
+         val = (val >> 1) | val;
+         val = (val >> 2) | val;
+         val = (val >> 4) | val;
+         val = (val >> 8) | val;
+         val = (val >> 16) | val;
+         val++;
+         return val;
+   }
+#endif
 }
 
 
_______________________________________________
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/mesa-dev

Reply via email to