[flang-commits] [flang] [flang] Fix spurious overflow warning folding exponentiation by integ… (PR #88188)
Peter Klausler via flang-commits
flang-commits at lists.llvm.org
Tue Apr 9 13:33:48 PDT 2024
https://github.com/klausler created https://github.com/llvm/llvm-project/pull/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.
>From fb573c4b07c6b4cc51cdd9e8260ddbf9e32f920a Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Tue, 9 Apr 2024 13:29:07 -0700
Subject: [PATCH] [flang] Fix spurious overflow warning folding exponentiation
by integer 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.
---
flang/lib/Evaluate/int-power.h | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
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