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

    <tr>
        <th>Summary</th>
        <td>
            FP constant expressions evaluated incorrectly with FLT_EVAL_METHOD == 2
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang:frontend,
            c99,
            floating-point
      </td>
    </tr>

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

    <tr>
      <th>Reporter</th>
      <td>
          jcranmer-intel
      </td>
    </tr>
</table>

<pre>
    On systems with FLT_EVAL_METHOD == 2 (i.e., x87-based floating-point computation), arithmetic expressions using `float` and `double` should be evaluated using `long double` instead. Clang isn't doing this:

```c
#include <float.h>
#include <stdio.h>

static double const_init = 1.0 + (DBL_EPSILON/2) + (DBL_EPSILON/2);
int main() {
    double nonconst_init = 1.0;
    nonconst_init = nonconst_init + (DBL_EPSILON/2) + (DBL_EPSILON/2);
    printf("FLT_EVAL_METHOD = %d\n", FLT_EVAL_METHOD);
    printf("const: %g\n", const_init - 1.0);
 printf("nonconst: %g\n", (double)nonconst_init - 1.0);
}
```

When compiled on an x86 system with `-m32 -mno-sse`, the correct result should be that the two lines both output `2.22045e-16`, which icc is able to successfully do. However clang and gcc both report 0 for the first line and `2.22045e-16` for the second: https://godbolt.org/z/4xGoaaoKP.

(At first glance, this sounds exactly like https://github.com/llvm/llvm-project/issues/88956, but that is about *constants* and this is about constant *expressions*).

C99 section 6.6p5 says
> If a floating expression is evaluated in the translation environment, the arithmetic precision and range shall be at least as great as if the expression were being evaluated in the execution environment.

C11 adds a footnote explicitly saying that `FLT_EVAL_METHOD` also applies to the translation environment, and C23 adds `DEC_EVAL_METHOD` to the footnote, but otherwise the text remains the same in all versions of C.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJykVlGP4jYQ_jXmZUQUHAjkgQcWlt6p295JPbWPK8eeJL4aG3mcXeivr-zAbuCu14eTVtnEnnzz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