Skip to content

Commit

Permalink
[libcxx] replaces sqrt(complex<T>) implementation with builtin
Browse files Browse the repository at this point in the history
This significantly improves the accuracy of the function.

Fixes llvm#122172
  • Loading branch information
cjdb committed Jan 9, 2025
1 parent 504f6ce commit 484e554
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions libcxx/include/complex
Original file line number Diff line number Diff line change
Expand Up @@ -1059,16 +1059,21 @@ inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> log10(const complex<_Tp>& __x) {

// sqrt

_LIBCPP_HIDE_FROM_ABI inline _Complex float __csqrt(_Complex float __v) {
return __builtin_csqrtf(__v);
}

_LIBCPP_HIDE_FROM_ABI inline _Complex double __csqrt(_Complex double __v) {
return __builtin_csqrt(__v);
}

_LIBCPP_HIDE_FROM_ABI inline _Complex long double __csqrt(_Complex long double __v) {
return __builtin_csqrtl(__v);
}

template <class _Tp>
_LIBCPP_HIDE_FROM_ABI complex<_Tp> sqrt(const complex<_Tp>& __x) {
if (std::isinf(__x.imag()))
return complex<_Tp>(_Tp(INFINITY), __x.imag());
if (std::isinf(__x.real())) {
if (__x.real() > _Tp(0))
return complex<_Tp>(__x.real(), std::isnan(__x.imag()) ? __x.imag() : std::copysign(_Tp(0), __x.imag()));
return complex<_Tp>(std::isnan(__x.imag()) ? __x.imag() : _Tp(0), std::copysign(__x.real(), __x.imag()));
}
return std::polar(std::sqrt(std::abs(__x)), std::arg(__x) / _Tp(2));
return complex<_Tp>(__from_builtin_tag(), std::__csqrt(__x.__builtin()));
}

// exp
Expand Down

0 comments on commit 484e554

Please sign in to comment.