Skip to content

Commit 4471d59

Browse files
authored
[flang] Implement acospi (#150234)
1 parent b043492 commit 4471d59

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

flang/include/flang/Optimizer/Builder/IntrinsicCall.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ struct IntrinsicLibrary {
174174
/// real and to the `hypot` math routine if the argument is of complex type.
175175
mlir::Value genAbs(mlir::Type, llvm::ArrayRef<mlir::Value>);
176176
mlir::Value genAcosd(mlir::Type, llvm::ArrayRef<mlir::Value>);
177+
mlir::Value genAcospi(mlir::Type, llvm::ArrayRef<mlir::Value>);
177178
template <void (*CallRuntime)(fir::FirOpBuilder &, mlir::Location loc,
178179
mlir::Value, mlir::Value)>
179180
fir::ExtendedValue genAdjustRtCall(mlir::Type,

flang/lib/Evaluate/intrinsics.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ static const IntrinsicInterface genericIntrinsicFunction[]{
340340
{"acos", {{"x", SameFloating}}, SameFloating},
341341
{"acosd", {{"x", SameFloating}}, SameFloating},
342342
{"acosh", {{"x", SameFloating}}, SameFloating},
343+
{"acospi", {{"x", SameFloating}}, SameFloating},
343344
{"adjustl", {{"string", SameChar}}, SameChar},
344345
{"adjustr", {{"string", SameChar}}, SameChar},
345346
{"aimag", {{"z", SameComplex}}, SameReal},

flang/lib/Optimizer/Builder/IntrinsicCall.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ static constexpr IntrinsicHandler handlers[]{
246246
{"abs", &I::genAbs},
247247
{"achar", &I::genChar},
248248
{"acosd", &I::genAcosd},
249+
{"acospi", &I::genAcospi},
249250
{"adjustl",
250251
&I::genAdjustRtCall<fir::runtime::genAdjustL>,
251252
{{{"string", asAddr}}},
@@ -2676,6 +2677,21 @@ mlir::Value IntrinsicLibrary::genAcosd(mlir::Type resultType,
26762677
return mlir::arith::MulFOp::create(builder, loc, result, factor);
26772678
}
26782679

2680+
// ACOSPI
2681+
mlir::Value IntrinsicLibrary::genAcospi(mlir::Type resultType,
2682+
llvm::ArrayRef<mlir::Value> args) {
2683+
assert(args.size() == 1);
2684+
mlir::MLIRContext *context = builder.getContext();
2685+
mlir::FunctionType ftype =
2686+
mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
2687+
mlir::Value acos = getRuntimeCallGenerator("acos", ftype)(builder, loc, args);
2688+
llvm::APFloat inv_pi = llvm::APFloat(llvm::numbers::inv_pi);
2689+
mlir::Value dfactor =
2690+
builder.createRealConstant(loc, mlir::Float64Type::get(context), inv_pi);
2691+
mlir::Value factor = builder.createConvert(loc, resultType, dfactor);
2692+
return mlir::arith::MulFOp::create(builder, loc, acos, factor);
2693+
}
2694+
26792695
// ADJUSTL & ADJUSTR
26802696
template <void (*CallRuntime)(fir::FirOpBuilder &, mlir::Location loc,
26812697
mlir::Value, mlir::Value)>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
! RUN: bbc -emit-fir -hlfir=false %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
2+
! RUN: bbc --math-runtime=precise -emit-fir -hlfir=false %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-PRECISE"
3+
! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
4+
5+
function test_real4(x)
6+
real :: x, test_real4
7+
test_real4 = acospi(x)
8+
end function
9+
10+
! CHECK-LABEL: @_QPtest_real4
11+
! CHECK-PRECISE: %[[acos:.*]] = fir.call @acosf({{%[A-Za-z0-9._]+}}) fastmath<contract> : (f32) -> f32
12+
! CHECK-FAST: %[[acos:.*]] = math.acos %{{.*}} : f32
13+
! CHECK: %[[dpi:.*]] = arith.constant 0.31830988618379069 : f64
14+
! CHECK: %[[inv_pi:.*]] = fir.convert %[[dpi]] : (f64) -> f32
15+
! CHECK: %{{.*}} = arith.mulf %[[acos]], %[[inv_pi]] fastmath<contract> : f32
16+
17+
function test_real8(x)
18+
real(8) :: x, test_real8
19+
test_real8 = acospi(x)
20+
end function
21+
22+
! CHECK-LABEL: @_QPtest_real8
23+
! CHECK-PRECISE: %[[acos:.*]] = fir.call @acos({{%[A-Za-z0-9._]+}}) fastmath<contract> : (f64) -> f64
24+
! CHECK-FAST: %[[acos:.*]] = math.acos %{{.*}} : f64
25+
! CHECK: %[[inv_pi:.*]] = arith.constant 0.31830988618379069 : f64
26+
! CHECK: %{{.*}} = arith.mulf %[[acos]], %[[inv_pi]] fastmath<contract> : f64

0 commit comments

Comments
 (0)