| 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 | |
| 23 | #include <charconv> |
| 24 | #include <stdfloat> |
| 25 | #include <iostream> |
| 26 | #include <cmath> |
| 27 | #include <testsuite_hooks.h> |
| 28 | |
| 29 | template<typename T> |
| 30 | void |
| 31 | test(std::chars_format fmt = std::chars_format{}) |
| 32 | { |
| 33 | char str1[128], str2[128], str3[128]; |
| 34 | union U { unsigned short s; T f; } u, v; |
| 35 | for (int i = 0; i <= (unsigned short) ~0; ++i) |
| 36 | { |
| 37 | u.s = i; |
| 38 | auto [ptr1, ec1] = (fmt == std::chars_format{} |
| 39 | ? std::to_chars(str1, str1 + sizeof(str1), u.f) |
| 40 | : std::to_chars(str1, str1 + sizeof(str1), u.f, |
| 41 | fmt)); |
| 42 | auto [ptr2, ec2] = (fmt == std::chars_format{} |
| 43 | ? std::to_chars(str2, str2 + sizeof(str2), |
| 44 | std::float32_t(u.f)) |
| 45 | : std::to_chars(str2, str2 + sizeof(str2), |
| 46 | std::float32_t(u.f), fmt)); |
| 47 | VERIFY( ec1 == std::errc() && ec2 == std::errc() ); |
| 48 | // std::cout << i << ' ' << std::string_view (str1, ptr1) |
| 49 | // << '\t' << std::string_view (str2, ptr2) << '\n'; |
| 50 | if (fmt == std::chars_format::fixed) |
| 51 | { |
| 52 | auto [ptr3, ec3] = std::to_chars(str3, str3 + (ptr1 - str1), u.f, fmt); |
| 53 | VERIFY( ec3 == std::errc() && ptr3 - str3 == ptr1 - str1 ); |
| 54 | auto [ptr4, ec4] = std::to_chars(str3, str3 + (ptr1 - str1 - 1), u.f, fmt); |
| 55 | VERIFY( ec4 != std::errc() ); |
| 56 | } |
| 57 | auto [ptr5, ec5] = std::from_chars(str1, ptr1, v.f, |
| 58 | fmt == std::chars_format{} |
| 59 | ? std::chars_format::general : fmt); |
| 60 | VERIFY( ec5 == std::errc() && ptr5 == ptr1 ); |
| 61 | VERIFY( u.s == v.s || (std::isnan(u.f) && std::isnan(v.f)) ); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | int |
| 66 | main() |
| 67 | { |
| 68 | #ifdef __STDCPP_FLOAT16_T__ |
| 69 | test<std::float16_t>(); |
| 70 | test<std::float16_t>(std::chars_format::fixed); |
| 71 | test<std::float16_t>(std::chars_format::scientific); |
| 72 | test<std::float16_t>(std::chars_format::general); |
| 73 | test<std::float16_t>(std::chars_format::hex); |
| 74 | #endif |
| 75 | #ifdef __STDCPP_BFLOAT16_T__ |
| 76 | test<std::bfloat16_t>(); |
| 77 | test<std::bfloat16_t>(std::chars_format::fixed); |
| 78 | test<std::bfloat16_t>(std::chars_format::scientific); |
| 79 | test<std::bfloat16_t>(std::chars_format::general); |
| 80 | test<std::bfloat16_t>(std::chars_format::hex); |
| 81 | #endif |
| 82 | } |