Skip to content

Commit 34920d8

Browse files
committed
[flang] Optimize acospi precision
1 parent 7fb8630 commit 34920d8

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

flang/lib/Optimizer/Builder/IntrinsicCall.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2687,10 +2687,10 @@ mlir::Value IntrinsicLibrary::genAcospi(mlir::Type resultType,
26872687
mlir::FunctionType ftype =
26882688
mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
26892689
mlir::Value acos = getRuntimeCallGenerator("acos", ftype)(builder, loc, args);
2690-
llvm::APFloat inv_pi = llvm::APFloat(llvm::numbers::inv_pi);
2691-
mlir::Value dfactor =
2692-
builder.createRealConstant(loc, mlir::Float64Type::get(context), inv_pi);
2693-
mlir::Value factor = builder.createConvert(loc, resultType, dfactor);
2690+
llvm::APFloat inv_pi =
2691+
llvm::APFloat(llvm::cast<mlir::FloatType>(resultType).getFloatSemantics(),
2692+
llvm::numbers::inv_pis);
2693+
mlir::Value factor = builder.createRealConstant(loc, resultType, inv_pi);
26942694
return mlir::arith::MulFOp::create(builder, loc, acos, factor);
26952695
}
26962696

flang/test/Lower/Intrinsics/acospi.f90

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ function test_real4(x)
1010
! CHECK-LABEL: @_QPtest_real4
1111
! CHECK-PRECISE: %[[acos:.*]] = fir.call @acosf({{%[A-Za-z0-9._]+}}) fastmath<contract> : (f32) -> f32
1212
! CHECK-FAST: %[[acos:.*]] = math.acos %{{.*}} : f32
13-
! CHECK: %[[dpi:.*]] = arith.constant 0.31830988618379069 : f64
14-
! CHECK: %[[inv_pi:.*]] = fir.convert %[[dpi]] : (f64) -> f32
13+
! CHECK: %[[inv_pi:.*]] = arith.constant 0.318309873 : f32
1514
! CHECK: %{{.*}} = arith.mulf %[[acos]], %[[inv_pi]] fastmath<contract> : f32
1615

1716
function test_real8(x)

llvm/include/llvm/Support/MathExtras.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ constexpr float ef = 0x1.5bf0a8P+1F, // (2.71828183) https://oeis.org/A
7474
sqrt3f = 0x1.bb67aeP+0F, // (1.73205081) https://oeis.org/A002194
7575
inv_sqrt3f = 0x1.279a74P-1F, // (.577350269)
7676
phif = 0x1.9e377aP+0F; // (1.61803399) https://oeis.org/A001622
77+
constexpr const char *pis = "3.141592653589793238462643383279502884",
78+
*inv_pis = "0.318309886183790671537767526745028724";
7779
// clang-format on
7880
} // namespace numbers
7981

0 commit comments

Comments
 (0)