Skip to content

Conversation

klausler
Copy link
Contributor

@klausler klausler commented Apr 9, 2024

…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 #88151.

…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 llvm#88151.
@klausler klausler requested a review from vzakhari April 9, 2024 20:33
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels Apr 9, 2024
@llvmbot
Copy link
Member

llvmbot commented Apr 9, 2024

@llvm/pr-subscribers-flang-semantics

Author: Peter Klausler (klausler)

Changes

…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 #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 --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;

@klausler klausler merged commit 31505c4 into llvm:main Apr 22, 2024
@klausler klausler deleted the bug88151 branch April 22, 2024 21:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

flang-new 19.0: "warning: overflow on power with INTEGER exponent“
3 participants