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
#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 #88151.
  • Loading branch information
klausler authored Apr 22, 2024
1 parent cb1b846 commit 31505c4
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 31505c4

Please sign in to comment.