Skip to content

Align Decimal32/64/128 surface with the approved API - #131098

Merged
tannergooding merged 7 commits into
dotnet:mainfrom
tannergooding:tannergooding-stunning-adventure
Jul 21, 2026
Merged

tannergooding merged 7 commits into
dotnet:mainfrom
tannergooding:tannergooding-stunning-adventure

Conversation

@tannergooding

Copy link
Copy Markdown
Member

This aligns the Decimal32/Decimal64/Decimal128 public surface with the API approved in #81376 (comment), and fills in the members that were approved but not yet implemented.


Naming

  • Rename Quantum/SameQuantum to GetQuantum/HaveSameQuantum to match the approved shape.

Approved-but-missing members

  • Expose Lerp on all three types.
  • Expose ReciprocalEstimate and ReciprocalSqrtEstimate on all three types.
  • Add EncodeBinary/DecodeBinary (IEEE 754-2019 §5.5.2 binary integer decimal, BID) on all three types.
  • Add EncodeDecimal/DecodeDecimal (IEEE 754-2019 §5.5.2 densely packed decimal, DPD) on all three types.

The BID/DPD conversions are implemented as a shared generic codec in Number.DecimalIeee754.cs that reuses the existing significand unpack/encode infrastructure, so the per-type members only vary by width. The declet tables come directly from IEEE 754-2019 Table 3.6, and the format-field widths derive from the existing NumberBitsSignificand constant rather than introducing new per-type state.

Compound is intentionally skipped -- it isn't exposed on IFloatingPointIeee754 either, so exposing it here would be inconsistent.


Tests

Round-trip coverage for BID and DPD across ±0, ±Infinity, NaN, NaN-with-payload, the leading-digit 8/9 combination path, and representative finite values. The DPD tests additionally assert against the published canonical encodings (e.g. decimal32(1) == 0x22500001, decimal64(1) == 0x2238000000000001, decimal128(1) == 0x22080000000000000000000000000001) to lock the bit orientation to the standard.

Full System.Runtime.Tests run passes (0 failed).

Note

This PR description and portions of the change were drafted with GitHub Copilot.

tannergooding and others added 4 commits July 20, 2026 09:43
The IEEE 754 spec operations lack a verb, so the public API surface uses
GetQuantum and HaveSameQuantum per the API review naming guidance.

Co-authored-by: Copilot App <[email protected]>
Surfaces the IFloatingPointIeee754 Lerp method as a public static on each
decimal type, matching the approved API surface and the binary IEEE types.

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

Surfaces the IFloatingPointIeee754 reciprocal estimate methods as public
statics on each decimal type, matching the approved API surface. The decimal
formats have no hardware estimate, so these compute the exact reciprocal.

Co-authored-by: Copilot App <[email protected]>
Implements the IEEE 754-2019 5.5.2 binary encoding conversions. The decimal
types already store the BID interchange representation, so these round-trip
the raw bit pattern. The decimal (DPD) encoding is deferred.

Co-authored-by: Copilot App <[email protected]>
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 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.

@tannergooding
tannergooding force-pushed the tannergooding-stunning-adventure branch from 08fa251 to aea6b83 Compare July 20, 2026 18:35

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 updates the System.Numerics.Decimal32 / Decimal64 / Decimal128 public API surface to match the approved shape, and fills in previously-approved-but-missing members (notably BID/DPD encode/decode helpers and several IEEE 754-2019 operations).

Changes:

  • Renames Quantum/SameQuantum to GetQuantum/HaveSameQuantum across interface, implementations, ref surface, and tests.
  • Adds public BID/DPD conversion APIs (EncodeBinary/DecodeBinary, EncodeDecimal/DecodeDecimal) for all three decimal IEEE types, backed by shared generic codec logic in Number.DecimalIeee754.cs.
  • Adds Lerp, ReciprocalEstimate, and ReciprocalSqrtEstimate for all three types, plus corresponding test coverage.
