From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id AD5623850F02 for ; Thu, 22 Dec 2022 10:15:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AD5623850F02 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1671704150; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=ZnCPplrnpgikc7ZrpRO4E0BmlgPify03uuoLuM5ip14=; b=VHf29NifTWTAVN9ncSHJvKW/+dTnZvR7QQ8mHK4dctDxkOZqoDly3L8mOOBGFdfXMU6OI3 6kFmXlQWhRqhGhR/zmuY8VmfjbLNcSgDE0QCgwrAyyx1KKAe1fLpGrbA7UdDmNnTFR9+xV OyajnQlZjlr+qk1D0CrVlNGY27BAx3E= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-260-fAkfGICoMAuA-_Ip6v2vcw-1; Thu, 22 Dec 2022 05:15:48 -0500 X-MC-Unique: fAkfGICoMAuA-_Ip6v2vcw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 75D99858F0E; Thu, 22 Dec 2022 10:15:48 +0000 (UTC) Received: from localhost (unknown [10.33.36.114]) by smtp.corp.redhat.com (Postfix) with ESMTP id 246FD14152F4; Thu, 22 Dec 2022 10:15:48 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Define and use variable templates in Date: Thu, 22 Dec 2022 10:15:47 +0000 Message-Id: <20221222101547.660304-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.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_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Tested x86_64-linux. Pushed to trunk. -- >8 -- Thi defines a variable template for the internal __is_duration helper trait, defines a new __is_time_point_v variable template (to be used in a subsequent commit), and adds explicit specializations of the standard chrono::treat_as_floating_point trait for common types. A fast path is added to chrono::duration_cast for the no-op case where no conversion is needed. Finally, some SFINAE constraints are simplified by using the __enable_if_t alias, or by using variable templates. libstdc++-v3/ChangeLog: * include/bits/chrono.h (__is_duration_v, __is_time_point_v): New variable templates. (duration_cast): Add simplified definition for noconv case. (treat_as_floating_point_v): Add explicit specializations. (duration::operator%=, floor, ceil, round): Simplify SFINAE constraints. --- libstdc++-v3/include/bits/chrono.h | 62 ++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 17 deletions(-) diff --git a/libstdc++-v3/include/bits/chrono.h b/libstdc++-v3/include/bits/chrono.h index 22c0be3fbe6..56751d1c3a0 100644 --- a/libstdc++-v3/include/bits/chrono.h +++ b/libstdc++-v3/include/bits/chrono.h @@ -244,6 +244,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION using __disable_if_is_duration = typename enable_if::value, _Tp>::type; +#if __cpp_variable_templates + template + inline constexpr bool __is_duration_v = false; + template + inline constexpr bool __is_duration_v> = true; + template + inline constexpr bool __is_time_point_v = false; + template + inline constexpr bool __is_time_point_v> = true; +#endif + /// @endcond /** Convert a `duration` to type `ToDur`. @@ -261,13 +272,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION constexpr __enable_if_is_duration<_ToDur> duration_cast(const duration<_Rep, _Period>& __d) { - typedef typename _ToDur::period __to_period; - typedef typename _ToDur::rep __to_rep; - typedef ratio_divide<_Period, __to_period> __cf; - typedef typename common_type<__to_rep, _Rep, intmax_t>::type __cr; - typedef __duration_cast_impl<_ToDur, __cf, __cr, - __cf::num == 1, __cf::den == 1> __dc; - return __dc::__cast(__d); +#if __cpp_inline_variables && __cpp_if_constexpr + if constexpr (is_same_v<_ToDur, duration<_Rep, _Period>>) + return __d; + else +#endif + { + using __to_period = typename _ToDur::period; + using __to_rep = typename _ToDur::rep; + using __cf = ratio_divide<_Period, __to_period>; + using __cr = typename common_type<__to_rep, _Rep, intmax_t>::type; + using __dc = __duration_cast_impl<_ToDur, __cf, __cr, + __cf::num == 1, __cf::den == 1>; + return __dc::__cast(__d); + } } /** Trait indicating whether to treat a type as a floating-point type. @@ -290,6 +308,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template inline constexpr bool treat_as_floating_point_v = treat_as_floating_point<_Rep>::value; + + template<> + inline constexpr bool treat_as_floating_point_v = false; + template<> + inline constexpr bool treat_as_floating_point_v = false; + template<> + inline constexpr bool treat_as_floating_point_v = false; + template<> + inline constexpr bool treat_as_floating_point_v = true; + template<> + inline constexpr bool treat_as_floating_point_v = true; + template<> + inline constexpr bool treat_as_floating_point_v = true; #endif // C++17 #if __cplusplus > 201703L @@ -632,8 +663,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // DR 934. template _GLIBCXX17_CONSTEXPR - typename enable_if::value, - duration&>::type + __enable_if_t::value, duration&> operator%=(const rep& __rhs) { __r %= __rhs; @@ -642,8 +672,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template _GLIBCXX17_CONSTEXPR - typename enable_if::value, - duration&>::type + __enable_if_t::value, duration&> operator%=(const duration& __d) { __r %= __d.count(); @@ -1019,7 +1048,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION */ template [[nodiscard]] constexpr - enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>> + enable_if_t<__is_duration_v<_ToDur>, time_point<_Clock, _ToDur>> floor(const time_point<_Clock, _Dur>& __tp) { return time_point<_Clock, _ToDur>{ @@ -1040,7 +1069,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION */ template [[nodiscard]] constexpr - enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>> + enable_if_t<__is_duration_v<_ToDur>, time_point<_Clock, _ToDur>> ceil(const time_point<_Clock, _Dur>& __tp) { return time_point<_Clock, _ToDur>{ @@ -1062,10 +1091,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION */ template [[nodiscard]] constexpr - enable_if_t< - __and_<__is_duration<_ToDur>, - __not_>>::value, - time_point<_Clock, _ToDur>> + enable_if_t<__is_duration_v<_ToDur> + && !treat_as_floating_point_v, + time_point<_Clock, _ToDur>> round(const time_point<_Clock, _Dur>& __tp) { return time_point<_Clock, _ToDur>{ -- 2.38.1