Hello,

I performed several tests of the logbase2() function.
This function is defined and used in these files:

src/mesa/main/teximage.c
src/mesa/drivers/dri/intel/intel_tex_image.c
src/mesa/drivers/dri/unichrome/via_tex.c
src/mesa/drivers/dri/i965/intel_tex_image.c
src/mesa/drivers/dri/tdfx/tdfx_tex.c

There is also a util_logbase2() function in
src/gallium/auxiliary/util/u_math.c which do the same thing.

Both functions from "src/mesa/main/teximage.c" and
"src/gallium/auxiliary/util/u_math.c" are called when I play to ETQW
with r600g.

First thing: is it possible to merge this function in one place? The
same function is implement in different place. I have no sufficient
experience of the Mesa architecture to answer this myself.

Second thing: there is a C implementation in Wikipedia (
http://en.wikipedia.org/wiki/Binary_logarithm#Algorithm ), I merged it
in Mesa and I noted that it is *at least* twice faster than logbase2()
and util_logbase2() (with or without GCC optimization).

Third thing: I think it's better to use an unsigned int than a int for
the logbase2's parameter. At least in "teximage.c" and
"intel_tex_image.c", the result is stored in a GLuint. But not in
"via_tex.c"

Here is a patch to merge it, at least in "src/mesa/main/teximage.c". I
will understand any comments, especially on the relevance of this patch.
(The patch still uses a integer parameter).

Regards.

Benjamin Bellec
diff --git a/src/mesa/main/teximage.c b/src/mesa/main/teximage.c
index 4ea9a48..e2eeaa0 100644
--- a/src/mesa/main/teximage.c
+++ b/src/mesa/main/teximage.c
@@ -81,31 +81,25 @@ _mesa_free_texmemory(void *m)
 
 
 /*
- * Compute floor(log_base_2(n)).
+ * Returns the floor form of binary logarithm for a 32 bit integer.
  * If n < 0 return -1.
  */
 static int
 logbase2( int n )
 {
-   GLint i = 1;
-   GLint log2 = 0;
+   if (n == 0)
+      return 0;
 
    if (n < 0)
       return -1;
 
-   if (n == 0)
-      return 0;
-
-   while ( n > i ) {
-      i *= 2;
-      log2++;
-   }
-   if (i != n) {
-      return log2 - 1;
-   }
-   else {
-      return log2;
-   }
+   int 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;
 }
 
 
_______________________________________________
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/mesa-dev

Reply via email to