Skip to content

Commit b368e7f

Browse files
authored
[flang] optimize acosd precision (#154118)
Part of #150452.
1 parent ae75884 commit b368e7f

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

flang/lib/Optimizer/Builder/IntrinsicCall.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2672,10 +2672,11 @@ mlir::Value IntrinsicLibrary::genAcosd(mlir::Type resultType,
26722672
mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
26732673
mlir::Value result =
26742674
getRuntimeCallGenerator("acos", ftype)(builder, loc, {args[0]});
2675-
llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);
2676-
mlir::Value dfactor = builder.createRealConstant(
2677-
loc, mlir::Float64Type::get(context), llvm::APFloat(180.0) / pi);
2678-
mlir::Value factor = builder.createConvert(loc, args[0].getType(), dfactor);
2675+
const llvm::fltSemantics &fltSem =
2676+
llvm::cast<mlir::FloatType>(resultType).getFloatSemantics();
2677+
llvm::APFloat pi = llvm::APFloat(fltSem, llvm::numbers::pis);
2678+
mlir::Value factor = builder.createRealConstant(
2679+
loc, resultType, llvm::APFloat(fltSem, "180.0") / pi);
26792680
return mlir::arith::MulFOp::create(builder, loc, result, factor);
26802681
}
26812682

Lines changed: 14 additions & 4 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,9 +7,8 @@ function test_real4(x)
67
end function
78

89
! CHECK-LABEL: @_QPtest_real4
9-
! CHECK: %[[dfactor:.*]] = arith.constant 57.295779513082323 : f64
10+
! CHECK: %[[factor:.*]] = arith.constant 57.2957763 : f32
1011
! CHECK: %[[result:.*]] = math.acos %{{.*}} fastmath<contract> : f32
11-
! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32
1212
! CHECK: %[[arg:.*]] = arith.mulf %[[result]], %[[factor]] fastmath<contract> : f32
1313

1414
function test_real8(x)
@@ -17,6 +17,16 @@ function test_real8(x)
1717
end function
1818

1919
! CHECK-LABEL: @_QPtest_real8
20-
! CHECK: %[[dfactor:.*]] = arith.constant 57.295779513082323 : f64
20+
! CHECK: %[[factor:.*]] = arith.constant 57.295779513082323 : f64
2121
! CHECK: %[[result:.*]] = math.acos %{{.*}} fastmath<contract> : f64
22-
! CHECK: %[[arg:.*]] = arith.mulf %[[result]], %[[dfactor]] fastmath<contract> : f64
22+
! CHECK: %[[arg:.*]] = arith.mulf %[[result]], %[[factor]] fastmath<contract> : f64
23+
24+
function test_real16(x)
25+
real(16) :: x, test_real16
26+
test_real16 = acosd(x)
27+
end function
28+
29+
! CHECK-LABEL: @_QPtest_real16
30+
! CHECK: %[[factor:.*]] = arith.constant 57.295779513082320876798154814105{{.*}} : f128
31+
! CHECK: %[[result:.*]] = fir.call @_FortranAAcosF128({{.*}}) fastmath<contract> : (f128) -> f128
32+
! CHECK: %[[arg:.*]] = arith.mulf %[[result]], %[[factor]] fastmath<contract> : f128

0 commit comments

Comments
 (0)