[committed,2/3] libstdc++: Define std::stof fallback in terms of std::stod [PR110653]

Message ID 20230719100625.2494437-2-jwakely@redhat.com
State Unresolved
Headers
Series [committed,1/3] libstdc++: Check autoconf macros for strtof and strtold [PR110653] |

Checks

Context Check Description
snail/gcc-patch-check warning Git am fail log

Commit Message

Jonathan Wakely July 19, 2023, 10:05 a.m. UTC
  Tested x86_64-linux. Pushed to trunk.

-- >8 --

For targets without std::strtof we can define std::stof by calling
std::stod and then checking if the result is out of range of float.

libstdc++-v3/ChangeLog:

	PR libstdc++/110653
	* include/bits/basic_string.h [!_GLIBCXX_HAVE_STRTOF] (stof):
	Define in terms of std::stod.
---
 libstdc++-v3/include/bits/basic_string.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
  

Patch

diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h
index 32f5d4421f7..e4cb9846025 100644
--- a/libstdc++-v3/include/bits/basic_string.h
+++ b/libstdc++-v3/include/bits/basic_string.h
@@ -4153,6 +4153,22 @@  _GLIBCXX_BEGIN_NAMESPACE_CXX11
   inline float
   stof(const string& __str, size_t* __idx = 0)
   { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); }
+#else
+  inline float
+  stof(const string& __str, size_t* __idx = 0)
+  {
+    double __d = std::stod(__str, __idx);
+    if (__builtin_isfinite(__d))
+      {
+	double __abs_d = __builtin_fabs(__d);
+	if (__abs_d < __FLT_MIN__ || __abs_d > __FLT_MAX__)
+	  {
+	    errno = ERANGE;
+	    std::__throw_out_of_range("stof");
+	  }
+      }
+    return __d;
+  }
 #endif
 
 #if _GLIBCXX_USE_C99_STDLIB || _GLIBCXX_HAVE_STRTOLD