public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org
Subject: [gcc r14-2636] libstdc++: Define std::stof fallback in terms of std::stod [PR110653]
Date: Wed, 19 Jul 2023 10:04:26 +0000 (GMT)	[thread overview]
Message-ID: <20230719100426.8EF473857739@sourceware.org> (raw)

https://gcc.gnu.org/g:f0b0c21152b337bc9a8ef3a72a15c8dcdff1d847

commit r14-2636-gf0b0c21152b337bc9a8ef3a72a15c8dcdff1d847
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Sat Jul 15 20:41:28 2023 +0100

    libstdc++: Define std::stof fallback in terms of std::stod [PR110653]
    
    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.

Diff:
---
 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

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

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20230719100426.8EF473857739@sourceware.org \
    --to=redi@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    --cc=libstdc++-cvs@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).