public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/96733] New: std::clamp for floats and doubles produces worse code than a combo of std::min / std::max
@ 2020-08-21 10:04 soonts at gmail dot com
  2020-08-21 10:42 ` [Bug middle-end/96733] " redi at gcc dot gnu.org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: soonts at gmail dot com @ 2020-08-21 10:04 UTC (permalink / raw)
  To: gcc-bugs

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

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2023-08-12 22:56 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-21 10:04 [Bug c++/96733] New: std::clamp for floats and doubles produces worse code than a combo of std::min / std::max soonts at gmail dot com
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

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).