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

via flang-commits flang-commits at lists.llvm.org
Mon Apr 22 14:39:34 PDT 2024


Author: Peter Klausler
Date: 2024-04-22T14:39:30-07:00
New Revision: 31505c4f6b977c2ccc785de29a047a31563e5c90

URL: https://github.com/llvm/llvm-project/commit/31505c4f6b977c2ccc785de29a047a31563e5c90
DIFF: https://github.com/llvm/llvm-project/commit/31505c4f6b977c2ccc785de29a047a31563e5c90.diff

LOG: [flang] Fix spurious overflow warning folding exponentiation by integ… (#88188)

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

Added: 
    

Modified: 
    flang/lib/Evaluate/int-power.h

Removed: 
    


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


        


More information about the flang-commits mailing list