public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Uros Bizjak <uros@kss-loka.si>
To: gcc@gcc.gnu.org
Subject: Missing if optimization
Date: Thu, 09 Sep 2004 05:24:00 -0000	[thread overview]
Message-ID: <413FE7D1.8000201@kss-loka.si> (raw)

Hello!

The if optimization, which produces quite optimized code for integer 
case is missing for floatin-point compares.

This code:

int test(int a) {
        return a == 0 ? 0 : (a > 0 ? 1 : -1);
}

produces (with -O2  -fomit-frame-pointer):

test:
        xorl    %eax, %eax
        cmpl    $0, 4(%esp)
        je      .L4
        setg    %al
        movzbl  %al, %eax
        leal    -1(%eax,%eax), %eax
.L4:
        ret

However, fp compares:

double testf(double a) {
        return a == 0.0 ? 0.0 : (a > 0.0 ? 1.0 : -1.0);
}

Produce somehow unoptimized code (-O2 -ffast-math -fomit-frame-pointer):

testf:
        fldl    4(%esp)
        ftst
        fnstsw  %ax
        sahf
        jne     .L9
        fstp    %st(0)
        fldz
        ret
        .p2align 4,,7
.L9:
        ftst
        fnstsw  %ax
        fstp    %st(0)
        sahf
        ja      .L12
        flds    .LC1
        ret
        .p2align 4,,7
.L12:
        fld1
        .p2align 4,,2
        ret

There is no need for second fp compare, two conditional jumps after 
first fp compare would be enough.

Uros.


             reply	other threads:[~2004-09-09  5:24 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-09-09  5:24 Uros Bizjak [this message]
2004-09-09  5:49 ` Andrew Pinski
2004-09-09 22:15 ` James E Wilson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=413FE7D1.8000201@kss-loka.si \
    --to=uros@kss-loka.si \
    --cc=gcc@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).