[flang-commits] [PATCH] D135203: [flang] Don't force SET_EXPONENT(I=...) argument to integer(4)

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Thu Oct 6 11:05:07 PDT 2022


klausler updated this revision to Diff 465789.
klausler added a comment.

Properly handle underflow case in SET_EXPONENT.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D135203/new/

https://reviews.llvm.org/D135203

Files:
  flang/include/flang/Evaluate/real.h
  flang/lib/Evaluate/fold-real.cpp
  flang/lib/Evaluate/real.cpp


Index: flang/lib/Evaluate/real.cpp
===================================================================
--- flang/lib/Evaluate/real.cpp
+++ flang/lib/Evaluate/real.cpp
@@ -756,7 +756,7 @@
 
 // 16.9.171
 template <typename W, int P>
-Real<W, P> Real<W, P>::SET_EXPONENT(int expo) const {
+Real<W, P> Real<W, P>::SET_EXPONENT(std::int64_t expo) const {
   if (IsNotANumber()) {
     return *this;
   } else if (IsInfinite()) {
@@ -764,7 +764,7 @@
   } else if (IsZero()) {
     return *this;
   } else {
-    return SCALE(Integer<32>(expo - UnbiasedExponent() - 1)).value;
+    return SCALE(Integer<64>(expo - UnbiasedExponent() - 1)).value;
   }
 }
 
Index: flang/lib/Evaluate/fold-real.cpp
===================================================================
--- flang/lib/Evaluate/fold-real.cpp
+++ flang/lib/Evaluate/fold-real.cpp
@@ -257,11 +257,18 @@
           byExpr->u);
     }
   } else if (name == "set_exponent") {
-    return FoldElementalIntrinsic<T, T, Int4>(context, std::move(funcRef),
-        ScalarFunc<T, T, Int4>(
-            [&](const Scalar<T> &x, const Scalar<Int4> &i) -> Scalar<T> {
-              return x.SET_EXPONENT(i.ToInt64());
-            }));
+    if (const auto *iExpr{UnwrapExpr<Expr<SomeInteger>>(args[1])}) {
+      return common::visit(
+          [&](const auto &iVal) {
+            using TY = ResultType<decltype(iVal)>;
+            return FoldElementalIntrinsic<T, T, TY>(context, std::move(funcRef),
+                ScalarFunc<T, T, TY>(
+                    [&](const Scalar<T> &x, const Scalar<TY> &i) -> Scalar<T> {
+                      return x.SET_EXPONENT(i.ToInt64());
+                    }));
+          },
+          iExpr->u);
+    }
   } else if (name == "sign") {
     return FoldElementalIntrinsic<T, T, T>(
         context, std::move(funcRef), &Scalar<T>::SIGN);
Index: flang/include/flang/Evaluate/real.h
===================================================================
--- flang/include/flang/Evaluate/real.h
+++ flang/include/flang/Evaluate/real.h
@@ -170,7 +170,7 @@
   static constexpr int MINEXPONENT{2 - exponentBias};
   Real RRSPACING() const;
   Real SPACING() const;
-  Real SET_EXPONENT(int) const;
+  Real SET_EXPONENT(std::int64_t) const;
   Real FRACTION() const;
 
   // SCALE(); also known as IEEE_SCALB and (in IEEE-754 '08) ScaleB.
@@ -182,16 +182,20 @@
     //  be subnormal.)
     auto adjust{exponentBias + binaryPrecision - 1};
     auto expo{adjust + by.ToInt64()};
+    Real twoPow;
+    RealFlags flags;
+    int rMask{1};
     if (IsZero()) {
       expo = exponentBias; // ignore by, don't overflow
     } else if (by > INT{maxExponent}) {
       expo = maxExponent + binaryPrecision - 1;
-    } else if (by < INT{-adjust}) {
-      expo = -1;
+    } else if (by < INT{-adjust}) { // underflow
+      expo = 0;
+      rMask = 0;
+      flags.set(RealFlag::Underflow);
     }
-    Real twoPow;
-    RealFlags flags{
-        twoPow.Normalize(false, static_cast<int>(expo), Fraction::MASKR(1))};
+    flags |=
+        twoPow.Normalize(false, static_cast<int>(expo), Fraction::MASKR(rMask));
     ValueWithRealFlags<Real> result{Multiply(twoPow, rounding)};
     result.flags |= flags;
     return result;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D135203.465789.patch
Type: text/x-patch
Size: 3199 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20221006/2021f445/attachment-0001.bin>


More information about the flang-commits mailing list