You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: clang/docs/UsersManual.rst
+39-1
Original file line number
Diff line number
Diff line change
@@ -1386,7 +1386,7 @@ Note that floating-point operations performed as part of constant initialization
1386
1386
Details:
1387
1387
1388
1388
* ``precise`` Disables optimizations that are not value-safe on floating-point data, although FP contraction (FMA) is enabled (``-ffp-contract=fast``). This is the default behavior.
1389
-
* ``strict`` Enables ``-frounding-math`` and ``-ffp-exception-behavior=strict``, and disables contractions (FMA). All of the ``-ffast-math`` enablements are disabled.
1389
+
* ``strict`` Enables ``-frounding-math`` and ``-ffp-exception-behavior=strict``, and disables contractions (FMA). All of the ``-ffast-math`` enablements are disabled. Enables ``STDC FENV_ACCESS``: by default ``FENV_ACCESS`` is disabled. This option setting behaves as though ``#pragma STDC FENV_ACESS ON`` appeared at the top of the source file.
1390
1390
* ``fast`` Behaves identically to specifying both ``-ffast-math`` and ``ffp-contract=fast``
1391
1391
1392
1392
Note: If your command line specifies multiple instances
@@ -1408,6 +1408,44 @@ Note that floating-point operations performed as part of constant initialization
1408
1408
* ``strict`` The compiler ensures that all transformations strictly preserve the floating point exception semantics of the original code.
1409
1409
1410
1410
1411
+
.. _fp-constant-eval:
1412
+
1413
+
A note about Floating Point Constant Evaluation
1414
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1415
+
1416
+
In C, the only place floating point operations are guaranteed to be evaluated
1417
+
during translation is in the initializers of variables of static storage
1418
+
duration, which are all notionally initialized before the program begins
1419
+
executing (and thus before a non-default floating point environment can be
1420
+
entered). But C++ has many more contexts where floating point constant
1421
+
evaluation occurs. Specifically: for static/thread-local variables,
1422
+
first try evaluating the initializer in a constant context, including in the
1423
+
constant floating point environment (just like in C), and then, if that fails,
1424
+
fall back to emitting runtime code to perform the initialization (which might
1425
+
in general be in a different floating point environment).
1426
+
1427
+
Consider this example when compiled with ``-frounding-math``
1428
+
1429
+
.. code-block:: console
1430
+
1431
+
constexpr float func_01(float x, float y) {
1432
+
return x + y;
1433
+
}
1434
+
float V1 = func_01(1.0F, 0x0.000001p0F);
1435
+
1436
+
The C++ rule is that initializers for static storage duration variables are
1437
+
first evaluated during translation (therefore, in the default rounding mode),
1438
+
and only evaluated at runtime (and therefore in the runtime rounding mode) if
1439
+
the compile-time evaluation fails. This is in line with the C rules;
1440
+
C11 F.8.5 says: *All computation for automatic initialization is done (as if)
1441
+
at execution time; thus, it is affected by any operative modes and raises
1442
+
floating-point exceptions as required by IEC 60559 (provided the state for the
1443
+
FENV_ACCESS pragma is ‘‘on’’). All computation for initialization of objects
1444
+
that have static or thread storage duration is done (as if) at translation
1445
+
time.* C++ generalizes this by adding another phase of initialization
1446
+
(at runtime) if the translation-time initialization fails, but the
1447
+
translation-time evaluation of the initializer of succeeds, it will be
0 commit comments