|
9 | 9 | #ifndef _LIBCPP___MATH_HYPOT_H
|
10 | 10 | #define _LIBCPP___MATH_HYPOT_H
|
11 | 11 |
|
| 12 | +#include <__algorithm/max.h> |
12 | 13 | #include <__config>
|
| 14 | +#include <__math/abs.h> |
| 15 | +#include <__math/roots.h> |
13 | 16 | #include <__type_traits/enable_if.h>
|
14 | 17 | #include <__type_traits/is_arithmetic.h>
|
15 | 18 | #include <__type_traits/is_same.h>
|
16 | 19 | #include <__type_traits/promote.h>
|
| 20 | +#include <array> |
| 21 | +#include <limits> |
17 | 22 |
|
18 | 23 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
19 | 24 | # pragma GCC system_header
|
@@ -41,6 +46,79 @@ inline _LIBCPP_HIDE_FROM_ABI typename __promote<_A1, _A2>::type hypot(_A1 __x, _
|
41 | 46 | return __math::hypot((__result_type)__x, (__result_type)__y);
|
42 | 47 | }
|
43 | 48 |
|
| 49 | +#if _LIBCPP_STD_VER >= 17 |
| 50 | +template <class _Real> |
| 51 | +struct __hypot_factors { |
| 52 | + _Real __threshold; |
| 53 | + _Real __scale_xyz; |
| 54 | + _Real __scale_M; |
| 55 | +}; |
| 56 | + |
| 57 | +// returns [underflow_factors, overflow_factors] |
| 58 | +template <class _Real> |
| 59 | +std::array<__hypot_factors<_Real>, 2> __create_factors() { |
| 60 | + static_assert(std::numeric_limits<_Real>::is_iec559); |
| 61 | + |
| 62 | + __hypot_factors<_Real> __underflow, __overflow; |
| 63 | + if constexpr (std::is_same_v<_Real, float>) { |
| 64 | + static_assert(-125 == std::numeric_limits<_Real>::min_exponent); |
| 65 | + static_assert(+128 == std::numeric_limits<_Real>::max_exponent); |
| 66 | + __underflow = __hypot_factors<_Real>{0x1.0p-62f, 0x1.0p70f, 0x1.0p-70f}; |
| 67 | + __overflow = __hypot_factors<_Real>{0x1.0p62f, 0x1.0p-70f, 0x1.0p+70f}; |
| 68 | + } else if constexpr (std::is_same_v<_Real, double>) { |
| 69 | + static_assert(-1021 == std::numeric_limits<_Real>::min_exponent); |
| 70 | + static_assert(+1024 == std::numeric_limits<_Real>::max_exponent); |
| 71 | + __underflow = __hypot_factors<_Real>{0x1.0p-510, 0x1.0p600, 0x1.0p-600}; |
| 72 | + __overflow = __hypot_factors<_Real>{0x1.0p510, 0x1.0p-600, 0x1.0p+600}; |
| 73 | + } else { // long double |
| 74 | + static_assert(std::is_same_v<_Real, long double>); |
| 75 | + static_assert(-16'381 == std::numeric_limits<_Real>::min_exponent); |
| 76 | + static_assert(+16'384 == std::numeric_limits<_Real>::max_exponent); |
| 77 | + __underflow = __hypot_factors<_Real>{0x1.0p-8'190l, 0x1.0p9'000l, 0x1.0p-9'000l}; |
| 78 | + __overflow = __hypot_factors<_Real>{0x1.0p8'190l, 0x1.0p-9'000l, 0x1.0p+9'000l}; |
| 79 | + } |
| 80 | + return {__underflow, __overflow}; |
| 81 | +} |
| 82 | + |
| 83 | +template <class _Real> |
| 84 | +_Real __hypot(_Real __x, _Real __y, _Real __z) { |
| 85 | + const auto [__underflow, __overflow] = __create_factors<_Real>(); |
| 86 | + _Real __M = std::max({__math::fabs(__x), __math::fabs(__y), __math::fabs(__z)}); |
| 87 | + if (__M > __overflow.__threshold) { // x*x + y*y + z*z might overflow |
| 88 | + __x *= __overflow.__scale_xyz; |
| 89 | + __y *= __overflow.__scale_xyz; |
| 90 | + __z *= __overflow.__scale_xyz; |
| 91 | + __M = __overflow.__scale_M; |
| 92 | + } else if (__M < __underflow.__threshold) { // x*x + y*y + z*z might underflow |
| 93 | + __x *= __underflow.__scale_xyz; |
| 94 | + __y *= __underflow.__scale_xyz; |
| 95 | + __z *= __underflow.__scale_xyz; |
| 96 | + __M = __underflow.__scale_M; |
| 97 | + } else |
| 98 | + __M = 1; |
| 99 | + return __M * __math::sqrt(__x * __x + __y * __y + __z * __z); |
| 100 | +} |
| 101 | + |
| 102 | +inline _LIBCPP_HIDE_FROM_ABI float hypot(float __x, float __y, float __z) { return __hypot(__x, __y, __z); } |
| 103 | + |
| 104 | +inline _LIBCPP_HIDE_FROM_ABI double hypot(double __x, double __y, double __z) { return __hypot(__x, __y, __z); } |
| 105 | + |
| 106 | +inline _LIBCPP_HIDE_FROM_ABI long double hypot(long double __x, long double __y, long double __z) { |
| 107 | + return __hypot(__x, __y, __z); |
| 108 | +} |
| 109 | + |
| 110 | +template <class _A1, |
| 111 | + class _A2, |
| 112 | + class _A3, |
| 113 | + std::enable_if_t< is_arithmetic_v<_A1> && is_arithmetic_v<_A2> && is_arithmetic_v<_A3>, int> = 0 > |
| 114 | +_LIBCPP_HIDE_FROM_ABI typename __promote<_A1, _A2, _A3>::type hypot(_A1 __x, _A2 __y, _A3 __z) _NOEXCEPT { |
| 115 | + using __result_type = typename __promote<_A1, _A2, _A3>::type; |
| 116 | + static_assert(!( |
| 117 | + std::is_same_v<_A1, __result_type> && std::is_same_v<_A2, __result_type> && std::is_same_v<_A3, __result_type>)); |
| 118 | + return __hypot(static_cast<__result_type>(__x), static_cast<__result_type>(__y), static_cast<__result_type>(__z)); |
| 119 | +} |
| 120 | +#endif |
| 121 | + |
44 | 122 | } // namespace __math
|
45 | 123 |
|
46 | 124 | _LIBCPP_END_NAMESPACE_STD
|
|
0 commit comments