public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "aldyh at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/103559] Can't optimize away < 0 check on sqrt
Date: Wed, 29 Mar 2023 17:46:09 +0000	[thread overview]
Message-ID: <bug-103559-4-j1bXcECw2e@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-103559-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103559

Aldy Hernandez <aldyh at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |DUPLICATE
                 CC|                            |aldyh at gcc dot gnu.org

--- Comment #5 from Aldy Hernandez <aldyh at gcc dot gnu.org> ---
(In reply to Jeremy R. from comment #0)
> For a simple invocation of sqrt, gcc inserts a < 0 check to set math errno
> if needed. E.g.
> 
> float f(float x) {
>     return sqrt(x);
> }
> 
> Is generated as
> 
> f(float):
>         vxorps  xmm1, xmm1, xmm1
>         vucomiss        xmm1, xmm0
>         ja      .L10
>         vsqrtss xmm0, xmm0, xmm0
>         ret
> .L10:
>         jmp     sqrtf
> 
> 
> Unfortunately, this check is still present when the GCC is able to prove
> that x is non-negative:
> 
> float f(float x) {
>     if(x < 0) [[unlikely]] {
>         __builtin_unreachable();
>     } else {
>         return sqrt(x);
>     }
> }

x could also be a NAN which I *think* the hardware sqrt won't handle, so a
better test would be:

if (x < 0.0 || __builtin_isnan(x)) [[unlikely]]
  __builtin_unreachable ();

or perhaps:

if (!__builtin_isgreater(x, 0.0))

Either way, this is a subset of PR91645 so I'm closing it as a duplicate.

*** This bug has been marked as a duplicate of bug 91645 ***

  parent reply	other threads:[~2023-03-29 17:46 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-04 20:58 [Bug tree-optimization/103559] New: " llvm at rifkin dot dev
2021-12-04 21:01 ` [Bug tree-optimization/103559] " pinskia at gcc dot gnu.org
2021-12-04 21:34 ` pinskia at gcc dot gnu.org
2021-12-04 21:38 ` pinskia at gcc dot gnu.org
2021-12-04 21:43 ` pinskia at gcc dot gnu.org
2021-12-05 11:38 ` vanyacpp at gmail dot com
2023-03-29 17:46 ` aldyh at gcc dot gnu.org [this message]
2023-03-29 18:34 ` llvm at rifkin dot dev
2023-03-29 18:39 ` jakub at gcc dot gnu.org
2023-03-30 10:40 ` xry111 at gcc dot gnu.org
2023-03-30 10:48 ` jakub at gcc dot gnu.org
2023-03-30 10:53 ` xry111 at gcc dot gnu.org
2023-03-30 11:41 ` jakub at gcc dot gnu.org

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=bug-103559-4-j1bXcECw2e@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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).