public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "soonts at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/96733] New: std::clamp for floats and doubles produces worse code than a combo of std::min / std::max
Date: Fri, 21 Aug 2020 10:04:58 +0000	[thread overview]
Message-ID: <bug-96733-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 96733
           Summary: std::clamp for floats and doubles produces worse code
                    than a combo of std::min / std::max
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: soonts at gmail dot com
  Target Milestone: ---

C++/17 introduced std::clamp in the standard library.

Very useful, but the code gcc generates for it is not optimal for doubles and
floats. Source code:

// This version is good in gcc, not so on vc++
double clamp_minmax( double x, double i, double ax )
{
    return std::min( std::max( x, i ), ax );
}

// This version should be identical but is not.
// gcc compiles to scalar comparisons
// vc++ code is outright horrible, compiles into RAM access.
double clamp_builtin( double x, double i, double ax )
{
    return std::clamp( x, i, ax );
} 

Demo there: https://godbolt.org/z/Gnvc1s

             reply	other threads:[~2020-08-21 10:04 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-21 10:04 soonts at gmail dot com [this message]
2020-08-21 10:42 ` [Bug middle-end/96733] " redi at gcc dot gnu.org
2020-08-23  2:52 ` pinskia at gcc dot gnu.org
2020-08-24 15:36 ` jakub at gcc dot gnu.org
2020-08-24 17:44 ` redi at gcc dot gnu.org
2020-08-24 17:49 ` jakub at gcc dot gnu.org
2020-08-24 19:17 ` [Bug libstdc++/96733] " redi at gcc dot gnu.org
2021-04-12  8:06 ` rguenth at gcc dot gnu.org
2021-05-12  9:55 ` redi at gcc dot gnu.org
2021-10-01 19:38 ` cvs-commit at gcc dot gnu.org
2021-10-01 19:46 ` redi at gcc dot gnu.org
2023-08-12 22:56 ` cosiekvfj at o2 dot pl

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-96733-4@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).