Add integer to Half and BFloat16 conversion rounding tests#130575
Open
tannergooding wants to merge 1 commit into
Open
Add integer to Half and BFloat16 conversion rounding tests#130575tannergooding wants to merge 1 commit into
tannergooding wants to merge 1 commit into
Conversation
Adds explicit conversion tests covering correctly-rounded integer to Half (Int32/UInt32/Int64/UInt64) and integer to BFloat16 (Int64/UInt64/Int128/UInt128) conversions. The BFloat16 cases exercise values that are inexact in float, proving the conversions round directly rather than double-rounding through float. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-numerics |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR expands System.Runtime numeric tests to cover explicit integer-to-Half and integer-to-BFloat16 conversions, with expected results specified by exact bit patterns to validate correct rounding behavior (including cases that would fail if an implementation ever double-rounded via float).
Changes:
- Add
Halfexplicit conversion test vectors forInt32/UInt32/Int64/UInt64, including round-to-even boundaries and finite/overflow boundaries. - Add
BFloat16explicit conversion test vectors forInt64/UInt64/Int128/UInt128, including mid-range double-rounding discriminators and high-magnitude round-to-even boundaries.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Numerics/BFloat16Tests.cs | Adds explicit integer conversion rounding tests (including double-rounding discriminator cases) for 64-bit and 128-bit integers. |
| src/libraries/System.Runtime/tests/System.Runtime.Tests/System/HalfTests.cs | Adds explicit integer conversion rounding/overflow tests for 32-bit and 64-bit signed/unsigned integers. |
Copilot's findings
- Files reviewed: 2/2 changed files
- Comments generated: 0
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Adds test coverage proving that integer->
Halfand integer->BFloat16conversions round correctly and do not double-round throughfloat. This is part of #112474.HalfTestspreviously had no integer-conversion tests at all, andBFloat16Testsonly coveredInt32/UInt32. The integer conversions are already correct in the current tree (BFloat16uses directRoundFromSigned/RoundFromUnsigned, andHalfsaturates below2^17so no integer can observably double-round); these tests guard that behavior against regressions.What's covered
Half:Int32/UInt32/Int64/UInt64, exercising the 11-bit round-to-even boundary (2048/2049/2050/2051, 4097/4098/4100), the finite/overflow boundary (65504/65519/65520 -> inf), and large 64-bit magnitudes -> inf.BFloat16:Int64/UInt64/Int128/UInt128, exercising the mid-range double-rounding boundary (0x101_0000/0x101_0001/0x100_FFFF- the values that are inexact infloat, so a two-step conversion would round the wrong way) plus high-magnitude round-to-even boundaries near2^62,2^63,2^100, and2^127, andMaxValue/MinValue.Validation
Every expected bit pattern was cross-checked against the single-step correctly-rounded
Half.Parse/BFloat16.Parseoracle (integer decimal strings are exact). The discriminating case was confirmed to differ from a two-step result:(BFloat16)0x101_0001is0x4B81(correct, direct) whereas(BFloat16)(float)0x101_0001is0x4B80(double-rounded) - so these tests fail if the conversion is ever routed throughfloat.Test-only change. Both classes pass (1605
Half, 1571BFloat16, 0 failures).Note
This PR description was generated by GitHub Copilot.