Skip to content

Accept canonical NaN in BFloat16 explicit conversions on WASM - #132307

Merged
pavelsavara merged 1 commit into
dotnet:mainfrom
pavelsavara:ExplicitConversion_FromSingle
Aug 14, 2026
Merged

Accept canonical NaN in BFloat16 explicit conversions on WASM#132307
pavelsavara merged 1 commit into
dotnet:mainfrom
pavelsavara:ExplicitConversion_FromSingle

Conversation

@pavelsavara

Copy link
Copy Markdown
Member

Applies the same fix as #130706 (which handled HalfTests.ExplicitConversion_FromSingle) to the two BFloat16 explicit conversion tests: ExplicitConversion_FromSingle and ExplicitConversion_FromDouble.

The software float/double -> BFloat16 conversions route the value through abs/min/max/add (f32.* / f64.* on WASM). The WebAssembly spec permits (but doesn't require) host engines to canonicalize NaN payloads on those ops, and the V8 engine used on the CI Helix queues does, so the bit-strict NaN cases don't round-trip there. Arch-native targets (and payload-preserving WASM engines) are unaffected.

Previously the NaN cases were skipped entirely via if (PlatformDetection.IsMonoRuntime && PlatformDetection.IsWasm && ...) continue;. That gate only fires on Mono, so once the browser CoreCLR work (#129634) started running these tests on CoreCLR+WASM they began failing on V8 with Assert.Equal() ... Expected: NaN / Actual: NaN (payloads differ). Dropping all NaN coverage on WASM also loses the sign-bit check, which is preserved (carried through the integer ALU).

Instead, this removes the skip and, on WASM only, accepts the canonical result (sign | 0x7FC0 for BFloat16) in place of the exact expected payload:

  • a canonicalizing run (V8 today) passes via the canonical value,
  • a payload-preserving run (the IEEE 754 / WASM recommended behavior, and what arch-native targets do) still passes via the strict exact-bits check,
  • a genuinely wrong payload (e.g. a dropped sign) still fails.

So the NaN cases now actually run on WASM for both Mono and CoreCLR, and the bit-strict AssertEqual stays in effect for all other targets.

Tracked in #103347.

Note

This PR was prepared with the assistance of GitHub Copilot.

Copilot AI lite review requested due to automatic review settings August 14, 2026 09:07
@pavelsavara pavelsavara self-assigned this Aug 14, 2026
@pavelsavara pavelsavara added the arch-wasm WebAssembly architecture label Aug 14, 2026
@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.

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

Updates BFloat16 explicit-conversion tests to be robust on WASM engines that canonicalize NaN payloads (notably V8), while still preserving bit-strict validation on platforms that retain NaN payloads.

Changes:

  • Removes the prior WASM+Mono-only skip that dropped NaN test cases from the conversion test data.
  • On WASM, allows the conversion result to match either the exact expected NaN payload or the canonical NaN bit pattern (with sign preserved) for float/doubleBFloat16 explicit conversions.

@pavelsavara
pavelsavara merged commit 495998c into dotnet:main Aug 14, 2026
88 checks passed
@pavelsavara
pavelsavara deleted the ExplicitConversion_FromSingle branch August 14, 2026 13:33
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Aug 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch-wasm WebAssembly architecture area-System.Numerics

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants