Skip to content

Implement INumberBase and conversions for the IEEE 754 decimal types - #130807

Merged
tannergooding merged 7 commits into
dotnet:mainfrom
tannergooding:tannergooding-decimal-ieee754-operators
Jul 16, 2026
Merged

tannergooding merged 7 commits into
dotnet:mainfrom
tannergooding:tannergooding-decimal-ieee754-operators

Conversation

@tannergooding

Copy link
Copy Markdown
Member

Continues the incremental buildout of the IEEE 754 decimal types (part of #81376). Prior PRs added the layout, parsing/formatting, arithmetic/comparison operators, and the simple statics. This one adds the conversion surface and the first interface implementation.

Adds to Decimal32, Decimal64, and Decimal128:

  • The full explicit/implicit conversion matrix -- to and from every built-in numeric type, System.Decimal, and the other two decimal types.
  • INumberBase<TSelf>, including CreateChecked/CreateSaturating/CreateTruncating and the TryConvertFrom*/TryConvertTo* dispatch.
  • ISpanFormattable, IUtf8SpanFormattable, and IUtf8SpanParsable<TSelf>.

Each type owns its own conversion dispatch because System.Decimal and the built-in numeric types predate the IEEE 754 decimal types and therefore never convert to them. This mirrors the System.Half design.


Non-trivial conversions are validated against Intel's BID reference vectors. Create*/TryConvert* add focused round-trip, checked-overflow, and saturation coverage -- including the near-decimal boundary where the nearest Decimal64 rounds just outside System.Decimal's range, which the saturating/truncating path handles by range-checking in exact Decimal128 space.

The larger remaining interfaces (INumber, ISignedNumber, IFloatingPoint, IFloatingPointIeee754/IDecimalFloatingPointIeee754, and the IBinaryInteger-style bit APIs) are deferred to follow-up PRs.

Note

This PR description was drafted by GitHub Copilot.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@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 the IEEE 754 decimal floating-point types (Decimal32, Decimal64, Decimal128) with conversion support (including generic-math conversion dispatch), adds UTF-8 parse/format interface support, and extends the Intel BID reference-vector test infrastructure to validate the new conversion behavior.

Changes:

  • Add conversion operator matrices and INumberBase<TSelf> conversion dispatch (Create*, TryConvertFrom*, TryConvertTo*) for the IEEE 754 decimal types.
  • Add UTF-8 parsing/formatting interface implementations and shared formatting helpers.
  • Extend Intel readtest.in-driven test data and add new theory coverage for integer/binary-float/cross-decimal conversions.
Show a summary per file
File Description
src/libraries/System.Private.CoreLib/src/System/Numerics/Decimal32.cs Adds interface implementations and conversion surfaces for Decimal32.
src/libraries/System.Private.CoreLib/src/System/Numerics/Decimal64.cs Adds interface implementations and conversion surfaces for Decimal64.
src/libraries/System.Private.CoreLib/src/System/Numerics/Decimal128.cs Adds interface implementations and conversion surfaces for Decimal128.
src/libraries/System.Private.CoreLib/src/System/Number.DecimalIeee754.cs Implements shared conversion logic used by the decimal IEEE 754 types.
src/libraries/System.Private.CoreLib/src/System/Number.Formatting.cs Adds/extends shared formatting helpers for the decimal IEEE 754 types.
src/libraries/System.Runtime/ref/System.Runtime.cs Updates public ref surface for new interfaces/members/operators.
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/DecimalIeee754IntelTestData.cs Extends Intel vector parsing/enumeration for conversion-related operations.
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Decimal32Tests.cs Adds Intel-vector and focused behavioral tests for Decimal32 conversions.
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Decimal64Tests.cs Adds Intel-vector and focused behavioral tests for Decimal64 conversions.
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Decimal128Tests.cs Adds Intel-vector and focused behavioral tests for Decimal128 conversions.

Copilot's findings

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

Copilot AI review requested due to automatic review settings July 15, 2026 18:42

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: 10/10 changed files
  • Comments generated: 5

Copilot AI review requested due to automatic review settings July 15, 2026 19:01

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: 10/10 changed files
  • Comments generated: 3

Copilot AI review requested due to automatic review settings July 15, 2026 19:40

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: 10/10 changed files
  • Comments generated: 2

Copilot AI review requested due to automatic review settings July 15, 2026 20:00

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: 10/10 changed files
  • Comments generated: 2

tannergooding and others added 5 commits July 15, 2026 16:55
Add the full explicit/implicit conversion matrix (to and from every
built-in numeric type, `System.Decimal`, and the other decimal types),
`INumberBase<TSelf>`, `ISpanFormattable`, `IUtf8SpanFormattable`, and
`IUtf8SpanParsable<TSelf>` to `Decimal32`, `Decimal64`, and `Decimal128`.

Each type owns its own conversion dispatch because `System.Decimal` and
the built-in numeric types predate the IEEE 754 decimal types and never
convert to them.

Co-authored-by: Copilot App <[email protected]>
Cover the conversion operators with Intel BID reference vectors and add
focused `Create*`/`TryConvert*` round-trip, checked-overflow, and
saturation tests, including the near-`decimal` boundary where the nearest
`Decimal64` rounds just outside `System.Decimal`'s range.

Co-authored-by: Copilot App <[email protected]>
…eration

Extract the single underscore-delimited token via spans instead of allocating a
string array plus per-token substrings for every readtest.in row.

Co-authored-by: Copilot App <[email protected]>
The byte-based Parse(ReadOnlySpan<byte>, NumberStyles, IFormatProvider?) overload
carries an extra NumberStyles parameter, so it inherits from the INumberBase member
rather than IUtf8SpanParsable, matching Half. Also drop an accidental duplicate of
the decimal-saturation comment on Decimal64.

Co-authored-by: Copilot App <[email protected]>
A negative value in (-1, 0) truncates toward zero into range, so a checked
conversion to an unsigned target returns zero rather than throwing, matching the
binary floating-point to integer conversions the decimal path mirrors.

Co-authored-by: Copilot App <[email protected]>
Copilot AI review requested due to automatic review settings July 15, 2026 23:58
@tannergooding
tannergooding force-pushed the tannergooding-decimal-ieee754-operators branch from 1c92dd6 to 7d476eb Compare July 15, 2026 23:58

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: 10/10 changed files
  • Comments generated: 1

Copilot AI review requested due to automatic review settings July 16, 2026 00:11

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: 10/10 changed files
  • Comments generated: 1

@tannergooding
tannergooding marked this pull request as ready for review July 16, 2026 00:21
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

tannergooding and others added 2 commits July 15, 2026 17:28
These are lossy on 64-bit platforms where nint/nuint carry up to ~20
significant digits, exceeding Decimal64's 16-digit precision, so the
approved API shape provides them as explicit rather than implicit.

Co-authored-by: Copilot App <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Copilot AI review requested due to automatic review settings July 16, 2026 00:36
@tannergooding
tannergooding force-pushed the tannergooding-decimal-ieee754-operators branch from c5e490b to 6205959 Compare July 16, 2026 00:36

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: 10/10 changed files
  • Comments generated: 1

@tannergooding
tannergooding enabled auto-merge (squash) July 16, 2026 07:31
@tannergooding
tannergooding merged commit d7281d9 into dotnet:main Jul 16, 2026
145 checks passed
@tannergooding
tannergooding deleted the tannergooding-decimal-ieee754-operators branch July 16, 2026 17:10
tannergooding added a commit that referenced this pull request Jul 17, 2026
…EEE 754 decimal types (#130890)

Continues the incremental buildout of the IEEE 754 decimal types (part
of #81376). #130807 added the conversion surface and
`INumberBase<TSelf>`; this one adds the next set of interfaces and the
modulus operator.

Adds to `Decimal32`, `Decimal64`, and `Decimal128`:
* `IFloatingPointConstants<TSelf>` -- `E`, `Pi`, and `Tau`, correctly
rounded to each format's precision.
* `ISignedNumber<TSelf>` -- `NegativeOne`.
* `INumber<TSelf>` --
`Max`/`Min`/`MaxNumber`/`MinNumber`/`Clamp`/`Sign`/`CopySign` and
friends, mirroring `System.Half`'s semantics (IEEE 2019
`minimum`/`maximum` cohort/signed-zero/NaN handling).
* `IFloatingPoint<TSelf>` -- `Round` (all overloads), the
`GetExponentByteCount`/`GetSignificandByteCount`/`TryWrite*` bit APIs,
and the remaining classification members.
* The modulus operator (`%`, `IModulusOperators<TSelf, TSelf, TSelf>`).

The `%` operator follows the same truncated-remainder (`fmod`) semantics
as `float`/`double`/`Half` -- `x - Truncate(x / y) * y` -- which is
distinct from `IEEE754Remainder` (round-to-nearest quotient); the latter
arrives with `IFloatingPointIeee754`.

----------

Non-trivial members are ported from the Intel BID reference and
validated bit-exact against its reference vectors (modulus and round),
plus the existing Python-oracle-generated coverage. The multi-argument
`Round(value, digits, mode)` overloads are the one bespoke piece --
Intel has no direct baseline for them, so they are built on the faithful
single-argument rounding primitive.

`IFloatingPointIeee754<TSelf>`/`IDecimalFloatingPointIeee754<TSelf>`
(and the transcendental sub-interfaces) are deferred to a follow-up that
stacks on this.

> [!NOTE]
> This PR description was drafted by GitHub Copilot.

---------

Co-authored-by: Copilot App <[email protected]>
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jul 17, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 17, 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