Show a summary per file
File Description
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/DecimalIeee754GenericSurface.cs Extends generic-surface verification to include Lerp/reciprocal and the renamed quantum APIs.
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Decimal64Tests.cs Updates quantum naming; adds BID/DPD encode/decode tests and known vectors for Decimal64.
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Decimal32Tests.cs Updates quantum naming; adds BID/DPD encode/decode tests and known vectors for Decimal32 (includes a leading-digit-9 case).
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Decimal128Tests.cs Updates quantum naming; adds BID/DPD encode/decode tests and known vectors for Decimal128.
src/libraries/System.Runtime/ref/System.Runtime.cs Updates reference surface for renamed quantum APIs; adds Lerp/reciprocal and BID/DPD encode/decode members.
src/libraries/System.Private.CoreLib/src/System/Numerics/IDecimalFloatingPointIeee754.cs Updates the interface to the approved GetQuantum / HaveSameQuantum naming.
src/libraries/System.Private.CoreLib/src/System/Numerics/Decimal64.cs Implements new public members: BID/DPD encode/decode, Lerp, reciprocal estimates, and renamed quantum APIs.
src/libraries/System.Private.CoreLib/src/System/Numerics/Decimal32.cs Implements new public members: BID/DPD encode/decode, Lerp, reciprocal estimates, and renamed quantum APIs.
src/libraries/System.Private.CoreLib/src/System/Numerics/Decimal128.cs Implements new public members: BID/DPD encode/decode, Lerp, reciprocal estimates, and renamed quantum APIs.
src/libraries/System.Private.CoreLib/src/System/Number.DecimalIeee754.cs Adds shared generic BID⇄DPD conversion implementation (declet packing/unpacking and combination-field handling).

Copilot's findings

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

Copilot AI review requested due to automatic review settings July 20, 2026 18:37

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

Comments suppressed due to low confidence (2)

src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Decimal64Tests.cs:3010

  • The new BID<->DPD codec has a distinct combination-field branch for leading digits 8/9 (see Number.EncodeDecimalIeee754 / DecodeDecimalIeee754). The Decimal64 tests added here only exercise msd<=7 inputs (+0/+1/123.45/NaN/etc), so the msd>=8 branch remains untested for Decimal64.
        [Theory]
        [InlineData(0x31C0000000000000UL)] // +0
        [InlineData(0xB1C0000000000000UL)] // -0
        [InlineData(0x31C0000000000001UL)] // +1
        [InlineData(0x3180000000003039UL)] // 123.45

src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Decimal128Tests.cs:3096

  • The new BID<->DPD codec has a distinct combination-field branch for leading digits 8/9. The added Decimal128 Encode/DecodeDecimal tests cover specials and small finite values, but don't currently include an msd>=8 finite value, so that branch isn't exercised for Decimal128.
        [Theory]
        [InlineData(0x3040000000000000UL, 0x0000000000000000UL)] // +0
        [InlineData(0xB040000000000000UL, 0x0000000000000000UL)] // -0
        [InlineData(0x3040000000000000UL, 0x0000000000000001UL)] // +1
        [InlineData(0x3041ED09BEAD87C0UL, 0x378D8E63FFFFFFFFUL)] // 34 nines (leading digit 9)
        [InlineData(0x7800000000000000UL, 0x0000000000000000UL)] // +Infinity
        [InlineData(0xF800000000000000UL, 0x0000000000000000UL)] // -Infinity
        [InlineData(0x7C00000000000000UL, 0x0000000000000000UL)] // NaN
  • Files reviewed: 10/10 changed files
  • Comments generated: 0 new

Copilot AI review requested due to automatic review settings July 20, 2026 18:47

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: 0 new

@github-actions

github-actions Bot commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow state for the Holistic Review Orchestrator.

{
  "version": 5,
  "last_dispatched_commit": "feb01377cdf3c03521b6e22c716799dac47bb05d",
  "last_dispatched_base_ref": "main",
  "last_dispatched_base_sha": "c39c3f371a1542dc1442a244bba117fb4e762299",
  "last_reviewed_commit": "feb01377cdf3c03521b6e22c716799dac47bb05d",
  "last_reviewed_base_ref": "main",
  "last_reviewed_base_sha": "c39c3f371a1542dc1442a244bba117fb4e762299",
  "last_recorded_worker_run_id": "29770130764",
  "review_attempt_commit": "",
  "review_attempt_base_ref": "",
  "review_attempt_count": 0,
  "max_review_attempts": 5,
  "review_history_format": "holistic-review-disclosure-v1",
  "review_history": [
    {
      "commit": "feb01377cdf3c03521b6e22c716799dac47bb05d",
      "review_id": 4738173834
    }
  ]
}

