Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:33

0001 //===----------------------------------------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef _LIBCPP___CXX03___MATH_HYPOT_H
0010 #define _LIBCPP___CXX03___MATH_HYPOT_H
0011 
0012 #include <__cxx03/__algorithm/max.h>
0013 #include <__cxx03/__config>
0014 #include <__cxx03/__math/abs.h>
0015 #include <__cxx03/__math/exponential_functions.h>
0016 #include <__cxx03/__math/roots.h>
0017 #include <__cxx03/__type_traits/enable_if.h>
0018 #include <__cxx03/__type_traits/is_arithmetic.h>
0019 #include <__cxx03/__type_traits/is_same.h>
0020 #include <__cxx03/__type_traits/promote.h>
0021 #include <__cxx03/__utility/pair.h>
0022 #include <__cxx03/limits>
0023 
0024 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0025 #  pragma GCC system_header
0026 #endif
0027 
0028 _LIBCPP_PUSH_MACROS
0029 #include <__cxx03/__undef_macros>
0030 
0031 _LIBCPP_BEGIN_NAMESPACE_STD
0032 
0033 namespace __math {
0034 
0035 inline _LIBCPP_HIDE_FROM_ABI float hypot(float __x, float __y) _NOEXCEPT { return __builtin_hypotf(__x, __y); }
0036 
0037 template <class = int>
0038 _LIBCPP_HIDE_FROM_ABI double hypot(double __x, double __y) _NOEXCEPT {
0039   return __builtin_hypot(__x, __y);
0040 }
0041 
0042 inline _LIBCPP_HIDE_FROM_ABI long double hypot(long double __x, long double __y) _NOEXCEPT {
0043   return __builtin_hypotl(__x, __y);
0044 }
0045 
0046 template <class _A1, class _A2, __enable_if_t<is_arithmetic<_A1>::value && is_arithmetic<_A2>::value, int> = 0>
0047 inline _LIBCPP_HIDE_FROM_ABI typename __promote<_A1, _A2>::type hypot(_A1 __x, _A2 __y) _NOEXCEPT {
0048   using __result_type = typename __promote<_A1, _A2>::type;
0049   static_assert(!(_IsSame<_A1, __result_type>::value && _IsSame<_A2, __result_type>::value), "");
0050   return __math::hypot((__result_type)__x, (__result_type)__y);
0051 }
0052 
0053 #if _LIBCPP_STD_VER >= 17
0054 // Computes the three-dimensional hypotenuse: `std::hypot(x,y,z)`.
0055 // The naive implementation might over-/underflow which is why this implementation is more involved:
0056 //    If the square of an argument might run into issues, we scale the arguments appropriately.
0057 // See https://github.com/llvm/llvm-project/issues/92782 for a detailed discussion and summary.
0058 template <class _Real>
0059 _LIBCPP_HIDE_FROM_ABI _Real __hypot(_Real __x, _Real __y, _Real __z) {
0060   // Factors needed to determine if over-/underflow might happen
0061   constexpr int __exp              = std::numeric_limits<_Real>::max_exponent / 2;
0062   const _Real __overflow_threshold = __math::ldexp(_Real(1), __exp);
0063   const _Real __overflow_scale     = __math::ldexp(_Real(1), -(__exp + 20));
0064 
0065   // Scale arguments depending on their size
0066   const _Real __max_abs = std::max(__math::fabs(__x), std::max(__math::fabs(__y), __math::fabs(__z)));
0067   _Real __scale;
0068   if (__max_abs > __overflow_threshold) { // x*x + y*y + z*z might overflow
0069     __scale = __overflow_scale;
0070   } else if (__max_abs < 1 / __overflow_threshold) { // x*x + y*y + z*z might underflow
0071     __scale = 1 / __overflow_scale;
0072   } else {
0073     __scale = 1;
0074   }
0075   __x *= __scale;
0076   __y *= __scale;
0077   __z *= __scale;
0078 
0079   // Compute hypot of scaled arguments and undo scaling
0080   return __math::sqrt(__x * __x + __y * __y + __z * __z) / __scale;
0081 }
0082 
0083 inline _LIBCPP_HIDE_FROM_ABI float hypot(float __x, float __y, float __z) { return __math::__hypot(__x, __y, __z); }
0084 
0085 inline _LIBCPP_HIDE_FROM_ABI double hypot(double __x, double __y, double __z) { return __math::__hypot(__x, __y, __z); }
0086 
0087 inline _LIBCPP_HIDE_FROM_ABI long double hypot(long double __x, long double __y, long double __z) {
0088   return __math::__hypot(__x, __y, __z);
0089 }
0090 
0091 template <class _A1,
0092           class _A2,
0093           class _A3,
0094           std::enable_if_t< is_arithmetic_v<_A1> && is_arithmetic_v<_A2> && is_arithmetic_v<_A3>, int> = 0 >
0095 _LIBCPP_HIDE_FROM_ABI typename __promote<_A1, _A2, _A3>::type hypot(_A1 __x, _A2 __y, _A3 __z) _NOEXCEPT {
0096   using __result_type = typename __promote<_A1, _A2, _A3>::type;
0097   static_assert(!(
0098       std::is_same_v<_A1, __result_type> && std::is_same_v<_A2, __result_type> && std::is_same_v<_A3, __result_type>));
0099   return __math::__hypot(
0100       static_cast<__result_type>(__x), static_cast<__result_type>(__y), static_cast<__result_type>(__z));
0101 }
0102 #endif
0103 
0104 } // namespace __math
0105 
0106 _LIBCPP_END_NAMESPACE_STD
0107 _LIBCPP_POP_MACROS
0108 
0109 #endif // _LIBCPP___CXX03___MATH_HYPOT_H