public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/110653] Support std::stoi etc. without C99 APIs
Date: Sat, 15 Jul 2023 19:39:04 +0000	[thread overview]
Message-ID: <bug-110653-4-gYJotNvfN6@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-110653-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110653

--- Comment #12 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to dave.anglin from comment #11)
> Yes, this works.

Great, thanks.

> hppa64-hpux does not have have strtof.  Could std::stof be implemented using
> strtod in this case?

Maybe something like this:

--- a/libstdc++-v3/include/bits/basic_string.h
+++ b/libstdc++-v3/include/bits/basic_string.h
@@ -4153,6 +4153,25 @@ _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__)
+         errno = ERANGE;
+       else if (__abs_d > __FLT_MAX__)
+         {
+           errno = ERANGE;
+           float __f = __builtin_huge_valf();
+           return __d > 0.0 ? __f : -__f;
+         }
+      }
+    return __d;
+  }
 #endif

 #if _GLIBCXX_USE_C99_STDLIB || _GLIBCXX_HAVE_STRTOLD


> I'm thinking a test to check the presence and maybe compliance of these
> routines might be good.

We have some:
testsuite/21_strings/basic_string/numeric_conversions/char/stof.cc
testsuite/21_strings/basic_string/numeric_conversions/char/stold.cc

But they depend on { dg-require-string-conversions "" } which is only true if
we have the full set of them:

    return [check_v3_target_prop_cached et_string_conversions {
        set cond "_GLIBCXX_USE_C99_STDIO && _GLIBCXX_USE_C99_STDLIB"
        set cond "$cond && _GLIBCXX_USE_C99_WCHAR"
        set cond "$cond && !defined _GLIBCXX_HAVE_BROKEN_VSWPRINTF"
        return [v3_check_preprocessor_condition string_conversions $cond]
    }]

  parent reply	other threads:[~2023-07-15 19:39 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-13  9:40 [Bug libstdc++/110653] New: " redi at gcc dot gnu.org
2023-07-13  9:43 ` [Bug libstdc++/110653] " redi at gcc dot gnu.org
2023-07-13 13:46 ` redi at gcc dot gnu.org
2023-07-13 15:14 ` dave.anglin at bell dot net
2023-07-13 16:59 ` cvs-commit at gcc dot gnu.org
2023-07-13 17:09 ` redi at gcc dot gnu.org
2023-07-13 17:57 ` dave.anglin at bell dot net
2023-07-13 18:16 ` dave.anglin at bell dot net
2023-07-14  1:20 ` dave.anglin at bell dot net
2023-07-14  7:39 ` redi at gcc dot gnu.org
2023-07-14  9:58 ` redi at gcc dot gnu.org
2023-07-15 14:52 ` dave.anglin at bell dot net
2023-07-15 19:39 ` redi at gcc dot gnu.org [this message]
2023-07-19 10:04 ` cvs-commit at gcc dot gnu.org
2023-07-19 10:04 ` cvs-commit at gcc dot gnu.org
2023-07-19 10:04 ` cvs-commit at gcc dot gnu.org
2023-07-19 10:10 ` redi at gcc dot gnu.org
2023-07-19 10:10 ` redi at gcc dot gnu.org
2023-07-19 13:48 ` dave.anglin at bell dot net
2023-07-19 13:51 ` redi at gcc dot gnu.org
2023-07-21 18:25 ` dave.anglin at bell dot net
2023-07-21 18:27 ` redi at gcc dot gnu.org
2023-07-24 20:04 ` cvs-commit at gcc dot gnu.org
2023-08-10 19:25 ` danglin at gcc dot gnu.org

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=bug-110653-4-gYJotNvfN6@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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).