From patchwork Fri Oct 1 19:40:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1535499 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=gxT6b/EK; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HLgcb6zcnz9t14 for ; Sat, 2 Oct 2021 05:47:26 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4D73B385781D for ; Fri, 1 Oct 2021 19:47:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4D73B385781D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1633117644; bh=D5YZTcbC8ZCRl/G4deoL9Gs0f6YHM8ibBpMDMNx4czc=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=gxT6b/EKt0dEvjQEi8r5ws0kdZs7DUmaJ/g7r6fSk6d/QDsAkJk87AxdCSakDP+8D RAv+5dMyLNhJ9TKWbxuPJBn4TxgT/JiB9JGih8ckloqAo8Z8o7CrL8jFJri+ob0Baz B8jZK8ofc/47yyQ1ERrGWPNLt0t75fGw8pA/RyeM= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 805F53857C5F for ; Fri, 1 Oct 2021 19:40:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 805F53857C5F Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-282-YQll6BSzNu60Qj8G_Q9eRA-1; Fri, 01 Oct 2021 15:40:24 -0400 X-MC-Unique: YQll6BSzNu60Qj8G_Q9eRA-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B812F84A5E4; Fri, 1 Oct 2021 19:40:23 +0000 (UTC) Received: from localhost (unknown [10.33.36.47]) by smtp.corp.redhat.com (Postfix) with ESMTP id 682CC60936; Fri, 1 Oct 2021 19:40:23 +0000 (UTC) Date: Fri, 1 Oct 2021 20:40:22 +0100 To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Implement std::clamp with std::min and std::max [PR 96733] Message-ID: MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-13.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" 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 libstdc++-v3/ChangeLog: PR libstdc++/96733 * include/bits/stl_algo.h (clamp): Use std::min and std::max. Tested powerpc64le-linux. Committed to trunk. commit 741c7350c08b0884689466867b6c9e711c7b109e Author: Jonathan Wakely Date: Sat Apr 17 22:34:09 2021 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 libstdc++-v3/ChangeLog: PR libstdc++/96733 * include/bits/stl_algo.h (clamp): Use std::min and std::max. diff --git a/libstdc++-v3/include/bits/stl_algo.h b/libstdc++-v3/include/bits/stl_algo.h index 5d12972ce2c..90f3162ff90 100644 --- a/libstdc++-v3/include/bits/stl_algo.h +++ b/libstdc++-v3/include/bits/stl_algo.h @@ -3621,7 +3621,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __gnu_cxx::__ops::__iter_comp_iter(__pred)); } -#if __cplusplus > 201402L +#if __cplusplus >= 201703L #define __cpp_lib_clamp 201603 @@ -3631,14 +3631,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * @param __val A value of arbitrary type. * @param __lo A lower limit of arbitrary type. * @param __hi An upper limit of arbitrary type. - * @return max(__val, __lo) if __val < __hi or min(__val, __hi) otherwise. + * @retval `__lo` if `__val < __lo` + * @retval `__hi` if `__hi < __val` + * @retval `__val` otherwise. + * @pre `_Tp` is LessThanComparable and `(__hi < __lo)` is false. */ template constexpr const _Tp& clamp(const _Tp& __val, const _Tp& __lo, const _Tp& __hi) { __glibcxx_assert(!(__hi < __lo)); - return (__val < __lo) ? __lo : (__hi < __val) ? __hi : __val; + return std::min(std::max(__val, __lo), __hi); } /** @@ -3648,15 +3651,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * @param __lo A lower limit of arbitrary type. * @param __hi An upper limit of arbitrary type. * @param __comp A comparison functor. - * @return max(__val, __lo, __comp) if __comp(__val, __hi) - * or min(__val, __hi, __comp) otherwise. + * @retval `__lo` if `__comp(__val, __lo)` + * @retval `__hi` if `__comp(__hi, __val)` + * @retval `__val` otherwise. + * @pre `__comp(__hi, __lo)` is false. */ template constexpr const _Tp& clamp(const _Tp& __val, const _Tp& __lo, const _Tp& __hi, _Compare __comp) { __glibcxx_assert(!__comp(__hi, __lo)); - return __comp(__val, __lo) ? __lo : __comp(__hi, __val) ? __hi : __val; + return std::min(std::max(__val, __lo, __comp), __hi, __comp); } #endif // C++17 #endif // C++14