Repository navigation
Ensure float to BFloat16 conversion keeps NaN as NaN - #130583
Merged
tarekgh merged 2 commits intoJul 13, 2026
Merged
Conversation
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]>
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-numerics |
Contributor
There was a problem hiding this comment.
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]>
tarekgh
approved these changes
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
float->BFloat16narrowing 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 viabits >> 16. ABFloat16significand is only 7 bits (the top 7 of thefloatsignificand), so for any NaN whose set payload bits live entirely in the low 16 bits -- i.e. a signaling NaN like0x7F80_0001-- the top 7 significand bits are zero and the result is0x7F80=+Infinity.This only affected signaling NaNs; quiet NaNs already have the significand MSB (
0x0040_0000) set, which maps directly toBFloat16'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 existingdouble->BFloat16path in the same file (whose comment notes it is "just setting the MSB of fraction") andHalf'sexplicit 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_FromSingletheory inBFloat16Tests.cs(bit-strict, already gated for the WASM/Mono NaN-canonicalization quirk):0x7F8000010x7FC0(payload only in low 16 bits)0xFF8000010xFFC0(negative variant)0x7FA000000x7FE0(surviving upper payload bit + quiet bit)Verified on Windows x64: with the fix the full
BFloat16Testsclass passes (1542/1542); reverting the fix reproduces exactly these 3 failures (one showingExpected: NaN, Actual: -Infinity).Note
This PR description was drafted by GitHub Copilot on @tannergooding's behalf.