From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 7FBCC38313A7; Wed, 27 Sep 2023 13:49:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7FBCC38313A7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1695822599; bh=CyAsa2fQcEi9WsGkQbv07jfpZBBbqJ6N5wVSFDDnrvc=; h=From:To:Subject:Date:From; b=yDAv9GLg4eAiZ+LfNmzAZQSDgKYYxpx4Q1IcDGKG0hO3+EHY23LBaBBdhsaZmVJNn NU8iZAAaEqAosllPOuV3yUoR8+M4fp2qToUsjN5njycPNF/J2RAl3oLbcDxtGLJa2j uH3Z6juPE9v4cHW5qSw/LBV/MU1tOiAgrX1Y32xs= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-7913] libstdc++: Fix constexpr functions to conform to older standards X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-13 X-Git-Oldrev: 81185fb76aa691a6d23db70070b79b0cba4bcbe2 X-Git-Newrev: 1e63d6fcaa500734d7c4de558ca020cbed7d500c Message-Id: <20230927134959.7FBCC38313A7@sourceware.org> Date: Wed, 27 Sep 2023 13:49:59 +0000 (GMT) List-Id: https://gcc.gnu.org/g:1e63d6fcaa500734d7c4de558ca020cbed7d500c commit r13-7913-g1e63d6fcaa500734d7c4de558ca020cbed7d500c Author: Jonathan Wakely Date: Wed Aug 9 11:11:31 2023 +0100 libstdc++: Fix constexpr functions to conform to older standards 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. (cherry picked from commit b3a2b307b9deea719fb725a86df43b82176fe459) Diff: --- 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 f4573d27b03..0aeea0ae57e 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 4a6f8195d98..5a8faedf92a 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) {