Skip to content

Commit c664ce4

Browse files
authored
[flang] Optimize asinpi precision (llvm#153203)
Part of llvm#150452.
1 parent a203546 commit c664ce4

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
@@ -2855,10 +2855,10 @@ mlir::Value IntrinsicLibrary::genAsinpi(mlir::Type resultType,
28552855
mlir::FunctionType ftype =
28562856
mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
28572857
mlir::Value asin = getRuntimeCallGenerator("asin", ftype)(builder, loc, args);
2858-
llvm::APFloat inv_pi = llvm::APFloat(llvm::numbers::inv_pi);
2859-
mlir::Value dfactor =
2860-
builder.createRealConstant(loc, mlir::Float64Type::get(context), inv_pi);
2861-
mlir::Value factor = builder.createConvert(loc, resultType, dfactor);
2858+
llvm::APFloat inv_pi =
2859+
llvm::APFloat(llvm::cast<mlir::FloatType>(resultType).getFloatSemantics(),
2860+
llvm::numbers::inv_pis);
2861+
mlir::Value factor = builder.createRealConstant(loc, resultType, inv_pi);
28622862
return mlir::arith::MulFOp::create(builder, loc, asin, factor);
28632863
}
28642864

flang/test/Lower/Intrinsics/asinpi.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: bbc -emit-fir -hlfir=false %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
23
! RUN: bbc --math-runtime=precise -emit-fir -hlfir=false %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-PRECISE"
34
! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
@@ -10,8 +11,7 @@ function test_real4(x)
1011
! CHECK-LABEL: @_QPtest_real4
1112
! CHECK-PRECISE: %[[asin:.*]] = fir.call @asinf({{%[A-Za-z0-9._]+}}) fastmath<contract> : (f32) -> f32
1213
! CHECK-FAST: %[[asin:.*]] = math.asin %{{.*}} : f32
13-
! CHECK: %[[dpi:.*]] = arith.constant 0.31830988618379069 : f64
14-
! CHECK: %[[inv_pi:.*]] = fir.convert %[[dpi]] : (f64) -> f32
14+
! CHECK: %[[inv_pi:.*]] = arith.constant 0.318309873 : f32
1515
! CHECK: %{{.*}} = arith.mulf %[[asin]], %[[inv_pi]] fastmath<contract> : f32
1616

1717
function test_real8(x)
@@ -24,3 +24,13 @@ function test_real8(x)
2424
! CHECK-FAST: %[[asin:.*]] = math.asin %{{.*}} : f64
2525
! CHECK: %[[inv_pi:.*]] = arith.constant 0.31830988618379069 : f64
2626
! CHECK: %{{.*}} = arith.mulf %[[asin]], %[[inv_pi]] fastmath<contract> : f64
27+
28+
function test_real16(x)
29+
real(16) :: x, test_real16
30+
test_real16 = asinpi(x)
31+
end function
32+
33+
! CHECK-LABEL: @_QPtest_real16
34+
! CHECK: %[[asin:.*]] = fir.call @_FortranAAsinF128({{.*}}) fastmath<contract> : (f128) -> f128
35+
! CHECK: %[[inv_pi:.*]] = arith.constant 0.3183098861837906715377675267450{{.*}} : f128
36+
! CHECK: %{{.*}} = arith.mulf %[[asin]], %[[inv_pi]] fastmath<contract> : f128

0 commit comments

Comments
 (0)