@github-actions github-actions Bot 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.

Holistic Review

Motivation: Justified. This aligns the Decimal32/Decimal64/Decimal128 surface with the API approved in #81376 (label api-approved), renaming Quantum/SameQuantum to GetQuantum/HaveSameQuantum and filling in approved-but-missing members (Lerp, ReciprocalEstimate, ReciprocalSqrtEstimate, EncodeBinary/DecodeBinary, EncodeDecimal/DecodeDecimal).

Approach: Sound. The BID/DPD codec is factored into shared generic helpers in Number.DecimalIeee754.cs keyed off the existing NumberBitsSignificand, so per-type members only vary by width. Lerp/ReciprocalEstimate/ReciprocalSqrtEstimate reuse existing primitives and match the binary IEEE types' definitions (e.g. Lerp == MultiplyAddEstimate(value1, One - amount, value2 * amount), identical to Double.Lerp). Compound is reasonably deferred, consistent with IFloatingPointIeee754.

Summary: ⚠️ Needs Human Review. I verified the public surface against the approved API shape and it matches exactly (names, signatures, parameter names, [CLSCompliant(false)] on the unsigned-integer overloads, and the interface rename). The renames are also propagated consistently to all callers and tests. My only reservation is test depth for the new DPD codec (details below) — a human familiar with the encoding should confirm the canonical-vector coverage is sufficient. No blocking correctness issues found.


Detailed Findings

✅ API approval — Matches the approved proposal

Issue #81376 carries the api-approved label, and the approval comment (#81376 comment 1433633909) explicitly calls for GetQuantum/HaveSameQuantum and lists Lerp, ReciprocalEstimate, ReciprocalSqrtEstimate, and the EncodeBinary/DecodeBinary/EncodeDecimal/DecodeDecimal overloads with the exact per-width return types used here. The ref/System.Runtime.cs changes and the IDecimalFloatingPointIeee754<TSelf> interface rename line up with that shape. No extra unapproved public surface was introduced.

✅ Consistency — Renames fully propagated

Quantum/SameQuantum → GetQuantum/HaveSameQuantum is applied across all three concrete types, the interface, the ref assembly, and every test call site (QuantumTest, SameQuantumTest, Quantum_IntelReferenceVectors, and the generic DecimalIeee754GenericSurface). No stale references remain.

⚠️ Test quality — DPD canonical coverage is thin

The new DPD tests are strong on round-trips (EncodeDecodeDecimalRoundTrips covers ±0, ±Infinity, NaN, NaN-with-payload, the leading-digit-8/9 path, and a representative finite value across all three widths). However, round-trip tests only prove Decode∘Encode is an involution; they cannot detect a systematic bit-orientation error in the declet packing (DigitsToDeclet/DecletToDigits) that is symmetric in encode and decode. The only tests that pin the encoding to the published canonical bit pattern (EncodeDecimalKnownVectors) cover just the values 0 and 1 per width. Consider adding a few more canonical DPD vectors from IEEE 754-2019 Table 3.6 / the standard's examples that exercise multi-digit declets and the 8/9 selector combinations (e.g. values containing digits 2–9 in several positions), so the declet tables are locked to the standard rather than only to their own inverse. This is advisory, not merge-blocking.

Note

This review was generated by this repository's Holistic Review agentic workflow to complement the built-in Copilot review.

Generated by Holistic Review · 125 AIC · ⌖ 15 AIC · ⊞ 10K

EncodeDecimal/DecodeDecimal are cross-encoding conversions per IEEE 754-2019
5.5.2 and must produce canonical results, so drop the reserved G_i bits while
preserving the sign, NaN marker, signaling bit, and payload. EncodeBinary and
DecodeBinary stay pure copies that propagate non-canonical bits.

Co-authored-by: Copilot App <[email protected]>
Copilot AI review requested due to automatic review settings July 20, 2026 20: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: 11/11 changed files
  • Comments generated: 0 new

@tannergooding
tannergooding enabled auto-merge (squash) July 20, 2026 22:31
@tannergooding
tannergooding merged commit 176718a into dotnet:main Jul 21, 2026
140 checks passed
@tannergooding
tannergooding deleted the tannergooding-stunning-adventure branch July 21, 2026 01:11
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 23, 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