Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions flang/lib/Evaluate/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ add_flang_library(FortranEvaluate
tools.cpp
type.cpp
variable.cpp
wrappers.c

LINK_LIBS
FortranCommon
Expand Down
28 changes: 27 additions & 1 deletion flang/lib/Evaluate/intrinsics-library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,32 @@ static std::complex<HostT> StdPowF2B(
return std::pow(x, y);
}

#ifdef _AIX
extern "C" {
void csqrtf_wrapper(const float[], float[]);
void csqrt_wrapper(const double[], double[]);
} // extern "C"
#endif

template <typename HostT>
static std::complex<HostT> CSqrt(const std::complex<HostT> &x) {
std::complex<HostT> res;
#if _AIX
HostT y[2]{x.real(), x.imag()};
HostT r[2];
if constexpr (std::is_same_v<HostT, float>) {
csqrtf_wrapper(y, r);
} else if constexpr (std::is_same_v<HostT, double>) {
csqrt_wrapper(y, r);
}
res.real(r[0]);
res.imag(r[1]);
#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 +328,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
25 changes: 25 additions & 0 deletions flang/lib/Evaluate/wrappers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===-- lib/Evaluate/wrappers.c -------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifdef _AIX
#include <complex.h>

void csqrtf_wrapper(const float x[2], float res[2]) {
float complex c = x[0] + I * x[1];
float complex r = csqrtf(c);
res[0] = crealf(r);
res[1] = cimagf(r);
}

void csqrt_wrapper(const double x[2], double res[2]) {
double complex c = x[0] + I * x[1];
double complex r = csqrt(c);
res[0] = creal(r);
res[1] = cimag(r);
}
#endif