Skip to content

Add integer to Half and BFloat16 conversion rounding tests#130575

Open
tannergooding wants to merge 1 commit into
dotnet:mainfrom
tannergooding:tannergooding-correct-int-decimal-to-fp-rounding
Open

Add integer to Half and BFloat16 conversion rounding tests#130575
tannergooding wants to merge 1 commit into
dotnet:mainfrom
tannergooding:tannergooding-correct-int-decimal-to-fp-rounding

Conversation

@tannergooding

Copy link
Copy Markdown
Member

Adds test coverage proving that integer->Half and integer->BFloat16 conversions round correctly and do not double-round through float. This is part of #112474.

HalfTests previously had no integer-conversion tests at all, and BFloat16Tests only covered Int32/UInt32. The integer conversions are already correct in the current tree (BFloat16 uses direct RoundFromSigned/RoundFromUnsigned, and Half saturates below 2^17 so 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 in float, so a two-step conversion would round the wrong way) plus high-magnitude round-to-even boundaries near 2^62, 2^63, 2^100, and 2^127, and MaxValue/MinValue.

Validation

Every expected bit pattern was cross-checked against the single-step correctly-rounded Half.Parse/BFloat16.Parse oracle (integer decimal strings are exact). The discriminating case was confirmed to differ from a two-step result: (BFloat16)0x101_0001 is 0x4B81 (correct, direct) whereas (BFloat16)(float)0x101_0001 is 0x4B80 (double-rounded) - so these tests fail if the conversion is ever routed through float.

Test-only change. Both classes pass (1605 Half, 1571 BFloat16, 0 failures).

Note

This PR description was generated by GitHub Copilot.

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>
@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 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 Half explicit conversion test vectors for Int32/UInt32/Int64/UInt64, including round-to-even boundaries and finite/overflow boundaries.
  • Add BFloat16 explicit conversion test vectors for Int64/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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants