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