Skip to content

Commit a0eb995

Browse files
authored
[flang] optimize asind precision (llvm#154350)
Part of llvm#150452.
1 parent a5f1ddd commit a0eb995

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
@@ -2841,10 +2841,11 @@ mlir::Value IntrinsicLibrary::genAsind(mlir::Type resultType,
28412841
mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
28422842
mlir::Value result =
28432843
getRuntimeCallGenerator("asin", ftype)(builder, loc, {args[0]});
2844-
llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);
2845-
mlir::Value dfactor = builder.createRealConstant(
2846-
loc, mlir::Float64Type::get(context), llvm::APFloat(180.0) / pi);
2847-
mlir::Value factor = builder.createConvert(loc, args[0].getType(), dfactor);
2844+
const llvm::fltSemantics &fltSem =
2845+
llvm::cast<mlir::FloatType>(resultType).getFloatSemantics();
2846+
llvm::APFloat pi = llvm::APFloat(fltSem, llvm::numbers::pis);
2847+
mlir::Value factor = builder.createRealConstant(
2848+
loc, resultType, llvm::APFloat(fltSem, "180.0") / pi);
28482849
return mlir::arith::MulFOp::create(builder, loc, result, factor);
28492850
}
28502851

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.asin %{{.*}} 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.asin %{{.*}} 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 = asind(x)
27+
end function
28+
29+
! CHECK-LABEL: @_QPtest_real16
30+
! CHECK: %[[factor:.*]] = arith.constant 57.295779513082320876798154814105{{.*}} : f128
31+
! CHECK: %[[result:.*]] = fir.call @_FortranAAsinF128({{.*}}) fastmath<contract> : (f128) -> f128
32+
! CHECK: %[[arg:.*]] = arith.mulf %[[result]], %[[factor]] fastmath<contract> : f128

0 commit comments

Comments
 (0)