@@ -359,28 +359,27 @@ Expr<Type<TypeCategory::Real, KIND>> FoldIntrinsicFunction(
359
359
using TS = ResultType<decltype (sVal )>;
360
360
bool badSConst{false };
361
361
if (auto sConst {GetScalarConstantValue<TS>(sVal )}; sConst &&
362
- sConst ->IsZero () &&
362
+ ( sConst ->IsZero () || sConst -> IsNotANumber () ) &&
363
363
context.languageFeatures ().ShouldWarn (
364
364
common::UsageWarning::FoldingValueChecks)) {
365
- context.messages ().Say (" NEAREST: S argument is zero" _warn_en_US);
365
+ context.messages ().Say (" NEAREST: S argument is %s" _warn_en_US,
366
+ sConst ->IsZero () ? " zero" : " NaN" );
366
367
badSConst = true ;
367
368
}
368
369
return FoldElementalIntrinsic<T, T, TS>(context, std::move (funcRef),
369
370
ScalarFunc<T, T, TS>([&](const Scalar<T> &x,
370
371
const Scalar<TS> &s) -> Scalar<T> {
371
- if (!badSConst && s.IsZero () &&
372
+ if (!badSConst && ( s.IsZero () || s. IsNotANumber () ) &&
372
373
context.languageFeatures ().ShouldWarn (
373
374
common::UsageWarning::FoldingValueChecks)) {
374
375
context.messages ().Say (
375
- " NEAREST: S argument is zero" _warn_en_US);
376
+ " NEAREST: S argument is %s" _warn_en_US,
377
+ s.IsZero () ? " zero" : " NaN" );
376
378
}
377
379
auto result{x.NEAREST (!s.IsNegative ())};
378
380
if (context.languageFeatures ().ShouldWarn (
379
381
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)) {
382
+ if (result.flags .test (RealFlag::InvalidArgument)) {
384
383
context.messages ().Say (
385
384
" NEAREST intrinsic folding: bad argument" _warn_en_US);
386
385
}
@@ -469,32 +468,26 @@ Expr<Type<TypeCategory::Real, KIND>> FoldIntrinsicFunction(
469
468
return FoldElementalIntrinsic<T, T, TY>(context, std::move (funcRef),
470
469
ScalarFunc<T, T, TY>([&](const Scalar<T> &x,
471
470
const Scalar<TY> &y) -> Scalar<T> {
472
- bool upward{true };
473
- switch (x.Compare (Scalar<T>::Convert (y).value )) {
471
+ bool reverseCompare{
472
+ Scalar<T>::binaryPrecision < Scalar<TY>::binaryPrecision};
473
+ switch (reverseCompare
474
+ ? y.Compare (Scalar<TY>::Convert (x).value )
475
+ : x.Compare (Scalar<T>::Convert (y).value )) {
474
476
case Relation::Unordered:
475
477
if (context.languageFeatures ().ShouldWarn (
476
478
common::UsageWarning::FoldingValueChecks)) {
477
479
context.messages ().Say (
478
- " IEEE_NEXT_AFTER intrinsic folding: bad argument " _warn_en_US);
480
+ " IEEE_NEXT_AFTER intrinsic folding: arguments are unordered " _warn_en_US);
479
481
}
480
- return x;
482
+ return x. NotANumber () ;
481
483
case Relation::Equal:
482
- return x;
483
- case Relation::Less:
484
- upward = true ;
485
484
break ;
485
+ case Relation::Less:
486
+ return x.NEAREST (!reverseCompare).value ;
486
487
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);
488
+ return x.NEAREST (reverseCompare).value ;
496
489
}
497
- return result. value ;
490
+ return x; // dodge bogus "missing return" GCC warning
498
491
}));
499
492
},
500
493
yExpr->u );
@@ -508,12 +501,9 @@ Expr<Type<TypeCategory::Real, KIND>> FoldIntrinsicFunction(
508
501
auto result{x.NEAREST (upward)};
509
502
if (context.languageFeatures ().ShouldWarn (
510
503
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)) {
504
+ if (result.flags .test (RealFlag::InvalidArgument)) {
515
505
context.messages ().Say (
516
- " %s intrinsic folding: bad argument" _warn_en_US, iName);
506
+ " %s intrinsic folding: argument is NaN " _warn_en_US, iName);
517
507
}
518
508
}
519
509
return result.value ;
0 commit comments