Skip to contentSkip to conversation

i386 / Use early clobber to eliminate rotate in desc

PATCH In review [email protected] 4 participants 6 messages Aug 11, 2005 – Aug 17, 2005

lore
Share & tools
lore
#0 lore raw

PATCH 5/14 i386 / Use early clobber to eliminate rotate in desc

Use an early clobber on addr to avoid the extra rorl instruction at the end of _set_tssldt_desc.

Also, get some C type checking on the descriptor struct here.

Patch-base: 2.6.13-rc5-mm1
Patch-keys: i386 desc cleanup optimize

include/asm-i386/desc.h +15 -11
Index: linux-2.6.13/include/asm-i386/desc.h
===================================================================
@@ -34,17 +34,21 @@
 extern struct desc_struct default_ldt[];
 extern void set_intr_gate(unsigned int irq, void * addr);
 
-#define _set_tssldt_desc(n,addr,limit,type) \
-__asm__ __volatile__ ("movw %w3,0(%2)\n\t" \
-	"movw %w1,2(%2)\n\t" \
-	"rorl $16,%1\n\t" \
-	"movb %b1,4(%2)\n\t" \
-	"movb %4,5(%2)\n\t" \
-	"movb $0,6(%2)\n\t" \
-	"movb %h1,7(%2)\n\t" \
-	"rorl $16,%1" \
-	: "=m"(*(n)) : "q" (addr), "r"(n), "ir"(limit), "i"(type))
-
+#define _set_tssldt_desc(desc,addr,limit,type) \
+do { \
+	unsigned long __tmp; \
+	typecheck(struct desc_struct *, desc); \
+	asm volatile ("movw %w4,0(%3)\n\t" \
+		      "movw %w2,2(%3)\n\t" \
+		      "rorl $16,%2\n\t" \
+		      "movb %b2,4(%3)\n\t" \
+		      "movb %5,5(%3)\n\t" \
+		      "movb $0,6(%3)\n\t" \
+		      "movb %h2,7(%3)\n\t" \
+		      : "=m"(*(desc)), "=&q" (__tmp) \
+		      : "1" (addr), "r"(desc), "ir"(limit), "i"(type)); \
+} while (0)
+  
 #include <mach_desc.h>
 
 #define set_tss_desc(cpu,addr) __set_tss_desc(cpu, GDT_ENTRY_TSS, addr)
#1 lore raw
Quoted 2 lines · On Wed, Aug 10, 2005 at 09:54:11PM -0700, [email protected] wrote:

Use an early clobber on addr to avoid the extra rorl instruction at the end of _set_tssldt_desc.

I would suggest to just use C for this. I do this on x86-64 and I don't think there is any reason to use this hard to maintain code for it.

It's probably a left over from Linus first experiments with inline assembly, similar to the old string.h and by now so obsolete it doesn't even stink anymore.

-Andi

#2 lore raw
Quoted 11 lines · Andi Kleen
Quoted 4 lines · On Wed, Aug 10, 2005 at 09:54:11PM -0700, [email protected] wrote:

Use an early clobber on addr to avoid the extra rorl instruction at the end of _set_tssldt_desc.

I would suggest to just use C for this. I do this on x86-64 and I don't think there is any reason to use this hard to maintain code for it.

This one in particular is non-optimal looking from C because the compiler misses the potential for rotation. But, composing into temporaries and then issuing two writes to memory instead of multiple writes within the same word could actually get you a better cycle count, and that is something GCC just might be able to do :)

Zach

#3 lore raw
Quoted 3 lines · Zachary Amsden

This one in particular is non-optimal looking from C because the compiler misses the potential for rotation. But, composing into temporaries and then issuing two writes to memory instead of multiple writes within the same word could actually get you a better cycle count, and that is something GCC just might be able to do :)

At least i386 and x86-64 gcc should recognize

	((foo << x) + (foo >> (32-x)))

... as a 32-bit rotate; similar for 8-, 16- and 64-bit rotates of appropriate sized items. Also, it seems it could just be an inline function instead of a macro.

-hpa

#4 lore raw
Quoted 5 lines · H. Peter Anvin

At least i386 and x86-64 gcc should recognize

	((foo << x) + (foo >> (32-x)))

... as a 32-bit rotate

Only for 1 <= x <= 31. For the x = 0 case the code posted is undefined and at least in some cases gcc will do "interesting" things as a result such as treating >> 32 as >> 0.

Alan

#5 lore raw
Quoted 4 lines · Alan Cox
Quoted 2 lines · H. Peter Anvin

At least i386 and x86-64 gcc should recognize

Quoted 9 lines
Quoted 3 lines
	((foo << x) + (foo >> (32-x)))

... as a 32-bit rotate

Only for 1 <= x <= 31. For the x = 0 case the code posted is undefined and at least in some cases gcc will do "interesting" things as a result such as treating >> 32 as >> 0.

True, although I thought gcc considered the above an idiom and would generate a rotate instruction. Perhaps it should be | rather than +.

-hpa