public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: Richard Biener <rguenther@suse.de>
Cc: Aldy Hernandez <aldyh@redhat.com>,
	"Joseph S. Myers" <joseph@codesourcery.com>,
	gcc-patches@gcc.gnu.org, Siddhesh Poyarekar <siddhesh@gotplt.org>,
	Andrew MacLeod <amacleod@redhat.com>
Subject: Re: [PATCH] Add targetm.libm_function_max_error
Date: Thu, 27 Apr 2023 11:20:52 +0200	[thread overview]
Message-ID: <ZEo+dHeI1zb/wbKL@tucnak> (raw)
In-Reply-To: <nycvar.YFH.7.77.849.2304270710170.4466@jbgna.fhfr.qr>

On Thu, Apr 27, 2023 at 07:18:52AM +0000, Richard Biener wrote:
> Humm.  Is it worth the trouble?  I think if we make use of this it needs

I think so.  Without that, frange is half blind, using at least most common
libm functions in floating point code is extremely common and without
knowing anything about what those functions can or can't return frange will
be mostly VARYING.  And simply assuming all libm implementations are perfect
0.5ulps precise for all inputs would be very risky when we know it clearly
is not the case.

Of course, by improving frange further, we run more and more into the
already reported and just tiny bit worked around bug on the optimized away
floating point exception generating statements and we need to decide what to
do for that case..

> to be with -funsafe-math-optimizations (or a new switch?).  I'll note

Why?  If we know or can reasonably assume that say on the boundary values
some function is always precise (say sqrt always in [-0.,+Inf] U NAN,
sin/cos always in [-1.,1.] U NAN etc., that isn't an unsafe math
optimization to assume it is the case.  If we know it is a few ulps away
from that, we can just widen the range, if we don't know anything or the
function implementation is uselessly buggy, we can punt.
Whether something is a known math library function or just some floating
point arithmetics which we already handle in 13.1 shouldn't make much
difference.

> Should we, when simplifying say
> 
>   x = sin (y);
>   if (x <= 1.)
> 
> simplify it to
> 
>   x = sin (y);
>   x = min (x, 1.);
> 
> for extra safety?

Why?  If we don't know anything about y, x could be NAN, so we can't fold
it, but if we know it will not be NAN, it is always true and we are there
back to the exceptions case (plus errno but that makes the function
non-const, doesn't it?).

> That said - what kind of code do we expect to optimize when producing
> ranges for math function [operands]?  Isn't it just defensive programming
> that we'd "undo"?  Are there any missed-optimization PRs around this?

I strongly doubt real-world code has such defensive programming checks.
The intent isn't to optimize those away, but generally propagate range
information, such that we say know that sqrt result isn't negative (except
for possible -0. or negative NaN), when you add sin(x)^2+cos(y)^2 it will be
never > 2. etc.
It can then e.g. help with expansion of other possibly error generating
functions, e.g. where cdce transforms library function calls into inline
fast hw instruction vs. slow libm function for error cases; if we can prove
those error cases will never happen or will always happen, we can create
smaller/faster code.

	Jakub


  reply	other threads:[~2023-04-27  9:21 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-26 14:21 Jakub Jelinek
2023-04-26 16:10 ` Michael Matz
2023-04-27  8:59   ` Jakub Jelinek
2023-04-27 11:05     ` Jakub Jelinek
2023-04-27 14:48       ` Michael Matz
2023-04-27  7:18 ` Richard Biener
2023-04-27  9:20   ` Jakub Jelinek [this message]
2023-04-27 10:34     ` Richard Biener
2023-04-27 11:08       ` [PATCH] v2: " Jakub Jelinek
2023-04-28 11:29         ` Richard Sandiford
2023-04-28 11:39           ` Jakub Jelinek

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=ZEo+dHeI1zb/wbKL@tucnak \
    --to=jakub@redhat.com \
    --cc=aldyh@redhat.com \
    --cc=amacleod@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=joseph@codesourcery.com \
    --cc=rguenther@suse.de \
    --cc=siddhesh@gotplt.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).