[PATCH] Use x86 bsr instruction in Log2 functions

Matt Turner mattst88 at gmail.com
Tue Sep 18 10:35:19 PDT 2007


This patch edits RADEONLog2 and ATILog2 to use the x86 bsr instruction
instread of looping through bits. It should provide a somewhat of a
speed increase in this function on x86 and AMD64 architectures. Note:
the bsr instruction was added with the 80386 CPU and is therefore not
compatible with earlier CPUs. Also note: it will only use inline
assembly code on GCC, since it uses gcc syntax asm() function.

diff --git a/src/radeon_exa.c b/src/radeon_exa.c
index d074f08..65bd4d2 100644
--- a/src/radeon_exa.c
+++ b/src/radeon_exa.c
@@ -99,10 +99,17 @@ static __inline__ int
 RADEONLog2(int val)
 {
      int bits;
-
+#if (defined __i386__ || defined __amd64__ || defined __x86_64__) &&
(defined __GNUC__)
+       __asm volatile("bsrl %1,%0\n"
+               : "=r" (bits)
+               : "c" (val)
+       );
+       return bits;
+#else
      for (bits = 0; val != 0; val >>= 1, ++bits)
              ;
      return bits - 1;
+#endif
 }

 static __inline__ CARD32 F_TO_DW(float val)
diff --git a/src/radeon_render.c b/src/radeon_render.c
index 5074fe1..da22f3f 100644
--- a/src/radeon_render.c
+++ b/src/radeon_render.c
@@ -250,10 +250,17 @@ static __inline__ int
 ATILog2(int val)
 {
      int bits;
-
+#if (defined __i386__ || defined __amd64__ || defined __x86_64__) &&
(defined __GNUC__)
+       __asm volatile("bsrl %1,%0\n"
+               : "=r" (bits)
+               : "c" (val)
+       );
+       return bits;
+#else
      for (bits = 0; val != 0; val >>= 1, ++bits)
              ;
      return bits - 1;
+#endif
 }

 static void


More information about the xorg-driver-ati mailing list