xref: /freebsd-src/contrib/llvm-project/libcxx/include/__math/hypot.h (revision 36b606ae6aa4b24061096ba18582e0a08ccd5dba)
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 
125f757f3fSDimitry Andric #include <__config>
135f757f3fSDimitry Andric #include <__type_traits/enable_if.h>
145f757f3fSDimitry Andric #include <__type_traits/is_arithmetic.h>
155f757f3fSDimitry Andric #include <__type_traits/is_same.h>
165f757f3fSDimitry Andric #include <__type_traits/promote.h>
175f757f3fSDimitry Andric 
18*36b606aeSDimitry Andric #if _LIBCPP_STD_VER >= 17
19*36b606aeSDimitry Andric #  include <__algorithm/max.h>
20*36b606aeSDimitry Andric #  include <__math/abs.h>
21*36b606aeSDimitry Andric #  include <__math/roots.h>
22*36b606aeSDimitry Andric #  include <__utility/pair.h>
23*36b606aeSDimitry Andric #  include <limits>
24*36b606aeSDimitry Andric #endif
25*36b606aeSDimitry Andric 
265f757f3fSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
275f757f3fSDimitry Andric #  pragma GCC system_header
285f757f3fSDimitry Andric #endif
295f757f3fSDimitry Andric 
30*36b606aeSDimitry Andric _LIBCPP_PUSH_MACROS
31*36b606aeSDimitry Andric #include <__undef_macros>
32*36b606aeSDimitry Andric 
335f757f3fSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
345f757f3fSDimitry Andric 
355f757f3fSDimitry Andric namespace __math {
365f757f3fSDimitry Andric 
375f757f3fSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI float hypot(float __x, float __y) _NOEXCEPT { return __builtin_hypotf(__x, __y); }
385f757f3fSDimitry Andric 
395f757f3fSDimitry Andric template <class = int>
405f757f3fSDimitry Andric _LIBCPP_HIDE_FROM_ABI double hypot(double __x, double __y) _NOEXCEPT {
415f757f3fSDimitry Andric   return __builtin_hypot(__x, __y);
425f757f3fSDimitry Andric }
435f757f3fSDimitry Andric 
445f757f3fSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI long double hypot(long double __x, long double __y) _NOEXCEPT {
455f757f3fSDimitry Andric   return __builtin_hypotl(__x, __y);
465f757f3fSDimitry Andric }
475f757f3fSDimitry Andric 
485f757f3fSDimitry Andric template <class _A1, class _A2, __enable_if_t<is_arithmetic<_A1>::value && is_arithmetic<_A2>::value, int> = 0>
495f757f3fSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI typename __promote<_A1, _A2>::type hypot(_A1 __x, _A2 __y) _NOEXCEPT {
505f757f3fSDimitry Andric   using __result_type = typename __promote<_A1, _A2>::type;
510fca6ea1SDimitry Andric   static_assert(!(_IsSame<_A1, __result_type>::value && _IsSame<_A2, __result_type>::value), "");
525f757f3fSDimitry Andric   return __math::hypot((__result_type)__x, (__result_type)__y);
535f757f3fSDimitry Andric }
545f757f3fSDimitry Andric 
55*36b606aeSDimitry Andric #if _LIBCPP_STD_VER >= 17
56*36b606aeSDimitry Andric // Factors needed to determine if over-/underflow might happen for `std::hypot(x,y,z)`.
57*36b606aeSDimitry Andric // returns [overflow_threshold, overflow_scale]
58*36b606aeSDimitry Andric template <class _Real>
59*36b606aeSDimitry Andric _LIBCPP_HIDE_FROM_ABI std::pair<_Real, _Real> __hypot_factors() {
60*36b606aeSDimitry Andric   static_assert(std::numeric_limits<_Real>::is_iec559);
61*36b606aeSDimitry Andric 
62*36b606aeSDimitry Andric   if constexpr (std::is_same_v<_Real, float>) {
63*36b606aeSDimitry Andric     static_assert(-125 == std::numeric_limits<_Real>::min_exponent);
64*36b606aeSDimitry Andric     static_assert(+128 == std::numeric_limits<_Real>::max_exponent);
65*36b606aeSDimitry Andric     return {0x1.0p+62f, 0x1.0p-70f};
66*36b606aeSDimitry Andric   } else if constexpr (std::is_same_v<_Real, double>) {
67*36b606aeSDimitry Andric     static_assert(-1021 == std::numeric_limits<_Real>::min_exponent);
68*36b606aeSDimitry Andric     static_assert(+1024 == std::numeric_limits<_Real>::max_exponent);
69*36b606aeSDimitry Andric     return {0x1.0p+510, 0x1.0p-600};
70*36b606aeSDimitry Andric   } else { // long double
71*36b606aeSDimitry Andric     static_assert(std::is_same_v<_Real, long double>);
72*36b606aeSDimitry Andric 
73*36b606aeSDimitry Andric     // preprocessor guard necessary, otherwise literals (e.g. `0x1.0p+8'190l`) throw warnings even when shielded by `if
74*36b606aeSDimitry Andric     // constexpr`
75*36b606aeSDimitry Andric #  if __DBL_MAX_EXP__ == __LDBL_MAX_EXP__
76*36b606aeSDimitry Andric     static_assert(sizeof(_Real) == sizeof(double));
77*36b606aeSDimitry Andric     return static_cast<std::pair<_Real, _Real>>(__math::__hypot_factors<double>());
78*36b606aeSDimitry Andric #  else
79*36b606aeSDimitry Andric     static_assert(sizeof(_Real) > sizeof(double));
80*36b606aeSDimitry Andric     static_assert(-16381 == std::numeric_limits<_Real>::min_exponent);
81*36b606aeSDimitry Andric     static_assert(+16384 == std::numeric_limits<_Real>::max_exponent);
82*36b606aeSDimitry Andric     return {0x1.0p+8190l, 0x1.0p-9000l};
83*36b606aeSDimitry Andric #  endif
84*36b606aeSDimitry Andric   }
85*36b606aeSDimitry Andric }
86*36b606aeSDimitry Andric 
87*36b606aeSDimitry Andric // Computes the three-dimensional hypotenuse: `std::hypot(x,y,z)`.
88*36b606aeSDimitry Andric // The naive implementation might over-/underflow which is why this implementation is more involved:
89*36b606aeSDimitry Andric //    If the square of an argument might run into issues, we scale the arguments appropriately.
90*36b606aeSDimitry Andric // See https://github.com/llvm/llvm-project/issues/92782 for a detailed discussion and summary.
91*36b606aeSDimitry Andric template <class _Real>
92*36b606aeSDimitry Andric _LIBCPP_HIDE_FROM_ABI _Real __hypot(_Real __x, _Real __y, _Real __z) {
93*36b606aeSDimitry Andric   const _Real __max_abs = std::max(__math::fabs(__x), std::max(__math::fabs(__y), __math::fabs(__z)));
94*36b606aeSDimitry Andric   const auto [__overflow_threshold, __overflow_scale] = __math::__hypot_factors<_Real>();
95*36b606aeSDimitry Andric   _Real __scale;
96*36b606aeSDimitry Andric   if (__max_abs > __overflow_threshold) { // x*x + y*y + z*z might overflow
97*36b606aeSDimitry Andric     __scale = __overflow_scale;
98*36b606aeSDimitry Andric     __x *= __scale;
99*36b606aeSDimitry Andric     __y *= __scale;
100*36b606aeSDimitry Andric     __z *= __scale;
101*36b606aeSDimitry Andric   } else if (__max_abs < 1 / __overflow_threshold) { // x*x + y*y + z*z might underflow
102*36b606aeSDimitry Andric     __scale = 1 / __overflow_scale;
103*36b606aeSDimitry Andric     __x *= __scale;
104*36b606aeSDimitry Andric     __y *= __scale;
105*36b606aeSDimitry Andric     __z *= __scale;
106*36b606aeSDimitry Andric   } else
107*36b606aeSDimitry Andric     __scale = 1;
108*36b606aeSDimitry Andric   return __math::sqrt(__x * __x + __y * __y + __z * __z) / __scale;
109*36b606aeSDimitry Andric }
110*36b606aeSDimitry Andric 
111*36b606aeSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI float hypot(float __x, float __y, float __z) { return __math::__hypot(__x, __y, __z); }
112*36b606aeSDimitry Andric 
113*36b606aeSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI double hypot(double __x, double __y, double __z) { return __math::__hypot(__x, __y, __z); }
114*36b606aeSDimitry Andric 
115*36b606aeSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI long double hypot(long double __x, long double __y, long double __z) {
116*36b606aeSDimitry Andric   return __math::__hypot(__x, __y, __z);
117*36b606aeSDimitry Andric }
118*36b606aeSDimitry Andric 
119*36b606aeSDimitry Andric template <class _A1,
120*36b606aeSDimitry Andric           class _A2,
121*36b606aeSDimitry Andric           class _A3,
122*36b606aeSDimitry Andric           std::enable_if_t< is_arithmetic_v<_A1> && is_arithmetic_v<_A2> && is_arithmetic_v<_A3>, int> = 0 >
123*36b606aeSDimitry Andric _LIBCPP_HIDE_FROM_ABI typename __promote<_A1, _A2, _A3>::type hypot(_A1 __x, _A2 __y, _A3 __z) _NOEXCEPT {
124*36b606aeSDimitry Andric   using __result_type = typename __promote<_A1, _A2, _A3>::type;
125*36b606aeSDimitry Andric   static_assert(!(
126*36b606aeSDimitry Andric       std::is_same_v<_A1, __result_type> && std::is_same_v<_A2, __result_type> && std::is_same_v<_A3, __result_type>));
127*36b606aeSDimitry Andric   return __math::__hypot(
128*36b606aeSDimitry Andric       static_cast<__result_type>(__x), static_cast<__result_type>(__y), static_cast<__result_type>(__z));
129*36b606aeSDimitry Andric }
130*36b606aeSDimitry Andric #endif
131*36b606aeSDimitry Andric 
1325f757f3fSDimitry Andric } // namespace __math
1335f757f3fSDimitry Andric 
1345f757f3fSDimitry Andric _LIBCPP_END_NAMESPACE_STD
135*36b606aeSDimitry Andric _LIBCPP_POP_MACROS
1365f757f3fSDimitry Andric 
1375f757f3fSDimitry Andric #endif // _LIBCPP___MATH_HYPOT_H
138