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: 3rd deficiency (was: Superfluous branches due to insufficient flow analysis)
Date: Sat, 14 Aug 2021 19:10:08 +0200	[thread overview]
Message-ID: <3E50C50CC3EB49E8B479700FB4BEB243@H270> (raw)
In-Reply-To: <79331ef3-3ca5-0c17-af2c-e6bbb83b39e2@gmail.com>

Gabriel Ravier <gabravier@gmail.com> wrote:

Independent from the defunct flow analysis in the presence of NaNs, my
example demonstrates another minor deficiency: know thy instruction set!
See the comments in the assembly below.

> 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
>> double nextafter(double from, double to)
>> {
>>      if (to != to)
>>          return to;        // to is NAN
>>
>>      if (from != from)
>>          return from;      // from is NAN
>>
>>      if (from == to)       // neither from nor to can be NAN here!
>>          return to;
>>
>>      if (from == 0.0)      // dito!
>>          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 repro.c
>> ...
>> nextafter:
[...]
>> .L4:
>>          comisd  %xmm0, %xmm1    # sets CF if first comparand < second comparand
>>          movq    %xmm0, %rdx
>>          leaq    -1(%rdx), %rax  # superfluous
>>          seta    %r8b            #      sbb %rcx, %rcx
>>          comisd  %xmm0, %xmm2
>>          seta    %cl             #      sbb %rax, %rax
>>          addq    $1, %rdx        #      xor %rcx, %rax
>>          cmpb    %cl, %r8b       #      or  $1, %rax
>>          cmovne  %rdx, %rax      #      add %rdx, %rax
>>          movq    %rax, %xmm0

Stefan

      parent reply	other threads:[~2021-08-14 17:12 UTC|newest]

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

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=3E50C50CC3EB49E8B479700FB4BEB243@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).