From patchwork Sun Nov 13 01:14:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 60503 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 25DF1389EC66 for ; Sun, 13 Nov 2022 01:15:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 25DF1389EC66 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668302134; bh=pV5xXQPP0z+qjUhV2/avre6yVxAdTmu/wgThFc2kJik=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=t3sKr3FvQugkZWbaorSeF2z/pDfoIauyoYXEOYdI3EZVcCGrWWDqHBURLZvnZhrSB iky/zAHyg+tT54qxS11zglhNGw2bOQ4ni51w10WsJ1wg93PT7MpNuc70duxiQTFGob 3Zp+cUpBM71h3P/jmwvnzC3W2tRpi1Ek0jHComQE= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id A7A96388B69E for ; Sun, 13 Nov 2022 01:14:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A7A96388B69E 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-413-o_WJjQeWONqo5T5rNvQT6A-1; Sat, 12 Nov 2022 20:14:48 -0500 X-MC-Unique: o_WJjQeWONqo5T5rNvQT6A-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 24C4285A583; Sun, 13 Nov 2022 01:14:48 +0000 (UTC) Received: from localhost (unknown [10.33.36.199]) by smtp.corp.redhat.com (Postfix) with ESMTP id E07AC1121330; Sun, 13 Nov 2022 01:14:47 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Allow std::to_chars for 128-bit integers in strict mode Date: Sun, 13 Nov 2022 01:14:45 +0000 Message-Id: <20221113011445.920711-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 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.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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Tested x86_64-linux and powerpc64le-linux. Pushed to trunk. -- >8 -- This allows std::format to support __int128 when __STRICT_ANSI__ is defined, which previously failed because __int128 is not an integral type in strict mode. With these changes, std::to_chars still rejects 128-bit integers in strict mode, but std::format will be able to use __detail::__to_chars_i for unsigned __int128. libstdc++-v3/ChangeLog: * include/bits/charconv.h (__integer_to_chars_is_unsigned): New variable template. (__to_chars_len, __to_chars_10_impl): Use variable template in assertions to allow unsigned __int128 in strict mode. * include/std/charconv (__to_chars, __to_chars_16) (__to_chars_10, __to_chars_8, __to_chars_2): Likewise. --- libstdc++-v3/include/bits/charconv.h | 18 ++++++++++++++---- libstdc++-v3/include/std/charconv | 19 +++++++++---------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/libstdc++-v3/include/bits/charconv.h b/libstdc++-v3/include/bits/charconv.h index d04aab77624..103cfcb8177 100644 --- a/libstdc++-v3/include/bits/charconv.h +++ b/libstdc++-v3/include/bits/charconv.h @@ -35,19 +35,28 @@ #if __cplusplus >= 201103L #include +#include namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_VERSION namespace __detail { +#if __cpp_variable_templates + // This accepts 128-bit integers even in strict mode. + template + constexpr bool __integer_to_chars_is_unsigned + = ! __gnu_cxx::__int_traits<_Tp>::__is_signed; +#endif + // Generic implementation for arbitrary bases. template _GLIBCXX14_CONSTEXPR unsigned __to_chars_len(_Tp __value, int __base = 10) noexcept { - static_assert(is_integral<_Tp>::value, "implementation bug"); - static_assert(is_unsigned<_Tp>::value, "implementation bug"); +#if __cpp_variable_templates + static_assert(__integer_to_chars_is_unsigned<_Tp>, "implementation bug"); +#endif unsigned __n = 1; const unsigned __b2 = __base * __base; @@ -71,8 +80,9 @@ namespace __detail _GLIBCXX23_CONSTEXPR void __to_chars_10_impl(char* __first, unsigned __len, _Tp __val) noexcept { - static_assert(is_integral<_Tp>::value, "implementation bug"); - static_assert(is_unsigned<_Tp>::value, "implementation bug"); +#if __cpp_variable_templates + static_assert(__integer_to_chars_is_unsigned<_Tp>, "implementation bug"); +#endif constexpr char __digits[201] = "0001020304050607080910111213141516171819" diff --git a/libstdc++-v3/include/std/charconv b/libstdc++-v3/include/std/charconv index c5ed6fac73b..8f02395172f 100644 --- a/libstdc++-v3/include/std/charconv +++ b/libstdc++-v3/include/std/charconv @@ -88,6 +88,10 @@ namespace __detail using __integer_to_chars_result_type = enable_if_t<__or_<__is_signed_integer<_Tp>, __is_unsigned_integer<_Tp>, +#if defined __SIZEOF_INT128__ && defined __STRICT_ANSI__ + is_same<_Tp, signed __int128>, + is_same<_Tp, unsigned __int128>, +#endif is_same>>::value, to_chars_result>; @@ -126,8 +130,7 @@ namespace __detail constexpr to_chars_result __to_chars(char* __first, char* __last, _Tp __val, int __base) noexcept { - static_assert(is_integral<_Tp>::value, "implementation bug"); - static_assert(is_unsigned<_Tp>::value, "implementation bug"); + static_assert(__integer_to_chars_is_unsigned<_Tp>, "implementation bug"); to_chars_result __res; @@ -167,8 +170,7 @@ namespace __detail constexpr __integer_to_chars_result_type<_Tp> __to_chars_16(char* __first, char* __last, _Tp __val) noexcept { - static_assert(is_integral<_Tp>::value, "implementation bug"); - static_assert(is_unsigned<_Tp>::value, "implementation bug"); + static_assert(__integer_to_chars_is_unsigned<_Tp>, "implementation bug"); to_chars_result __res; @@ -214,8 +216,7 @@ namespace __detail constexpr __integer_to_chars_result_type<_Tp> __to_chars_10(char* __first, char* __last, _Tp __val) noexcept { - static_assert(is_integral<_Tp>::value, "implementation bug"); - static_assert(is_unsigned<_Tp>::value, "implementation bug"); + static_assert(__integer_to_chars_is_unsigned<_Tp>, "implementation bug"); to_chars_result __res; @@ -238,8 +239,7 @@ namespace __detail constexpr __integer_to_chars_result_type<_Tp> __to_chars_8(char* __first, char* __last, _Tp __val) noexcept { - static_assert(is_integral<_Tp>::value, "implementation bug"); - static_assert(is_unsigned<_Tp>::value, "implementation bug"); + static_assert(__integer_to_chars_is_unsigned<_Tp>, "implementation bug"); to_chars_result __res; unsigned __len; @@ -292,8 +292,7 @@ namespace __detail constexpr __integer_to_chars_result_type<_Tp> __to_chars_2(char* __first, char* __last, _Tp __val) noexcept { - static_assert(is_integral<_Tp>::value, "implementation bug"); - static_assert(is_unsigned<_Tp>::value, "implementation bug"); + static_assert(__integer_to_chars_is_unsigned<_Tp>, "implementation bug"); to_chars_result __res;