Skip to content

Commit 36f6e4b

Browse files
committed
[flang] Optimize acospi precision
1 parent 3d38a92 commit 36f6e4b

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-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: 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: %[[acos:.*]] = fir.call @acosf({{%[A-Za-z0-9._]+}}) fastmath<contract> : (f32) -> f32
1213
! CHECK-FAST: %[[acos:.*]] = math.acos %{{.*}} : 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 %[[acos]], %[[inv_pi]] fastmath<contract> : f32
1616

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

llvm/include/llvm/Support/MathExtras.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ 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+
// These string literals are taken from below:
78+
// https://github.com/bminor/glibc/blob/8543577b04ded6d979ffcc5a818930e4d74d0645/math/math.h#L1215-L1229
79+
constexpr const char *pis = "3.141592653589793238462643383279502884",
80+
*inv_pis = "0.318309886183790671537767526745028724";
7781
// clang-format on
7882
} // namespace numbers
7983

0 commit comments

Comments
 (0)