This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: i386 and asm jumping


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Tue, May 07, 2002 at 07:32:39PM +0200, Johan Rydberg wrote:
> What I would like to do is to replace the following code
> with inline assembler:
> 
> 	if (--cnt == 0)
> 	  goto HANDLER;
>         goto NEXT;
> 
> The code above will generate; load cnt into register,
> decrement register, store register and jump to HANDLER
> if zero flag is set.  I would like to replace this with
> the following assembler code:
> 
> 	decl	cnt
> 	jz	HANDLER
> 	jmp	NEXT
> 
> Speed is everything :)

[Note I'm using gcc 2.95.3 here - for all I know later version might
optimize foo() to foo() { return 0; }]

$ cat foo.c
int foo(int cnt)
{
        int retval = 0;

NEXT:
        if(--cnt == 0)
                goto HANDLER;
        goto NEXT;

HANDLER:
        retval += cnt;

        return retval;
}
$ gcc -S -O2 -f-omit-frame-pointer
$ cat foo.s
        .file   "foo.c"
        .version        "01.01"
gcc2_compiled.:
.text
        .align 4
.globl foo
        .type    foo,@function
foo:
        movl 4(%esp),%eax
.L3:
        decl %eax
        jnz .L3
        xorl %eax,%eax
        ret
.Lfe1:
        .size    foo,.Lfe1-foo
        .ident  "GCC: (GNU) 2.95.3 20010315 (release)"


Is that efficient enough for you?  Note that gcc has done *better* than
naive hand-optimization.  Of course, it depends on what the loop does - in
my simple example the loop does pretty much nothing.

Rather just write clear, readable code and let GCC worry about making it
fast.  If you try to second-guess GCC, you often just succeed in making
your intent unclear, and the compiler just has to to what you say, step by
step.  Tell GCC *what* you want, not *how*.

Bernd Jendrissek
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.4 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE82QLs/FmLrNfLpjMRAr5VAJ9S3Gtkz+gsSO5oPcfkM+hF4TijrACeO3rv
fuaKPoD8a/3VnhGheAgpyx8=
=rkqr
-----END PGP SIGNATURE-----


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]