public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Patrick Palka <ppalka@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: libstdc++@gcc.gnu.org, Patrick Palka <ppalka@redhat.com>
Subject: [PATCH 4/5] libstdc++: Use fast_float in std::from_chars for binary32/64
Date: Mon, 15 Nov 2021 19:25:04 -0500	[thread overview]
Message-ID: <20211116002505.2324582-4-ppalka@redhat.com> (raw)
In-Reply-To: <20211116002505.2324582-1-ppalka@redhat.com>

This makes our std::from_chars implementation use fast_float for parsing
chars_format::scientific/fixed/general parsing into binary32/64 values.
For chars_format::hex (and long double) we still use the fallback
implementation that goes through the strtod family of functions.

libstdc++-v3/ChangeLog:

	* src/c++17/floating_from_chars.cc: (USE_LIB_FAST_FLOAT):
	Conditionally define, and use it to conditionally include
	fast_float.
	(from_chars): Use fast_float for float and double when
	USE_LIB_FAST_FLOAT.
---
 libstdc++-v3/src/c++17/floating_from_chars.cc | 23 +++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/libstdc++-v3/src/c++17/floating_from_chars.cc b/libstdc++-v3/src/c++17/floating_from_chars.cc
index aa074869872..13834f54d38 100644
--- a/libstdc++-v3/src/c++17/floating_from_chars.cc
+++ b/libstdc++-v3/src/c++17/floating_from_chars.cc
@@ -34,6 +34,7 @@
 #include <string>
 #include <memory_resource>
 #include <cfenv>
+#include <cfloat>
 #include <cmath>
 #include <cstdlib>
 #include <cstring>
@@ -52,6 +53,18 @@
 extern "C" __ieee128 __strtoieee128(const char*, char**);
 #endif
 
+#if _GLIBCXX_FLOAT_IS_IEEE_BINARY32 && _GLIBCXX_DOUBLE_IS_IEEE_BINARY64
+# define USE_LIB_FAST_FLOAT 1
+#endif
+
+#if USE_LIB_FAST_FLOAT
+# define FASTFLOAT_DEBUG_ASSERT __glibcxx_assert
+namespace
+{
+# include "fast_float/fast_float.h"
+} // anon namespace
+#endif
+
 #if _GLIBCXX_HAVE_USELOCALE
 namespace std _GLIBCXX_VISIBILITY(default)
 {
@@ -406,6 +419,11 @@ from_chars_result
 from_chars(const char* first, const char* last, float& value,
 	   chars_format fmt) noexcept
 {
+#if USE_LIB_FAST_FLOAT
+  if (fmt != chars_format::hex)
+    return fast_float::from_chars(first, last, value, fmt);
+#endif
+
   errc ec = errc::invalid_argument;
 #if _GLIBCXX_USE_CXX11_ABI
   buffer_resource mr;
@@ -432,6 +450,11 @@ from_chars_result
 from_chars(const char* first, const char* last, double& value,
 	   chars_format fmt) noexcept
 {
+#if USE_LIB_FAST_FLOAT
+  if (fmt != chars_format::hex)
+    return fast_float::from_chars(first, last, value, fmt);
+#endif
+
   errc ec = errc::invalid_argument;
 #if _GLIBCXX_USE_CXX11_ABI
   buffer_resource mr;
-- 
2.34.0


  parent reply	other threads:[~2021-11-16  0:25 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-16  0:25 [PATCH 1/5] libstdc++: Import the fast_float library Patrick Palka
2021-11-16  0:25 ` [PATCH 2/5] libstdc++: Apply modifications to our local copy of fast_float Patrick Palka
2021-11-19 21:47   ` Patrick Palka
2021-11-16  0:25 ` [PATCH 3/5] libstdc++: Adjust fast_float's over/underflow behavior for conformnace Patrick Palka
2021-11-19 21:49   ` Patrick Palka
2021-11-16  0:25 ` Patrick Palka [this message]
2021-11-16  0:25 ` [PATCH 5/5] libstdc++: Import MSVC floating-point std::from_chars testcases Patrick Palka
2021-11-16  7:59 ` [PATCH 1/5] libstdc++: Import the fast_float library Florian Weimer
2021-11-16  9:32   ` Jonathan Wakely
2021-11-16  9:46     ` Florian Weimer
2021-11-16 11:34       ` Jonathan Wakely
2021-11-16 15:30   ` Patrick Palka
2021-11-16 16:18     ` Daniel Krügler

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=20211116002505.2324582-4-ppalka@redhat.com \
    --to=ppalka@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@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).