public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed 2/3] libstdc++: Define std::stof fallback in terms of std::stod [PR110653]
Date: Wed, 19 Jul 2023 11:05:11 +0100	[thread overview]
Message-ID: <20230719100625.2494437-2-jwakely@redhat.com> (raw)
In-Reply-To: <20230719100625.2494437-1-jwakely@redhat.com>

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(+)

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
-- 
2.41.0


  reply	other threads:[~2023-07-19 10:06 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-19 10:05 [committed 1/3] libstdc++: Check autoconf macros for strtof and strtold [PR110653] Jonathan Wakely
2023-07-19 10:05 ` Jonathan Wakely [this message]
2023-07-19 10:05 ` [committed 3/3] libstdc++: Enable tests for std::stoi etc. unconditionally [PR110653] Jonathan Wakely

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=20230719100625.2494437-2-jwakely@redhat.com \
    --to=jwakely@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).