Skip to content

Commit 12e0d52

Browse files
authored
[flang] Optimize sinpi precision (#153211)
Part of #150452.
1 parent d9074db commit 12e0d52

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
@@ -8136,10 +8136,10 @@ mlir::Value IntrinsicLibrary::genSinpi(mlir::Type resultType,
81368136
mlir::MLIRContext *context = builder.getContext();
81378137
mlir::FunctionType ftype =
81388138
mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
8139-
llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);
8140-
mlir::Value dfactor =
8141-
builder.createRealConstant(loc, mlir::Float64Type::get(context), pi);
8142-
mlir::Value factor = builder.createConvert(loc, args[0].getType(), dfactor);
8139+
llvm::APFloat pi =
8140+
llvm::APFloat(llvm::cast<mlir::FloatType>(resultType).getFloatSemantics(),
8141+
llvm::numbers::pis);
8142+
mlir::Value factor = builder.createRealConstant(loc, resultType, pi);
81438143
mlir::Value arg = mlir::arith::MulFOp::create(builder, loc, args[0], factor);
81448144
return getRuntimeCallGenerator("sin", ftype)(builder, loc, {arg});
81458145
}

flang/test/Lower/Intrinsics/sinpi.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: %[[sin:.*]] = math.sin %[[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: %[[sin:.*]] = math.sin %[[mul]] fastmath<contract> : f64
23+
24+
function test_real16(x)
25+
real(16) :: x, test_real16
26+
test_real16 = sinpi(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: %[[sin:.*]] = fir.call @_FortranASinF128(%[[mul]]) fastmath<contract> : (f128) -> f128

0 commit comments

Comments
 (0)