1 //===-- lib/Evaluate/fold-real.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>
FoldIntrinsicFunction(FoldingContext & context,FunctionRef<Type<TypeCategory::Real,KIND>> && funcRef)14 Expr<Type<TypeCategory::Real, KIND>> FoldIntrinsicFunction(
15 FoldingContext &context,
16 FunctionRef<Type<TypeCategory::Real, KIND>> &&funcRef) {
17 using T = Type<TypeCategory::Real, KIND>;
18 using ComplexT = Type<TypeCategory::Complex, KIND>;
19 ActualArguments &args{funcRef.arguments()};
20 auto *intrinsic{std::get_if<SpecificIntrinsic>(&funcRef.proc().u)};
21 CHECK(intrinsic);
22 std::string name{intrinsic->name};
23 if (name == "acos" || name == "acosh" || name == "asin" || name == "asinh" ||
24 (name == "atan" && args.size() == 1) || name == "atanh" ||
25 name == "bessel_j0" || name == "bessel_j1" || name == "bessel_y0" ||
26 name == "bessel_y1" || name == "cos" || name == "cosh" || name == "erf" ||
27 name == "erfc" || name == "erfc_scaled" || name == "exp" ||
28 name == "gamma" || name == "log" || name == "log10" ||
29 name == "log_gamma" || name == "sin" || name == "sinh" ||
30 name == "sqrt" || name == "tan" || name == "tanh") {
31 CHECK(args.size() == 1);
32 if (auto callable{GetHostRuntimeWrapper<T, T>(name)}) {
33 return FoldElementalIntrinsic<T, T>(
34 context, std::move(funcRef), *callable);
35 } else {
36 context.messages().Say(
37 "%s(real(kind=%d)) cannot be folded on host"_en_US, name, KIND);
38 }
39 } else if (name == "amax0" || name == "amin0" || name == "amin1" ||
40 name == "amax1" || name == "dmin1" || name == "dmax1") {
41 return RewriteSpecificMINorMAX(context, std::move(funcRef));
42 } else if (name == "atan" || name == "atan2" || name == "hypot" ||
43 name == "mod") {
44 std::string localName{name == "atan" ? "atan2" : name};
45 CHECK(args.size() == 2);
46 if (auto callable{GetHostRuntimeWrapper<T, T, T>(localName)}) {
47 return FoldElementalIntrinsic<T, T, T>(
48 context, std::move(funcRef), *callable);
49 } else {
50 context.messages().Say(
51 "%s(real(kind=%d), real(kind%d)) cannot be folded on host"_en_US,
52 name, KIND, KIND);
53 }
54 } else if (name == "bessel_jn" || name == "bessel_yn") {
55 if (args.size() == 2) { // elemental
56 // runtime functions use int arg
57 using Int4 = Type<TypeCategory::Integer, 4>;
58 if (auto callable{GetHostRuntimeWrapper<T, Int4, T>(name)}) {
59 return FoldElementalIntrinsic<T, Int4, T>(
60 context, std::move(funcRef), *callable);
61 } else {
62 context.messages().Say(
63 "%s(integer(kind=4), real(kind=%d)) cannot be folded on host"_en_US,
64 name, KIND);
65 }
66 }
67 } else if (name == "abs") {
68 // Argument can be complex or real
69 if (auto *x{UnwrapExpr<Expr<SomeReal>>(args[0])}) {
70 return FoldElementalIntrinsic<T, T>(
71 context, std::move(funcRef), &Scalar<T>::ABS);
72 } else if (auto *z{UnwrapExpr<Expr<SomeComplex>>(args[0])}) {
73 if (auto callable{GetHostRuntimeWrapper<T, ComplexT>("abs")}) {
74 return FoldElementalIntrinsic<T, ComplexT>(
75 context, std::move(funcRef), *callable);
76 } else {
77 context.messages().Say(
78 "abs(complex(kind=%d)) cannot be folded on host"_en_US, KIND);
79 }
80 } else {
81 common::die(" unexpected argument type inside abs");
82 }
83 } else if (name == "aimag") {
84 return FoldElementalIntrinsic<T, ComplexT>(
85 context, std::move(funcRef), &Scalar<ComplexT>::AIMAG);
86 } else if (name == "aint" || name == "anint") {
87 // ANINT rounds ties away from zero, not to even
88 common::RoundingMode mode{name == "aint"
89 ? common::RoundingMode::ToZero
90 : common::RoundingMode::TiesAwayFromZero};
91 return FoldElementalIntrinsic<T, T>(context, std::move(funcRef),
92 ScalarFunc<T, T>([&name, &context, mode](
93 const Scalar<T> &x) -> Scalar<T> {
94 ValueWithRealFlags<Scalar<T>> y{x.ToWholeNumber(mode)};
95 if (y.flags.test(RealFlag::Overflow)) {
96 context.messages().Say("%s intrinsic folding overflow"_en_US, name);
97 }
98 return y.value;
99 }));
100 } else if (name == "dprod") {
101 if (auto scalars{GetScalarConstantArguments<T, T>(context, args)}) {
102 return Fold(context,
103 Expr<T>{Multiply<T>{
104 Expr<T>{std::get<0>(*scalars)}, Expr<T>{std::get<1>(*scalars)}}});
105 }
106 } else if (name == "epsilon") {
107 return Expr<T>{Scalar<T>::EPSILON()};
108 } else if (name == "huge") {
109 return Expr<T>{Scalar<T>::HUGE()};
110 } else if (name == "max") {
111 return FoldMINorMAX(context, std::move(funcRef), Ordering::Greater);
112 } else if (name == "merge") {
113 return FoldMerge<T>(context, std::move(funcRef));
114 } else if (name == "min") {
115 return FoldMINorMAX(context, std::move(funcRef), Ordering::Less);
116 } else if (name == "real") {
117 if (auto *expr{args[0].value().UnwrapExpr()}) {
118 return ToReal<KIND>(context, std::move(*expr));
119 }
120 } else if (name == "sign") {
121 return FoldElementalIntrinsic<T, T, T>(
122 context, std::move(funcRef), &Scalar<T>::SIGN);
123 } else if (name == "tiny") {
124 return Expr<T>{Scalar<T>::TINY()};
125 }
126 // TODO: cshift, dim, dot_product, eoshift, fraction, matmul,
127 // maxval, minval, modulo, nearest, norm2, pack, product,
128 // reduce, rrspacing, scale, set_exponent, spacing, spread,
129 // sum, transfer, transpose, unpack, bessel_jn (transformational) and
130 // bessel_yn (transformational)
131 return Expr<T>{std::move(funcRef)};
132 }
133
134 template <int KIND>
FoldOperation(FoldingContext & context,ComplexComponent<KIND> && x)135 Expr<Type<TypeCategory::Real, KIND>> FoldOperation(
136 FoldingContext &context, ComplexComponent<KIND> &&x) {
137 using Operand = Type<TypeCategory::Complex, KIND>;
138 using Result = Type<TypeCategory::Real, KIND>;
139 if (auto array{ApplyElementwise(context, x,
140 std::function<Expr<Result>(Expr<Operand> &&)>{
141 [=](Expr<Operand> &&operand) {
142 return Expr<Result>{ComplexComponent<KIND>{
143 x.isImaginaryPart, std::move(operand)}};
144 }})}) {
145 return *array;
146 }
147 using Part = Type<TypeCategory::Real, KIND>;
148 auto &operand{x.left()};
149 if (auto value{GetScalarConstantValue<Operand>(operand)}) {
150 if (x.isImaginaryPart) {
151 return Expr<Part>{Constant<Part>{value->AIMAG()}};
152 } else {
153 return Expr<Part>{Constant<Part>{value->REAL()}};
154 }
155 }
156 return Expr<Part>{std::move(x)};
157 }
158
159 FOR_EACH_REAL_KIND(template class ExpressionBase, )
160 template class ExpressionBase<SomeReal>;
161 } // namespace Fortran::evaluate
162