| 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 |
#include "fold-matmul.h" |
| 11 |
#include "fold-reduction.h" |
| 12 |
|
| 13 |
namespace Fortran::evaluate { |
| 14 |
|
| 15 |
template <typename T> |
| 16 |
static Expr<T> FoldTransformationalBessel( |
| 17 |
FunctionRef<T> &&funcRef, FoldingContext &context) { |
| 18 |
CHECK(funcRef.arguments().size() == 3); |
| 19 |
/// Bessel runtime functions use `int` integer arguments. Convert integer |
| 20 |
/// arguments to Int4, any overflow error will be reported during the |
| 21 |
/// conversion folding. |
| 22 |
using Int4 = Type<TypeCategory::Integer, 4>; |
| 23 |
if (auto args{ |
| 24 |
GetConstantArguments<Int4, Int4, T>(context, funcRef.arguments())}) { |
| 25 |
const std::string &name{std::get<SpecificIntrinsic>(funcRef.proc().u).name}; |
| 26 |
if (auto elementalBessel{GetHostRuntimeWrapper<T, Int4, T>(name)}) { |
| 27 |
std::vector<Scalar<T>> results; |
| 28 |
int n1{static_cast<int>( |
| 29 |
std::get<0>(*args)->GetScalarValue().value().ToInt64())}; |
| 30 |
int n2{static_cast<int>( |
| 31 |
std::get<1>(*args)->GetScalarValue().value().ToInt64())}; |
| 32 |
Scalar<T> x{std::get<2>(*args)->GetScalarValue().value()}; |
| 33 |
for (int i{n1}; i <= n2; ++i) { |
| 34 |
results.emplace_back((*elementalBessel)(context, Scalar<Int4>{i}, x)); |
| 35 |
} |
| 36 |
return Expr<T>{Constant<T>{ |
| 37 |
std::move(results), ConstantSubscripts{std::max(n2 - n1 + 1, 0)}}}; |
| 38 |
} else if (context.languageFeatures().ShouldWarn( |
| 39 |
common::UsageWarning::FoldingFailure)) { |
| 40 |
context.messages().Say( |
| 41 |
"%s(integer(kind=4), real(kind=%d)) cannot be folded on host"_warn_en_US, |
| 42 |
name, T::kind); |
| 43 |
} |
| 44 |
} |
| 45 |
return Expr<T>{std::move(funcRef)}; |
| 46 |
} |
| 47 |
|
| 48 |
// NORM2 |
| 49 |
template <int KIND> class Norm2Accumulator { |
| 50 |
using T = Type<TypeCategory::Real, KIND>; |
| 51 |
|
| 52 |
public: |
| 53 |
Norm2Accumulator( |
| 54 |
const Constant<T> &array, const Constant<T> &maxAbs, Rounding rounding) |
| 55 |
: array_{array}, maxAbs_{maxAbs}, rounding_{rounding} {}; |
| 56 |
void operator()( |
| 57 |
Scalar<T> &element, const ConstantSubscripts &at, bool /*first*/) { |
| 58 |
// Summation of scaled elements: |
| 59 |
// Naively, |
| 60 |
// NORM2(A(:)) = SQRT(SUM(A(:)**2)) |
| 61 |
// For any T > 0, we have mathematically |
| 62 |
// SQRT(SUM(A(:)**2)) |
| 63 |
// = SQRT(T**2 * (SUM(A(:)**2) / T**2)) |
| 64 |
// = SQRT(T**2 * SUM(A(:)**2 / T**2)) |
| 65 |
// = SQRT(T**2 * SUM((A(:)/T)**2)) |
| 66 |
// = SQRT(T**2) * SQRT(SUM((A(:)/T)**2)) |
| 67 |
// = T * SQRT(SUM((A(:)/T)**2)) |
| 68 |
// By letting T = MAXVAL(ABS(A)), we ensure that |
| 69 |
// ALL(ABS(A(:)/T) <= 1), so ALL((A(:)/T)**2 <= 1), and the SUM will |
| 70 |
// not overflow unless absolutely necessary. |
| 71 |
auto scale{maxAbs_.At(maxAbsAt_)}; |
| 72 |
if (scale.IsZero()) { |
| 73 |
// Maximum value is zero, and so will the result be. |
| 74 |
// Avoid division by zero below. |
| 75 |
element = scale; |
| 76 |
} else { |
| 77 |
auto item{array_.At(at)}; |
| 78 |
auto scaled{item.Divide(scale).value}; |
| 79 |
auto square{scaled.Multiply(scaled).value}; |
| 80 |
if constexpr (useKahanSummation) { |
| 81 |
auto next{square.Add(correction_, rounding_)}; |
| 82 |
overflow_ |= next.flags.test(RealFlag::Overflow); |
| 83 |
auto sum{element.Add(next.value, rounding_)}; |
| 84 |
overflow_ |= sum.flags.test(RealFlag::Overflow); |
| 85 |
correction_ = sum.value.Subtract(element, rounding_) |
| 86 |
.value.Subtract(next.value, rounding_) |
| 87 |
.value; |
| 88 |
element = sum.value; |
| 89 |
} else { |
| 90 |
auto sum{element.Add(square, rounding_)}; |
| 91 |
overflow_ |= sum.flags.test(RealFlag::Overflow); |
| 92 |
element = sum.value; |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
bool overflow() const { return overflow_; } |
| 97 |
void Done(Scalar<T> &result) { |
| 98 |
// incoming result = SUM((data(:)/maxAbs)**2) |
| 99 |
// outgoing result = maxAbs * SQRT(result) |
| 100 |
auto root{result.SQRT().value}; |
| 101 |
auto product{root.Multiply(maxAbs_.At(maxAbsAt_))}; |
| 102 |
maxAbs_.IncrementSubscripts(maxAbsAt_); |
| 103 |
overflow_ |= product.flags.test(RealFlag::Overflow); |
| 104 |
result = product.value; |
| 105 |
} |
| 106 |
|
| 107 |
private: |
| 108 |
const Constant<T> &array_; |
| 109 |
const Constant<T> &maxAbs_; |
| 110 |
const Rounding rounding_; |
| 111 |
bool overflow_{false}; |
| 112 |
Scalar<T> correction_{}; |
| 113 |
ConstantSubscripts maxAbsAt_{maxAbs_.lbounds()}; |
| 114 |
}; |
| 115 |
|
| 116 |
template <int KIND> |
| 117 |
static Expr<Type<TypeCategory::Real, KIND>> FoldNorm2(FoldingContext &context, |
| 118 |
FunctionRef<Type<TypeCategory::Real, KIND>> &&funcRef) { |
| 119 |
using T = Type<TypeCategory::Real, KIND>; |
| 120 |
using Element = typename Constant<T>::Element; |
| 121 |
std::optional<int> dim; |
| 122 |
if (std::optional<ArrayAndMask<T>> arrayAndMask{ |
| 123 |
ProcessReductionArgs<T>(context, funcRef.arguments(), dim, |
| 124 |
/*X=*/0, /*DIM=*/1)}) { |
| 125 |
MaxvalMinvalAccumulator<T, /*ABS=*/true> maxAbsAccumulator{ |
| 126 |
RelationalOperator::GT, context, arrayAndMask->array}; |
| 127 |
const Element identity{}; |
| 128 |
Constant<T> maxAbs{DoReduction<T>(arrayAndMask->array, arrayAndMask->mask, |
| 129 |
dim, identity, maxAbsAccumulator)}; |
| 130 |
Norm2Accumulator norm2Accumulator{arrayAndMask->array, maxAbs, |
| 131 |
context.targetCharacteristics().roundingMode()}; |
| 132 |
Constant<T> result{DoReduction<T>(arrayAndMask->array, arrayAndMask->mask, |
| 133 |
dim, identity, norm2Accumulator)}; |
| 134 |
if (norm2Accumulator.overflow() && |
| 135 |
context.languageFeatures().ShouldWarn( |
| 136 |
common::UsageWarning::FoldingException)) { |
| 137 |
context.messages().Say( |
| 138 |
"NORM2() of REAL(%d) data overflowed"_warn_en_US, KIND); |
| 139 |
} |
| 140 |
return Expr<T>{std::move(result)}; |
| 141 |
} |
| 142 |
return Expr<T>{std::move(funcRef)}; |
| 143 |
} |
| 144 |
|
| 145 |
template <int KIND> |
| 146 |
Expr<Type<TypeCategory::Real, KIND>> FoldIntrinsicFunction( |
| 147 |
FoldingContext &context, |
| 148 |
FunctionRef<Type<TypeCategory::Real, KIND>> &&funcRef) { |
| 149 |
using T = Type<TypeCategory::Real, KIND>; |
| 150 |
using ComplexT = Type<TypeCategory::Complex, KIND>; |
| 151 |
using Int4 = Type<TypeCategory::Integer, 4>; |
| 152 |
ActualArguments &args{funcRef.arguments()}; |
| 153 |
auto *intrinsic{std::get_if<SpecificIntrinsic>(&funcRef.proc().u)}; |
| 154 |
CHECK(intrinsic); |
| 155 |
std::string name{intrinsic->name}; |
| 156 |
if (name == "acos" || name == "acosh" || name == "asin" || name == "asinh" || |
| 157 |
(name == "atan" && args.size() == 1) || name == "atanh" || |
| 158 |
name == "bessel_j0" || name == "bessel_j1" || name == "bessel_y0" || |
| 159 |
name == "bessel_y1" || name == "cos" || name == "cosh" || name == "erf" || |
| 160 |
name == "erfc" || name == "erfc_scaled" || name == "exp" || |
| 161 |
name == "gamma" || name == "log" || name == "log10" || |
| 162 |
name == "log_gamma" || name == "sin" || name == "sinh" || name == "tan" || |
| 163 |
name == "tanh") { |
| 164 |
CHECK(args.size() == 1); |
| 165 |
if (auto callable{GetHostRuntimeWrapper<T, T>(name)}) { |
| 166 |
return FoldElementalIntrinsic<T, T>( |
| 167 |
context, std::move(funcRef), *callable); |
| 168 |
} else if (context.languageFeatures().ShouldWarn( |
| 169 |
common::UsageWarning::FoldingFailure)) { |
| 170 |
context.messages().Say( |
| 171 |
"%s(real(kind=%d)) cannot be folded on host"_warn_en_US, name, KIND); |
| 172 |
} |
| 173 |
} else if (name == "amax0" || name == "amin0" || name == "amin1" || |
| 174 |
name == "amax1" || name == "dmin1" || name == "dmax1") { |
| 175 |
return RewriteSpecificMINorMAX(context, std::move(funcRef)); |
| 176 |
} else if (name == "atan" || name == "atan2") { |
| 177 |
std::string localName{name == "atan" ? "atan2" : name}; |
| 178 |
CHECK(args.size() == 2); |
| 179 |
if (auto callable{GetHostRuntimeWrapper<T, T, T>(localName)}) { |
| 180 |
return FoldElementalIntrinsic<T, T, T>( |
| 181 |
context, std::move(funcRef), *callable); |
| 182 |
} else if (context.languageFeatures().ShouldWarn( |
| 183 |
common::UsageWarning::FoldingFailure)) { |
| 184 |
context.messages().Say( |
| 185 |
"%s(real(kind=%d), real(kind%d)) cannot be folded on host"_warn_en_US, |
| 186 |
name, KIND, KIND); |
| 187 |
} |
| 188 |
} else if (name == "bessel_jn" || name == "bessel_yn") { |
| 189 |
if (args.size() == 2) { // elemental |
| 190 |
// runtime functions use int arg |
| 191 |
if (auto callable{GetHostRuntimeWrapper<T, Int4, T>(name)}) { |
| 192 |
return FoldElementalIntrinsic<T, Int4, T>( |
| 193 |
context, std::move(funcRef), *callable); |
| 194 |
} else if (context.languageFeatures().ShouldWarn( |
| 195 |
common::UsageWarning::FoldingFailure)) { |
| 196 |
context.messages().Say( |
| 197 |
"%s(integer(kind=4), real(kind=%d)) cannot be folded on host"_warn_en_US, |
| 198 |
name, KIND); |
| 199 |
} |
| 200 |
} else { |
| 201 |
return FoldTransformationalBessel<T>(std::move(funcRef), context); |
| 202 |
} |
| 203 |
} else if (name == "abs") { // incl. zabs & cdabs |
| 204 |
// Argument can be complex or real |
| 205 |
if (UnwrapExpr<Expr<SomeReal>>(args[0])) { |
| 206 |
return FoldElementalIntrinsic<T, T>( |
| 207 |
context, std::move(funcRef), &Scalar<T>::ABS); |
| 208 |
} else if (UnwrapExpr<Expr<SomeComplex>>(args[0])) { |
| 209 |
return FoldElementalIntrinsic<T, ComplexT>(context, std::move(funcRef), |
| 210 |
ScalarFunc<T, ComplexT>([&name, &context]( |
| 211 |
const Scalar<ComplexT> &z) -> Scalar<T> { |
| 212 |
ValueWithRealFlags<Scalar<T>> y{z.ABS()}; |
| 213 |
if (y.flags.test(RealFlag::Overflow) && |
| 214 |
context.languageFeatures().ShouldWarn( |
| 215 |
common::UsageWarning::FoldingException)) { |
| 216 |
context.messages().Say( |
| 217 |
"complex ABS intrinsic folding overflow"_warn_en_US, name); |
| 218 |
} |
| 219 |
return y.value; |
| 220 |
})); |
| 221 |
} else { |
| 222 |
common::die(" unexpected argument type inside abs"); |
| 223 |
} |
| 224 |
} else if (name == "aimag") { |
| 225 |
if (auto *zExpr{UnwrapExpr<Expr<ComplexT>>(args[0])}) { |
| 226 |
return Fold(context, Expr<T>{ComplexComponent{true, std::move(*zExpr)}}); |
| 227 |
} |
| 228 |
} else if (name == "aint" || name == "anint") { |
| 229 |
// ANINT rounds ties away from zero, not to even |
| 230 |
common::RoundingMode mode{name == "aint" |
| 231 |
? common::RoundingMode::ToZero |
| 232 |
: common::RoundingMode::TiesAwayFromZero}; |
| 233 |
return FoldElementalIntrinsic<T, T>(context, std::move(funcRef), |
| 234 |
ScalarFunc<T, T>( |
| 235 |
[&name, &context, mode](const Scalar<T> &x) -> Scalar<T> { |
| 236 |
ValueWithRealFlags<Scalar<T>> y{x.ToWholeNumber(mode)}; |
| 237 |
if (y.flags.test(RealFlag::Overflow) && |
| 238 |
context.languageFeatures().ShouldWarn( |
| 239 |
common::UsageWarning::FoldingException)) { |
| 240 |
context.messages().Say( |
| 241 |
"%s intrinsic folding overflow"_warn_en_US, name); |
| 242 |
} |
| 243 |
return y.value; |
| 244 |
})); |
| 245 |
} else if (name == "dim") { |
| 246 |
return FoldElementalIntrinsic<T, T, T>(context, std::move(funcRef), |
| 247 |
ScalarFunc<T, T, T>([&context](const Scalar<T> &x, |
| 248 |
const Scalar<T> &y) -> Scalar<T> { |
| 249 |
ValueWithRealFlags<Scalar<T>> result{x.DIM(y)}; |
| 250 |
if (result.flags.test(RealFlag::Overflow) && |
| 251 |
context.languageFeatures().ShouldWarn( |
| 252 |
common::UsageWarning::FoldingException)) { |
| 253 |
context.messages().Say("DIM intrinsic folding overflow"_warn_en_US); |
| 254 |
} |
| 255 |
return result.value; |
| 256 |
})); |
| 257 |
} else if (name == "dot_product") { |
| 258 |
return FoldDotProduct<T>(context, std::move(funcRef)); |
| 259 |
} else if (name == "dprod") { |
| 260 |
// Rewrite DPROD(x,y) -> DBLE(x)*DBLE(y) |
| 261 |
if (args.at(0) && args.at(1)) { |
| 262 |
const auto *xExpr{args[0]->UnwrapExpr()}; |
| 263 |
const auto *yExpr{args[1]->UnwrapExpr()}; |
| 264 |
if (xExpr && yExpr) { |
| 265 |
return Fold(context, |
| 266 |
ToReal<T::kind>(context, common::Clone(*xExpr)) * |
| 267 |
ToReal<T::kind>(context, common::Clone(*yExpr))); |
| 268 |
} |
| 269 |
} |
| 270 |
} else if (name == "epsilon") { |
| 271 |
return Expr<T>{Scalar<T>::EPSILON()}; |
| 272 |
} else if (name == "fraction") { |
| 273 |
return FoldElementalIntrinsic<T, T>(context, std::move(funcRef), |
| 274 |
ScalarFunc<T, T>( |
| 275 |
[](const Scalar<T> &x) -> Scalar<T> { return x.FRACTION(); })); |
| 276 |
} else if (name == "huge") { |
| 277 |
return Expr<T>{Scalar<T>::HUGE()}; |
| 278 |
} else if (name == "hypot") { |
| 279 |
CHECK(args.size() == 2); |
| 280 |
return FoldElementalIntrinsic<T, T, T>(context, std::move(funcRef), |
| 281 |
ScalarFunc<T, T, T>( |
| 282 |
[&](const Scalar<T> &x, const Scalar<T> &y) -> Scalar<T> { |
| 283 |
ValueWithRealFlags<Scalar<T>> result{x.HYPOT(y)}; |
| 284 |
if (result.flags.test(RealFlag::Overflow) && |
| 285 |
context.languageFeatures().ShouldWarn( |
| 286 |
common::UsageWarning::FoldingException)) { |
| 287 |
context.messages().Say( |
| 288 |
"HYPOT intrinsic folding overflow"_warn_en_US); |
| 289 |
} |
| 290 |
return result.value; |
| 291 |
})); |
| 292 |
} else if (name == "matmul") { |
| 293 |
return FoldMatmul(context, std::move(funcRef)); |
| 294 |
} else if (name == "max") { |
| 295 |
return FoldMINorMAX(context, std::move(funcRef), Ordering::Greater); |
| 296 |
} else if (name == "maxval") { |
| 297 |
return FoldMaxvalMinval<T>(context, std::move(funcRef), |
| 298 |
RelationalOperator::GT, T::Scalar::HUGE().Negate()); |
| 299 |
} else if (name == "min") { |
| 300 |
return FoldMINorMAX(context, std::move(funcRef), Ordering::Less); |
| 301 |
} else if (name == "minval") { |
| 302 |
return FoldMaxvalMinval<T>( |
| 303 |
context, std::move(funcRef), RelationalOperator::LT, T::Scalar::HUGE()); |
| 304 |
} else if (name == "mod") { |
| 305 |
CHECK(args.size() == 2); |
| 306 |
bool badPConst{false}; |
| 307 |
if (auto *pExpr{UnwrapExpr<Expr<T>>(args[1])}) { |
| 308 |
*pExpr = Fold(context, std::move(*pExpr)); |
| 309 |
if (auto pConst{GetScalarConstantValue<T>(*pExpr)}; pConst && |
| 310 |
pConst->IsZero() && |
| 311 |
context.languageFeatures().ShouldWarn( |
| 312 |
common::UsageWarning::FoldingAvoidsRuntimeCrash)) { |
| 313 |
context.messages().Say("MOD: P argument is zero"_warn_en_US); |
| 314 |
badPConst = true; |
| 315 |
} |
| 316 |
} |
| 317 |
return FoldElementalIntrinsic<T, T, T>(context, std::move(funcRef), |
| 318 |
ScalarFunc<T, T, T>([&context, badPConst](const Scalar<T> &x, |
| 319 |
const Scalar<T> &y) -> Scalar<T> { |
| 320 |
auto result{x.MOD(y)}; |
| 321 |
if (!badPConst && result.flags.test(RealFlag::DivideByZero) && |
| 322 |
context.languageFeatures().ShouldWarn( |
| 323 |
common::UsageWarning::FoldingAvoidsRuntimeCrash)) { |
| 324 |
context.messages().Say( |
| 325 |
"second argument to MOD must not be zero"_warn_en_US); |
| 326 |
} |
| 327 |
return result.value; |
| 328 |
})); |
| 329 |
} else if (name == "modulo") { |
| 330 |
CHECK(args.size() == 2); |
| 331 |
bool badPConst{false}; |
| 332 |
if (auto *pExpr{UnwrapExpr<Expr<T>>(args[1])}) { |
| 333 |
*pExpr = Fold(context, std::move(*pExpr)); |
| 334 |
if (auto pConst{GetScalarConstantValue<T>(*pExpr)}; pConst && |
| 335 |
pConst->IsZero() && |
| 336 |
context.languageFeatures().ShouldWarn( |
| 337 |
common::UsageWarning::FoldingAvoidsRuntimeCrash)) { |
| 338 |
context.messages().Say("MODULO: P argument is zero"_warn_en_US); |
| 339 |
badPConst = true; |
| 340 |
} |
| 341 |
} |
| 342 |
return FoldElementalIntrinsic<T, T, T>(context, std::move(funcRef), |
| 343 |
ScalarFunc<T, T, T>([&context, badPConst](const Scalar<T> &x, |
| 344 |
const Scalar<T> &y) -> Scalar<T> { |
| 345 |
auto result{x.MODULO(y)}; |
| 346 |
if (!badPConst && result.flags.test(RealFlag::DivideByZero) && |
| 347 |
context.languageFeatures().ShouldWarn( |
| 348 |
common::UsageWarning::FoldingAvoidsRuntimeCrash)) { |
| 349 |
context.messages().Say( |
| 350 |
"second argument to MODULO must not be zero"_warn_en_US); |
| 351 |
} |
| 352 |
return result.value; |
| 353 |
})); |
| 354 |
} else if (name == "nearest") { |
| 355 |
if (auto *sExpr{UnwrapExpr<Expr<SomeReal>>(args[1])}) { |
| 356 |
*sExpr = Fold(context, std::move(*sExpr)); |
| 357 |
return common::visit( |
| 358 |
[&](const auto &sVal) { |
| 359 |
using TS = ResultType<decltype(sVal)>; |
| 360 |
bool badSConst{false}; |
| 361 |
if (auto sConst{GetScalarConstantValue<TS>(sVal)}; sConst && |
| 362 |
sConst->IsZero() && |
| 363 |
context.languageFeatures().ShouldWarn( |
| 364 |
common::UsageWarning::FoldingValueChecks)) { |
| 365 |
context.messages().Say("NEAREST: S argument is zero"_warn_en_US); |
| 366 |
badSConst = true; |
| 367 |
} |
| 368 |
return FoldElementalIntrinsic<T, T, TS>(context, std::move(funcRef), |
| 369 |
ScalarFunc<T, T, TS>([&](const Scalar<T> &x, |
| 370 |
const Scalar<TS> &s) -> Scalar<T> { |
| 371 |
if (!badSConst && s.IsZero() && |
| 372 |
context.languageFeatures().ShouldWarn( |
| 373 |
common::UsageWarning::FoldingValueChecks)) { |
| 374 |
context.messages().Say( |
| 375 |
"NEAREST: S argument is zero"_warn_en_US); |
| 376 |
} |
| 377 |
auto result{x.NEAREST(!s.IsNegative())}; |
| 378 |
if (context.languageFeatures().ShouldWarn( |
| 379 |
common::UsageWarning::FoldingException)) { |
| 380 |
if (result.flags.test(RealFlag::Overflow)) { |
| 381 |
context.messages().Say( |
| 382 |
"NEAREST intrinsic folding overflow"_warn_en_US); |
| 383 |
} else if (result.flags.test(RealFlag::InvalidArgument)) { |
| 384 |
context.messages().Say( |
| 385 |
"NEAREST intrinsic folding: bad argument"_warn_en_US); |
| 386 |
} |
| 387 |
} |
| 388 |
return result.value; |
| 389 |
})); |
| 390 |
}, |
| 391 |
sExpr->u); |
| 392 |
} |
| 393 |
} else if (name == "norm2") { |
| 394 |
return FoldNorm2<T::kind>(context, std::move(funcRef)); |
| 395 |
} else if (name == "product") { |
| 396 |
auto one{Scalar<T>::FromInteger(value::Integer<8>{1}).value}; |
| 397 |
return FoldProduct<T>(context, std::move(funcRef), one); |
| 398 |
} else if (name == "real" || name == "dble") { |
| 399 |
if (auto *expr{args[0].value().UnwrapExpr()}) { |
| 400 |
return ToReal<KIND>(context, std::move(*expr)); |
| 401 |
} |
| 402 |
} else if (name == "rrspacing") { |
| 403 |
return FoldElementalIntrinsic<T, T>(context, std::move(funcRef), |
| 404 |
ScalarFunc<T, T>( |
| 405 |
[](const Scalar<T> &x) -> Scalar<T> { return x.RRSPACING(); })); |
| 406 |
} else if (name == "scale") { |
| 407 |
if (const auto *byExpr{UnwrapExpr<Expr<SomeInteger>>(args[1])}) { |
| 408 |
return common::visit( |
| 409 |
[&](const auto &byVal) { |
| 410 |
using TBY = ResultType<decltype(byVal)>; |
| 411 |
return FoldElementalIntrinsic<T, T, TBY>(context, |
| 412 |
std::move(funcRef), |
| 413 |
ScalarFunc<T, T, TBY>( |
| 414 |
[&](const Scalar<T> &x, const Scalar<TBY> &y) -> Scalar<T> { |
| 415 |
ValueWithRealFlags<Scalar<T>> result{ |
| 416 |
x. |
| 417 |
// MSVC chokes on the keyword "template" here in a call to a |
| 418 |
// member function template. |
| 419 |
#ifndef _MSC_VER |
| 420 |
template |
| 421 |
#endif |
| 422 |
SCALE<Scalar<TBY>>(y)}; |
| 423 |
if (result.flags.test(RealFlag::Overflow) && |
| 424 |
context.languageFeatures().ShouldWarn( |
| 425 |
common::UsageWarning::FoldingException)) { |
| 426 |
context.messages().Say( |
| 427 |
"SCALE intrinsic folding overflow"_warn_en_US); |
| 428 |
} |
| 429 |
return result.value; |
| 430 |
})); |
| 431 |
}, |
| 432 |
byExpr->u); |
| 433 |
} |
| 434 |
} else if (name == "set_exponent") { |
| 435 |
if (const auto *iExpr{UnwrapExpr<Expr<SomeInteger>>(args[1])}) { |
| 436 |
return common::visit( |
| 437 |
[&](const auto &iVal) { |
| 438 |
using TY = ResultType<decltype(iVal)>; |
| 439 |
return FoldElementalIntrinsic<T, T, TY>(context, std::move(funcRef), |
| 440 |
ScalarFunc<T, T, TY>( |
| 441 |
[&](const Scalar<T> &x, const Scalar<TY> &i) -> Scalar<T> { |
| 442 |
return x.SET_EXPONENT(i.ToInt64()); |
| 443 |
})); |
| 444 |
}, |
| 445 |
iExpr->u); |
| 446 |
} |
| 447 |
} else if (name == "sign") { |
| 448 |
return FoldElementalIntrinsic<T, T, T>( |
| 449 |
context, std::move(funcRef), &Scalar<T>::SIGN); |
| 450 |
} else if (name == "spacing") { |
| 451 |
return FoldElementalIntrinsic<T, T>(context, std::move(funcRef), |
| 452 |
ScalarFunc<T, T>( |
| 453 |
[](const Scalar<T> &x) -> Scalar<T> { return x.SPACING(); })); |
| 454 |
} else if (name == "sqrt") { |
| 455 |
return FoldElementalIntrinsic<T, T>(context, std::move(funcRef), |
| 456 |
ScalarFunc<T, T>( |
| 457 |
[](const Scalar<T> &x) -> Scalar<T> { return x.SQRT().value; })); |
| 458 |
} else if (name == "sum") { |
| 459 |
return FoldSum<T>(context, std::move(funcRef)); |
| 460 |
} else if (name == "tiny") { |
| 461 |
return Expr<T>{Scalar<T>::TINY()}; |
| 462 |
} else if (name == "__builtin_fma") { |
| 463 |
CHECK(args.size() == 3); |
| 464 |
} else if (name == "__builtin_ieee_next_after") { |
| 465 |
if (const auto *yExpr{UnwrapExpr<Expr<SomeReal>>(args[1])}) { |
| 466 |
return common::visit( |
| 467 |
[&](const auto &yVal) { |
| 468 |
using TY = ResultType<decltype(yVal)>; |
| 469 |
return FoldElementalIntrinsic<T, T, TY>(context, std::move(funcRef), |
| 470 |
ScalarFunc<T, T, TY>([&](const Scalar<T> &x, |
| 471 |
const Scalar<TY> &y) -> Scalar<T> { |
| 472 |
bool upward{true}; |
| 473 |
switch (x.Compare(Scalar<T>::Convert(y).value)) { |
| 474 |
case Relation::Unordered: |
| 475 |
if (context.languageFeatures().ShouldWarn( |
| 476 |
common::UsageWarning::FoldingValueChecks)) { |
| 477 |
context.messages().Say( |
| 478 |
"IEEE_NEXT_AFTER intrinsic folding: bad argument"_warn_en_US); |
| 479 |
} |
| 480 |
return x; |
| 481 |
case Relation::Equal: |
| 482 |
return x; |
| 483 |
case Relation::Less: |
| 484 |
upward = true; |
| 485 |
break; |
| 486 |
case Relation::Greater: |
| 487 |
upward = false; |
| 488 |
break; |
| 489 |
} |
| 490 |
auto result{x.NEAREST(upward)}; |
| 491 |
if (result.flags.test(RealFlag::Overflow) && |
| 492 |
context.languageFeatures().ShouldWarn( |
| 493 |
common::UsageWarning::FoldingException)) { |
| 494 |
context.messages().Say( |
| 495 |
"IEEE_NEXT_AFTER intrinsic folding overflow"_warn_en_US); |
| 496 |
} |
| 497 |
return result.value; |
| 498 |
})); |
| 499 |
}, |
| 500 |
yExpr->u); |
| 501 |
} |
| 502 |
} else if (name == "__builtin_ieee_next_up" || |
| 503 |
name == "__builtin_ieee_next_down") { |
| 504 |
bool upward{name == "__builtin_ieee_next_up"}; |
| 505 |
const char *iName{upward ? "IEEE_NEXT_UP" : "IEEE_NEXT_DOWN"}; |
| 506 |
return FoldElementalIntrinsic<T, T>(context, std::move(funcRef), |
| 507 |
ScalarFunc<T, T>([&](const Scalar<T> &x) -> Scalar<T> { |
| 508 |
auto result{x.NEAREST(upward)}; |
| 509 |
if (context.languageFeatures().ShouldWarn( |
| 510 |
common::UsageWarning::FoldingException)) { |
| 511 |
if (result.flags.test(RealFlag::Overflow)) { |
| 512 |
context.messages().Say( |
| 513 |
"%s intrinsic folding overflow"_warn_en_US, iName); |
| 514 |
} else if (result.flags.test(RealFlag::InvalidArgument)) { |
| 515 |
context.messages().Say( |
| 516 |
"%s intrinsic folding: bad argument"_warn_en_US, iName); |
| 517 |
} |
| 518 |
} |
| 519 |
return result.value; |
| 520 |
})); |
| 521 |
} |
| 522 |
return Expr<T>{std::move(funcRef)}; |
| 523 |
} |
| 524 |
|
| 525 |
#ifdef _MSC_VER // disable bogus warning about missing definitions |
| 526 |
#pragma warning(disable : 4661) |
| 527 |
#endif |
| 528 |
FOR_EACH_REAL_KIND(template class ExpressionBase, ) |
| 529 |
template class ExpressionBase<SomeReal>; |
| 530 |
} // namespace Fortran::evaluate |