15f757f3fSDimitry Andric //===----------------------------------------------------------------------===// 25f757f3fSDimitry Andric // 35f757f3fSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 45f757f3fSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 55f757f3fSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 65f757f3fSDimitry Andric // 75f757f3fSDimitry Andric //===----------------------------------------------------------------------===// 85f757f3fSDimitry Andric 95f757f3fSDimitry Andric #ifndef _LIBCPP___MATH_HYPOT_H 105f757f3fSDimitry Andric #define _LIBCPP___MATH_HYPOT_H 115f757f3fSDimitry Andric 12*62987288SDimitry Andric #include <__algorithm/max.h> 135f757f3fSDimitry Andric #include <__config> 14*62987288SDimitry Andric #include <__math/abs.h> 15*62987288SDimitry Andric #include <__math/exponential_functions.h> 16*62987288SDimitry Andric #include <__math/roots.h> 175f757f3fSDimitry Andric #include <__type_traits/enable_if.h> 185f757f3fSDimitry Andric #include <__type_traits/is_arithmetic.h> 195f757f3fSDimitry Andric #include <__type_traits/is_same.h> 205f757f3fSDimitry Andric #include <__type_traits/promote.h> 2136b606aeSDimitry Andric #include <__utility/pair.h> 2236b606aeSDimitry Andric #include <limits> 2336b606aeSDimitry Andric 245f757f3fSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 255f757f3fSDimitry Andric # pragma GCC system_header 265f757f3fSDimitry Andric #endif 275f757f3fSDimitry Andric 2836b606aeSDimitry Andric _LIBCPP_PUSH_MACROS 2936b606aeSDimitry Andric #include <__undef_macros> 3036b606aeSDimitry Andric 315f757f3fSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 325f757f3fSDimitry Andric 335f757f3fSDimitry Andric namespace __math { 345f757f3fSDimitry Andric 355f757f3fSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI float hypot(float __x, float __y) _NOEXCEPT { return __builtin_hypotf(__x, __y); } 365f757f3fSDimitry Andric 375f757f3fSDimitry Andric template <class = int> 385f757f3fSDimitry Andric _LIBCPP_HIDE_FROM_ABI double hypot(double __x, double __y) _NOEXCEPT { 395f757f3fSDimitry Andric return __builtin_hypot(__x, __y); 405f757f3fSDimitry Andric } 415f757f3fSDimitry Andric 425f757f3fSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI long double hypot(long double __x, long double __y) _NOEXCEPT { 435f757f3fSDimitry Andric return __builtin_hypotl(__x, __y); 445f757f3fSDimitry Andric } 455f757f3fSDimitry Andric 465f757f3fSDimitry Andric template <class _A1, class _A2, __enable_if_t<is_arithmetic<_A1>::value && is_arithmetic<_A2>::value, int> = 0> 475f757f3fSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI typename __promote<_A1, _A2>::type hypot(_A1 __x, _A2 __y) _NOEXCEPT { 485f757f3fSDimitry Andric using __result_type = typename __promote<_A1, _A2>::type; 490fca6ea1SDimitry Andric static_assert(!(_IsSame<_A1, __result_type>::value && _IsSame<_A2, __result_type>::value), ""); 505f757f3fSDimitry Andric return __math::hypot((__result_type)__x, (__result_type)__y); 515f757f3fSDimitry Andric } 525f757f3fSDimitry Andric 5336b606aeSDimitry Andric #if _LIBCPP_STD_VER >= 17 5436b606aeSDimitry Andric // Computes the three-dimensional hypotenuse: `std::hypot(x,y,z)`. 5536b606aeSDimitry Andric // The naive implementation might over-/underflow which is why this implementation is more involved: 5636b606aeSDimitry Andric // If the square of an argument might run into issues, we scale the arguments appropriately. 5736b606aeSDimitry Andric // See https://github.com/llvm/llvm-project/issues/92782 for a detailed discussion and summary. 5836b606aeSDimitry Andric template <class _Real> 5936b606aeSDimitry Andric _LIBCPP_HIDE_FROM_ABI _Real __hypot(_Real __x, _Real __y, _Real __z) { 60*62987288SDimitry Andric // Factors needed to determine if over-/underflow might happen 61*62987288SDimitry Andric constexpr int __exp = std::numeric_limits<_Real>::max_exponent / 2; 62*62987288SDimitry Andric const _Real __overflow_threshold = __math::ldexp(_Real(1), __exp); 63*62987288SDimitry Andric const _Real __overflow_scale = __math::ldexp(_Real(1), -(__exp + 20)); 64*62987288SDimitry Andric 65*62987288SDimitry Andric // Scale arguments depending on their size 6636b606aeSDimitry Andric const _Real __max_abs = std::max(__math::fabs(__x), std::max(__math::fabs(__y), __math::fabs(__z))); 6736b606aeSDimitry Andric _Real __scale; 6836b606aeSDimitry Andric if (__max_abs > __overflow_threshold) { // x*x + y*y + z*z might overflow 6936b606aeSDimitry Andric __scale = __overflow_scale; 7036b606aeSDimitry Andric } else if (__max_abs < 1 / __overflow_threshold) { // x*x + y*y + z*z might underflow 7136b606aeSDimitry Andric __scale = 1 / __overflow_scale; 72*62987288SDimitry Andric } else { 73*62987288SDimitry Andric __scale = 1; 74*62987288SDimitry Andric } 7536b606aeSDimitry Andric __x *= __scale; 7636b606aeSDimitry Andric __y *= __scale; 7736b606aeSDimitry Andric __z *= __scale; 78*62987288SDimitry Andric 79*62987288SDimitry Andric // Compute hypot of scaled arguments and undo scaling 8036b606aeSDimitry Andric return __math::sqrt(__x * __x + __y * __y + __z * __z) / __scale; 8136b606aeSDimitry Andric } 8236b606aeSDimitry Andric 8336b606aeSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI float hypot(float __x, float __y, float __z) { return __math::__hypot(__x, __y, __z); } 8436b606aeSDimitry Andric 8536b606aeSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI double hypot(double __x, double __y, double __z) { return __math::__hypot(__x, __y, __z); } 8636b606aeSDimitry Andric 8736b606aeSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI long double hypot(long double __x, long double __y, long double __z) { 8836b606aeSDimitry Andric return __math::__hypot(__x, __y, __z); 8936b606aeSDimitry Andric } 9036b606aeSDimitry Andric 9136b606aeSDimitry Andric template <class _A1, 9236b606aeSDimitry Andric class _A2, 9336b606aeSDimitry Andric class _A3, 9436b606aeSDimitry Andric std::enable_if_t< is_arithmetic_v<_A1> && is_arithmetic_v<_A2> && is_arithmetic_v<_A3>, int> = 0 > 9536b606aeSDimitry Andric _LIBCPP_HIDE_FROM_ABI typename __promote<_A1, _A2, _A3>::type hypot(_A1 __x, _A2 __y, _A3 __z) _NOEXCEPT { 9636b606aeSDimitry Andric using __result_type = typename __promote<_A1, _A2, _A3>::type; 9736b606aeSDimitry Andric static_assert(!( 9836b606aeSDimitry Andric std::is_same_v<_A1, __result_type> && std::is_same_v<_A2, __result_type> && std::is_same_v<_A3, __result_type>)); 9936b606aeSDimitry Andric return __math::__hypot( 10036b606aeSDimitry Andric static_cast<__result_type>(__x), static_cast<__result_type>(__y), static_cast<__result_type>(__z)); 10136b606aeSDimitry Andric } 10236b606aeSDimitry Andric #endif 10336b606aeSDimitry Andric 1045f757f3fSDimitry Andric } // namespace __math 1055f757f3fSDimitry Andric 1065f757f3fSDimitry Andric _LIBCPP_END_NAMESPACE_STD 10736b606aeSDimitry Andric _LIBCPP_POP_MACROS 1085f757f3fSDimitry Andric 1095f757f3fSDimitry Andric #endif // _LIBCPP___MATH_HYPOT_H 110