Skip to content

Commit cd6311b

Browse files
authored
[flang] Implement COSPI (#149343)
This feature is added in the Fortran 2023 standard.
1 parent 9c26f37 commit cd6311b

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
@@ -245,6 +245,7 @@ struct IntrinsicLibrary {
245245
fir::ExtendedValue genCPtrCompare(mlir::Type,
246246
llvm::ArrayRef<fir::ExtendedValue>);
247247
mlir::Value genCosd(mlir::Type, llvm::ArrayRef<mlir::Value>);
248+
mlir::Value genCospi(mlir::Type, llvm::ArrayRef<mlir::Value>);
248249
void genDateAndTime(llvm::ArrayRef<fir::ExtendedValue>);
249250
mlir::Value genDim(mlir::Type, llvm::ArrayRef<mlir::Value>);
250251
fir::ExtendedValue genDotProduct(mlir::Type,

flang/lib/Evaluate/intrinsics.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ static const IntrinsicInterface genericIntrinsicFunction[]{
428428
{"conjg", {{"z", SameComplex}}, SameComplex},
429429
{"cos", {{"x", SameFloating}}, SameFloating},
430430
{"cosd", {{"x", SameFloating}}, SameFloating},
431+
{"cospi", {{"x", SameFloating}}, SameFloating},
431432
{"cosh", {{"x", SameFloating}}, SameFloating},
432433
{"coshape", {{"coarray", AnyData, Rank::coarray}, SizeDefaultKIND}, KINDInt,
433434
Rank::vector, IntrinsicClass::inquiryFunction},

flang/lib/Optimizer/Builder/IntrinsicCall.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ static constexpr IntrinsicHandler handlers[]{
396396
{"command_argument_count", &I::genCommandArgumentCount},
397397
{"conjg", &I::genConjg},
398398
{"cosd", &I::genCosd},
399+
{"cospi", &I::genCospi},
399400
{"count",
400401
&I::genCount,
401402
{{{"mask", asAddr}, {"dim", asValue}, {"kind", asValue}}},
@@ -3623,6 +3624,21 @@ mlir::Value IntrinsicLibrary::genCosd(mlir::Type resultType,
36233624
return getRuntimeCallGenerator("cos", ftype)(builder, loc, {arg});
36243625
}
36253626

3627+
// COSPI
3628+
mlir::Value IntrinsicLibrary::genCospi(mlir::Type resultType,
3629+
llvm::ArrayRef<mlir::Value> args) {
3630+
assert(args.size() == 1);
3631+
mlir::MLIRContext *context = builder.getContext();
3632+
mlir::FunctionType ftype =
3633+
mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
3634+
llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);
3635+
mlir::Value dfactor =
3636+
builder.createRealConstant(loc, mlir::Float64Type::get(context), pi);
3637+
mlir::Value factor = builder.createConvert(loc, args[0].getType(), dfactor);
3638+
mlir::Value arg = builder.create<mlir::arith::MulFOp>(loc, args[0], factor);
3639+
return getRuntimeCallGenerator("cos", ftype)(builder, loc, {arg});
3640+
}
3641+
36263642
// COUNT
36273643
fir::ExtendedValue
36283644
IntrinsicLibrary::genCount(mlir::Type resultType,

flang/test/Lower/Intrinsics/cospi.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 = cospi(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: %[[cos:.*]] = math.cos %[[mul]] fastmath<contract> : f32
13+
14+
function test_real8(x)
15+
real(8) :: x, test_real8
16+
test_real8 = cospi(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: %[[cos:.*]] = math.cos %[[mul]] fastmath<contract> : f64

0 commit comments

Comments
 (0)