Skip to content

Commit 7056f72

Browse files
authored
Merge pull request #20 from mjklemm/cosd_sind_mapping
Provide initial implementation for COSD and SIND
2 parents 6f25158 + 8028066 commit 7056f72

File tree

5 files changed

+88
-2
lines changed

5 files changed

+88
-2
lines changed

flang/docs/Extensions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,8 @@ end
314314
fixed form source by a '0' in column 6, can contain spaces
315315
between the letters of the word INCLUDE, and can have a
316316
numeric character literal kind prefix on the file name.
317-
* Intrinsic procedures TAND and ATAND. Constant folding is currently
318-
not supported for these procedures but this is planned.
317+
* Intrinsic procedures SIND, COSD, TAND and ATAND. Constant folding
318+
is currently not supported for these procedures but this is planned.
319319
* When a pair of quotation marks in a character literal are split
320320
by a line continuation in free form, the second quotation mark
321321
may appear at the beginning of the continuation line without an

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ struct IntrinsicLibrary {
205205
void genCFProcPointer(llvm::ArrayRef<fir::ExtendedValue>);
206206
fir::ExtendedValue genCFunLoc(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
207207
fir::ExtendedValue genCLoc(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
208+
mlir::Value genCosd(mlir::Type, llvm::ArrayRef<mlir::Value>);
208209
void genDateAndTime(llvm::ArrayRef<fir::ExtendedValue>);
209210
mlir::Value genDim(mlir::Type, llvm::ArrayRef<mlir::Value>);
210211
fir::ExtendedValue genDotProduct(mlir::Type,
@@ -332,6 +333,7 @@ struct IntrinsicLibrary {
332333
mlir::Value genShift(mlir::Type resultType, llvm::ArrayRef<mlir::Value>);
333334
mlir::Value genShiftA(mlir::Type resultType, llvm::ArrayRef<mlir::Value>);
334335
mlir::Value genSign(mlir::Type, llvm::ArrayRef<mlir::Value>);
336+
mlir::Value genSind(mlir::Type, llvm::ArrayRef<mlir::Value>);
335337
fir::ExtendedValue genSize(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
336338
mlir::Value genSpacing(mlir::Type resultType,
337339
llvm::ArrayRef<mlir::Value> args);

flang/lib/Optimizer/Builder/IntrinsicCall.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ static constexpr IntrinsicHandler handlers[]{
179179
{{{"x", asValue}, {"y", asValue, handleDynamicOptional}}}},
180180
{"command_argument_count", &I::genCommandArgumentCount},
181181
{"conjg", &I::genConjg},
182+
{"cosd", &I::genCosd},
182183
{"count",
183184
&I::genCount,
184185
{{{"mask", asAddr}, {"dim", asValue}, {"kind", asValue}}},
@@ -550,6 +551,7 @@ static constexpr IntrinsicHandler handlers[]{
550551
{"shiftl", &I::genShift<mlir::arith::ShLIOp>},
551552
{"shiftr", &I::genShift<mlir::arith::ShRUIOp>},
552553
{"sign", &I::genSign},
554+
{"sind", &I::genSind},
553555
{"size",
554556
&I::genSize,
555557
{{{"array", asBox},
@@ -2639,6 +2641,21 @@ mlir::Value IntrinsicLibrary::genConjg(mlir::Type resultType,
26392641
cplx, negImag, /*isImagPart=*/true);
26402642
}
26412643

2644+
// COSD
2645+
mlir::Value IntrinsicLibrary::genCosd(mlir::Type resultType,
2646+
llvm::ArrayRef<mlir::Value> args) {
2647+
assert(args.size() == 1);
2648+
mlir::MLIRContext *context = builder.getContext();
2649+
mlir::FunctionType ftype =
2650+
mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
2651+
llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);
2652+
mlir::Value dfactor = builder.createRealConstant(
2653+
loc, mlir::FloatType::getF64(context), pi / llvm::APFloat(180.0));
2654+
mlir::Value factor = builder.createConvert(loc, args[0].getType(), dfactor);
2655+
mlir::Value arg = builder.create<mlir::arith::MulFOp>(loc, args[0], factor);
2656+
return getRuntimeCallGenerator("cos", ftype)(builder, loc, {arg});
2657+
}
2658+
26422659
// COUNT
26432660
fir::ExtendedValue
26442661
IntrinsicLibrary::genCount(mlir::Type resultType,
@@ -5593,6 +5610,21 @@ mlir::Value IntrinsicLibrary::genSign(mlir::Type resultType,
55935610
return genRuntimeCall("sign", resultType, args);
55945611
}
55955612

5613+
// SIND
5614+
mlir::Value IntrinsicLibrary::genSind(mlir::Type resultType,
5615+
llvm::ArrayRef<mlir::Value> args) {
5616+
assert(args.size() == 1);
5617+
mlir::MLIRContext *context = builder.getContext();
5618+
mlir::FunctionType ftype =
5619+
mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
5620+
llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);
5621+
mlir::Value dfactor = builder.createRealConstant(
5622+
loc, mlir::FloatType::getF64(context), pi / llvm::APFloat(180.0));
5623+
mlir::Value factor = builder.createConvert(loc, args[0].getType(), dfactor);
5624+
mlir::Value arg = builder.create<mlir::arith::MulFOp>(loc, args[0], factor);
5625+
return getRuntimeCallGenerator("sin", ftype)(builder, loc, {arg});
5626+
}
5627+
55965628
// SIZE
55975629
fir::ExtendedValue
55985630
IntrinsicLibrary::genSize(mlir::Type resultType,

flang/test/Lower/Intrinsics/cosd.f90

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
! RUN: bbc -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
2+
! RUN: bbc --math-runtime=precise -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-PRECISE"
3+
! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
4+
5+
function test_real4(x)
6+
real :: x, test_real4
7+
test_real4 = cosd(x)
8+
end function
9+
10+
! CHECK-LABEL: @_QPtest_real4
11+
! CHECK: %[[dfactor:.*]] = arith.constant 0.017453292519943295 : f64
12+
! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32
13+
! CHECK: %[[arg:.*]] = arith.mulf %{{[A-Za-z0-9._]+}}, %[[factor]] fastmath<contract> : f32
14+
! CHECK-PRECISE: %{{.*}} = fir.call @cosf(%[[arg]]) fastmath<contract> : (f32) -> f32
15+
! CHECK-FAST: %{{.*}} = math.cos %[[arg]] fastmath<contract> : f32
16+
17+
function test_real8(x)
18+
real(8) :: x, test_real8
19+
test_real8 = cosd(x)
20+
end function
21+
22+
! CHECK-LABEL: @_QPtest_real8
23+
! CHECK: %[[factor:.*]] = arith.constant 0.017453292519943295 : f64
24+
! CHECK: %[[arg:.*]] = arith.mulf %{{[A-Za-z0-9._]+}}, %[[factor]] fastmath<contract> : f64
25+
! CHECK-PRECISE: %{{.*}} = fir.call @cos(%[[arg]]) fastmath<contract> : (f64) -> f64
26+
! CHECK-FAST: %{{.*}} = math.cos %[[arg]] fastmath<contract> : f64

flang/test/Lower/Intrinsics/sind.f90

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

0 commit comments

Comments
 (0)