Skip to content

Commit cbcea13

Browse files
committed
[flang] Implement tanpi
1 parent bbbe69f commit cbcea13

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

flang/include/flang/Optimizer/Builder/IntrinsicCall.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ struct IntrinsicLibrary {
439439
mlir::ArrayRef<fir::ExtendedValue> args);
440440
void genSystemClock(llvm::ArrayRef<fir::ExtendedValue>);
441441
mlir::Value genTand(mlir::Type, llvm::ArrayRef<mlir::Value>);
442+
mlir::Value genTanpi(mlir::Type, llvm::ArrayRef<mlir::Value>);
442443
mlir::Value genTime(mlir::Type, llvm::ArrayRef<mlir::Value>);
443444
mlir::Value genTrailz(mlir::Type, llvm::ArrayRef<mlir::Value>);
444445
fir::ExtendedValue genTransfer(mlir::Type,

flang/lib/Evaluate/intrinsics.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,7 @@ static const IntrinsicInterface genericIntrinsicFunction[]{
989989
{"tan", {{"x", SameFloating}}, SameFloating},
990990
{"tand", {{"x", SameFloating}}, SameFloating},
991991
{"tanh", {{"x", SameFloating}}, SameFloating},
992+
{"tanpi", {{"x", SameFloating}}, SameFloating},
992993
{"team_number", {OptionalTEAM}, DefaultInt, Rank::scalar,
993994
IntrinsicClass::transformationalFunction},
994995
{"this_image",

flang/lib/Optimizer/Builder/IntrinsicCall.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,7 @@ static constexpr IntrinsicHandler handlers[]{
942942
{{{"count", asAddr}, {"count_rate", asAddr}, {"count_max", asAddr}}},
943943
/*isElemental=*/false},
944944
{"tand", &I::genTand},
945+
{"tanpi", &I::genTanpi},
945946
{"this_grid", &I::genThisGrid, {}, /*isElemental=*/false},
946947
{"this_thread_block", &I::genThisThreadBlock, {}, /*isElemental=*/false},
947948
{"this_warp", &I::genThisWarp, {}, /*isElemental=*/false},
@@ -8177,6 +8178,21 @@ mlir::Value IntrinsicLibrary::genTand(mlir::Type resultType,
81778178
return getRuntimeCallGenerator("tan", ftype)(builder, loc, {arg});
81788179
}
81798180

8181+
// TANPI
8182+
mlir::Value IntrinsicLibrary::genTanpi(mlir::Type resultType,
8183+
llvm::ArrayRef<mlir::Value> args) {
8184+
assert(args.size() == 1);
8185+
mlir::MLIRContext *context = builder.getContext();
8186+
mlir::FunctionType ftype =
8187+
mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
8188+
llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);
8189+
mlir::Value dfactor =
8190+
builder.createRealConstant(loc, mlir::Float64Type::get(context), pi);
8191+
mlir::Value factor = builder.createConvert(loc, args[0].getType(), dfactor);
8192+
mlir::Value arg = builder.create<mlir::arith::MulFOp>(loc, args[0], factor);
8193+
return getRuntimeCallGenerator("tan", ftype)(builder, loc, {arg});
8194+
}
8195+
81808196
// THIS_GRID
81818197
mlir::Value IntrinsicLibrary::genThisGrid(mlir::Type resultType,
81828198
llvm::ArrayRef<mlir::Value> args) {

flang/test/Lower/Intrinsics/tanpi.f90

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK"
2+
3+
function test_real4(x)
4+
real :: x, test_real4
5+
test_real4 = tanpi(x)
6+
end function
7+
8+
! CHECK-LABEL: @_QPtest_real4
9+
! CHECK: %[[dfactor:.*]] = arith.constant 3.1415926535897931 : f64
10+
! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32
11+
! CHECK: %[[mul:.*]] = arith.mulf %{{.*}}, %[[factor]] fastmath<contract> : f32
12+
! CHECK: %[[tan:.*]] = math.tan %[[mul]] fastmath<contract> : f32
13+
14+
function test_real8(x)
15+
real(8) :: x, test_real8
16+
test_real8 = tanpi(x)
17+
end function
18+
19+
! CHECK-LABEL: @_QPtest_real8
20+
! CHECK: %[[dfactor:.*]] = arith.constant 3.1415926535897931 : f64
21+
! CHECK: %[[mul:.*]] = arith.mulf %{{.*}}, %[[dfactor]] fastmath<contract> : f64
22+
! CHECK: %[[tan:.*]] = math.tan %[[mul]] fastmath<contract> : f64

0 commit comments

Comments
 (0)