diff --git a/flang/docs/Extensions.md b/flang/docs/Extensions.md index 28f672ac28596..6e34db182da7b 100644 --- a/flang/docs/Extensions.md +++ b/flang/docs/Extensions.md @@ -314,8 +314,8 @@ end fixed form source by a '0' in column 6, can contain spaces between the letters of the word INCLUDE, and can have a numeric character literal kind prefix on the file name. -* Intrinsic procedures TAND and ATAND. Constant folding is currently - not supported for these procedures but this is planned. +* Intrinsic procedures SIND, COSD, TAND and ATAND. Constant folding + is currently not supported for these procedures but this is planned. * When a pair of quotation marks in a character literal are split by a line continuation in free form, the second quotation mark may appear at the beginning of the continuation line without an diff --git a/flang/include/flang/Optimizer/Builder/IntrinsicCall.h b/flang/include/flang/Optimizer/Builder/IntrinsicCall.h index 04f6ab4a35bb0..a2d2f4ccad048 100644 --- a/flang/include/flang/Optimizer/Builder/IntrinsicCall.h +++ b/flang/include/flang/Optimizer/Builder/IntrinsicCall.h @@ -205,6 +205,7 @@ struct IntrinsicLibrary { void genCFProcPointer(llvm::ArrayRef); fir::ExtendedValue genCFunLoc(mlir::Type, llvm::ArrayRef); fir::ExtendedValue genCLoc(mlir::Type, llvm::ArrayRef); + mlir::Value genCosd(mlir::Type, llvm::ArrayRef); void genDateAndTime(llvm::ArrayRef); mlir::Value genDim(mlir::Type, llvm::ArrayRef); fir::ExtendedValue genDotProduct(mlir::Type, @@ -332,6 +333,7 @@ struct IntrinsicLibrary { mlir::Value genShift(mlir::Type resultType, llvm::ArrayRef); mlir::Value genShiftA(mlir::Type resultType, llvm::ArrayRef); mlir::Value genSign(mlir::Type, llvm::ArrayRef); + mlir::Value genSind(mlir::Type, llvm::ArrayRef); fir::ExtendedValue genSize(mlir::Type, llvm::ArrayRef); mlir::Value genSpacing(mlir::Type resultType, llvm::ArrayRef args); diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp index 273aee3733bfa..b4ac7f5bd52b8 100644 --- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp +++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp @@ -179,6 +179,7 @@ static constexpr IntrinsicHandler handlers[]{ {{{"x", asValue}, {"y", asValue, handleDynamicOptional}}}}, {"command_argument_count", &I::genCommandArgumentCount}, {"conjg", &I::genConjg}, + {"cosd", &I::genCosd}, {"count", &I::genCount, {{{"mask", asAddr}, {"dim", asValue}, {"kind", asValue}}}, @@ -554,6 +555,7 @@ static constexpr IntrinsicHandler handlers[]{ &I::genSignalSubroutine, {{{"number", asValue}, {"handler", asAddr}, {"status", asAddr}}}, /*isElemental=*/false}, + {"sind", &I::genSind}, {"size", &I::genSize, {{{"array", asBox}, @@ -2644,6 +2646,21 @@ mlir::Value IntrinsicLibrary::genConjg(mlir::Type resultType, cplx, negImag, /*isImagPart=*/true); } +// COSD +mlir::Value IntrinsicLibrary::genCosd(mlir::Type resultType, + llvm::ArrayRef args) { + assert(args.size() == 1); + mlir::MLIRContext *context = builder.getContext(); + mlir::FunctionType ftype = + mlir::FunctionType::get(context, {resultType}, {args[0].getType()}); + llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi); + mlir::Value dfactor = builder.createRealConstant( + loc, mlir::FloatType::getF64(context), pi / llvm::APFloat(180.0)); + mlir::Value factor = builder.createConvert(loc, args[0].getType(), dfactor); + mlir::Value arg = builder.create(loc, args[0], factor); + return getRuntimeCallGenerator("cos", ftype)(builder, loc, {arg}); +} + // COUNT fir::ExtendedValue IntrinsicLibrary::genCount(mlir::Type resultType, @@ -5610,6 +5627,21 @@ mlir::Value IntrinsicLibrary::genSign(mlir::Type resultType, return genRuntimeCall("sign", resultType, args); } +// SIND +mlir::Value IntrinsicLibrary::genSind(mlir::Type resultType, + llvm::ArrayRef args) { + assert(args.size() == 1); + mlir::MLIRContext *context = builder.getContext(); + mlir::FunctionType ftype = + mlir::FunctionType::get(context, {resultType}, {args[0].getType()}); + llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi); + mlir::Value dfactor = builder.createRealConstant( + loc, mlir::FloatType::getF64(context), pi / llvm::APFloat(180.0)); + mlir::Value factor = builder.createConvert(loc, args[0].getType(), dfactor); + mlir::Value arg = builder.create(loc, args[0], factor); + return getRuntimeCallGenerator("sin", ftype)(builder, loc, {arg}); +} + // SIZE fir::ExtendedValue IntrinsicLibrary::genSize(mlir::Type resultType, diff --git a/flang/test/Lower/Intrinsics/cosd.f90 b/flang/test/Lower/Intrinsics/cosd.f90 new file mode 100644 index 0000000000000..677de3704d894 --- /dev/null +++ b/flang/test/Lower/Intrinsics/cosd.f90 @@ -0,0 +1,26 @@ +! RUN: bbc -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST" +! RUN: bbc --math-runtime=precise -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-PRECISE" +! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST" + +function test_real4(x) + real :: x, test_real4 + test_real4 = cosd(x) +end function + +! CHECK-LABEL: @_QPtest_real4 +! CHECK: %[[dfactor:.*]] = arith.constant 0.017453292519943295 : f64 +! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32 +! CHECK: %[[arg:.*]] = arith.mulf %{{[A-Za-z0-9._]+}}, %[[factor]] fastmath : f32 +! CHECK-PRECISE: %{{.*}} = fir.call @cosf(%[[arg]]) fastmath : (f32) -> f32 +! CHECK-FAST: %{{.*}} = math.cos %[[arg]] fastmath : f32 + +function test_real8(x) + real(8) :: x, test_real8 + test_real8 = cosd(x) +end function + +! CHECK-LABEL: @_QPtest_real8 +! CHECK: %[[factor:.*]] = arith.constant 0.017453292519943295 : f64 +! CHECK: %[[arg:.*]] = arith.mulf %{{[A-Za-z0-9._]+}}, %[[factor]] fastmath : f64 +! CHECK-PRECISE: %{{.*}} = fir.call @cos(%[[arg]]) fastmath : (f64) -> f64 +! CHECK-FAST: %{{.*}} = math.cos %[[arg]] fastmath : f64 diff --git a/flang/test/Lower/Intrinsics/sind.f90 b/flang/test/Lower/Intrinsics/sind.f90 new file mode 100644 index 0000000000000..ce47d90bb73dc --- /dev/null +++ b/flang/test/Lower/Intrinsics/sind.f90 @@ -0,0 +1,26 @@ +! RUN: bbc -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST" +! RUN: bbc --math-runtime=precise -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-PRECISE" +! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST" + +function test_real4(x) + real :: x, test_real4 + test_real4 = sind(x) +end function + +! CHECK-LABEL: @_QPtest_real4 +! CHECK: %[[dfactor:.*]] = arith.constant 0.017453292519943295 : f64 +! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32 +! CHECK: %[[arg:.*]] = arith.mulf %{{[A-Za-z0-9._]+}}, %[[factor]] fastmath : f32 +! CHECK-PRECISE: %{{.*}} = fir.call @sinf(%[[arg]]) fastmath : (f32) -> f32 +! CHECK-FAST: %{{.*}} = math.sin %[[arg]] fastmath : f32 + +function test_real8(x) + real(8) :: x, test_real8 + test_real8 = sind(x) +end function + +! CHECK-LABEL: @_QPtest_real8 +! CHECK: %[[factor:.*]] = arith.constant 0.017453292519943295 : f64 +! CHECK: %[[arg:.*]] = arith.mulf %{{[A-Za-z0-9._]+}}, %[[factor]] fastmath : f64 +! CHECK-PRECISE: %{{.*}} = fir.call @sin(%[[arg]]) fastmath : (f64) -> f64 +! CHECK-FAST: %{{.*}} = math.sin %[[arg]] fastmath : f64