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

    <tr>
        <th>Summary</th>
        <td>
            -frounding-math is buggy at -O1 and above
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          vinc17fr
      </td>
    </tr>
</table>

<pre>
    https://clang.llvm.org/docs/UsersManual.html says:
> The option -frounding-math forces the compiler to honor the dynamically-set rounding mode. This prevents optimizations which might affect results if the rounding mode changes or is different from the default; for example, it prevents floating-point operations from being reordered across most calls and prevents constant-folding when the result is not exactly representable.

But with Clang from 13 to 16, while it appears to make a difference at `-O0`, it seems ignored at higher optimization levels (assuming that `#pragma STDC FENV_ACCESS ON` is not used).

For instance, consider the following C program (similar to the one on https://gitlab.inria.fr/core-math/core-math/-/issues/8 but with this pragma removed):

```
#include <stdio.h>
#include <fenv.h>

float foo (void) { return 0x1.fffffep127f * 0x1.fffffep127f; }

int main (void)
{
  fesetround (FE_TONEAREST);
  printf ("FE_TONEAREST: %a\n", foo ());
  fesetround (FE_TOWARDZERO);
  printf ("FE_TOWARDZERO: %a\n", foo ());
  fesetround (FE_UPWARD);
  printf ("FE_UPWARD: %a\n", foo ());
  fesetround (FE_DOWNWARD);
  printf ("FE_DOWNWARD: %a\n", foo ());
  return 0;
}
```

On an x86_64 Debian machine, with `-frounding-math` and `-O1` (or above), I get
```
FE_TONEAREST: inf
FE_TOWARDZERO: inf
FE_UPWARD: inf
FE_DOWNWARD: inf
```
instead of
```
FE_TONEAREST: inf
FE_TOWARDZERO: 0x1.fffffep+127
FE_UPWARD: inf
FE_DOWNWARD: 0x1.fffffep+127
```

Note: at least on this example, the pragma works at any optimization level, without the need of `-frounding-math`.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJykVk2P4zYM_T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