From: David Truby Date: Mon, 27 Feb 2023 16:41:26 +0000 (+0000) Subject: [flang] Implement atand intrinsic X-Git-Tag: upstream/17.0.6~15512 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=00671c0fa7ae18c11ab46b012c1aedcf4ef0f6f1;p=platform%2Fupstream%2Fllvm.git [flang] Implement atand intrinsic This implements the atand intrinsic by performing a multiplication by 180/pi to the result of a call to atan inline. Differential Revision: https://reviews.llvm.org/D144885 --- diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp index 53bc24c..26ea98c 100644 --- 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 @@ struct IntrinsicLibrary { 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 @@ static constexpr IntrinsicHandler handlers[]{ &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 @@ IntrinsicLibrary::genAny(mlir::Type resultType, 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 index 0000000..eea9c79 --- /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