<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/104984>104984</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            APFloat's fusedMultiplyAdd gives an incorrectly rounded result for IEEEfloat
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          sizn-sc
      </td>
    </tr>
</table>

<pre>
    While comparing APFloat against [berkeley-softfloat-3e](https://github.com/ucb-bar/berkeley-softfloat-3) I found a discrepancy in `fusedMultiplyAdd` in a particular corner-case:

```cpp
#include <cmath>
#include <iostream>
#include <bit>
#include <cstdint>

int main()
{
 static_assert(sizeof(float) == 4);
    auto a = 0.24999998f;
 auto b = 2.3509885e-38f;
    auto c = -1e-45f;
    auto d = std::fmaf(a, b, c);
    // Clang with optimizations folds d to 3ffffe, without optimizations 3fffff. 
    std::cout << std::hex << std::bit_cast<uint32_t>(d) << "\n";
}
```

Reproduction available at [compiler explorer](https://godbolt.org/z/vaWoexG17). This occurs for NearestTiesToEven and NearestTiesToAway rounding modes. Originally this issue was discovered by linking against LLVMSupport and using APFloat directly, but it also affects constant folding with default rounding mode.

GCC and native x86 FPU seem to agree with clang without optimizations.
This is likely a case of incorrect rounding and is unrelated to https://github.com/llvm/llvm-project/issues/63895.

(cc @eddyb @beetrees )
 
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVcFu4zYQ_Rr6MrAhk5ItHXTI2vFigd120abdYzAiRzYbmhRIyonz9QVpO06y2R5qGJTANxw-Pr4ZYQh6a4laVn1i1XqCY9w53wb9bKdBTjqnju2PnTYE0u0H9Npu4eb7xjiMgFvUNkRg1aeO_AMZOk6D62Of0KkgVq0Zr3cxDoGJG8Y3jG-2Ou7GbibdnvHNKLtph57xzUfrGW_gC_RutAoQlA7S04BWHkFbYIuiHw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