From patchwork Sun Jan 23 22:50:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 50374 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 F0137385840E for ; Sun, 23 Jan 2022 22:53:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F0137385840E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1642978403; bh=iVSwBjSifwCHuAhuCfUPwaDHQeJwiwM5DAjpWiLaW0A=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=oPFZdcUGeLwsGu9qiKXzJFSuDBHeKlsCyc6Ap8UwU5qqB9/bgIxaTE/Mf/GUEUz41 PtxevsAWRh9OaONJkM1MkHWn+0fXKfCu/ffq7hCcSqXQtTQaRSK4Q/HWxxbrpY9/RV aXS9MGaRui35pmtz7eChQ1bWGvYv/c9dUuIpR7nc= 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 0FC493858417 for ; Sun, 23 Jan 2022 22:50:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0FC493858417 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-108-ZyJXPieeObyYimQFt4a4FA-1; Sun, 23 Jan 2022 17:50:14 -0500 X-MC-Unique: ZyJXPieeObyYimQFt4a4FA-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 C5FA21006AA3; Sun, 23 Jan 2022 22:50:13 +0000 (UTC) Received: from localhost (unknown [10.33.36.2]) by smtp.corp.redhat.com (Postfix) with ESMTP id 731AB4DC1C; Sun, 23 Jan 2022 22:50:13 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Use fast_float for long double if it uses binary64 format Date: Sun, 23 Jan 2022 22:50:12 +0000 Message-Id: <20220123225012.641915-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.7 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_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Tested powerpc64le-linux, pushed to trunk. We can use the new from_chars implementation when long double and double have the same representation. libstdc++-v3/ChangeLog: * src/c++17/floating_from_chars.cc (USE_STRTOD_FOR_FROM_CHARS): Define macro for case where std::from_chars is implemented in terms of strtod, strtof or strtold. (buffer_resource, valid_fmt, find_end_of_float, pattern) (from_chars_impl, make_result, reserve_string): Do not define unless USE_STRTOD_FOR_FROM_CHARS is defined. (from_chars): Define when at least one of USE_LIB_FAST_FLOAT and USE_STRTOD_FOR_FROM_CHARS is defined, instead of _GLIBCXX_HAVE_USELOCALE. Use fast_float for long double when it is binary64. --- libstdc++-v3/src/c++17/floating_from_chars.cc | 38 ++++++++++++++++--- 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/libstdc++-v3/src/c++17/floating_from_chars.cc b/libstdc++-v3/src/c++17/floating_from_chars.cc index 3158a3a96d3..ba1345db3f2 100644 --- a/libstdc++-v3/src/c++17/floating_from_chars.cc +++ b/libstdc++-v3/src/c++17/floating_from_chars.cc @@ -46,6 +46,13 @@ # include #endif +#if _GLIBCXX_HAVE_USELOCALE +// FIXME: This should be reimplemented so it doesn't use strtod and newlocale. +// That will avoid the need for any memory allocation, meaning that the +// non-conforming errc::not_enough_memory result cannot happen. +# define USE_STRTOD_FOR_FROM_CHARS 1 +#endif + #ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT #ifndef __LONG_DOUBLE_IBM128__ #error "floating_from_chars.cc must be compiled with -mabi=ibmlongdouble" @@ -56,6 +63,9 @@ extern "C" __ieee128 __strtoieee128(const char*, char**); #if _GLIBCXX_FLOAT_IS_IEEE_BINARY32 && _GLIBCXX_DOUBLE_IS_IEEE_BINARY64 # define USE_LIB_FAST_FLOAT 1 +# if __LDBL_MANT_DIG__ == __DBL_MANT_DIG__ +# undef USE_STRTOD_FOR_FROM_CHARS +# endif #endif #if USE_LIB_FAST_FLOAT @@ -66,13 +76,13 @@ namespace } // anon namespace #endif -#if _GLIBCXX_HAVE_USELOCALE namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_VERSION namespace { +#if USE_STRTOD_FOR_FROM_CHARS // A memory resource with a static buffer that can be used for small // allocations. At most one allocation using the freestore can be done // if the static buffer is insufficient. The callers below only require @@ -409,6 +419,7 @@ namespace return true; } #endif +#endif // USE_STRTOD_FOR_FROM_CHARS #if _GLIBCXX_FLOAT_IS_IEEE_BINARY32 && _GLIBCXX_DOUBLE_IS_IEEE_BINARY64 // If the given ASCII character represents a hexit, return that hexit. @@ -771,13 +782,11 @@ namespace return {first, errc{}}; } -#endif +#endif // _GLIBCXX_FLOAT_IS_IEEE_BINARY32 && _GLIBCXX_DOUBLE_IS_IEEE_BINARY64 } // namespace -// FIXME: This should be reimplemented so it doesn't use strtod and newlocale. -// That will avoid the need for any memory allocation, meaning that the -// non-conforming errc::not_enough_memory result cannot happen. +#if USE_LIB_FAST_FLOAT || USE_STRTOD_FOR_FROM_CHARS from_chars_result from_chars(const char* first, const char* last, float& value, @@ -855,6 +864,21 @@ from_chars_result from_chars(const char* first, const char* last, long double& value, chars_format fmt) noexcept { +#if _GLIBCXX_FLOAT_IS_IEEE_BINARY32 && _GLIBCXX_DOUBLE_IS_IEEE_BINARY64 \ + && ! USE_STRTOD_FOR_FROM_CHARS + double dbl_value; + from_chars_result result; + if (fmt == chars_format::hex) + result = __floating_from_chars_hex(first, last, dbl_value); + else + { + static_assert(USE_LIB_FAST_FLOAT); + result = fast_float::from_chars(first, last, dbl_value, fmt); + } + if (result.ec == errc{}) + value = dbl_value; + return result; +#else errc ec = errc::invalid_argument; #if _GLIBCXX_USE_CXX11_ABI buffer_resource mr; @@ -875,6 +899,7 @@ from_chars(const char* first, const char* last, long double& value, fmt = chars_format{}; } return make_result(first, len, fmt, ec); +#endif } #ifdef _GLIBCXX_LONG_DOUBLE_COMPAT @@ -909,6 +934,7 @@ from_chars(const char* first, const char* last, __ieee128& value, } #endif +#endif // USE_LIB_FAST_FLOAT || USE_STRTOD_FOR_FROM_CHARS + _GLIBCXX_END_NAMESPACE_VERSION } // namespace std -#endif // _GLIBCXX_HAVE_USELOCALE