Skip to content

Commit f4dd442

Browse files
authored
[flang] Optimize tanpi precision (#153215)
Part of #150452.
1 parent 12e0d52 commit f4dd442

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

flang/lib/Optimizer/Builder/IntrinsicCall.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8233,10 +8233,10 @@ mlir::Value IntrinsicLibrary::genTanpi(mlir::Type resultType,
82338233
mlir::MLIRContext *context = builder.getContext();
82348234
mlir::FunctionType ftype =
82358235
mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
8236-
llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);
8237-
mlir::Value dfactor =
8238-
builder.createRealConstant(loc, mlir::Float64Type::get(context), pi);
8239-
mlir::Value factor = builder.createConvert(loc, args[0].getType(), dfactor);
8236+
llvm::APFloat pi =
8237+
llvm::APFloat(llvm::cast<mlir::FloatType>(resultType).getFloatSemantics(),
8238+
llvm::numbers::pis);
8239+
mlir::Value factor = builder.createRealConstant(loc, resultType, pi);
82408240
mlir::Value arg = mlir::arith::MulFOp::create(builder, loc, args[0], factor);
82418241
return getRuntimeCallGenerator("tan", ftype)(builder, loc, {arg});
82428242
}

flang/test/Lower/Intrinsics/tanpi.f90

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
! REQUIRES: flang-supports-f128-math
12
! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK"
23

34
function test_real4(x)
@@ -6,8 +7,7 @@ function test_real4(x)
67
end function
78

89
! CHECK-LABEL: @_QPtest_real4
9-
! CHECK: %[[dfactor:.*]] = arith.constant 3.1415926535897931 : f64
10-
! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32
10+
! CHECK: %[[factor:.*]] = arith.constant 3.14159274 : f32
1111
! CHECK: %[[mul:.*]] = arith.mulf %{{.*}}, %[[factor]] fastmath<contract> : f32
1212
! CHECK: %[[tan:.*]] = math.tan %[[mul]] fastmath<contract> : f32
1313

@@ -20,3 +20,13 @@ function test_real8(x)
2020
! CHECK: %[[dfactor:.*]] = arith.constant 3.1415926535897931 : f64
2121
! CHECK: %[[mul:.*]] = arith.mulf %{{.*}}, %[[dfactor]] fastmath<contract> : f64
2222
! CHECK: %[[tan:.*]] = math.tan %[[mul]] fastmath<contract> : f64
23+
24+
function test_real16(x)
25+
real(16) :: x, test_real16
26+
test_real16 = tanpi(x)
27+
end function
28+
29+
! CHECK-LABEL: @_QPtest_real16
30+
! CHECK: %[[factor:.*]] = arith.constant 3.141592653589793238462643383279{{.*}} : f128
31+
! CHECK: %[[mul:.*]] = arith.mulf %{{.*}}, %[[factor]] fastmath<contract> : f128
32+
! CHECK: %[[tan:.*]] = fir.call @_FortranATanF128(%[[mul]]) fastmath<contract> : (f128) -> f128

0 commit comments

Comments
 (0)