Skip to content

Commit

Permalink
[flang] Fix spurious overflow warning folding exponentiation by integ…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
klausler committed Apr 9, 2024
1 parent e8e6795 commit fb573c4
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions flang/lib/Evaluate/int-power.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand Down

0 comments on commit fb573c4

Please sign in to comment.