diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp --- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp +++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp @@ -40,8 +40,11 @@ #include "mlir/Dialect/Complex/IR/Complex.h" #include "mlir/Dialect/LLVMIR/LLVMDialect.h" #include "mlir/Dialect/Math/IR/Math.h" +#include "llvm/ADT/APFloat.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" +#include "llvm/Support/MathExtras.h" +#include #include #define DEBUG_TYPE "flang-lower-intrinsic" @@ -179,6 +182,7 @@ llvm::ArrayRef); mlir::Value genAnint(mlir::Type, llvm::ArrayRef); fir::ExtendedValue genAny(mlir::Type, llvm::ArrayRef); + mlir::Value genAtand(mlir::Type, llvm::ArrayRef); fir::ExtendedValue genCommandArgumentCount(mlir::Type, llvm::ArrayRef); fir::ExtendedValue genAssociated(mlir::Type, @@ -485,6 +489,7 @@ &I::genAssociated, {{{"pointer", asInquired}, {"target", asInquired}}}, /*isElemental=*/false}, + {"atand", &I::genAtand}, {"bessel_jn", &I::genBesselJn, {{{"n1", asValue}, {"n2", asValue}, {"x", asValue}}}, @@ -2313,6 +2318,22 @@ return readAndAddCleanUp(resultMutableBox, resultType, "ANY"); } +mlir::Value IntrinsicLibrary::genAtand(mlir::Type resultType, + llvm::ArrayRef args) { + assert(args.size() == 1); + mlir::MLIRContext *context = builder.getContext(); + mlir::FunctionType ftype = + mlir::FunctionType::get(context, {resultType}, {args[0].getType()}); + mlir::Value atan = getRuntimeCallGenerator("atan", ftype)(builder, loc, args); + llvm::APFloat lpi = llvm::APFloat(llvm::numbers::pi); + mlir::Value dpi = + builder.createRealConstant(loc, mlir::FloatType::getF64(context), lpi); + mlir::Value pi = builder.createConvert(loc, resultType, dpi); + mlir::Value factor = builder.create( + loc, builder.createRealConstant(loc, resultType, 180), pi); + return builder.create(loc, atan, factor); +} + // ASSOCIATED fir::ExtendedValue IntrinsicLibrary::genAssociated(mlir::Type resultType, diff --git a/flang/test/Lower/Intrinsics/atand.f90 b/flang/test/Lower/Intrinsics/atand.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Lower/Intrinsics/atand.f90 @@ -0,0 +1,30 @@ +! RUN: bbc -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST" +! RUN: bbc --math-runtime=precise -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-PRECISE" +! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST" + +function test_real4(x) + real :: x, test_real4 + test_real4 = atand(x) +end function + +! CHECK-LABEL: @_QPtest_real4 +! CHECK-PRECISE: %[[atan:.*]] = fir.call @atanf({{%[A-Za-z0-9._]+}}) {{.*}}: (f32) -> f32 +! CHECK-FAST: %[[atan:.*]] = math.atan %{{.*}} {{.*}} : f32 +! CHECK: %[[dpi:.*]] = arith.constant 3.1415926535897931 : f64 +! CHECK: %[[pi:.*]] = fir.convert %[[dpi]] : (f64) -> f32 +! CHECK: %[[deg:.*]] = arith.constant 1.800000e+02 : f32 +! CHECK: %[[factor:.*]] = arith.divf %[[deg]], %[[pi]] {{.*}} : f32 +! CHECK: %{{.*}} = arith.mulf %[[atan]], %[[factor]] {{.*}} : f32 + +function test_real8(x) + real(8) :: x, test_real8 + test_real8 = atand(x) +end function + +! CHECK-LABEL: @_QPtest_real8 +! CHECK-PRECISE: %[[atan:.*]] = fir.call @atan({{%[A-Za-z0-9._]+}}) {{.*}}: (f64) -> f64 +! CHECK-FAST: %[[atan:.*]] = math.atan %{{.*}} {{.*}} : f64 +! CHECK: %[[pi:.*]] = arith.constant 3.1415926535897931 : f64 +! CHECK: %[[deg:.*]] = arith.constant 1.800000e+02 : f64 +! CHECK: %[[factor:.*]] = arith.divf %[[deg]], %[[pi]] {{.*}} : f64 +! CHECK: %{{.*}} = arith.mulf %[[atan]], %[[factor]] {{.*}} : f64