[flang-commits] [flang] 43d41b2 - [flang] Correct overflow detection in folding of real->integer conversions

peter klausler via flang-commits flang-commits at lists.llvm.org
Fri Sep 17 09:50:29 PDT 2021


Author: peter klausler
Date: 2021-09-17T09:50:22-07:00
New Revision: 43d41b295e15fcf9487279f700038d57c8eb43d8

URL: https://github.com/llvm/llvm-project/commit/43d41b295e15fcf9487279f700038d57c8eb43d8
DIFF: https://github.com/llvm/llvm-project/commit/43d41b295e15fcf9487279f700038d57c8eb43d8.diff

LOG: [flang] Correct overflow detection in folding of real->integer conversions

INT, NINT, FLOOR, and CEILING were failing to report overflow as an
error while folding operations with constant operands.

Differential Revision: https://reviews.llvm.org/D109922

Added: 
    

Modified: 
    flang/include/flang/Evaluate/real.h

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Evaluate/real.h b/flang/include/flang/Evaluate/real.h
index fa637abe9c2e7..bf43e68f67b73 100644
--- a/flang/include/flang/Evaluate/real.h
+++ b/flang/include/flang/Evaluate/real.h
@@ -221,21 +221,26 @@ class Real : public common::RealDetails<PREC> {
       return result;
     }
     ValueWithRealFlags<Real> intPart{ToWholeNumber(mode)};
-    int exponent{intPart.value.Exponent()};
-    result.flags.set(
-        RealFlag::Overflow, exponent >= exponentBias + result.value.bits);
     result.flags |= intPart.flags;
-    int shift{
-        exponent - exponentBias - binaryPrecision + 1}; // positive -> left
-    result.value =
-        result.value.ConvertUnsigned(intPart.value.GetFraction().SHIFTR(-shift))
-            .value.SHIFTL(shift);
+    int exponent{intPart.value.Exponent()};
+    // shift positive -> left shift, negative -> right shift
+    int shift{exponent - exponentBias - binaryPrecision + 1};
+    // Apply any right shift before moving to the result type
+    auto rshifted{intPart.value.GetFraction().SHIFTR(-shift)};
+    auto converted{result.value.ConvertUnsigned(rshifted)};
+    if (converted.overflow) {
+      result.flags.set(RealFlag::Overflow);
+    }
+    result.value = converted.value.SHIFTL(shift);
+    if (converted.value.CompareUnsigned(result.value.SHIFTR(shift)) !=
+        Ordering::Equal) {
+      result.flags.set(RealFlag::Overflow);
+    }
     if (IsSignBitSet()) {
-      auto negated{result.value.Negate()};
-      result.value = negated.value;
-      if (negated.overflow) {
-        result.flags.set(RealFlag::Overflow);
-      }
+      result.value = result.value.Negate().value;
+    }
+    if (IsSignBitSet() != result.value.IsNegative()) {
+      result.flags.set(RealFlag::Overflow);
     }
     if (result.flags.test(RealFlag::Overflow)) {
       result.value =


        


More information about the flang-commits mailing list