Skip to content

transport: canonicalize NaN floats on encode#1263

Open
cargopete wants to merge 1 commit into
bytecodealliance:mainfrom
cargopete:feat/canonicalize-nan
Open

transport: canonicalize NaN floats on encode#1263
cargopete wants to merge 1 commit into
bytecodealliance:mainfrom
cargopete:feat/canonicalize-nan

Conversation

@cargopete
Copy link
Copy Markdown
Contributor

Closes #20.

The Component Model canonical ABI mandates a single canonical NaN representation for floating-point values (canonicalize_nan{32,64}). Previously f32/f64 were encoded with a raw byte copy (wasm-tokio's F32Codec/F64Codec), so a non-canonical NaN (signalling, or a payload-carrying NaN) would be transmitted verbatim rather than canonicalized.

This wraps the float codecs so that any NaN is canonicalized on encode:

  • f320x7fc0_0000
  • f640x7ff8_0000_0000_0000

Decoding is left lenient and accepts any NaN representation (as suggested in the issue), so the decode path simply delegates to the existing wasm-tokio codec.

Implementation

A small impl_canonical_nan_codec! macro generates a wrapper codec (CanonicalNanF32Codec / CanonicalNanF64Codec) that canonicalizes in its Encoder and delegates decoding to the inner wasm-tokio codec. The existing impl_copy_codec! registrations for f32/f64 are switched to the wrappers, so every encode path (scalar and list) funnels through the canonicalization.

Tests

Added canonical_nan_f32 / canonical_nan_f64 exercising the real Encode path:

  • a signalling/non-canonical NaN encodes to the canonical bit pattern
  • a negative NaN canonicalizes to the positive canonical NaN
  • non-NaN values encode unchanged

@cargopete cargopete requested a review from rvolosatovs as a code owner May 30, 2026 20:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

canonicalize NaNs

1 participant