Issue 237728
Summary [APFloat] next(nextDown=true) returns a negative value in formats without a sign
Labels new issue
Assignees
Reporter Tim096
    `APFloat::next(/*nextDown=*/true)` leaves `sign` set in `Float8E5M3FNU` and `Float8E8M0FNU`, which have no sign. It happens where the format has no value below the input: zero in `Float8E5M3FNU`, and the smallest value in `Float8E8M0FNU`, which has no zero.

```c++
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/SmallString.h"
#include <cstdio>
using namespace llvm;

static void show(APFloat X) {
  X.next(/*nextDown=*/true);
 SmallString<32> S;
  X.toString(S);
  printf("%s negative=%d bits=0x%02llx\n", S.c_str(), X.isNegative(),
         (unsigned long long)X.bitcastToAPInt().getZExtValue());
}

int main() {
 show(APFloat::getZero(APFloat::Float8E5M3FNU()));
 show(APFloat::getSmallest(APFloat::Float8E8M0FNU()));
}
```

```
-7.62E-6 negative=1 bits=0x01
-5.9E-39 negative=1 bits=0x00
```

The bits have no sign, so the value changes on a round trip through `bitcastToAPInt`. Both calls return `opOK`.

The cause is the `changeSign()` at each end of `next`, which computes `nextDown(x)` as `-nextUp(-x)`.

I'm not sure what the result should be, since the format has nothing below the input. #230086 turns other negative results in these formats into NaN with `opInvalidOp`, which would be one option. Keeping the input is another.

Tested on `5f890fc6161a`. I found this while checking #230086, which does not change `next`.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to