public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "ejtttje at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/50724] cmath's floating-point classification implementations inconsistent with math.h
Date: Tue, 18 Oct 2011 21:09:00 -0000	[thread overview]
Message-ID: <bug-50724-4-GRuY0EMpTL@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-50724-4@http.gcc.gnu.org/bugzilla/>

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50724

--- Comment #35 from Ethan Tira-Thompson <ejtttje at gmail dot com> 2011-10-18 21:09:07 UTC ---
Thanks all for the info!

I should have realized there was literally an attribute/pragma called
'optimize' (duh), and it's already in the docs... for some reason I had gotten
the impression this was a brand new development (i.e. hadn't been released
yet), I should've checked.

FYI, the optimize attribute seems to work for fast-math, but interestingly, the
pragma doesn't.  I've created a new bug 50782 for this.

> change RTL optimizers to not do comparison code folding based on
> flag_finite_math_only (so you can expand isnan to !(x==x) even with
> -ffinite-math-only)

What would you say to a solution which allows finite math to optimize the
comparisons, but at the cost of explicit classification being full function
calls?  And of course when finite math is not in effect, everything is inlined
as normal.

This would allow core computations to be fully optimized and only explicit
classification calls would be affected.  This presumes the classification calls
are less common in order to be a good tradeoff, my intuition is that this is
the case.  It also allows those of you who want to optimize-away nan checks to
continue to do so, just use a (x!=x), and as a bonus this approach will also
work consistently between gcc variants.  What do you think?

This is also easy to implement without touching the compiler source, just apply
attributes in libstdc++ to keep the classification calls no-finite-math-only,
for example the isnan implementation would be:

#if defined(__FINITE_MATH_ONLY__) && __FINITE_MATH_ONLY__
    // apply attributes to retain classification functionality
  template<typename _Tp>
    inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
                       int>::__type
    isnan(_Tp __f) __attribute__ ((optimize ("no-finite-math-only"),noinline));
#endif

  template<typename _Tp>
    inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
                       int>::__type
    isnan(_Tp __f)
    {
        return std::__is_integer<_Tp>::__value ? false : __builtin_isnan(__f);
    }

(and obviously similar for fpclassify, isfinite, isinf, and isnormal)

I tweaked isnan to short circuit on __is_integer instead of a roundabout
promotion to double.  If you don't like that it can certainly go back to the
promotion style.

This works (tested with 4.6.1) because __builtin_isnan is expanded in the isnan
context, where the optimize attribute is in effect.  If you think that
expansion behavior is subject to change (see also bug 50782), we could bump
this up to apply to the builtin itself instead of the user function...?

As written, this relies on the noinline attribute trumping the inline keyword. 
We rewrite this to avoid that conflict if needed.  (Is the inline keyword
superfluous here anyway? Testing it appears so.)


  parent reply	other threads:[~2011-10-18 21:09 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-10-14  3:37 [Bug libstdc++/50724] New: isnan broken by -ffinite-math-only in g++ ejt at andrew dot cmu.edu
2011-10-14  9:17 ` [Bug libstdc++/50724] " rguenth at gcc dot gnu.org
2011-10-14 14:27 ` ejtttje at gmail dot com
2011-10-14 14:31 ` rguenth at gcc dot gnu.org
2011-10-14 14:49 ` redi at gcc dot gnu.org
2011-10-14 14:57 ` paolo.carlini at oracle dot com
2011-10-14 15:08 ` paolo.carlini at oracle dot com
2011-10-14 15:37 ` [Bug middle-end/50724] " matz at gcc dot gnu.org
2011-10-14 16:03 ` pinskia at gcc dot gnu.org
2011-10-14 20:08 ` ejtttje at gmail dot com
2011-10-14 21:13 ` marc.glisse at normalesup dot org
2011-10-14 22:08 ` ejtttje at gmail dot com
2011-10-15  8:52 ` rguenth at gcc dot gnu.org
2011-10-15  9:06 ` marc.glisse at normalesup dot org
2011-10-15 14:01 ` ejtttje at gmail dot com
2011-10-15 18:00 ` pinskia at gcc dot gnu.org
2011-10-16 10:00 ` rguenth at gcc dot gnu.org
2011-10-17  4:12 ` ejtttje at gmail dot com
2011-10-17  7:14 ` rguenth at gcc dot gnu.org
2011-10-17 15:31 ` [Bug libstdc++/50724] cmath's floating-point classification implementations inconsistent with math.h ejtttje at gmail dot com
2011-10-17 15:41 ` paolo.carlini at oracle dot com
2011-10-17 15:45 ` [Bug middle-end/50724] " paolo.carlini at oracle dot com
2011-10-17 16:47 ` ejtttje at gmail dot com
2011-10-17 16:50 ` paolo.carlini at oracle dot com
2011-10-17 18:38 ` ejtttje at gmail dot com
2011-10-17 18:54 ` paolo.carlini at oracle dot com
2011-10-17 19:22 ` ejtttje at gmail dot com
2011-10-17 19:26 ` pinskia at gcc dot gnu.org
2011-10-17 20:21 ` ejtttje at gmail dot com
2011-10-17 21:05 ` redi at gcc dot gnu.org
2011-10-17 21:22 ` redi at gcc dot gnu.org
2011-10-18  0:34 ` ejtttje at gmail dot com
2011-10-18  1:33 ` matz at gcc dot gnu.org
2011-10-18  9:42 ` redi at gcc dot gnu.org
2011-10-18 10:04 ` rguenth at gcc dot gnu.org
2011-10-18 21:09 ` ejtttje at gmail dot com [this message]
2021-09-21  1:12 ` egallager 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-50724-4-GRuY0EMpTL@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).