public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: Patrick Palka <ppalka@redhat.com>
Cc: "libstdc++" <libstdc++@gcc.gnu.org>,
	gcc Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] libstdc++: Ensure that std::from_chars is declared when supported
Date: Wed, 16 Mar 2022 16:54:37 +0000	[thread overview]
Message-ID: <CACb0b4n24-OXEKjp5Q4E4gj-eKqbasZtDzhpm1R5j=LxyDTGug@mail.gmail.com> (raw)
In-Reply-To: <d3a7cc04-b15a-0829-f2df-7935a2513818@idea>

[-- Attachment #1: Type: text/plain, Size: 501 bytes --]

On Mon, 14 Mar 2022 at 14:15, Patrick Palka wrote:
> I think __floating_from_chars_hex should work fine on 16 bit targets,
> so I suppose we could use it in the !USE_LIB_FAST_FLOAT branch as well.

Good point, and even for SIZE_WIDTH >= 32, it might be faster to use
your __floating_from_chars_hex function than to change locale twice
and allocate memory and call strto{f,d,ld}. It certainly avoids the
non-standard errc::out_of_memory result.

So maybe something like the attached semi-tested patch.

[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 3408 bytes --]

commit 57b9e2227c7b14f04f2593ec41e177631b6f3fc2
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Mar 16 16:46:07 2022

    libstdc++: Prefer our own __floating_from_chars_hex if supported
    
    This adjust the floating-point overloads of std::from_chars so that they
    prefer to use __floating_from_chars_hex if possible, instead of using
    strtof/strtod/strtold. This avoids changing the per-thread C locale and
    allocating memory for a null-terminated string when we can use the
    hex-specific function instead.
    
    libstdc++-v3/ChangeLog:
    
            * src/c++17/floating_from_chars.cc (from_chars): Always use
            __floating_from_chars_hex for hex format if possible.

diff --git a/libstdc++-v3/src/c++17/floating_from_chars.cc b/libstdc++-v3/src/c++17/floating_from_chars.cc
index 4aa2483bc28..ce30752ee95 100644
--- a/libstdc++-v3/src/c++17/floating_from_chars.cc
+++ b/libstdc++-v3/src/c++17/floating_from_chars.cc
@@ -821,13 +821,12 @@ from_chars_result
 from_chars(const char* first, const char* last, float& value,
 	   chars_format fmt) noexcept
 {
-#if USE_LIB_FAST_FLOAT
+#if _GLIBCXX_FLOAT_IS_IEEE_BINARY32 && _GLIBCXX_DOUBLE_IS_IEEE_BINARY64
   if (fmt == chars_format::hex)
     return __floating_from_chars_hex(first, last, value);
-  else
-    {
-      return fast_float::from_chars(first, last, value, fmt);
-    }
+#endif
+#if USE_LIB_FAST_FLOAT
+  return fast_float::from_chars(first, last, value, fmt);
 #else
   return from_chars_strtod(first, last, value, fmt);
 #endif
@@ -837,13 +836,12 @@ from_chars_result
 from_chars(const char* first, const char* last, double& value,
 	   chars_format fmt) noexcept
 {
-#if USE_LIB_FAST_FLOAT
+#if _GLIBCXX_FLOAT_IS_IEEE_BINARY32 && _GLIBCXX_DOUBLE_IS_IEEE_BINARY64
   if (fmt == chars_format::hex)
     return __floating_from_chars_hex(first, last, value);
-  else
-    {
-      return fast_float::from_chars(first, last, value, fmt);
-    }
+#endif
+#if USE_LIB_FAST_FLOAT
+  return fast_float::from_chars(first, last, value, fmt);
 #else
   return from_chars_strtod(first, last, value, fmt);
 #endif
@@ -853,18 +851,12 @@ 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 ! USE_STRTOD_FOR_FROM_CHARS || __LDBL_MANT_DIG__ == __DBL_MANT_DIG__
   // 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).
   double dbl_value;
-  from_chars_result result;
-  if (fmt == chars_format::hex)
-    result = __floating_from_chars_hex(first, last, dbl_value);
-  else
-    {
-      result = fast_float::from_chars(first, last, dbl_value, fmt);
-    }
+  from_chars_result result = std::from_chars(first, last, dbl_value, fmt);
   if (result.ec == errc{})
     value = dbl_value;
   return result;
@@ -888,7 +880,8 @@ from_chars_result
 from_chars(const char* first, const char* last, __ieee128& value,
 	   chars_format fmt) noexcept
 {
-  // fast_float doesn't support IEEE binary128 format, but we can use strtold.
+  // fast_float doesn't support IEEE binary128 format, and neither does our
+  // __floating_from_chars_hex, but we can use __strtoieee128 on this target.
   return from_chars_strtod(first, last, value, fmt);
 }
 #endif

      parent reply	other threads:[~2022-03-16 16:54 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-11 18:32 Jonathan Wakely
2022-03-14 14:15 ` Patrick Palka
2022-03-14 21:15   ` Jonathan Wakely
2022-03-15 14:12     ` Patrick Palka
2022-03-16 16:26       ` Jonathan Wakely
2022-03-16 16:54   ` Jonathan Wakely [this message]

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='CACb0b4n24-OXEKjp5Q4E4gj-eKqbasZtDzhpm1R5j=LxyDTGug@mail.gmail.com' \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    --cc=ppalka@redhat.com \
    /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).