[flang-commits] [flang] [flang] Fix spurious overflow warning folding exponentiation by integ… (PR #88188)

via flang-commits flang-commits at lists.llvm.org
Tue Apr 9 13:34:20 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-semantics

Author: Peter Klausler (klausler)

<details>
<summary>Changes</summary>

…er powers

The code that folds exponentiation by an integer power can report a spurious overflow warning because it calculates one last unnecessary square of the base value.  10.**(+/-32) exposes the problem -- the value of 10.**64 is calculated but not needed.  Rearrange the implementation to only calculate squares that are necessary.

Fixes https://github.com/llvm/llvm-project/issues/88151.

---
Full diff: https://github.com/llvm/llvm-project/pull/88188.diff


1 Files Affected:

- (modified) flang/lib/Evaluate/int-power.h (+4-2) 


``````````diff
diff --git a/flang/lib/Evaluate/int-power.h b/flang/lib/Evaluate/int-power.h
index 0d6a133ae73c51..2ee012ceb77a38 100644
--- a/flang/lib/Evaluate/int-power.h
+++ b/flang/lib/Evaluate/int-power.h
@@ -33,6 +33,10 @@ ValueWithRealFlags<REAL> TimesIntPowerOf(const REAL &factor, const REAL &base,
     REAL squares{base};
     int nbits{INT::bits - absPower.LEADZ()};
     for (int j{0}; j < nbits; ++j) {
+      if (j > 0) { // avoid spurious overflow on last iteration
+        squares =
+            squares.Multiply(squares, rounding).AccumulateFlags(result.flags);
+      }
       if (absPower.BTEST(j)) {
         if (negativePower) {
           result.value = result.value.Divide(squares, rounding)
@@ -42,8 +46,6 @@ ValueWithRealFlags<REAL> TimesIntPowerOf(const REAL &factor, const REAL &base,
                              .AccumulateFlags(result.flags);
         }
       }
-      squares =
-          squares.Multiply(squares, rounding).AccumulateFlags(result.flags);
     }
   }
   return result;

``````````

</details>


https://github.com/llvm/llvm-project/pull/88188


More information about the flang-commits mailing list