]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/to_chars/float16_c++23.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / to_chars / float16_c++23.cc
1 // Copyright (C) 2022-2023 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-options "-std=gnu++2b" }
19 // { dg-do run { target c++23 } }
20 // { dg-require-effective-target ieee_floats }
21 // { dg-require-effective-target size32plus }
22 // { dg-add-options ieee }
23
24 #include <charconv>
25 #include <stdfloat>
26 #include <iostream>
27 #include <cmath>
28 #include <testsuite_hooks.h>
29
30 template<typename T>
31 void
32 test(std::chars_format fmt = std::chars_format{})
33 {
34 char str1[128], str2[128], str3[128];
35 union U { unsigned short s; T f; } u, v;
36 for (int i = 0; i <= (unsigned short) ~0; ++i)
37 {
38 u.s = i;
39 auto [ptr1, ec1] = (fmt == std::chars_format{}
40 ? std::to_chars(str1, str1 + sizeof(str1), u.f)
41 : std::to_chars(str1, str1 + sizeof(str1), u.f,
42 fmt));
43 auto [ptr2, ec2] = (fmt == std::chars_format{}
44 ? std::to_chars(str2, str2 + sizeof(str2),
45 std::float32_t(u.f))
46 : std::to_chars(str2, str2 + sizeof(str2),
47 std::float32_t(u.f), fmt));
48 VERIFY( ec1 == std::errc() && ec2 == std::errc() );
49 // std::cout << i << ' ' << std::string_view (str1, ptr1)
50 // << '\t' << std::string_view (str2, ptr2) << '\n';
51 if (fmt == std::chars_format::fixed)
52 {
53 auto [ptr3, ec3] = std::to_chars(str3, str3 + (ptr1 - str1), u.f, fmt);
54 VERIFY( ec3 == std::errc() && ptr3 - str3 == ptr1 - str1 );
55 auto [ptr4, ec4] = std::to_chars(str3, str3 + (ptr1 - str1 - 1), u.f, fmt);
56 VERIFY( ec4 != std::errc() );
57 }
58 auto [ptr5, ec5] = std::from_chars(str1, ptr1, v.f,
59 fmt == std::chars_format{}
60 ? std::chars_format::general : fmt);
61 VERIFY( ec5 == std::errc() && ptr5 == ptr1 );
62 VERIFY( u.s == v.s || (std::isnan(u.f) && std::isnan(v.f)) );
63 }
64 }
65
66 int
67 main()
68 {
69 #ifdef __STDCPP_FLOAT16_T__
70 test<std::float16_t>();
71 test<std::float16_t>(std::chars_format::fixed);
72 test<std::float16_t>(std::chars_format::scientific);
73 test<std::float16_t>(std::chars_format::general);
74 test<std::float16_t>(std::chars_format::hex);
75 #endif
76 #ifdef __STDCPP_BFLOAT16_T__
77 test<std::bfloat16_t>();
78 test<std::bfloat16_t>(std::chars_format::fixed);
79 test<std::bfloat16_t>(std::chars_format::scientific);
80 test<std::bfloat16_t>(std::chars_format::general);
81 test<std::bfloat16_t>(std::chars_format::hex);
82 #endif
83 }