Skip to content

Commit d9074db

Browse files
authored
[flang] Optimize cospi precision (llvm#153208)
Part of llvm#150452.
1 parent 4537f0e commit d9074db

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
@@ -3684,10 +3684,10 @@ mlir::Value IntrinsicLibrary::genCospi(mlir::Type resultType,
36843684
mlir::MLIRContext *context = builder.getContext();
36853685
mlir::FunctionType ftype =
36863686
mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
3687-
llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);
3688-
mlir::Value dfactor =
3689-
builder.createRealConstant(loc, mlir::Float64Type::get(context), pi);
3690-
mlir::Value factor = builder.createConvert(loc, args[0].getType(), dfactor);
3687+
llvm::APFloat pi =
3688+
llvm::APFloat(llvm::cast<mlir::FloatType>(resultType).getFloatSemantics(),
3689+
llvm::numbers::pis);
3690+
mlir::Value factor = builder.createRealConstant(loc, resultType, pi);
36913691
mlir::Value arg = mlir::arith::MulFOp::create(builder, loc, args[0], factor);
36923692
return getRuntimeCallGenerator("cos", ftype)(builder, loc, {arg});
36933693
}

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

0 commit comments

Comments
 (0)