| 1 |
// Copyright (C) 2022-2025 Free Software Foundation, Inc. |
| 2 |
// |
| 3 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 4 |
// software; you can redistribute it and/or modify it under the |
| 5 |
// terms of the GNU General Public License as published by the |
| 6 |
// Free Software Foundation; either version 3, or (at your option) |
| 7 |
// any later version. |
| 8 |
|
| 9 |
// This library is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
|
| 14 |
// You should have received a copy of the GNU General Public License along |
| 15 |
// with this library; see the file COPYING3. If not see |
| 16 |
// <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
// { dg-do run { target c++23 } } |
| 19 |
// { dg-require-effective-target ieee_floats } |
| 20 |
// { dg-require-effective-target size32plus } |
| 21 |
// { dg-add-options ieee } |
| 22 |
// { dg-xfail-run-if "from_chars limited to double-precision" { aarch64-*-vxworks* aarch64-*-rtems* } } |
| 23 |
|
| 24 |
#include <charconv> |
| 25 |
#include <stdfloat> |
| 26 |
#include <limits> |
| 27 |
#include <numbers> |
| 28 |
#include <testsuite_hooks.h> |
| 29 |
|
| 30 |
#if defined(__STDCPP_FLOAT128_T__) \ |
| 31 |
&& (defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128) \ |
| 32 |
|| defined(_GLIBCXX_HAVE_FLOAT128_MATH)) |
| 33 |
void |
| 34 |
test(std::chars_format fmt = std::chars_format{}) |
| 35 |
{ |
| 36 |
std::float128_t tests[] = { |
| 37 |
std::numeric_limits<std::float128_t>::denorm_min(), |
| 38 |
std::numeric_limits<std::float128_t>::min(), |
| 39 |
0.0f128, |
| 40 |
-42.0f128, |
| 41 |
1234.5678912345f128, |
| 42 |
std::numbers::e_v<std::float128_t>, |
| 43 |
std::numbers::log2e_v<std::float128_t>, |
| 44 |
std::numbers::log10e_v<std::float128_t>, |
| 45 |
std::numbers::pi_v<std::float128_t>, |
| 46 |
std::numbers::inv_pi_v<std::float128_t>, |
| 47 |
std::numbers::inv_sqrtpi_v<std::float128_t>, |
| 48 |
std::numbers::ln2_v<std::float128_t>, |
| 49 |
std::numbers::ln10_v<std::float128_t>, |
| 50 |
std::numbers::sqrt2_v<std::float128_t>, |
| 51 |
std::numbers::sqrt3_v<std::float128_t>, |
| 52 |
std::numbers::inv_sqrt3_v<std::float128_t>, |
| 53 |
std::numbers::egamma_v<std::float128_t>, |
| 54 |
std::numbers::phi_v<std::float128_t>, |
| 55 |
// Solaris has non-conforming printf, see PR98384 and PR107815. |
| 56 |
#if !(defined(__sun__) && defined(__svr4__)) |
| 57 |
std::numeric_limits<std::float128_t>::max() |
| 58 |
#endif |
| 59 |
}; |
| 60 |
char str1[10000], str2[10000]; |
| 61 |
for (auto u : tests) |
| 62 |
{ |
| 63 |
auto [ptr1, ec1] = (fmt == std::chars_format{} |
| 64 |
? std::to_chars(str1, str1 + sizeof(str1), u) |
| 65 |
: std::to_chars(str1, str1 + sizeof(str1), u, fmt)); |
| 66 |
VERIFY( ec1 == std::errc() ); |
| 67 |
// std::cout << u << ' ' << std::string_view (str1, ptr1) << '\n'; |
| 68 |
if (fmt == std::chars_format::fixed) |
| 69 |
{ |
| 70 |
auto [ptr2, ec2] = std::to_chars(str2, str2 + (ptr1 - str1), u, fmt); |
| 71 |
VERIFY( ec2 == std::errc() && ptr2 - str2 == ptr1 - str1 ); |
| 72 |
auto [ptr3, ec3] = std::to_chars(str2, str2 + (ptr1 - str1 - 1), u, fmt); |
| 73 |
VERIFY( ec3 != std::errc() ); |
| 74 |
} |
| 75 |
std::float128_t v; |
| 76 |
auto [ptr4, ec4] = std::from_chars(str1, ptr1, v, |
| 77 |
fmt == std::chars_format{} |
| 78 |
? std::chars_format::general : fmt); |
| 79 |
VERIFY( ec4 == std::errc() && ptr4 == ptr1 ); |
| 80 |
VERIFY( u == v ); |
| 81 |
|
| 82 |
if (fmt == std::chars_format{}) |
| 83 |
continue; |
| 84 |
|
| 85 |
auto [ptr5, ec5] = std::to_chars(str1, str1 + sizeof(str1), u, fmt, 90); |
| 86 |
VERIFY( ec5 == std::errc() ); |
| 87 |
// std::cout << u << ' ' << std::string_view (str1, ptr5) << '\n'; |
| 88 |
v = 4.0f128; |
| 89 |
auto [ptr6, ec6] = std::from_chars(str1, ptr5, v, fmt); |
| 90 |
VERIFY( ec6 == std::errc() && ptr6 == ptr5 ); |
| 91 |
if (fmt == std::chars_format::fixed && u > 0.0f128 && u < 0.000001f128) |
| 92 |
VERIFY( v == 0.0 ); |
| 93 |
else |
| 94 |
VERIFY( u == v ); |
| 95 |
} |
| 96 |
} |
| 97 |
#endif |
| 98 |
|
| 99 |
int |
| 100 |
main() |
| 101 |
{ |
| 102 |
#if defined(__STDCPP_FLOAT128_T__) \ |
| 103 |
&& (defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128) \ |
| 104 |
|| defined(_GLIBCXX_HAVE_FLOAT128_MATH)) |
| 105 |
test(); |
| 106 |
test(std::chars_format::fixed); |
| 107 |
test(std::chars_format::scientific); |
| 108 |
test(std::chars_format::general); |
| 109 |
test(std::chars_format::hex); |
| 110 |
#endif |
| 111 |
} |