Skip to content
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion flang/lib/Evaluate/intrinsics-library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "fold-implementation.h"
#include "host.h"
#include "flang/Common/erfc-scaled.h"
#include "flang/Common/idioms.h"
#include "flang/Common/static-multimap-view.h"
#include "flang/Evaluate/expression.h"
#include <cfloat>
Expand Down Expand Up @@ -277,6 +278,46 @@ static std::complex<HostT> StdPowF2B(
return std::pow(x, y);
}

#ifdef _AIX
#ifdef __clang_major__
#pragma clang diagnostic ignored "-Wc99-extensions"
#endif

extern "C" {
float _Complex csqrtf(float _Complex);
double _Complex csqrt(double _Complex);
}
#endif

template <typename HostT>
static std::complex<HostT> CSqrt(const std::complex<HostT> &x) {
std::complex<HostT> res;
#ifdef _AIX
// On AIX, the implementation of csqrt[f] and std::sqrt is different,
// use csqrt[f] in folding.
if constexpr (std::is_same_v<HostT, float>) {
float _Complex c;
reinterpret_cast<HostT(&)[2]>(c)[0] = x.real();
reinterpret_cast<HostT(&)[2]>(c)[1] = x.imag();
float _Complex r{csqrtf(c)};
res.real(reinterpret_cast<HostT(&)[2]>(r)[0]);
res.imag(reinterpret_cast<HostT(&)[2]>(r)[1]);
} else if constexpr (std::is_same_v<HostT, double>) {
double _Complex c;
reinterpret_cast<HostT(&)[2]>(c)[0] = x.real();
reinterpret_cast<HostT(&)[2]>(c)[1] = x.imag();
double _Complex r{csqrt(c)};
res.real(reinterpret_cast<HostT(&)[2]>(r)[0]);
res.imag(reinterpret_cast<HostT(&)[2]>(r)[1]);
} else {
DIE("bad complex component type");
}
#else
res = std::sqrt(x);
#endif
return res;
}

template <typename HostT>
struct HostRuntimeLibrary<std::complex<HostT>, LibraryVersion::Libm> {
using F = FuncPointer<std::complex<HostT>, const std::complex<HostT> &>;
Expand All @@ -302,7 +343,7 @@ struct HostRuntimeLibrary<std::complex<HostT>, LibraryVersion::Libm> {
FolderFactory<F2B, F2B{StdPowF2B}>::Create("pow"),
FolderFactory<F, F{std::sin}>::Create("sin"),
FolderFactory<F, F{std::sinh}>::Create("sinh"),
FolderFactory<F, F{std::sqrt}>::Create("sqrt"),
FolderFactory<F, F{CSqrt}>::Create("sqrt"),
FolderFactory<F, F{std::tan}>::Create("tan"),
FolderFactory<F, F{std::tanh}>::Create("tanh"),
};
Expand Down