[clang] [clang] Use constant rounding mode for floating literals (PR #90877)

Serge Pavlov via cfe-commits cfe-commits at lists.llvm.org
Fri May 3 10:05:18 PDT 2024


================
@@ -79,3 +79,16 @@ float V7 = []() -> float {
   0x0.000001p0F);
 }();
 // CHECK: @V7 = {{.*}} float 1.000000e+00
+
+template<float V> struct L {
+  constexpr L() : value(V) {}
+  float value;
+};
+
+#pragma STDC FENV_ROUND FE_DOWNWARD
----------------
spavloff wrote:

> Does the FENV_ROUND exclusively change the behavior of literals? I thought it messed with math as well?

Of course, FENV_ROUND affects any math, but this patch fixes misbehavior of literals only. And the note was about different expectation.

Assignment isn't an operation that depends on rounding mode, but changing the snippet to such:
```
template<typename T, T V> void foo() {
#pragma STDC FENV_ROUND FE_DOWNWARD
T Val = V + 1;
}
```
would make use of the rounding mode. In this case the addition in `foo` would be evaluated using rounding down, but the literal is converted to AST object using rounding up.

> I'm more concerned about a case where the point-of-instantiation and the template have different rounding modes.

Does this test address your concern: https://github.com/spavloff/llvm-project/blob/aeb6074444513587924106081213335f73ba6eb0/clang/test/AST/ast-dump-fpfeatures.cpp#L124-L143 ?

> One thing to ensure: can we have an AST test (like your 'foo' example) that shows that they are different instantiations? Are there any cases where we could get two instantiation-requests spelled differently, but because of different rounding modes, are the same instantiation?

All instantiations of a function template use the rounding mode specified in the template definition. Otherwise we would have bad things like two instantiations in different translation units that use different rounding modes. The rounding mode is attached to AST nodes in the AST representation for function templates, tests in `AST/ast-dump-fpfeatures.cpp` check this property.

https://github.com/llvm/llvm-project/pull/90877


More information about the cfe-commits mailing list