From patchwork Mon Feb 14 17:27:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1592734 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=eRgg8lqy; 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 4JyB7D1g6qz9s0r for ; Tue, 15 Feb 2022 04:30:00 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8B5E5385802D for ; Mon, 14 Feb 2022 17:29:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8B5E5385802D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1644859797; bh=1t3bYe/sHbIr62koIaSGG3Oa/Xz75NuyOYigsqObkDE=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=eRgg8lqy0rXZpaW1qK5nn1hlcShA6cn1dPJIYrSapUKaNWO34i+Ue2i8HB9NNW7Uc /pqxVKpzS726B6PMfwRSRQ2XLncldV2xOrXJKtbLNmWKmQmq0YJwoMXU50wUlOBX98 xunPRhnMcyz7hsk3GSkQUFHR99YmTN+5pb/ajqiY= 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 83F3B385800F for ; Mon, 14 Feb 2022 17:28:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 83F3B385800F Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-491-ht6iLoz-Pxa75I_8Eg473Q-1; Mon, 14 Feb 2022 12:27:59 -0500 X-MC-Unique: ht6iLoz-Pxa75I_8Eg473Q-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E3D391853028; Mon, 14 Feb 2022 17:27:58 +0000 (UTC) Received: from localhost (unknown [10.33.36.25]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7EBF677452; Mon, 14 Feb 2022 17:27:58 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix stream extraction of IEEE128 long double [PR100912] Date: Mon, 14 Feb 2022 17:27:57 +0000 Message-Id: <20220214172757.3657818-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.5 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, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=unavailable 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" Tested powerpc64le-linux with both IEEE/IBM ABIs. Pushed to trunk. -- >8 -- The std::__convert_from_v helper that formats double and long double values into a char buffer was not being duplicated for the two long double ABIs. This resulted in an ODR violation inside the library, where some callers needed it to use snprintf to format __ibm128 values and other callers needed it to use __snprintfieee128 to format __ieee128 values. The linker discarded one of the definitions, leaving one set of callers using the wrong code. This puts __convert_from_v in the __gnu_cxx_ieee128 inline namespace when long double is __ieee128, so that there are two different definitions of the function. The std::money_put::__do_put overload for __ibm128 values needs a different fix, because that is defined when long double is __ieee128 and so would call the one in the inline namespace. That can be fixed by just inlining the code directly into the function and using an asm alias to call the right version of snprintf for the __ibm128 format. The code to do that can be simpler than __convert_from_v because if we're defining the ALT128_COMPAT symbols we know that we have a recent glibc and so we can assume that uselocale and snprintf are supported. libstdc++-v3/ChangeLog: PR libstdc++/100912 * config/locale/gnu/c_locale.h (__convert_from_v): Use inline namespace for IEEE128 long double mode. * config/os/gnu-linux/ldbl-ieee128-extra.ver: Add new symbol version and export __gnu_cxx_ieee128::__convert_from_v. * include/bits/locale_facets_nonio.tcc (money_put::__do_put): Make __ibm128 overload use snprintf directly * testsuite/util/testsuite_abi.cc: Add new symbol version. Remove stable IEEE128/LDBL versions. --- libstdc++-v3/config/locale/gnu/c_locale.h | 10 ++++++++ .../os/gnu-linux/ldbl-ieee128-extra.ver | 7 +++++- .../include/bits/locale_facets_nonio.tcc | 23 ++++++++----------- libstdc++-v3/testsuite/util/testsuite_abi.cc | 8 +++---- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/libstdc++-v3/config/locale/gnu/c_locale.h b/libstdc++-v3/config/locale/gnu/c_locale.h index 176e022b85c..526b62215e1 100644 --- a/libstdc++-v3/config/locale/gnu/c_locale.h +++ b/libstdc++-v3/config/locale/gnu/c_locale.h @@ -61,6 +61,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION typedef __locale_t __c_locale; +#if defined _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT \ + && defined __LONG_DOUBLE_IEEE128__ +namespace __gnu_cxx_ieee128 { +#endif + // Convert numeric value of type double and long double to string and // return length of string. If vsnprintf is available use it, otherwise // fall back to the unsafe vsprintf which, in general, can be dangerous @@ -108,6 +113,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return __ret; } +#if defined _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT \ + && defined __LONG_DOUBLE_IEEE128__ +} // namespace __gnu_cxx_ieee128 +#endif + _GLIBCXX_END_NAMESPACE_VERSION } // namespace diff --git a/libstdc++-v3/config/os/gnu-linux/ldbl-ieee128-extra.ver b/libstdc++-v3/config/os/gnu-linux/ldbl-ieee128-extra.ver index 9b421b6f1f9..830cb8c40f4 100644 --- a/libstdc++-v3/config/os/gnu-linux/ldbl-ieee128-extra.ver +++ b/libstdc++-v3/config/os/gnu-linux/ldbl-ieee128-extra.ver @@ -2,7 +2,8 @@ GLIBCXX_IEEE128_3.4.29 { - *__gnu_cxx_ieee128*; + _Z*St17__gnu_cxx_ieee1287num_get*; + _Z*St17__gnu_cxx_ieee1287num_put*; _ZNSt14numeric_limitsIu9__ieee128E*; _ZNSirsERu9__ieee128; @@ -45,6 +46,10 @@ GLIBCXX_IEEE128_3.4.29 { } GLIBCXX_3.4.29; +GLIBCXX_IEEE128_3.4.30 { + _ZNSt17__gnu_cxx_ieee12816__convert_from_vERKP15__locale_structPciPKcz; +} GLIBCXX_3.4.30; + CXXABI_IEEE128_1.3.13 { _ZT[IS]u9__ieee128; diff --git a/libstdc++-v3/include/bits/locale_facets_nonio.tcc b/libstdc++-v3/include/bits/locale_facets_nonio.tcc index 64737823374..98442418f51 100644 --- a/libstdc++-v3/include/bits/locale_facets_nonio.tcc +++ b/libstdc++-v3/include/bits/locale_facets_nonio.tcc @@ -635,6 +635,9 @@ _GLIBCXX_BEGIN_NAMESPACE_LDBL_OR_CXX11 #if defined _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT \ && defined __LONG_DOUBLE_IEEE128__ +extern "C" +__typeof__(__builtin_snprintf) __glibcxx_snprintfibm128 __asm__("snprintf"); + template _OutIter money_put<_CharT, _OutIter>:: @@ -643,30 +646,24 @@ _GLIBCXX_BEGIN_NAMESPACE_LDBL_OR_CXX11 { const locale __loc = __io.getloc(); const ctype<_CharT>& __ctype = use_facet >(__loc); -#if _GLIBCXX_USE_C99_STDIO // First try a buffer perhaps big enough. int __cs_size = 64; char* __cs = static_cast(__builtin_alloca(__cs_size)); + const __c_locale __old = __gnu_cxx::__uselocale(_S_get_c_locale()); + // _GLIBCXX_RESOLVE_LIB_DEFECTS // 328. Bad sprintf format modifier in money_put<>::do_put() - int __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size, - "%.*Lf", 0, __units); + int __len = __glibcxx_snprintfibm128(__cs, __cs_size, "%.*Lf", 0, + __units); // If the buffer was not large enough, try again with the correct size. if (__len >= __cs_size) { __cs_size = __len + 1; __cs = static_cast(__builtin_alloca(__cs_size)); - __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size, - "%.*Lf", 0, __units); + __len = __glibcxx_snprintfibm128(__cs, __cs_size, "%.*Lf", 0, + __units); } -#else - // max_exponent10 + 1 for the integer part, + 2 for sign and '\0'. - const int __cs_size = - __gnu_cxx::__numeric_traits::__max_exponent10 + 3; - char* __cs = static_cast(__builtin_alloca(__cs_size)); - int __len = std::__convert_from_v(_S_get_c_locale(), __cs, 0, "%.*Lf", - 0, __units); -#endif + __gnu_cxx::__uselocale(__old); string_type __digits(__len, char_type()); __ctype.widen(__cs, __cs + __len, &__digits[0]); return __intl ? _M_insert(__s, __io, __fill, __digits) diff --git a/libstdc++-v3/testsuite/util/testsuite_abi.cc b/libstdc++-v3/testsuite/util/testsuite_abi.cc index 0ac0ea3fa90..3dee737b2e5 100644 --- a/libstdc++-v3/testsuite/util/testsuite_abi.cc +++ b/libstdc++-v3/testsuite/util/testsuite_abi.cc @@ -213,6 +213,7 @@ check_version(symbol& test, bool added) known_versions.push_back("GLIBCXX_3.4.30"); known_versions.push_back("GLIBCXX_LDBL_3.4.29"); known_versions.push_back("GLIBCXX_IEEE128_3.4.29"); + known_versions.push_back("GLIBCXX_IEEE128_3.4.30"); known_versions.push_back("CXXABI_1.3"); known_versions.push_back("CXXABI_LDBL_1.3"); known_versions.push_back("CXXABI_1.3.1"); @@ -247,11 +248,8 @@ check_version(symbol& test, bool added) // Check that added symbols are added in the latest pre-release version. bool latestp = (test.version_name == "GLIBCXX_3.4.30" - // XXX remove next 3 lines when baselines have been regenerated - // to include {IEEE128,LDBL} symbols: - || test.version_name == "GLIBCXX_LDBL_3.4.29" - || test.version_name == "GLIBCXX_IEEE128_3.4.29" - || test.version_name == "CXXABI_IEEE128_1.3.13" + // XXX remove next line when baselines have been regenerated. + || test.version_name == "GLIBCXX_IEEE128_3.4.30" || test.version_name == "CXXABI_1.3.13" || test.version_name == "CXXABI_FLOAT128" || test.version_name == "CXXABI_TM_1");