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 @@ -42,6 +42,7 @@ #include "mlir/Dialect/Math/IR/Math.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" +#include "llvm/Support/MathExtras.h" #include #define DEBUG_TYPE "flang-lower-intrinsic" @@ -179,6 +180,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, @@ -489,6 +491,7 @@ &I::genAssociated, {{{"pointer", asInquired}, {"target", asInquired}}}, /*isElemental=*/false}, + {"atand", &I::genAtand}, {"bessel_jn", &I::genBesselJn, {{{"n1", asValue}, {"n2", asValue}, {"x", asValue}}}, @@ -2333,6 +2336,20 @@ 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 pi = llvm::APFloat(llvm::numbers::pi); + mlir::Value dfactor = builder.createRealConstant( + loc, mlir::FloatType::getF64(context), llvm::APFloat(180.0) / pi); + mlir::Value factor = builder.createConvert(loc, resultType, dfactor); + 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,26 @@ +! 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._]+}}) fastmath : (f32) -> f32 +! CHECK-FAST: %[[atan:.*]] = math.atan %{{.*}} : f32 +! CHECK: %[[dfactor:.*]] = arith.constant 57.295779513082323 : f64 +! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32 +! CHECK: %{{.*}} = arith.mulf %[[atan]], %[[factor]] fastmath : 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._]+}}) fastmath : (f64) -> f64 +! CHECK-FAST: %[[atan:.*]] = math.atan %{{.*}} : f64 +! CHECK: %[[factor:.*]] = arith.constant 57.295779513082323 : f64 +! CHECK: %{{.*}} = arith.mulf %[[atan]], %[[factor]] fastmath : f64