From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id BC4893858D35; Fri, 21 Aug 2020 10:04:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BC4893858D35 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1598004298; bh=inqzrwDFAAthY7hr2rc/lUC6hsGtQTRmvWi/U7JFJL4=; h=From:To:Subject:Date:From; b=iCeInTvQNUUQgZujwJKI7UenfOHG6ExAOuk7QKNtrKqPyIqD3l5lTxEIrx3jrL1I3 QKJoYw5PRaMPdAe/6TxbVnvOyLvNodSqqzcmlWpIgnIQSIQrdspoiiaa9ZR7eNxKVy Vfi+B7geExXYUvHtNlHwcuEo5F5u47/CrIm1MSls= From: "soonts at gmail dot com" 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 10.2.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: normal X-Bugzilla-Who: soonts at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status keywords bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 21 Aug 2020 10:04:58 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D96733 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 a= nd 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 ); }=20 Demo there: https://godbolt.org/z/Gnvc1s=