[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Single asm instruction
On Tue, Feb 04, 2003 at 03:02:29PM +0100, Jan Hudec wrote:
> On Mon, Feb 03, 2003 at 04:36:01PM +0100, Erik Mouw wrote:
> > On Mon, Feb 03, 2003 at 04:46:28PM +0200, Gilad Benjamini wrote:
> > > What's the exact syntax ?
> >
> > __asm__ ( "asm code" : "input variables" : "output variables" : "clobber" );
>
> While assembly can't be optimized, this syntax allows gcc to optimize
> the code around the assembly. It allows gcc to minimize number of stores
> and loads needed around the assembly bit.
Most important it allows gcc to decide which registers to assign to the
instructions. On register-starved architectures like the x86 there are
not that many choices, but on RISC architectures (which usually have at
least 8 general purpose registers) this can make a difference.
> > BIG FAT WARNING: Try to avoid assembly! It makes your code non-portable
> > and hard to maintain!
>
> And a notice: you don't need gcc to optimize assembly, because you only
> write code in assembly if you already optimized it better than gcc can
> ever do.
It also helps to rewrite the C code so the optimiser can do a better
job. Properly rewritten code can sometimes yield four times faster
code. For a (documented) example with properly rewritten C code, have a
look at the CRC32 implementation I did for the blob ARM bootloader:
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/blob/blob/src/lib/crc32.c?rev=1.3&content-type=text/vnd.viewcvs-markup
Erik
--
J.A.K. (Erik) Mouw
Email: J.A.K.Mouw@its.tudelft.nl mouw@nl.linux.org
WWW: http://www-ict.its.tudelft.nl/~erik/
PGP signature