| 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 |
// skip: ldouble * powerpc64* * |
| 20 |
// expensive: * [1-9] * * |
| 21 |
#include "bits/main.h" |
| 22 |
|
| 23 |
// 3-arg std::hypot needs to be fixed, this is a better reference: |
| 24 |
template <typename T> |
| 25 |
[[gnu::optimize("-fno-unsafe-math-optimizations")]] |
| 26 |
T |
| 27 |
hypot3(T x, T y, T z) |
| 28 |
{ |
| 29 |
x = std::abs(x); |
| 30 |
y = std::abs(y); |
| 31 |
z = std::abs(z); |
| 32 |
if (std::isinf(x) || std::isinf(y) || std::isinf(z)) |
| 33 |
return std::__infinity_v<T>; |
| 34 |
else if (std::isnan(x) || std::isnan(y) || std::isnan(z)) |
| 35 |
return std::__quiet_NaN_v<T>; |
| 36 |
else if (x == y && y == z) |
| 37 |
return x * std::sqrt(T(3)); |
| 38 |
else if (z == 0 && y == 0) |
| 39 |
return x; |
| 40 |
else if (x == 0 && z == 0) |
| 41 |
return y; |
| 42 |
else if (x == 0 && y == 0) |
| 43 |
return z; |
| 44 |
else |
| 45 |
{ |
| 46 |
T hi = std::max(std::max(x, y), z); |
| 47 |
T lo0 = std::min(std::max(x, y), z); |
| 48 |
T lo1 = std::min(x, y); |
| 49 |
int e = 0; |
| 50 |
hi = std::frexp(hi, &e); |
| 51 |
lo0 = std::ldexp(lo0, -e); |
| 52 |
lo1 = std::ldexp(lo1, -e); |
| 53 |
T lo = lo0 * lo0 + lo1 * lo1; |
| 54 |
return std::ldexp(std::sqrt(hi * hi + lo), e); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
template <typename V> |
| 59 |
void |
| 60 |
test() |
| 61 |
{ |
| 62 |
vir::test::setFuzzyness<float>(1); |
| 63 |
vir::test::setFuzzyness<double>(1); |
| 64 |
vir::test::setFuzzyness<long double>(2); // because of the bad reference |
| 65 |
|
| 66 |
using T = typename V::value_type; |
| 67 |
test_values_3arg<V>( |
| 68 |
{ |
| 69 |
#ifdef __STDC_IEC_559__ |
| 70 |
std::__quiet_NaN_v<T>, |
| 71 |
std::__infinity_v<T>, |
| 72 |
-std::__infinity_v<T>, |
| 73 |
std::__norm_min_v<T> / 3, |
| 74 |
-0., |
| 75 |
std::__denorm_min_v<T>, |
| 76 |
#endif |
| 77 |
0., |
| 78 |
1., |
| 79 |
-1., |
| 80 |
std::__norm_min_v<T>, |
| 81 |
-std::__norm_min_v<T>, |
| 82 |
2., |
| 83 |
-2., |
| 84 |
std::__finite_max_v<T> / 5, |
| 85 |
std::__finite_max_v<T> / 3, |
| 86 |
std::__finite_max_v<T> / 2, |
| 87 |
-std::__finite_max_v<T> / 5, |
| 88 |
-std::__finite_max_v<T> / 3, |
| 89 |
-std::__finite_max_v<T> / 2, |
| 90 |
#ifdef __FAST_MATH__ |
| 91 |
// fast-math hypot is imprecise for the max exponent |
| 92 |
}, |
| 93 |
{100000, std::__finite_max_v<T> / 2}, |
| 94 |
#else |
| 95 |
std::__finite_max_v<T>, -std::__finite_max_v<T>}, |
| 96 |
{100000}, |
| 97 |
#endif |
| 98 |
MAKE_TESTER_2(hypot, hypot3)); |
| 99 |
#if !__FINITE_MATH_ONLY__ |
| 100 |
COMPARE(hypot(V(std::__finite_max_v<T>), V(std::__finite_max_v<T>), V()), |
| 101 |
V(std::__infinity_v<T>)); |
| 102 |
COMPARE(hypot(V(std::__finite_max_v<T>), V(), V(std::__finite_max_v<T>)), |
| 103 |
V(std::__infinity_v<T>)); |
| 104 |
COMPARE(hypot(V(), V(std::__finite_max_v<T>), V(std::__finite_max_v<T>)), |
| 105 |
V(std::__infinity_v<T>)); |
| 106 |
#endif |
| 107 |
COMPARE(hypot(V(std::__norm_min_v<T>), V(std::__norm_min_v<T>), |
| 108 |
V(std::__norm_min_v<T>)), |
| 109 |
V(std::__norm_min_v<T> * std::sqrt(T(3)))); |
| 110 |
auto&& hypot3_test |
| 111 |
= [](auto a, auto b, auto c) -> decltype(hypot(a, b, c)) { return {}; }; |
| 112 |
VERIFY((sfinae_is_callable<V, V, V>(hypot3_test))); |
| 113 |
VERIFY((sfinae_is_callable<T, T, V>(hypot3_test))); |
| 114 |
VERIFY((sfinae_is_callable<V, T, T>(hypot3_test))); |
| 115 |
VERIFY((sfinae_is_callable<T, V, T>(hypot3_test))); |
| 116 |
VERIFY((sfinae_is_callable<T, V, V>(hypot3_test))); |
| 117 |
VERIFY((sfinae_is_callable<V, T, V>(hypot3_test))); |
| 118 |
VERIFY((sfinae_is_callable<V, V, T>(hypot3_test))); |
| 119 |
VERIFY((sfinae_is_callable<int, int, V>(hypot3_test))); |
| 120 |
VERIFY((sfinae_is_callable<int, V, int>(hypot3_test))); |
| 121 |
VERIFY((sfinae_is_callable<V, T, int>(hypot3_test))); |
| 122 |
VERIFY(!(sfinae_is_callable<bool, V, V>(hypot3_test))); |
| 123 |
VERIFY(!(sfinae_is_callable<V, bool, V>(hypot3_test))); |
| 124 |
VERIFY(!(sfinae_is_callable<V, V, bool>(hypot3_test))); |
| 125 |
|
| 126 |
vir::test::setFuzzyness<float>(0); |
| 127 |
vir::test::setFuzzyness<double>(0); |
| 128 |
test_values_3arg<V>( |
| 129 |
{ |
| 130 |
#ifdef __STDC_IEC_559__ |
| 131 |
std::__quiet_NaN_v<T>, std::__infinity_v<T>, -std::__infinity_v<T>, -0., |
| 132 |
std::__norm_min_v<T> / 3, std::__denorm_min_v<T>, |
| 133 |
#endif |
| 134 |
0., std::__norm_min_v<T>, std::__finite_max_v<T>}, |
| 135 |
{10000, -std::__finite_max_v<T> / 2, std::__finite_max_v<T> / 2}, |
| 136 |
MAKE_TESTER(fma)); |
| 137 |
auto&& fma_test |
| 138 |
= [](auto a, auto b, auto c) -> decltype(fma(a, b, c)) { return {}; }; |
| 139 |
VERIFY((sfinae_is_callable<V, V, V>(fma_test))); |
| 140 |
VERIFY((sfinae_is_callable<T, T, V>(fma_test))); |
| 141 |
VERIFY((sfinae_is_callable<V, T, T>(fma_test))); |
| 142 |
VERIFY((sfinae_is_callable<T, V, T>(fma_test))); |
| 143 |
VERIFY((sfinae_is_callable<T, V, V>(fma_test))); |
| 144 |
VERIFY((sfinae_is_callable<V, T, V>(fma_test))); |
| 145 |
VERIFY((sfinae_is_callable<V, V, T>(fma_test))); |
| 146 |
VERIFY((sfinae_is_callable<int, int, V>(fma_test))); |
| 147 |
VERIFY((sfinae_is_callable<int, V, int>(fma_test))); |
| 148 |
VERIFY((sfinae_is_callable<V, T, int>(fma_test))); |
| 149 |
VERIFY((!sfinae_is_callable<V, T, bool>(fma_test))); |
| 150 |
VERIFY((!sfinae_is_callable<bool, V, V>(fma_test))); |
| 151 |
} |