llvm / llvm/llvm-project

[APFloat] Overflow flag is dropped when rounding delivers the largest finite value

Open Beginner friendly
#220,018 0 comments 0 reactions 0 assignees View on GitHub
llvm:adt
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

`IEEEFloat::handleOverflow` ( in `llvm/lib/Support/APFloat.cpp`) returns `opInexact` alone on the finite-result path.
IEEE 754-2019 7.4 requires the overflow whenever the largest finite number is exceeded in magnitude by what would have been the rounded floating point result with an unbounded exponent range.
Whether the default result subsequently produced by the selected rounding mode is infinity or the largest finite value does not change whether overflow occurred.

In particular, the specification details overflow for `roundTowardZero` and `roundTowardNegative` even with a non inf result.

Reproducer:

```cpp
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/raw_ostream.h"

using namespace llvm;

static void report(const char *Name, APFloat::roundingMode RM) {
auto X = APFloat::getLargest(APFloat::IEEEquad());
const auto Max = X;
auto Status = X.multiply(Max, RM);
SmallString<64> Result;
X.toString(Result);
outs() << Name << ": result=" << Result
<< " overflow=" << ((Status & APFloat::opOverflow) ? "yes" : "no")
<< " inexact=" << ((Status & APFloat::opInexact) ? "yes" : "no")
<< "\n";
}

int main() {
report("rmNearestTiesToEven", APFloat::rmNearestTiesToEven);
report("rmTowardZero ", APFloat::rmTowardZero);
report("rmTowardNegative ", APFloat::rmTowardNegative);
}
```

Expected result:
```
rmNearestTiesToEven: result=+Inf overflow=yes inexact=yes
rmTowardZero : result=1.18973149535723176508575932662800702E+4932 overflow=yes inexact=yes
rmTowardNegative : result=1.18973149535723176508575932662800702E+4932 overflow=yes inexact=yes
```

Current result:
```
rmNearestTiesToEven: result=+Inf overflow=yes inexact=yes
rmTowardZero : result=1.18973149535723176508575932662800702E+4932 overflow=no inexact=yes
rmTowardNegative : result=1.18973149535723176508575932662800702E+4932 overflow=no inexact=yes
```

Contributor guide

Open the contributing guide

Research direction

Start in llvm/lib/Support/APFloat.cpp at IEEEFloat::handleOverflow and use the provided APFloat reproducer to observe the rounding-mode results. Update the overflow status behavior for finite results, then verify that roundTowardZero and roundTowardNegative report overflow while preserving their expected largest-finite result.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
82/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.