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{GetHostRuntimeWrapper<T, T>(name)}) { 27 return FoldElementalIntrinsic<T, T>( 28 context, std::move(funcRef), *callable); 29 } else { 30 context.messages().Say( 31 "%s(complex(kind=%d)) cannot be folded on host"_en_US, name, KIND); 32 } 33 } else if (name == "conjg") { 34 return FoldElementalIntrinsic<T, T>( 35 context, std::move(funcRef), &Scalar<T>::CONJG); 36 } else if (name == "cmplx") { 37 if (args.size() > 0 && args[0].has_value()) { 38 if (auto *x{UnwrapExpr<Expr<SomeComplex>>(args[0])}) { 39 // CMPLX(X [, KIND]) with complex X 40 return Fold(context, ConvertToType<T>(std::move(*x))); 41 } else { 42 // CMPLX(X [, Y [, KIND]]) with non-complex X 43 using Part = typename T::Part; 44 Expr<SomeType> re{std::move(*args[0].value().UnwrapExpr())}; 45 Expr<SomeType> im{args.size() >= 2 && args[1].has_value() 46 ? std::move(*args[1]->UnwrapExpr()) 47 : AsGenericExpr(Constant<Part>{Scalar<Part>{}})}; 48 return Fold(context, 49 Expr<T>{ 50 ComplexConstructor<KIND>{ToReal<KIND>(context, std::move(re)), 51 ToReal<KIND>(context, std::move(im))}}); 52 } 53 } 54 } else if (name == "merge") { 55 return FoldMerge<T>(context, std::move(funcRef)); 56 } 57 // TODO: cshift, dot_product, eoshift, matmul, pack, product, 58 // reduce, spread, sum, transfer, transpose, unpack 59 return Expr<T>{std::move(funcRef)}; 60 } 61 62 template <int KIND> 63 Expr<Type<TypeCategory::Complex, KIND>> FoldOperation( 64 FoldingContext &context, ComplexConstructor<KIND> &&x) { 65 if (auto array{ApplyElementwise(context, x)}) { 66 return *array; 67 } 68 using Result = Type<TypeCategory::Complex, KIND>; 69 if (auto folded{OperandsAreConstants(x)}) { 70 return Expr<Result>{ 71 Constant<Result>{Scalar<Result>{folded->first, folded->second}}}; 72 } 73 return Expr<Result>{std::move(x)}; 74 } 75 76 FOR_EACH_COMPLEX_KIND(template class ExpressionBase, ) 77 template class ExpressionBase<SomeComplex>; 78 } // namespace Fortran::evaluate 79