Skip to content

Commit 1742f2a

Browse files
committed
[flang] Use libm routine for compile-time folding on AIX
1 parent b6e9ba0 commit 1742f2a

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

flang/lib/Evaluate/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ add_flang_library(FortranEvaluate
5858
tools.cpp
5959
type.cpp
6060
variable.cpp
61+
wrappers.c
6162

6263
LINK_LIBS
6364
FortranCommon

flang/lib/Evaluate/intrinsics-library.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,26 @@ static std::complex<HostT> StdPowF2B(
277277
return std::pow(x, y);
278278
}
279279

280+
#ifdef _AIX
281+
extern "C" {
282+
void csqrtf_wrapper(const float[], float[]);
283+
void csqrt_wrapper(const double[], double[]);
284+
} // extern "C"
285+
286+
template <typename HostT>
287+
static std::complex<HostT> CSQRT(const std::complex<HostT> &x) {
288+
HostT y[2]{x.real(), x.imag()};
289+
HostT r[2];
290+
if constexpr (std::is_same_v<HostT, float>) {
291+
csqrtf_wrapper(y, r);
292+
} else if constexpr (std::is_same_v<HostT, double>) {
293+
csqrt_wrapper(y, r);
294+
}
295+
std::complex<HostT> res(r[0], r[1]);
296+
return res;
297+
}
298+
#endif
299+
280300
template <typename HostT>
281301
struct HostRuntimeLibrary<std::complex<HostT>, LibraryVersion::Libm> {
282302
using F = FuncPointer<std::complex<HostT>, const std::complex<HostT> &>;
@@ -302,7 +322,11 @@ struct HostRuntimeLibrary<std::complex<HostT>, LibraryVersion::Libm> {
302322
FolderFactory<F2B, F2B{StdPowF2B}>::Create("pow"),
303323
FolderFactory<F, F{std::sin}>::Create("sin"),
304324
FolderFactory<F, F{std::sinh}>::Create("sinh"),
325+
#ifdef _AIX
326+
FolderFactory<F, F{CSQRT}>::Create("sqrt"),
327+
#else
305328
FolderFactory<F, F{std::sqrt}>::Create("sqrt"),
329+
#endif
306330
FolderFactory<F, F{std::tan}>::Create("tan"),
307331
FolderFactory<F, F{std::tanh}>::Create("tanh"),
308332
};

flang/lib/Evaluate/wrappers.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <complex.h>
2+
3+
void csqrtf_wrapper(const float x[2], float res[2])
4+
{
5+
float complex c = x[0] + I * x[1];
6+
float complex r = csqrtf(c);
7+
res[0] = crealf(r);
8+
res[1] = cimagf(r);
9+
}
10+
11+
void csqrt_wrapper(const double x[2], double res[2])
12+
{
13+
double complex c = x[0] + I * x[1];
14+
double complex r = csqrt(c);
15+
res[0] = creal(r);
16+
res[1] = cimag(r);
17+
}

0 commit comments

Comments
 (0)