Skip to content

Commit 484e554

Browse files
committed
[libcxx] replaces sqrt(complex<T>) implementation with builtin
This significantly improves the accuracy of the function. Fixes #122172
1 parent 504f6ce commit 484e554

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

libcxx/include/complex

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,16 +1059,21 @@ inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> log10(const complex<_Tp>& __x) {
10591059

10601060
// sqrt
10611061

1062+
_LIBCPP_HIDE_FROM_ABI inline _Complex float __csqrt(_Complex float __v) {
1063+
return __builtin_csqrtf(__v);
1064+
}
1065+
1066+
_LIBCPP_HIDE_FROM_ABI inline _Complex double __csqrt(_Complex double __v) {
1067+
return __builtin_csqrt(__v);
1068+
}
1069+
1070+
_LIBCPP_HIDE_FROM_ABI inline _Complex long double __csqrt(_Complex long double __v) {
1071+
return __builtin_csqrtl(__v);
1072+
}
1073+
10621074
template <class _Tp>
10631075
_LIBCPP_HIDE_FROM_ABI complex<_Tp> sqrt(const complex<_Tp>& __x) {
1064-
if (std::isinf(__x.imag()))
1065-
return complex<_Tp>(_Tp(INFINITY), __x.imag());
1066-
if (std::isinf(__x.real())) {
1067-
if (__x.real() > _Tp(0))
1068-
return complex<_Tp>(__x.real(), std::isnan(__x.imag()) ? __x.imag() : std::copysign(_Tp(0), __x.imag()));
1069-
return complex<_Tp>(std::isnan(__x.imag()) ? __x.imag() : _Tp(0), std::copysign(__x.real(), __x.imag()));
1070-
}
1071-
return std::polar(std::sqrt(std::abs(__x)), std::arg(__x) / _Tp(2));
1076+
return complex<_Tp>(__from_builtin_tag(), std::__csqrt(__x.__builtin()));
10721077
}
10731078

10741079
// exp

0 commit comments

Comments
 (0)