From patchwork Wed May 31 12:22:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 101411 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2841261vqr; Wed, 31 May 2023 05:30:45 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6J8nNSf5UPkzTjq378zXXFk5PQQdiujrUg+rC+s1C8Vz67tp3C1a6g/ntpgUf0MO08X30L X-Received: by 2002:a2e:9042:0:b0:2ac:7505:de2d with SMTP id n2-20020a2e9042000000b002ac7505de2dmr2670152ljg.23.1685536245611; Wed, 31 May 2023 05:30:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685536245; cv=none; d=google.com; s=arc-20160816; b=utthOUO/M4IcppOiB0pOyCXkF4qQOlHzRGtOwAyfPTapSiijDA2qBdumgjF0OpUMS1 pvOUPUQ7FgtWKxBFGuT+ntC/nNA4tgmEGDq6J6Ze4QDPe3r74BFfvi1lqMQx8lyWoO0+ wSctCjeGDxd1rO8vXyoRnQ8VBg9g/AO/GNhhtHdr66PhOIZi/wFYNiNMzvU6yEvbTu1A TiQaflwrPE5jbZcjfTYt+zwPxEKIFM2eS9Q70sLWODRXc0okkRpGBZxO4af4b5Yo7YSl yEem8KdsqyBHGpzcVyxRoRyfceW/ha5SBUkoSSRHrUx0EpTAKxZrCbCPdG02zlE7aPTL Jf6A== 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=77nKU8cDeFJFpMlE6GtHSLOc1Fs54FOsLbVQKeB374A=; b=b1JAxSTXR6HIOhKQD/GwU0NAXnOEJ4K1liYhV8pohTz3IA1QxSc/zHAA63wmDTFdZx t37MImhRvtzj0oglDyM5DPjfx67WJtYrXLpipUqi6yYP8VrWLhIWJjeSgUv7sJHWlrf/ vi5zDqSaVWwE+Hkdet+urgKuKof+iIi2OuuLRlqKBGyfNuG1Q8qbltSx8mNWXfzS6iEQ o8L7/Tz5uz6Jioz5Qfw387Wat5+l9T46ZI7iTDRqo/3xT2t7KjQs4cSpfgKk8KJtdXel 6J2OYbAR32TEchyMDujiO7TBmI5hYFHGqnm3dspbQ8dPH/fFAfhxjuEq+AmQaLiyg6n1 O5Mw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Tv5vDiJU; 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 sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id x6-20020a170906804600b0096b5c347a64si2270050ejw.246.2023.05.31.05.30.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 May 2023 05:30:45 -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=Tv5vDiJU; 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 5E0C9385C6D5 for ; Wed, 31 May 2023 12:25:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5E0C9385C6D5 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685535957; bh=77nKU8cDeFJFpMlE6GtHSLOc1Fs54FOsLbVQKeB374A=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Tv5vDiJU4GW432o/whYQtwT/7eYduEqV4KXKM84vrwYuWR1V3oD30E54Fiijy6x0R HvznLz+17/0SQWtmDoEsnSpezPLE27axPn6O7LKJ8LyYvPn7oA3yeI786DsGOfANri XWKDC1ctZloGBtB6dFgNFTVfKIubM9JUGP5N6u+s= 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 4A3C83858024 for ; Wed, 31 May 2023 12:24:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4A3C83858024 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-445-m0izTC8OMw2cPby2BaVwYw-1; Wed, 31 May 2023 08:22:23 -0400 X-MC-Unique: m0izTC8OMw2cPby2BaVwYw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 82A4F101A59E; Wed, 31 May 2023 12:22:23 +0000 (UTC) Received: from localhost (unknown [10.42.28.139]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3DE6C40C6EC4; Wed, 31 May 2023 12:22:23 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix preprocessor conditions for std::from_chars [PR109921] Date: Wed, 31 May 2023 13:22:22 +0100 Message-Id: <20230531122222.4116868-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767412854358803892?= X-GMAIL-MSGID: =?utf-8?q?1767412854358803892?= Tested powerpc64le-linux. Pushed to trunk. -- >8 -- We use the from_chars_strtod function with __strtof128 to read a _Float128 value, but from_chars_strtod is not defined unless uselocale is available. This can lead to compilation failures for some targets, because we try to define the _Flaot128 overload in terms of a non-existing from_chars_strtod function. Only try to use __strtof128 if uselocale is available, otherwise fallback to the long double overload of std::from_chars (which might fallback to the double overload, which should use fast_float). This ensures we always define the full set of overloads, even if they are not always accurate for all values of the wider types. libstdc++-v3/ChangeLog: PR libstdc++/109921 * src/c++17/floating_from_chars.cc (USE_STRTOF128_FOR_FROM_CHARS): Only define when USE_STRTOD_FOR_FROM_CHARS is also defined. (USE_STRTOD_FOR_FROM_CHARS): Do not undefine when long double is binary64. (from_chars(const char*, const char*, double&, chars_format)): Check __LDBL_MANT_DIG__ == __DBL_MANT_DIG__ here. (from_chars(const char*, const char*, _Float128&, chars_format)) Only use from_chars_strtod when USE_STRTOD_FOR_FROM_CHARS is defined, otherwise parse a long double and convert to _Float128. --- libstdc++-v3/src/c++17/floating_from_chars.cc | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/libstdc++-v3/src/c++17/floating_from_chars.cc b/libstdc++-v3/src/c++17/floating_from_chars.cc index ebd428d5be3..eea878072b0 100644 --- a/libstdc++-v3/src/c++17/floating_from_chars.cc +++ b/libstdc++-v3/src/c++17/floating_from_chars.cc @@ -64,7 +64,7 @@ // strtold for __ieee128 extern "C" __ieee128 __strtoieee128(const char*, char**); #elif __FLT128_MANT_DIG__ == 113 && __LDBL_MANT_DIG__ != 113 \ - && defined(__GLIBC_PREREQ) + && defined(__GLIBC_PREREQ) && defined(USE_STRTOD_FOR_FROM_CHARS) #define USE_STRTOF128_FOR_FROM_CHARS 1 extern "C" _Float128 __strtof128(const char*, char**) __asm ("strtof128") @@ -77,10 +77,6 @@ extern "C" _Float128 __strtof128(const char*, char**) #if _GLIBCXX_FLOAT_IS_IEEE_BINARY32 && _GLIBCXX_DOUBLE_IS_IEEE_BINARY64 \ && __SIZE_WIDTH__ >= 32 # define USE_LIB_FAST_FLOAT 1 -# if __LDBL_MANT_DIG__ == __DBL_MANT_DIG__ -// No need to use strtold. -# undef USE_STRTOD_FOR_FROM_CHARS -# endif #endif #if USE_LIB_FAST_FLOAT @@ -1261,7 +1257,7 @@ from_chars_result from_chars(const char* first, const char* last, long double& value, chars_format fmt) noexcept { -#if ! USE_STRTOD_FOR_FROM_CHARS +#if __LDBL_MANT_DIG__ == __DBL_MANT_DIG__ || !defined USE_STRTOD_FOR_FROM_CHARS // Either long double is the same as double, or we can't use strtold. // In the latter case, this might give an incorrect result (e.g. values // out of range of double give an error, even if they fit in long double). @@ -1329,13 +1325,23 @@ _ZSt10from_charsPKcS0_RDF128_St12chars_format(const char* first, __ieee128& value, chars_format fmt) noexcept __attribute__((alias ("_ZSt10from_charsPKcS0_Ru9__ieee128St12chars_format"))); -#elif defined(USE_STRTOF128_FOR_FROM_CHARS) +#else from_chars_result from_chars(const char* first, const char* last, _Float128& value, chars_format fmt) noexcept { +#ifdef USE_STRTOF128_FOR_FROM_CHARS // fast_float doesn't support IEEE binary128 format, but we can use strtold. return from_chars_strtod(first, last, value, fmt); +#else + // Read a long double. This might give an incorrect result (e.g. values + // out of range of long double give an error, even if they fit in _Float128). + long double ldbl_val; + auto res = std::from_chars(first, last, ldbl_val, fmt); + if (res.ec == errc{}) + value = ldbl_val; + return res; +#endif } #endif