Skip to content

Ensure float to BFloat16 conversion keeps NaN as NaN - #130583

Merged
tarekgh merged 2 commits into
dotnet:mainfrom
tannergooding:tannergooding-fix-bfloat16-nan-cast
Jul 13, 2026
Merged

tarekgh merged 2 commits into
dotnet:mainfrom
tannergooding:tannergooding-fix-bfloat16-nan-cast

Conversation

@tannergooding

Copy link
Copy Markdown
Member

The float->BFloat16 narrowing conversion could silently turn a NaN into an Infinity, violating IEEE 754 (a NaN must stay a NaN across narrowing conversions).

The NaN path in explicit operator BFloat16(float) simply truncated the top 16 bits via bits >> 16. A BFloat16 significand is only 7 bits (the top 7 of the float significand), so for any NaN whose set payload bits live entirely in the low 16 bits -- i.e. a signaling NaN like 0x7F80_0001 -- the top 7 significand bits are zero and the result is 0x7F80 = +Infinity.

This only affected signaling NaNs; quiet NaNs already have the significand MSB (0x0040_0000) set, which maps directly to BFloat16's quiet bit and survives the shift.

Fix

Force the significand's quiet bit (1 << (TrailingSignificandLength - 1) = 0x0040) on the NaN path so the result stays a NaN while preserving the sign and any surviving upper payload bits. This quiets the NaN, matching the existing double->BFloat16 path in the same file (whose comment notes it is "just setting the MSB of fraction") and Half's explicit operator Half(float). IEEE 754 permits returning a quiet NaN from a narrowing conversion.

Note that "always round" is not a valid alternative here: plain round-to-nearest-ties-to-even still rounds low-payload NaNs to Infinity, and can carry an all-ones-significand NaN out through the (already all-ones) exponent into the sign bit, producing +/-0.0.

Test

Added three cases to the existing ExplicitConversion_FromSingle theory in BFloat16Tests.cs (bit-strict, already gated for the WASM/Mono NaN-canonicalization quirk):

input expected
0x7F800001 0x7FC0 (payload only in low 16 bits)
0xFF800001 0xFFC0 (negative variant)
0x7FA00000 0x7FE0 (surviving upper payload bit + quiet bit)

Verified on Windows x64: with the fix the full BFloat16Tests class passes (1542/1542); reverting the fix reproduces exactly these 3 failures (one showing Expected: NaN, Actual: -Infinity).

Note

This PR description was drafted by GitHub Copilot on @tannergooding's behalf.

The NaN path truncated the top 16 bits of the float, which could zero out all of BFloat16's significand bits (e.g. 0x7F80_0001 -> 0x7F80 = +Infinity), silently turning a NaN into an Infinity and violating IEEE 754. Force the significand's quiet bit on the NaN path so the result stays a NaN while preserving the sign and any surviving payload, matching the double->BFloat16 and Half paths.

Co-authored-by: Copilot App <[email protected]>
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-numerics
See info in area-owners.md if you want to be subscribed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes float → BFloat16 narrowing conversion so that NaN inputs remain NaN (instead of becoming ±Infinity for certain signaling-NaN payload patterns), and extends the existing conversion test data to cover those edge cases.

Changes:

  • Update explicit operator BFloat16(float) to force the BFloat16 quiet-NaN bit when the input is NaN.
  • Add targeted test cases for signaling NaNs whose payload would otherwise be lost by truncation (leading to Infinity).
  • Ensure the tests remain compatible with existing WASM/Mono NaN-canonicalization gating in the same theory.
Show a summary per file
File Description
src/libraries/System.Private.CoreLib/src/System/Numerics/BFloat16.cs Forces the quiet bit on NaN conversions to prevent NaN→Infinity corruption during truncation.
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Numerics/BFloat16Tests.cs Adds explicit conversion test vectors for signaling NaNs with low-only payload (and one with a surviving high payload bit).

Copilot's findings

  • Files reviewed: 2/2 changed files
  • Comments generated: 1

Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Copilot AI review requested due to automatic review settings July 12, 2026 20:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 2/2 changed files
  • Comments generated: 1

@tarekgh
tarekgh merged commit e8e99e2 into dotnet:main Jul 13, 2026
145 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jul 13, 2026
eiriktsarpalis pushed a commit that referenced this pull request Jul 15, 2026
The `float`->`BFloat16` narrowing conversion could silently turn a NaN
into an Infinity, violating IEEE 754 (a NaN must stay a NaN across
narrowing conversions).

The NaN path in `explicit operator BFloat16(float)` simply truncated the
top 16 bits via `bits >> 16`. A `BFloat16` significand is only 7 bits
(the top 7 of the `float` significand), so for any NaN whose set payload
bits live entirely in the low 16 bits -- i.e. a signaling NaN like
`0x7F80_0001` -- the top 7 significand bits are zero and the result is
`0x7F80` = `+Infinity`.

This only affected signaling NaNs; quiet NaNs already have the
significand MSB (`0x0040_0000`) set, which maps directly to `BFloat16`'s
quiet bit and survives the shift.

## Fix

Force the significand's quiet bit (`1 << (TrailingSignificandLength -
1)` = `0x0040`) on the NaN path so the result stays a NaN while
preserving the sign and any surviving upper payload bits. This quiets
the NaN, matching the existing `double`->`BFloat16` path in the same
file (whose comment notes it is "just setting the MSB of fraction") and
`Half`'s `explicit operator Half(float)`. IEEE 754 permits returning a
quiet NaN from a narrowing conversion.

Note that "always round" is **not** a valid alternative here: plain
round-to-nearest-ties-to-even still rounds low-payload NaNs to Infinity,
and can carry an all-ones-significand NaN out through the (already
all-ones) exponent into the sign bit, producing `+/-0.0`.

## Test

Added three cases to the existing `ExplicitConversion_FromSingle` theory
in `BFloat16Tests.cs` (bit-strict, already gated for the WASM/Mono
NaN-canonicalization quirk):

| input | expected |
|---|---|
| `0x7F800001` | `0x7FC0` (payload only in low 16 bits) |
| `0xFF800001` | `0xFFC0` (negative variant) |
| `0x7FA00000` | `0x7FE0` (surviving upper payload bit + quiet bit) |

Verified on Windows x64: with the fix the full `BFloat16Tests` class
passes (1542/1542); reverting the fix reproduces exactly these 3
failures (one showing `Expected: NaN, Actual: -Infinity`).

> [!NOTE]
> This PR description was drafted by GitHub Copilot on @tannergooding's
behalf.

---------

Co-authored-by: Copilot App <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 12, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants