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
* [Bug middle-end/96733] std::clamp for floats and doubles produces worse code than a combo of std::min / std::max
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 ` redi at gcc dot gnu.org
2020-08-23 2:52 ` pinskia at gcc dot gnu.org
` (9 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: redi at gcc dot gnu.org @ 2020-08-21 10:42 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96733
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Component|c++ |middle-end
Last reconfirmed| |2020-08-21
Status|UNCONFIRMED |NEW
Ever confirmed|0 |1
--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Without the library dependency:
namespace std
{
template<typename _Tp>
constexpr const _Tp&
min(const _Tp& __a, const _Tp& __b)
{
//return __b < __a ? __b : __a;
if (__b < __a)
return __b;
return __a;
}
template<typename _Tp>
constexpr const _Tp&
max(const _Tp& __a, const _Tp& __b)
{
//return __a < __b ? __b : __a;
if (__a < __b)
return __b;
return __a;
}
template<typename _Tp>
constexpr const _Tp&
clamp(const _Tp& __val, const _Tp& __lo, const _Tp& __hi)
{
return (__val < __lo) ? __lo : (__hi < __val) ? __hi : __val;
}
}
// 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 );
}
^ permalink raw reply [flat|nested] 12+ messages in thread
* [Bug middle-end/96733] std::clamp for floats and doubles produces worse code than a combo of std::min / std::max
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
` (8 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2020-08-23 2:52 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96733
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Severity|normal |enhancement
^ permalink raw reply [flat|nested] 12+ messages in thread
* [Bug middle-end/96733] std::clamp for floats and doubles produces worse code than a combo of std::min / std::max
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
` (7 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-08-24 15:36 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96733
Jakub Jelinek <jakub at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |jakub at gcc dot gnu.org,
| |redi at gcc dot gnu.org
--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
I'm afraid there is nothing that the compiler can do to optimize the
clamp_builtin into similar code to clamp_minmax, because the optimizer doesn't
know an important restriction on std::clamp
- "The behavior is undefined if the value of lo is greater than hi".
In the IL this isn't expressed in any way, and when the compiler sees
(__val < __lo) ? __lo : (__hi < __val) ? __hi : __val
it can only optimize (for -Ofast) the (__hi < __val) ? __hi : __val part into
min (__hi, __val), but as it needs to assume e.g. __lo could be 10 and __hi 5,
it can't emit max.
^ permalink raw reply [flat|nested] 12+ messages in thread
* [Bug middle-end/96733] std::clamp for floats and doubles produces worse code than a combo of std::min / std::max
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
` (2 preceding siblings ...)
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
` (6 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: redi at gcc dot gnu.org @ 2020-08-24 17:44 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96733
--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Shouldn't this help in the library then?
if (__hi < __lo)
__builtin_unreachable();
^ permalink raw reply [flat|nested] 12+ messages in thread
* [Bug middle-end/96733] std::clamp for floats and doubles produces worse code than a combo of std::min / std::max
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
` (3 preceding siblings ...)
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
` (5 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-08-24 17:49 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96733
--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #3)
> Shouldn't this help in the library then?
>
> if (__hi < __lo)
> __builtin_unreachable();
For integers, I guess it could if Andrew's/Aldy's stuff gets in and can be used
for that, for floating point types, we don't really have any value range
handling, and without -ffast-math it might be even impossible to optimize away.
Can't std::clamp be implemented using std::min/std::max instead, or at least as
code that can be optimized into that?
^ permalink raw reply [flat|nested] 12+ messages in thread
* [Bug libstdc++/96733] std::clamp for floats and doubles produces worse code than a combo of std::min / std::max
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
` (4 preceding siblings ...)
2020-08-24 17:49 ` jakub at gcc dot gnu.org
@ 2020-08-24 19:17 ` redi at gcc dot gnu.org
2021-04-12 8:06 ` rguenth at gcc dot gnu.org
` (4 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: redi at gcc dot gnu.org @ 2020-08-24 19:17 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96733
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Component|middle-end |libstdc++
--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Yes, it looks like we might want to do that then. We can have different code
for different types if needed.
Reassigning to libstdc++.
^ permalink raw reply [flat|nested] 12+ messages in thread
* [Bug libstdc++/96733] std::clamp for floats and doubles produces worse code than a combo of std::min / std::max
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
` (5 preceding siblings ...)
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
` (3 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-04-12 8:06 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96733
Richard Biener <rguenth at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |vittorio.romeo at outlook dot com
--- Comment #6 from Richard Biener <rguenth at gcc dot gnu.org> ---
*** Bug 100008 has been marked as a duplicate of this bug. ***
^ permalink raw reply [flat|nested] 12+ messages in thread
* [Bug libstdc++/96733] std::clamp for floats and doubles produces worse code than a combo of std::min / std::max
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
` (6 preceding siblings ...)
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
` (2 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: redi at gcc dot gnu.org @ 2021-05-12 9:55 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96733
--- Comment #7 from Jonathan Wakely <redi at gcc dot gnu.org> ---
In my GCC fork I just switched clamp to use min and max unconditionally:
https://gitlab.com/jonathan-wakely/gcc/-/commit/ec5ecc2e142fbb7fc532a5ef9527b61675bcbfc4
^ permalink raw reply [flat|nested] 12+ messages in thread
* [Bug libstdc++/96733] std::clamp for floats and doubles produces worse code than a combo of std::min / std::max
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
` (7 preceding siblings ...)
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
10 siblings, 0 replies; 12+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-10-01 19:38 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96733
--- Comment #8 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:
https://gcc.gnu.org/g:741c7350c08b0884689466867b6c9e711c7b109e
commit r12-4061-g741c7350c08b0884689466867b6c9e711c7b109e
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Sat Apr 17 22:34:09 2021 +0100
libstdc++: Implement std::clamp with std::min and std::max [PR 96733]
The compiler doesn't know about the precondition of std::clamp that
(hi < lo) is false, and so can't optimize as well as we'd like. By using
std::min and std::max we help the compiler.
Signed-off-by: Jonathan Wakely <jwakely@redhat.com>
libstdc++-v3/ChangeLog:
PR libstdc++/96733
* include/bits/stl_algo.h (clamp): Use std::min and std::max.
^ permalink raw reply [flat|nested] 12+ messages in thread
* [Bug libstdc++/96733] std::clamp for floats and doubles produces worse code than a combo of std::min / std::max
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
` (8 preceding siblings ...)
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
10 siblings, 0 replies; 12+ messages in thread
From: redi at gcc dot gnu.org @ 2021-10-01 19:46 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96733
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |RESOLVED
Resolution|--- |FIXED
--- Comment #9 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Should be fixed for GCC 12.
^ permalink raw reply [flat|nested] 12+ messages in thread
* [Bug libstdc++/96733] std::clamp for floats and doubles produces worse code than a combo of std::min / std::max
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
` (9 preceding siblings ...)
2021-10-01 19:46 ` redi at gcc dot gnu.org
@ 2023-08-12 22:56 ` cosiekvfj at o2 dot pl
10 siblings, 0 replies; 12+ messages in thread
From: cosiekvfj at o2 dot pl @ 2023-08-12 22:56 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96733
Kacper <cosiekvfj at o2 dot pl> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |cosiekvfj at o2 dot pl
--- Comment #10 from Kacper <cosiekvfj at o2 dot pl> ---
Still not fixed?
https://godbolt.org/z/1ehf9EsEa
^ 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).