Skip to content

Commit 01b23c8

Browse files
authored
[flang] Implement asinpi (#150238)
1 parent 4471d59 commit 01b23c8

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
@@ -204,6 +204,7 @@ struct IntrinsicLibrary {
204204
fir::ExtendedValue
205205
genCommandArgumentCount(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
206206
mlir::Value genAsind(mlir::Type, llvm::ArrayRef<mlir::Value>);
207+
mlir::Value genAsinpi(mlir::Type, llvm::ArrayRef<mlir::Value>);
207208
fir::ExtendedValue genAssociated(mlir::Type,
208209
llvm::ArrayRef<fir::ExtendedValue>);
209210
mlir::Value genAtand(mlir::Type, llvm::ArrayRef<mlir::Value>);

flang/lib/Evaluate/intrinsics.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ static const IntrinsicInterface genericIntrinsicFunction[]{
359359
{"asin", {{"x", SameFloating}}, SameFloating},
360360
{"asind", {{"x", SameFloating}}, SameFloating},
361361
{"asinh", {{"x", SameFloating}}, SameFloating},
362+
{"asinpi", {{"x", SameFloating}}, SameFloating},
362363
{"associated",
363364
{{"pointer", AnyPointer, Rank::anyOrAssumedRank, Optionality::required,
364365
common::Intent::In, {ArgFlag::canBeNullPointer}},

flang/lib/Optimizer/Builder/IntrinsicCall.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ static constexpr IntrinsicHandler handlers[]{
279279
{{{"mask", asValue}, {"pred", asValue}}},
280280
/*isElemental=*/false},
281281
{"asind", &I::genAsind},
282+
{"asinpi", &I::genAsinpi},
282283
{"associated",
283284
&I::genAssociated,
284285
{{{"pointer", asInquired}, {"target", asInquired}}},
@@ -2845,6 +2846,21 @@ mlir::Value IntrinsicLibrary::genAsind(mlir::Type resultType,
28452846
return mlir::arith::MulFOp::create(builder, loc, result, factor);
28462847
}
28472848

2849+
// ASINPI
2850+
mlir::Value IntrinsicLibrary::genAsinpi(mlir::Type resultType,
2851+
llvm::ArrayRef<mlir::Value> args) {
2852+
assert(args.size() == 1);
2853+
mlir::MLIRContext *context = builder.getContext();
2854+
mlir::FunctionType ftype =
2855+
mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
2856+
mlir::Value asin = getRuntimeCallGenerator("asin", ftype)(builder, loc, args);
2857+
llvm::APFloat inv_pi = llvm::APFloat(llvm::numbers::inv_pi);
2858+
mlir::Value dfactor =
2859+
builder.createRealConstant(loc, mlir::Float64Type::get(context), inv_pi);
2860+
mlir::Value factor = builder.createConvert(loc, resultType, dfactor);
2861+
return mlir::arith::MulFOp::create(builder, loc, asin, factor);
2862+
}
2863+
28482864
// ATAND, ATAN2D
28492865
mlir::Value IntrinsicLibrary::genAtand(mlir::Type resultType,
28502866
llvm::ArrayRef<mlir::Value> args) {
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 = asinpi(x)
8+
end function
9+
10+
! CHECK-LABEL: @_QPtest_real4
11+
! CHECK-PRECISE: %[[asin:.*]] = fir.call @asinf({{%[A-Za-z0-9._]+}}) fastmath<contract> : (f32) -> f32
12+
! CHECK-FAST: %[[asin:.*]] = math.asin %{{.*}} : f32
13+
! CHECK: %[[dpi:.*]] = arith.constant 0.31830988618379069 : f64
14+
! CHECK: %[[inv_pi:.*]] = fir.convert %[[dpi]] : (f64) -> f32
15+
! CHECK: %{{.*}} = arith.mulf %[[asin]], %[[inv_pi]] fastmath<contract> : f32
16+
17+
function test_real8(x)
18+
real(8) :: x, test_real8
19+
test_real8 = asinpi(x)
20+
end function
21+
22+
! CHECK-LABEL: @_QPtest_real8
23+
! CHECK-PRECISE: %[[asin:.*]] = fir.call @asin({{%[A-Za-z0-9._]+}}) fastmath<contract> : (f64) -> f64
24+
! CHECK-FAST: %[[asin:.*]] = math.asin %{{.*}} : f64
25+
! CHECK: %[[inv_pi:.*]] = arith.constant 0.31830988618379069 : f64
26+
! CHECK: %{{.*}} = arith.mulf %[[asin]], %[[inv_pi]] fastmath<contract> : f64

0 commit comments

Comments
 (0)