public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Stefan Kanthak" <stefan.kanthak@nexgo.de>
To: "GCC Development" <gcc@gcc.gnu.org>,
	"Gabriel Ravier" <gabravier@gmail.com>
Subject: Re: Superfluous branches due to insufficient flow analysis
Date: Sat, 14 Aug 2021 16:22:50 +0200	[thread overview]
Message-ID: <DE3CFE93AAC943F5B8F59087E800B941@H270> (raw)
In-Reply-To: <79331ef3-3ca5-0c17-af2c-e6bbb83b39e2@gmail.com>

"Gabriel Ravier" <gabravier@gmail.com> wrote:

Please don't FULL QUOTE!

> On 8/13/21 8:58 PM, Stefan Kanthak wrote:
>> Hi,
>>
>> compile the following naive implementation of nextafter() for AMD64:
>>
>> JFTR: ignore the aliasing casts, they don't matter here!
>>
>> $ cat repro.c

[...]

> Shouldn't this kind of stuff go to the Bugzilla ?

I don't mind if you or someone else take it there.
If you do so, here's one for the road:

$ cat alternate.c
double nextafter(double from, double to)
{
    if (from == to)
        return to;

    if ((from != from) || (to != to))
        return from + to;

    if (from == 0.0)
        return to < 0.0 ? -0x1.0p-1074 : 0x1.0p-1074;

    unsigned long long ull = *(unsigned long long *) &from;

    if ((from > to) == (from > 0.0))
        ull--;
    else
        ull++;

    return *(double *) &ull;
}
$ gcc -m64 -o- -O3 -S alternate.c
nextafter:
        ucomisd %xmm0, %xmm1 # sets PF if comparands are unordered,
                             #  i.e. at least one is a NaN
        jnp     .L20         # OUCH: this branch is unlikely taken!
.L13:                        # reached only if at least one
                             #  argument is a NaN!
        ucomisd %xmm0, %xmm0 # OUCH: SUPERFLUOUS!
        jnp     .L21         # OUCH: SUPERFLUOUS!
.L14:
        addsd   %xmm1, %xmm0
        ret
.L21:                        # reached only if at least one
                             #  argument is a NaN!
        ucomisd %xmm1, %xmm1 # OUCH: SUPERFLUOUS!
        jp      .L14         # OUCH: SUPERFLUOUS!
        pxor    %xmm2, %xmm2
        ucomisd %xmm2, %xmm0
        jnp     .L22         # OUCH: SUPERFLUOUS!
                             #       xmm0 can't be NaN here!
                             # OUCH: and if it were, this branch
                             #        would unlikely be taken!
.L8:
        comisd  %xmm0, %xmm1
        movq    %xmm0, %rdx
        leaq    -1(%rdx), %rax
        seta    %r8b
        comisd  %xmm0, %xmm2
        seta    %cl
        addq    $1, %rdx
        cmpb    %cl, %r8b
        cmovne  %rdx, %rax
        movq    %rax, %xmm0
        ret
.L20:
        jne     .L13
        movapd  %xmm1, %xmm0
        ret
.L22:
        jne     .L8
        movabsq $-9223372036854775808, %rdx
        movq    %xmm1, %rax
        andq    %rdx, %rax
        orq     $1, %rax
        movq    %rax, %xmm0
        ret

Stefan

  reply	other threads:[~2021-08-14 14:35 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-13 18:58 Stefan Kanthak
2021-08-13 19:22 ` Gabriel Ravier
2021-08-14 14:22   ` Stefan Kanthak [this message]
2021-08-14 17:10   ` 3rd deficiency (was: Superfluous branches due to insufficient flow analysis) Stefan Kanthak

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=DE3CFE93AAC943F5B8F59087E800B941@H270 \
    --to=stefan.kanthak@nexgo.de \
    --cc=gabravier@gmail.com \
    --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).