diff --git a/flang/docs/Extensions.md b/flang/docs/Extensions.md --- a/flang/docs/Extensions.md +++ b/flang/docs/Extensions.md @@ -289,6 +289,8 @@ fixed form source by a '0' in column 6, can contain spaces between the letters of the word INCLUDE, and can have a numeric character literal kind prefix on the file name. +* Intrinsic procedures TAND and ATAND. Constant folding is currently + not supported for these procedures but this is planned. ### Extensions supported when enabled by options diff --git a/flang/include/flang/Optimizer/Builder/IntrinsicCall.h b/flang/include/flang/Optimizer/Builder/IntrinsicCall.h --- a/flang/include/flang/Optimizer/Builder/IntrinsicCall.h +++ b/flang/include/flang/Optimizer/Builder/IntrinsicCall.h @@ -316,6 +316,7 @@ llvm::ArrayRef); fir::ExtendedValue genSum(mlir::Type, llvm::ArrayRef); void genSystemClock(llvm::ArrayRef); + mlir::Value genTand(mlir::Type, llvm::ArrayRef); mlir::Value genTrailz(mlir::Type, llvm::ArrayRef); fir::ExtendedValue genTransfer(mlir::Type, llvm::ArrayRef); 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 @@ -511,6 +511,7 @@ &I::genSystemClock, {{{"count", asAddr}, {"count_rate", asAddr}, {"count_max", asAddr}}}, /*isElemental=*/false}, + {"tand", &I::genTand}, {"trailz", &I::genTrailz}, {"transfer", &I::genTransfer, @@ -4997,6 +4998,21 @@ .getResults()[0]; } +// TAND +mlir::Value IntrinsicLibrary::genTand(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()}); + llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi); + mlir::Value dfactor = builder.createRealConstant( + loc, mlir::FloatType::getF64(context), pi / llvm::APFloat(180.0)); + mlir::Value factor = builder.createConvert(loc, args[0].getType(), dfactor); + mlir::Value arg = builder.create(loc, args[0], factor); + return getRuntimeCallGenerator("tan", ftype)(builder, loc, {arg}); +} + // TRAILZ mlir::Value IntrinsicLibrary::genTrailz(mlir::Type resultType, llvm::ArrayRef args) { diff --git a/flang/test/Lower/Intrinsics/tand.f90 b/flang/test/Lower/Intrinsics/tand.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Lower/Intrinsics/tand.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 = tand(x) +end function + +! CHECK-LABEL: @_QPtest_real4 +! CHECK: %[[dfactor:.*]] = arith.constant 0.017453292519943295 : f64 +! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32 +! CHECK: %[[arg:.*]] = arith.mulf %{{[A-Za-z0-9._]+}}, %[[factor]] fastmath : f32 +! CHECK-PRECISE: %{{.*}} = fir.call @tanf(%[[arg]]) fastmath : (f32) -> f32 +! CHECK-FAST: %{{.*}} = math.tan %[[arg]] fastmath : f32 + +function test_real8(x) + real(8) :: x, test_real8 + test_real8 = tand(x) +end function + +! CHECK-LABEL: @_QPtest_real8 +! CHECK: %[[factor:.*]] = arith.constant 0.017453292519943295 : f64 +! CHECK: %[[arg:.*]] = arith.mulf %{{[A-Za-z0-9._]+}}, %[[factor]] fastmath : f64 +! CHECK-PRECISE: %{{.*}} = fir.call @tan(%[[arg]]) fastmath : (f64) -> f64 +! CHECK-FAST: %{{.*}} = math.tan %[[arg]] fastmath : f64