Skip to content

[flang] Optimize acospi precision #152869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions flang/lib/Optimizer/Builder/IntrinsicCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2687,10 +2687,10 @@ mlir::Value IntrinsicLibrary::genAcospi(mlir::Type resultType,
mlir::FunctionType ftype =
mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
mlir::Value acos = getRuntimeCallGenerator("acos", ftype)(builder, loc, args);
llvm::APFloat inv_pi = llvm::APFloat(llvm::numbers::inv_pi);
mlir::Value dfactor =
builder.createRealConstant(loc, mlir::Float64Type::get(context), inv_pi);
mlir::Value factor = builder.createConvert(loc, resultType, dfactor);
llvm::APFloat inv_pi =
llvm::APFloat(llvm::cast<mlir::FloatType>(resultType).getFloatSemantics(),
llvm::numbers::inv_pis);
mlir::Value factor = builder.createRealConstant(loc, resultType, inv_pi);
return mlir::arith::MulFOp::create(builder, loc, acos, factor);
}

Expand Down
14 changes: 12 additions & 2 deletions flang/test/Lower/Intrinsics/acospi.f90
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
! REQUIRES: flang-supports-f128-math
! RUN: bbc -emit-fir -hlfir=false %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
! RUN: bbc --math-runtime=precise -emit-fir -hlfir=false %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-PRECISE"
! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
Expand All @@ -10,8 +11,7 @@ function test_real4(x)
! CHECK-LABEL: @_QPtest_real4
! CHECK-PRECISE: %[[acos:.*]] = fir.call @acosf({{%[A-Za-z0-9._]+}}) fastmath<contract> : (f32) -> f32
! CHECK-FAST: %[[acos:.*]] = math.acos %{{.*}} : f32
! CHECK: %[[dpi:.*]] = arith.constant 0.31830988618379069 : f64
! CHECK: %[[inv_pi:.*]] = fir.convert %[[dpi]] : (f64) -> f32
! CHECK: %[[inv_pi:.*]] = arith.constant 0.318309873 : f32
! CHECK: %{{.*}} = arith.mulf %[[acos]], %[[inv_pi]] fastmath<contract> : f32

function test_real8(x)
Expand All @@ -24,3 +24,13 @@ function test_real8(x)
! CHECK-FAST: %[[acos:.*]] = math.acos %{{.*}} : f64
! CHECK: %[[inv_pi:.*]] = arith.constant 0.31830988618379069 : f64
! CHECK: %{{.*}} = arith.mulf %[[acos]], %[[inv_pi]] fastmath<contract> : f64

function test_real16(x)
real(16) :: x, test_real16
test_real16 = acospi(x)
end function

! CHECK-LABEL: @_QPtest_real16
! CHECK: %[[acos:.*]] = fir.call @_FortranAAcosF128({{.*}}) fastmath<contract> : (f128) -> f128
! CHECK: %[[inv_pi:.*]] = arith.constant 0.3183098861837906715377675267450{{.*}} : f128
! CHECK: %{{.*}} = arith.mulf %[[acos]], %[[inv_pi]] fastmath<contract> : f128
4 changes: 4 additions & 0 deletions llvm/include/llvm/Support/MathExtras.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ constexpr float ef = 0x1.5bf0a8P+1F, // (2.71828183) https://oeis.org/A
sqrt3f = 0x1.bb67aeP+0F, // (1.73205081) https://oeis.org/A002194
inv_sqrt3f = 0x1.279a74P-1F, // (.577350269)
phif = 0x1.9e377aP+0F; // (1.61803399) https://oeis.org/A001622
// These string literals are taken from below:
// https://github.com/bminor/glibc/blob/8543577b04ded6d979ffcc5a818930e4d74d0645/math/math.h#L1215-L1229
constexpr const char *pis = "3.141592653589793238462643383279502884",
*inv_pis = "0.318309886183790671537767526745028724";
// clang-format on
} // namespace numbers

Expand Down
Loading