xref: /freebsd-src/contrib/llvm-project/libcxx/include/__math/hypot.h (revision 81e300df5e654eee3835ec2c6a2ae34cf317df72)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___MATH_HYPOT_H
10 #define _LIBCPP___MATH_HYPOT_H
11 
12 #include <__config>
13 #include <__type_traits/enable_if.h>
14 #include <__type_traits/is_arithmetic.h>
15 #include <__type_traits/is_same.h>
16 #include <__type_traits/promote.h>
17 
18 #if _LIBCPP_STD_VER >= 17
19 #  include <__algorithm/max.h>
20 #  include <__math/abs.h>
21 #  include <__math/roots.h>
22 #  include <__utility/pair.h>
23 #  include <limits>
24 #endif
25 
26 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
27 #  pragma GCC system_header
28 #endif
29 
30 _LIBCPP_PUSH_MACROS
31 #include <__undef_macros>
32 
33 _LIBCPP_BEGIN_NAMESPACE_STD
34 
35 namespace __math {
36 
37 inline _LIBCPP_HIDE_FROM_ABI float hypot(float __x, float __y) _NOEXCEPT { return __builtin_hypotf(__x, __y); }
38 
39 template <class = int>
40 _LIBCPP_HIDE_FROM_ABI double hypot(double __x, double __y) _NOEXCEPT {
41   return __builtin_hypot(__x, __y);
42 }
43 
44 inline _LIBCPP_HIDE_FROM_ABI long double hypot(long double __x, long double __y) _NOEXCEPT {
45   return __builtin_hypotl(__x, __y);
46 }
47 
48 template <class _A1, class _A2, __enable_if_t<is_arithmetic<_A1>::value && is_arithmetic<_A2>::value, int> = 0>
49 inline _LIBCPP_HIDE_FROM_ABI typename __promote<_A1, _A2>::type hypot(_A1 __x, _A2 __y) _NOEXCEPT {
50   using __result_type = typename __promote<_A1, _A2>::type;
51   static_assert(!(_IsSame<_A1, __result_type>::value && _IsSame<_A2, __result_type>::value), "");
52   return __math::hypot((__result_type)__x, (__result_type)__y);
53 }
54 
55 #if _LIBCPP_STD_VER >= 17
56 // Factors needed to determine if over-/underflow might happen for `std::hypot(x,y,z)`.
57 // returns [overflow_threshold, overflow_scale]
58 template <class _Real>
59 _LIBCPP_HIDE_FROM_ABI std::pair<_Real, _Real> __hypot_factors() {
60   static_assert(std::numeric_limits<_Real>::is_iec559);
61 
62   if constexpr (std::is_same_v<_Real, float>) {
63     static_assert(-125 == std::numeric_limits<_Real>::min_exponent);
64     static_assert(+128 == std::numeric_limits<_Real>::max_exponent);
65     return {0x1.0p+62f, 0x1.0p-70f};
66   } else if constexpr (std::is_same_v<_Real, double>) {
67     static_assert(-1021 == std::numeric_limits<_Real>::min_exponent);
68     static_assert(+1024 == std::numeric_limits<_Real>::max_exponent);
69     return {0x1.0p+510, 0x1.0p-600};
70   } else { // long double
71     static_assert(std::is_same_v<_Real, long double>);
72 
73     // preprocessor guard necessary, otherwise literals (e.g. `0x1.0p+8'190l`) throw warnings even when shielded by `if
74     // constexpr`
75 #  if __DBL_MAX_EXP__ == __LDBL_MAX_EXP__
76     static_assert(sizeof(_Real) == sizeof(double));
77     return static_cast<std::pair<_Real, _Real>>(__math::__hypot_factors<double>());
78 #  else
79     static_assert(sizeof(_Real) > sizeof(double));
80     static_assert(-16381 == std::numeric_limits<_Real>::min_exponent);
81     static_assert(+16384 == std::numeric_limits<_Real>::max_exponent);
82     return {0x1.0p+8190l, 0x1.0p-9000l};
83 #  endif
84   }
85 }
86 
87 // Computes the three-dimensional hypotenuse: `std::hypot(x,y,z)`.
88 // The naive implementation might over-/underflow which is why this implementation is more involved:
89 //    If the square of an argument might run into issues, we scale the arguments appropriately.
90 // See https://github.com/llvm/llvm-project/issues/92782 for a detailed discussion and summary.
91 template <class _Real>
92 _LIBCPP_HIDE_FROM_ABI _Real __hypot(_Real __x, _Real __y, _Real __z) {
93   const _Real __max_abs = std::max(__math::fabs(__x), std::max(__math::fabs(__y), __math::fabs(__z)));
94   const auto [__overflow_threshold, __overflow_scale] = __math::__hypot_factors<_Real>();
95   _Real __scale;
96   if (__max_abs > __overflow_threshold) { // x*x + y*y + z*z might overflow
97     __scale = __overflow_scale;
98     __x *= __scale;
99     __y *= __scale;
100     __z *= __scale;
101   } else if (__max_abs < 1 / __overflow_threshold) { // x*x + y*y + z*z might underflow
102     __scale = 1 / __overflow_scale;
103     __x *= __scale;
104     __y *= __scale;
105     __z *= __scale;
106   } else
107     __scale = 1;
108   return __math::sqrt(__x * __x + __y * __y + __z * __z) / __scale;
109 }
110 
111 inline _LIBCPP_HIDE_FROM_ABI float hypot(float __x, float __y, float __z) { return __math::__hypot(__x, __y, __z); }
112 
113 inline _LIBCPP_HIDE_FROM_ABI double hypot(double __x, double __y, double __z) { return __math::__hypot(__x, __y, __z); }
114 
115 inline _LIBCPP_HIDE_FROM_ABI long double hypot(long double __x, long double __y, long double __z) {
116   return __math::__hypot(__x, __y, __z);
117 }
118 
119 template <class _A1,
120           class _A2,
121           class _A3,
122           std::enable_if_t< is_arithmetic_v<_A1> && is_arithmetic_v<_A2> && is_arithmetic_v<_A3>, int> = 0 >
123 _LIBCPP_HIDE_FROM_ABI typename __promote<_A1, _A2, _A3>::type hypot(_A1 __x, _A2 __y, _A3 __z) _NOEXCEPT {
124   using __result_type = typename __promote<_A1, _A2, _A3>::type;
125   static_assert(!(
126       std::is_same_v<_A1, __result_type> && std::is_same_v<_A2, __result_type> && std::is_same_v<_A3, __result_type>));
127   return __math::__hypot(
128       static_cast<__result_type>(__x), static_cast<__result_type>(__y), static_cast<__result_type>(__z));
129 }
130 #endif
131 
132 } // namespace __math
133 
134 _LIBCPP_END_NAMESPACE_STD
135 _LIBCPP_POP_MACROS
136 
137 #endif // _LIBCPP___MATH_HYPOT_H
138