Skip to content

Commit ad615db

Browse files
committed
specify NaN behavior more precisely
1 parent 5e4e2a5 commit ad615db

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

llvm/docs/LangRef.rst

+30-6
Original file line numberDiff line numberDiff line change
@@ -3394,17 +3394,41 @@ Floating-Point Environment
33943394
The default LLVM floating-point environment assumes that traps are disabled and
33953395
status flags are not observable. Therefore, floating-point math operations do
33963396
not have side effects and may be speculated freely. Results assume the
3397-
round-to-nearest rounding mode.
3397+
round-to-nearest rounding mode, and subnormals are assumed to be preserved.
3398+
Running default LLVM code in an environment where these assumptions are not met
3399+
can lead to undefined behavior.
3400+
3401+
The representation bits of a floating-point value do not mutate arbitrarily; if
3402+
there is no floating-point operation being performed, the NaN payload (if any)
3403+
is preserved.
3404+
3405+
When a floating-point math operation produces a NaN value, the result has a
3406+
non-deterministic sign. The payload is non-deterministically chosen from the
3407+
following set:
3408+
3409+
- The payload that is all-zero except that the ``quiet`` bit is set.
3410+
("Preferred NaN" case)
3411+
- The payload of any input operand that is a NaN, bit-wise ORed with a payload that has
3412+
the ``quiet`` bit set. ("Quieting NaN propagation" case)
3413+
- The payload of any input operand that is a NaN. ("Unchanged NaN propagation" case)
3414+
- A target-specific set of further NaN payloads, that definitely all have their
3415+
``quiet`` bit set. The set can depend on the payloads of the input NaNs.
3416+
This set is empty on x86 and ARM, but can be non-empty on other architectures.
3417+
(For instance, on wasm, if any input NaN is not the preferred NaN, then
3418+
this set contains all quiet NaNs; otherwise, it is empty.
3419+
On SPARC, this set consists of the all-one payload.)
3420+
3421+
In particular, if all input NaNs are quiet, then the output NaN is definitely
3422+
quiet. Signaling NaN outputs can only occur if they are provided as an input
3423+
value. For example, "fmul SNaN, 1.0" may be simplified to SNaN rather than QNaN.
33983424

33993425
Floating-point math operations are allowed to treat all NaNs as if they were
3400-
quiet NaNs. For example, "pow(1.0, SNaN)" may be simplified to 1.0. This also
3401-
means that SNaN may be passed through a math operation without quieting. For
3402-
example, "fmul SNaN, 1.0" may be simplified to SNaN rather than QNaN. However,
3403-
SNaN values are never created by math operations. They may only occur when
3404-
provided as a program input value.
3426+
quiet NaNs. For example, "pow(1.0, SNaN)" may be simplified to 1.0.
34053427

34063428
Code that requires different behavior than this should use the
34073429
:ref:`Constrained Floating-Point Intrinsics <constrainedfp>`.
3430+
In particular, constrained intrinsics rule out the "Unchanged NaN propagation" case;
3431+
they are guaranteed to return a QNaN.
34083432

34093433
.. _fastmath:
34103434

0 commit comments

Comments
 (0)