diff mbox series

[5/6] libstdc++: Use fast_float in std::from_chars for binary32/64

Message ID 20220116180652.3694791-5-ppalka@redhat.com
State New
Headers show
Series [1/6,v2] libstdc++: Directly implement hexfloat std::from_chars for binary32/64 | expand

Commit Message

Patrick Palka Jan. 16, 2022, 6:06 p.m. UTC
This makes our std::from_chars implementation use fast_float for parsing
chars_format::scientific/fixed/general parsing into binary32/64 values.
For other floating-point formats 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 | 31 ++++++++++++++++---
 1 file changed, 27 insertions(+), 4 deletions(-)

Comments

Jonathan Wakely Jan. 17, 2022, 10:50 a.m. UTC | #1
On Sun, 16 Jan 2022 at 18:09, Patrick Palka via Libstdc++ <
libstdc++@gcc.gnu.org> wrote:

> This makes our std::from_chars implementation use fast_float for parsing
> chars_format::scientific/fixed/general parsing into binary32/64 values.
> For other floating-point formats 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.
>
>
OK for trunk.
diff mbox series

Patch

diff --git a/libstdc++-v3/src/c++17/floating_from_chars.cc b/libstdc++-v3/src/c++17/floating_from_chars.cc
index b186da9a955..c340eb18ea8 100644
--- a/libstdc++-v3/src/c++17/floating_from_chars.cc
+++ b/libstdc++-v3/src/c++17/floating_from_chars.cc
@@ -35,6 +35,7 @@ 
 #include <string>
 #include <memory_resource>
 #include <cfenv>
+#include <cfloat>
 #include <cmath>
 #include <cstdlib>
 #include <cstring>
@@ -53,6 +54,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)
 {
@@ -775,8 +788,12 @@  from_chars(const char* first, const char* last, float& value,
 #if _GLIBCXX_FLOAT_IS_IEEE_BINARY32 && _GLIBCXX_DOUBLE_IS_IEEE_BINARY64
   if (fmt == chars_format::hex)
     return __floating_from_chars_hex(first, last, value);
-#endif
-
+  else
+    {
+      static_assert(USE_LIB_FAST_FLOAT);
+      return fast_float::from_chars(first, last, value, fmt);
+    }
+#else
   errc ec = errc::invalid_argument;
 #if _GLIBCXX_USE_CXX11_ABI
   buffer_resource mr;
@@ -797,6 +814,7 @@  from_chars(const char* first, const char* last, float& value,
       fmt = chars_format{};
     }
   return make_result(first, len, fmt, ec);
+#endif
 }
 
 from_chars_result
@@ -806,8 +824,12 @@  from_chars(const char* first, const char* last, double& value,
 #if _GLIBCXX_FLOAT_IS_IEEE_BINARY32 && _GLIBCXX_DOUBLE_IS_IEEE_BINARY64
   if (fmt == chars_format::hex)
     return __floating_from_chars_hex(first, last, value);
-#endif
-
+  else
+    {
+      static_assert(USE_LIB_FAST_FLOAT);
+      return fast_float::from_chars(first, last, value, fmt);
+    }
+#else
   errc ec = errc::invalid_argument;
 #if _GLIBCXX_USE_CXX11_ABI
   buffer_resource mr;
@@ -828,6 +850,7 @@  from_chars(const char* first, const char* last, double& value,
       fmt = chars_format{};
     }
   return make_result(first, len, fmt, ec);
+#endif
 }
 
 from_chars_result