From patchwork Wed Aug 9 14:22:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 133283 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp2839265vqr; Wed, 9 Aug 2023 07:31:01 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFXrwZMfnb17YY6aYIkWJwCDaFAIQYRTrUMAxdDA6M72gUxUyK4ldyy0zPPFvTcrdIQbrHC X-Received: by 2002:a17:906:20cb:b0:99c:1ca3:859f with SMTP id c11-20020a17090620cb00b0099c1ca3859fmr2332237ejc.11.1691591461339; Wed, 09 Aug 2023 07:31:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691591461; cv=none; d=google.com; s=arc-20160816; b=TctPl5eMJ3x62hORK9UzTcF4UoMb61CR+6/CY+X0gTUwPXZyuzUwBfXGx5gAMVQy77 riYS84J5EpyBB79QjewzxXbzb8slgyfn/+GlD/s01b70NegASJsTJw8yA+ESV557ohtL PoSRMrK0NSRN2+QKTrZcEdWz3gFXCFEfjzxgVH/H1cbnzXgsbU5PgAZT8GxNRnCHGHmn /VYrX4GEZc8sn4jZ/KM+cxbqElv7u0zWuAkVJHM9yPUTnFboNA1zuXf0zLmaGTB7dA8i +RqONTmLLvxQefEfOzVNQ08OliRWaqwHhcrCmZfpWOjA3jEREMLHXvNAYJp2kK+N1U4d HoPg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=figEv4gzlT7/v/WYsTlTIQg7ShGI5T8IjpmgTDbUdkY=; fh=sC8lnOfyH6gL5FYlMbZ1VDz6qVPMj/3ViLJWKNUCUgQ=; b=xxvmodUt96G2MYg35Z5cPKh+phqiXI8Qo6aEL7GvtHrKP+ycyFNQj/ZmEo4bTLl1SA W+knmEL8cGbxcoZNu5oJZc1139FQIh4qvEdK/MLfFNW6HKswhn6XWx2GuCYm0EMMF/ey yjg7PFokcav5fg3+DxRA9uQP7nTrwB7NpifIdXLyML7Vae7ma26Kil++qX+Iie7KGEj5 ll54LOcNz+/WE5EWvkqM8/+d0v1hzK87vp4Mum6YRl9PFXHCY3dI7Lg7AyxRGVJn1/IR svMzfbOlahljJPzkbLBgyRe4n9wuc3HJjvE5/2LPIMXSngH9yyEgULqymNgaqsx9z9XF qatA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="rq/6V3do"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id bm16-20020a170906c05000b0099bd646e72asi8949817ejb.309.2023.08.09.07.31.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Aug 2023 07:31:01 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="rq/6V3do"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A2CDA38A815C for ; Wed, 9 Aug 2023 14:25:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A2CDA38A815C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691591134; bh=figEv4gzlT7/v/WYsTlTIQg7ShGI5T8IjpmgTDbUdkY=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=rq/6V3dovaKvde1BM0Jtbf7MPqqrFbspyN+WzN8zHx5MJiNCvnJZNyrXknK2g251m 8/EjdyPPxEpwAQKZzOw1C58lmuLcq7u2wAxnUrAcpW0Cz2TplW+JQjPZdB7ziXLmfF EYJ1Hr4zJU3not+azPsngLO4VGfVrpftunTbNX2U= 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 ESMTPS id C47E3385E008 for ; Wed, 9 Aug 2023 14:23:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C47E3385E008 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-696-bJY5PD5GN5i6MGKVsBaC6A-1; Wed, 09 Aug 2023 10:23:03 -0400 X-MC-Unique: bJY5PD5GN5i6MGKVsBaC6A-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0FC1C80C909; Wed, 9 Aug 2023 14:23:02 +0000 (UTC) Received: from localhost (unknown [10.42.28.187]) by smtp.corp.redhat.com (Postfix) with ESMTP id A4CCC40C2079; Wed, 9 Aug 2023 14:23:01 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix constexpr functions to conform to older standards Date: Wed, 9 Aug 2023 15:22:53 +0100 Message-ID: <20230809142300.1015066-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773762208072529416 X-GMAIL-MSGID: 1773762208072529416 Tested x86_64-linux. Pushed to trunk. -- >8 -- Some constexpr functions were inadvertently relying on relaxed constexpr rules from later standards. libstdc++-v3/ChangeLog: * include/bits/chrono.h (duration_cast): Do not use braces around statements for C++11 constexpr rules. * include/bits/stl_algobase.h (__lg): Rewrite as a single statement for C++11 constexpr rules. * include/experimental/bits/fs_path.h (path::string): Use _GLIBCXX17_CONSTEXPR not _GLIBCXX_CONSTEXPR for 'if constexpr'. * include/std/charconv (__to_chars_8): Initialize variable for C++17 constexpr rules. --- libstdc++-v3/include/bits/chrono.h | 6 ++++-- libstdc++-v3/include/bits/stl_algobase.h | 15 ++++++--------- libstdc++-v3/include/experimental/bits/fs_path.h | 2 +- libstdc++-v3/include/std/charconv | 2 +- 4 files changed, 12 insertions(+), 13 deletions(-) diff --git a/libstdc++-v3/include/bits/chrono.h b/libstdc++-v3/include/bits/chrono.h index e9490767c56..b2713d57ef1 100644 --- a/libstdc++-v3/include/bits/chrono.h +++ b/libstdc++-v3/include/bits/chrono.h @@ -276,8 +276,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION 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>; @@ -285,7 +285,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION using __dc = __duration_cast_impl<_ToDur, __cf, __cr, __cf::num == 1, __cf::den == 1>; return __dc::__cast(__d); - } +#if __cpp_inline_variables && __cpp_if_constexpr + } +#endif } /** Trait indicating whether to treat a type as a floating-point type. diff --git a/libstdc++-v3/include/bits/stl_algobase.h b/libstdc++-v3/include/bits/stl_algobase.h index dd95e94f7e9..2037d6c0443 100644 --- a/libstdc++-v3/include/bits/stl_algobase.h +++ b/libstdc++-v3/include/bits/stl_algobase.h @@ -1518,15 +1518,12 @@ _GLIBCXX_END_NAMESPACE_CONTAINER return std::__bit_width(make_unsigned_t<_Tp>(__n)) - 1; #else // Use +__n so it promotes to at least int. - const int __sz = sizeof(+__n); - int __w = __sz * __CHAR_BIT__ - 1; - if (__sz == sizeof(long long)) - __w -= __builtin_clzll(+__n); - else if (__sz == sizeof(long)) - __w -= __builtin_clzl(+__n); - else if (__sz == sizeof(int)) - __w -= __builtin_clz(+__n); - return __w; + return (sizeof(+__n) * __CHAR_BIT__ - 1) + - (sizeof(+__n) == sizeof(long long) + ? __builtin_clzll(+__n) + : (sizeof(+__n) == sizeof(long) + ? __builtin_clzl(+__n) + : __builtin_clz(+__n))); #endif } diff --git a/libstdc++-v3/include/experimental/bits/fs_path.h b/libstdc++-v3/include/experimental/bits/fs_path.h index e0e47188bb9..bf07754fd84 100644 --- a/libstdc++-v3/include/experimental/bits/fs_path.h +++ b/libstdc++-v3/include/experimental/bits/fs_path.h @@ -1042,7 +1042,7 @@ namespace __detail inline std::basic_string<_CharT, _Traits, _Allocator> path::string(const _Allocator& __a) const { - if _GLIBCXX_CONSTEXPR (is_same<_CharT, value_type>::value) + if _GLIBCXX17_CONSTEXPR (is_same<_CharT, value_type>::value) return { _M_pathname.begin(), _M_pathname.end(), __a }; using _WString = basic_string<_CharT, _Traits, _Allocator>; diff --git a/libstdc++-v3/include/std/charconv b/libstdc++-v3/include/std/charconv index b34d672f5bd..cf2b1161014 100644 --- a/libstdc++-v3/include/std/charconv +++ b/libstdc++-v3/include/std/charconv @@ -242,7 +242,7 @@ namespace __detail static_assert(__integer_to_chars_is_unsigned<_Tp>, "implementation bug"); to_chars_result __res; - unsigned __len; + unsigned __len = 0; if _GLIBCXX17_CONSTEXPR (__gnu_cxx::__int_traits<_Tp>::__digits <= 16) {