From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id A4D4A3858D3C; Sat, 15 Jul 2023 19:39:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A4D4A3858D3C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1689449944; bh=h5xF7yAEb7XpOo7i9Qy/UEz7nNHiMpRmF20THACTgsU=; h=From:To:Subject:Date:In-Reply-To:References:From; b=O59Jxgf0oiM0J+DyVYsxFGyVBzcKqyx3xSRGY9ur71Lyo3w5GTyot5wcreA+ZSKTa LKBFmQnJuiWQKj47EnZDeOFWKMKoD90l46iu9k7FyNHLJER4ipeGkog8TfloG6W4iS gsXb/VgRqfJ29Iw0m68U44BKxXY3EU3lYfezmido= From: "redi at gcc dot 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: redi at gcc dot gnu.org X-Bugzilla-Status: ASSIGNED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: redi at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D110653 --- Comment #12 from Jonathan Wakely --- (In reply to dave.anglin from comment #11) > Yes, this works. Great, thanks. > hppa64-hpux does not have have strtof.=C2=A0 Could std::stof be implement= ed 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 =3D 0) { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); } +#else + inline float + stof(const string& __str, size_t* __idx =3D 0) + { + double __d =3D std::stod(__str, __idx); + if (__builtin_isfinite(__d)) + { + double __abs_d =3D __builtin_fabs(__d); + if (__abs_d < __FLT_MIN__) + errno =3D ERANGE; + else if (__abs_d > __FLT_MAX__) + { + errno =3D ERANGE; + float __f =3D __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] }]=