diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp index bfa470dd5690d..3e6fbafe8a6b3 100644 --- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp +++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp @@ -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(resultType).getFloatSemantics(), + llvm::numbers::inv_pis); + mlir::Value factor = builder.createRealConstant(loc, resultType, inv_pi); return mlir::arith::MulFOp::create(builder, loc, acos, factor); } diff --git a/flang/test/Lower/Intrinsics/acospi.f90 b/flang/test/Lower/Intrinsics/acospi.f90 index dcacd25bca480..38c547fd5dd13 100644 --- a/flang/test/Lower/Intrinsics/acospi.f90 +++ b/flang/test/Lower/Intrinsics/acospi.f90 @@ -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" @@ -10,8 +11,7 @@ function test_real4(x) ! CHECK-LABEL: @_QPtest_real4 ! CHECK-PRECISE: %[[acos:.*]] = fir.call @acosf({{%[A-Za-z0-9._]+}}) fastmath : (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 : f32 function test_real8(x) @@ -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 : 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 : (f128) -> f128 +! CHECK: %[[inv_pi:.*]] = arith.constant 0.3183098861837906715377675267450{{.*}} : f128 +! CHECK: %{{.*}} = arith.mulf %[[acos]], %[[inv_pi]] fastmath : f128 diff --git a/llvm/include/llvm/Support/MathExtras.h b/llvm/include/llvm/Support/MathExtras.h index 7bbf1b3aab761..c9dae7ae7346d 100644 --- a/llvm/include/llvm/Support/MathExtras.h +++ b/llvm/include/llvm/Support/MathExtras.h @@ -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