Debian Codegraph @

libstdc++-v3/testsuite/experimental/simd/tests/fpclassify.cc

raw
1 // Copyright (C) 2020-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 // only: float|double|ldouble * * *
19 // expensive: * [1-9] * *
20 #include "bits/main.h"
21 #include <cfenv>
22
23 template <typename F>
24 auto
25 verify_no_fp_exceptions(F&& fun)
26 {
27 std::feclearexcept(FE_ALL_EXCEPT);
28 auto r = fun();
29 COMPARE(std::fetestexcept(FE_ALL_EXCEPT), 0);
30 return r;
31 }
32
33 #define NOFPEXCEPT(...) verify_no_fp_exceptions([&]() { return __VA_ARGS__; })
34
35 template <typename V>
36 void
37 test()
38 {
39 using T = typename V::value_type;
40 using intv = std::experimental::fixed_size_simd<int, V::size()>;
41 #if __GCC_IEC_559 >= 2
42 constexpr T inf = std::__infinity_v<T>;
43 constexpr T denorm_min = std::__infinity_v<T>;
44 constexpr T nan = std::__quiet_NaN_v<T>;
45 #endif
46 constexpr T max = std::__finite_max_v<T>;
47 constexpr T norm_min = std::__norm_min_v<T>;
48 test_values<V>(
49 {0., 1., -1.,
50 #if __GCC_IEC_559 >= 2
51 -0., inf, -inf, denorm_min, -denorm_min, nan,
52 norm_min * 0.9, -norm_min * 0.9,
53 #endif
54 max, -max, norm_min, -norm_min
55 },
56 [](const V input) {
57 COMPARE(NOFPEXCEPT(isfinite(input)),
58 !V([&](auto i) { return std::isfinite(input[i]) ? 0 : 1; }))
59 << input;
60 COMPARE(NOFPEXCEPT(isinf(input)),
61 !V([&](auto i) { return std::isinf(input[i]) ? 0 : 1; }))
62 << input;
63 COMPARE(NOFPEXCEPT(isnan(input)),
64 !V([&](auto i) { return std::isnan(input[i]) ? 0 : 1; }))
65 << input;
66 COMPARE(NOFPEXCEPT(isnormal(input)),
67 !V([&](auto i) { return std::isnormal(input[i]) ? 0 : 1; }))
68 << input;
69 COMPARE(NOFPEXCEPT(signbit(input)),
70 !V([&](auto i) { return std::signbit(input[i]) ? 0 : 1; }))
71 << input;
72 COMPARE(NOFPEXCEPT(isunordered(input, V())),
73 !V([&](auto i) { return std::isunordered(input[i], 0) ? 0 : 1; }))
74 << input;
75 COMPARE(NOFPEXCEPT(isunordered(V(), input)),
76 !V([&](auto i) { return std::isunordered(0, input[i]) ? 0 : 1; }))
77 << input;
78 COMPARE(NOFPEXCEPT(fpclassify(input)),
79 intv([&](auto i) { return std::fpclassify(input[i]); }))
80 << input;
81 });
82 #ifdef __SUPPORT_SNAN__
83 const V snan = std::__signaling_NaN_v<T>;
84 COMPARE(isfinite(snan),
85 !V([&](auto i) { return std::isfinite(snan[i]) ? 0 : 1; }))
86 << snan;
87 COMPARE(isinf(snan), !V([&](auto i) { return std::isinf(snan[i]) ? 0 : 1; }))
88 << snan;
89 COMPARE(isnan(snan), !V([&](auto i) { return std::isnan(snan[i]) ? 0 : 1; }))
90 << snan;
91 COMPARE(isnormal(snan),
92 !V([&](auto i) { return std::isnormal(snan[i]) ? 0 : 1; }))
93 << snan;
94 COMPARE(signbit(snan),
95 !V([&](auto i) { return std::signbit(snan[i]) ? 0 : 1; }))
96 << snan;
97 COMPARE(isunordered(snan, V()),
98 !V([&](auto i) { return std::isunordered(snan[i], 0) ? 0 : 1; }))
99 << snan;
100 COMPARE(isunordered(V(), snan),
101 !V([&](auto i) { return std::isunordered(0, snan[i]) ? 0 : 1; }))
102 << snan;
103 COMPARE(fpclassify(snan),
104 intv([&](auto i) { return std::fpclassify(snan[i]); }))
105 << snan;
106 #endif
107 }