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 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 TimesIntPowerOf(const REAL &factor, const REAL &base, .AccumulateFlags(result.flags); } } - squares = - squares.Multiply(squares, rounding).AccumulateFlags(result.flags); } } return result;