public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org
Subject: [gcc r14-1431] libstdc++: Fix preprocessor conditions for std::from_chars [PR109921]
Date: Wed, 31 May 2023 12:21:35 +0000 (GMT)	[thread overview]
Message-ID: <20230531122135.8D5EE3858020@sourceware.org> (raw)

https://gcc.gnu.org/g:7037e7b6e4ac41e536bdb9a2efcf546ec4d77166

commit r14-1431-g7037e7b6e4ac41e536bdb9a2efcf546ec4d77166
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu May 25 10:32:33 2023 +0100

    libstdc++: Fix preprocessor conditions for std::from_chars [PR109921]
    
    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.

Diff:
---
 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

                 reply	other threads:[~2023-05-31 12:21 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230531122135.8D5EE3858020@sourceware.org \
    --to=redi@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    --cc=libstdc++-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).