1 //===-- lib/Evaluate/fold-complex.cpp -------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "fold-implementation.h" 10 11 namespace Fortran::evaluate { 12 13 template <int KIND> 14 Expr<Type<TypeCategory::Complex, KIND>> FoldIntrinsicFunction( 15 FoldingContext &context, 16 FunctionRef<Type<TypeCategory::Complex, KIND>> &&funcRef) { 17 using T = Type<TypeCategory::Complex, KIND>; 18 ActualArguments &args{funcRef.arguments()}; 19 auto *intrinsic{std::get_if<SpecificIntrinsic>(&funcRef.proc().u)}; 20 CHECK(intrinsic); 21 std::string name{intrinsic->name}; 22 if (name == "acos" || name == "acosh" || name == "asin" || name == "asinh" || 23 name == "atan" || name == "atanh" || name == "cos" || name == "cosh" || 24 name == "exp" || name == "log" || name == "sin" || name == "sinh" || 25 name == "sqrt" || name == "tan" || name == "tanh") { 26 if (auto callable{context.hostIntrinsicsLibrary() 27 .GetHostProcedureWrapper<Scalar, T, T>(name)}) { 28 return FoldElementalIntrinsic<T, T>( 29 context, std::move(funcRef), *callable); 30 } else { 31 context.messages().Say( 32 "%s(complex(kind=%d)) cannot be folded on host"_en_US, name, KIND); 33 } 34 } else if (name == "conjg") { 35 return FoldElementalIntrinsic<T, T>( 36 context, std::move(funcRef), &Scalar<T>::CONJG); 37 } else if (name == "cmplx") { 38 using Part = typename T::Part; 39 if (args.size() == 2) { // CMPLX(X, [KIND]) 40 if (auto *x{UnwrapExpr<Expr<SomeComplex>>(args[0])}) { 41 return Fold(context, ConvertToType<T>(std::move(*x))); 42 } 43 Expr<SomeType> re{std::move(*args[0].value().UnwrapExpr())}; 44 Expr<SomeType> im{AsGenericExpr(Constant<Part>{Scalar<Part>{}})}; 45 return Fold(context, 46 Expr<T>{ComplexConstructor<KIND>{ToReal<KIND>(context, std::move(re)), 47 ToReal<KIND>(context, std::move(im))}}); 48 } 49 // CMPLX(X, [Y, KIND]) 50 CHECK(args.size() == 3); 51 Expr<SomeType> re{std::move(*args[0].value().UnwrapExpr())}; 52 Expr<SomeType> im{args[1] ? std::move(*args[1].value().UnwrapExpr()) 53 : AsGenericExpr(Constant<Part>{Scalar<Part>{}})}; 54 return Fold(context, 55 Expr<T>{ComplexConstructor<KIND>{ToReal<KIND>(context, std::move(re)), 56 ToReal<KIND>(context, std::move(im))}}); 57 } else if (name == "merge") { 58 return FoldMerge<T>(context, std::move(funcRef)); 59 } 60 // TODO: cshift, dot_product, eoshift, matmul, pack, product, 61 // reduce, spread, sum, transfer, transpose, unpack 62 return Expr<T>{std::move(funcRef)}; 63 } 64 65 template <int KIND> 66 Expr<Type<TypeCategory::Complex, KIND>> FoldOperation( 67 FoldingContext &context, ComplexConstructor<KIND> &&x) { 68 if (auto array{ApplyElementwise(context, x)}) { 69 return *array; 70 } 71 using Result = Type<TypeCategory::Complex, KIND>; 72 if (auto folded{OperandsAreConstants(x)}) { 73 return Expr<Result>{ 74 Constant<Result>{Scalar<Result>{folded->first, folded->second}}}; 75 } 76 return Expr<Result>{std::move(x)}; 77 } 78 79 FOR_EACH_COMPLEX_KIND(template class ExpressionBase, ) 80 template class ExpressionBase<SomeComplex>; 81 } // namespace Fortran::evaluate 82