Skip to content

Commit bbbe69f

Browse files
authored
[flang] Implement sinpi (#149525)
1 parent dbc41dd commit bbbe69f

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ struct IntrinsicLibrary {
419419
mlir::Value genShiftA(mlir::Type resultType, llvm::ArrayRef<mlir::Value>);
420420
mlir::Value genSign(mlir::Type, llvm::ArrayRef<mlir::Value>);
421421
mlir::Value genSind(mlir::Type, llvm::ArrayRef<mlir::Value>);
422+
mlir::Value genSinpi(mlir::Type, llvm::ArrayRef<mlir::Value>);
422423
fir::ExtendedValue genSize(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
423424
fir::ExtendedValue genSizeOf(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
424425
mlir::Value genSpacing(mlir::Type resultType,

flang/lib/Evaluate/intrinsics.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,7 @@ static const IntrinsicInterface genericIntrinsicFunction[]{
957957
{"sin", {{"x", SameFloating}}, SameFloating},
958958
{"sind", {{"x", SameFloating}}, SameFloating},
959959
{"sinh", {{"x", SameFloating}}, SameFloating},
960+
{"sinpi", {{"x", SameFloating}}, SameFloating},
960961
{"size",
961962
{{"array", AnyData, Rank::arrayOrAssumedRank},
962963
OptionalDIM, // unless array is assumed-size

flang/lib/Optimizer/Builder/IntrinsicCall.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,7 @@ static constexpr IntrinsicHandler handlers[]{
901901
{{{"number", asValue}, {"handler", asAddr}, {"status", asAddr}}},
902902
/*isElemental=*/false},
903903
{"sind", &I::genSind},
904+
{"sinpi", &I::genSinpi},
904905
{"size",
905906
&I::genSize,
906907
{{{"array", asBox},
@@ -8079,6 +8080,21 @@ mlir::Value IntrinsicLibrary::genSind(mlir::Type resultType,
80798080
return getRuntimeCallGenerator("sin", ftype)(builder, loc, {arg});
80808081
}
80818082

8083+
// SINPI
8084+
mlir::Value IntrinsicLibrary::genSinpi(mlir::Type resultType,
8085+
llvm::ArrayRef<mlir::Value> args) {
8086+
assert(args.size() == 1);
8087+
mlir::MLIRContext *context = builder.getContext();
8088+
mlir::FunctionType ftype =
8089+
mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
8090+
llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);
8091+
mlir::Value dfactor =
8092+
builder.createRealConstant(loc, mlir::Float64Type::get(context), pi);
8093+
mlir::Value factor = builder.createConvert(loc, args[0].getType(), dfactor);
8094+
mlir::Value arg = builder.create<mlir::arith::MulFOp>(loc, args[0], factor);
8095+
return getRuntimeCallGenerator("sin", ftype)(builder, loc, {arg});
8096+
}
8097+
80828098
// SIZE
80838099
fir::ExtendedValue
80848100
IntrinsicLibrary::genSize(mlir::Type resultType,

flang/test/Lower/Intrinsics/sinpi.f90

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK"
2+
3+
function test_real4(x)
4+
real :: x, test_real4
5+
test_real4 = sinpi(x)
6+
end function
7+
8+
! CHECK-LABEL: @_QPtest_real4
9+
! CHECK: %[[dfactor:.*]] = arith.constant 3.1415926535897931 : f64
10+
! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32
11+
! CHECK: %[[mul:.*]] = arith.mulf %{{.*}}, %[[factor]] fastmath<contract> : f32
12+
! CHECK: %[[sin:.*]] = math.sin %[[mul]] fastmath<contract> : f32
13+
14+
function test_real8(x)
15+
real(8) :: x, test_real8
16+
test_real8 = sinpi(x)
17+
end function
18+
19+
! CHECK-LABEL: @_QPtest_real8
20+
! CHECK: %[[dfactor:.*]] = arith.constant 3.1415926535897931 : f64
21+
! CHECK: %[[mul:.*]] = arith.mulf %{{.*}}, %[[dfactor]] fastmath<contract> : f64
22+
! CHECK: %[[sin:.*]] = math.sin %[[mul]] fastmath<contract> : f64

0 commit comments

Comments
 (0)