| 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 | |
| 22 | template <typename V> |
| 23 | void |
| 24 | test() |
| 25 | { |
| 26 | vir::test::setFuzzyness<float>(0); |
| 27 | vir::test::setFuzzyness<double>(0); |
| 28 | |
| 29 | using T = typename V::value_type; |
| 30 | |
| 31 | // See https://sourceware.org/bugzilla/show_bug.cgi?id=18031 |
| 32 | const bool modf_is_broken = [] { |
| 33 | volatile T x = T(5e20) / 7; |
| 34 | T tmp; |
| 35 | return std::fabs(std::modf(x, &tmp)) >= 1; |
| 36 | }(); |
| 37 | if (modf_is_broken) |
| 38 | __builtin_fprintf(stderr, |
| 39 | "NOTE: Skipping modf because std::modf is broken.\n"); |
| 40 | |
| 41 | test_values<V>( |
| 42 | { |
| 43 | #ifdef __STDC_IEC_559__ |
| 44 | std::__quiet_NaN_v<T>, |
| 45 | std::__infinity_v<T>, |
| 46 | -std::__infinity_v<T>, |
| 47 | -0., |
| 48 | std::__denorm_min_v<T>, |
| 49 | std::__norm_min_v<T> / 3, |
| 50 | -std::__denorm_min_v<T>, |
| 51 | -std::__norm_min_v<T> / 3, |
| 52 | #endif |
| 53 | +0., |
| 54 | +1.3, |
| 55 | -1.3, |
| 56 | 2.1, |
| 57 | -2.1, |
| 58 | 0.99, |
| 59 | 0.9, |
| 60 | -0.9, |
| 61 | -0.99, |
| 62 | std::__norm_min_v<T>, |
| 63 | std::__finite_max_v<T>, |
| 64 | -std::__norm_min_v<T>, |
| 65 | -std::__finite_max_v<T>}, |
| 66 | {10000}, |
| 67 | [](const V input) { |
| 68 | for (int exp : {-10000, -100, -10, -1, 0, 1, 10, 100, 10000}) |
| 69 | { |
| 70 | const auto totest = ldexp(input, exp); |
| 71 | using R = std::remove_const_t<decltype(totest)>; |
| 72 | auto&& expected = [&](const auto& v) -> const R { |
| 73 | R tmp = {}; |
| 74 | using std::ldexp; |
| 75 | for (std::size_t i = 0; i < R::size(); ++i) |
| 76 | { |
| 77 | tmp[i] = ldexp(v[i], exp); |
| 78 | } |
| 79 | return tmp; |
| 80 | }; |
| 81 | const R expect1 = expected(input); |
| 82 | COMPARE(isnan(totest), isnan(expect1)) |
| 83 | << "ldexp(" << input << ", " << exp << ") = " << totest |
| 84 | << " != " << expect1; |
| 85 | FUZZY_COMPARE(ldexp(iif(isnan(expect1), 0, input), exp), |
| 86 | expected(iif(isnan(expect1), 0, input))) |
| 87 | << "\nclean = " << iif(isnan(expect1), 0, input); |
| 88 | } |
| 89 | }, |
| 90 | [](const V input) { |
| 91 | for (int exp : {-10000, -100, -10, -1, 0, 1, 10, 100, 10000}) |
| 92 | { |
| 93 | const auto totest = scalbn(input, exp); |
| 94 | using R = std::remove_const_t<decltype(totest)>; |
| 95 | auto&& expected = [&](const auto& v) -> const R { |
| 96 | R tmp = {}; |
| 97 | using std::scalbn; |
| 98 | for (std::size_t i = 0; i < R::size(); ++i) |
| 99 | { |
| 100 | tmp[i] = scalbn(v[i], exp); |
| 101 | } |
| 102 | return tmp; |
| 103 | }; |
| 104 | const R expect1 = expected(input); |
| 105 | COMPARE(isnan(totest), isnan(expect1)) |
| 106 | << "scalbn(" << input << ", " << exp << ") = " << totest |
| 107 | << " != " << expect1; |
| 108 | FUZZY_COMPARE(scalbn(iif(isnan(expect1), 0, input), exp), |
| 109 | expected(iif(isnan(expect1), 0, input))) |
| 110 | << "\nclean = " << iif(isnan(expect1), 0, input); |
| 111 | } |
| 112 | }, |
| 113 | [](const V input) { |
| 114 | for (long exp : {-10000, -100, -10, -1, 0, 1, 10, 100, 10000}) |
| 115 | { |
| 116 | const auto totest = scalbln(input, exp); |
| 117 | using R = std::remove_const_t<decltype(totest)>; |
| 118 | auto&& expected = [&](const auto& v) -> const R { |
| 119 | R tmp = {}; |
| 120 | using std::scalbln; |
| 121 | for (std::size_t i = 0; i < R::size(); ++i) |
| 122 | { |
| 123 | tmp[i] = scalbln(v[i], exp); |
| 124 | } |
| 125 | return tmp; |
| 126 | }; |
| 127 | const R expect1 = expected(input); |
| 128 | COMPARE(isnan(totest), isnan(expect1)) |
| 129 | << "scalbln(" << input << ", " << exp << ") = " << totest |
| 130 | << " != " << expect1; |
| 131 | FUZZY_COMPARE(scalbln(iif(isnan(expect1), 0, input), exp), |
| 132 | expected(iif(isnan(expect1), 0, input))) |
| 133 | << "\nclean = " << iif(isnan(expect1), 0, input); |
| 134 | } |
| 135 | }, |
| 136 | [modf_is_broken](const V input) { |
| 137 | if (modf_is_broken) |
| 138 | return; |
| 139 | V integral = {}; |
| 140 | auto&& expected = [&](const auto& v) -> std::pair<const V, const V> { |
| 141 | std::pair<V, V> tmp = {}; |
| 142 | using std::modf; |
| 143 | for (std::size_t i = 0; i < V::size(); ++i) |
| 144 | { |
| 145 | typename V::value_type tmp2; |
| 146 | tmp.first[i] = modf(v[i], &tmp2); |
| 147 | tmp.second[i] = tmp2; |
| 148 | } |
| 149 | return tmp; |
| 150 | }; |
| 151 | #ifdef __STDC_IEC_559__ |
| 152 | const V totest = modf(input, &integral); |
| 153 | const auto expect1 = expected(input); |
| 154 | COMPARE(isnan(totest), isnan(expect1.first)) |
| 155 | << "modf(" << input << ", iptr) = " << totest << " != " << expect1; |
| 156 | COMPARE(isnan(integral), isnan(expect1.second)) |
| 157 | << "modf(" << input << ", iptr) = " << totest << " != " << expect1; |
| 158 | COMPARE(isnan(totest), isnan(integral)) |
| 159 | << "modf(" << input << ", iptr) = " << totest << " != " << expect1; |
| 160 | const V clean = iif(isnan(totest), V(), input); |
| 161 | #else |
| 162 | const V clean = iif(isnormal(input), input, V()); |
| 163 | #endif |
| 164 | const auto expect2 = expected(clean); |
| 165 | COMPARE(modf(clean, &integral), expect2.first) << "\nclean = " << clean; |
| 166 | COMPARE(integral, expect2.second); |
| 167 | }); |
| 168 | } |