Skip to content

Canonicalize NaNs when casting between float and double#8645

Open
stevenfontanella wants to merge 1 commit intomainfrom
nan-spec-test
Open

Canonicalize NaNs when casting between float and double#8645
stevenfontanella wants to merge 1 commit intomainfrom
nan-spec-test

Conversation

@stevenfontanella
Copy link
Copy Markdown
Member

@stevenfontanella stevenfontanella commented Apr 23, 2026

Fixes #8626. Also part of #8261.

The bug is due to the fact that C++ doesn't guarantee a bitwise representation for NaNs that are casted from double -> float or vice versa. From https://en.cppreference.com/cpp/language/implicit_conversion:

Floating-point promotion
A prvalue of type float can be converted to a prvalue of type double. The value does not change.

In the case of NaNs, the value (NaN) is preserved regardless of the particular NaN that is picked, so we have no guarantee of the bitwise representation. And for double -> float:

If the conversion is listed under floating-point promotions, it is a promotion and not a conversion.

  • If the source value can be represented exactly in the destination type, it does not change.
  • If the source value is between two representable values of the destination type, the result is one of those two values (it is implementation-defined which one, although if IEEE arithmetic is supported, rounding defaults to nearest).
  • Otherwise, the behavior is undefined.

I assume NaN conversions again fall under case 1 meaning that the "value" (NaN) is preserved but not the bitwise representation.

Canonicalize NaNs after promotion or demotion to ensure that the quiet bit remains set.

@stevenfontanella
Copy link
Copy Markdown
Member Author

@rathann, would you be able to try to repro your test from this branch? I'm not aware of a good way to run this architecture myself locally.

@rathann
Copy link
Copy Markdown
Contributor

rathann commented Apr 23, 2026

Thanks for the PR. I'll try to test it over the weekend.

@stevenfontanella stevenfontanella marked this pull request as ready for review April 23, 2026 22:13
@stevenfontanella stevenfontanella requested a review from a team as a code owner April 23, 2026 22:13
@stevenfontanella stevenfontanella requested review from tlively and removed request for a team April 23, 2026 22:13
Copy link
Copy Markdown
Member

@tlively tlively left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM if this fixes the problem

@stevenfontanella
Copy link
Copy Markdown
Member Author

Thanks! I'll wait for the test to confirm.

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.

4 tests from spec/testsuite/conversions.wast and spec/testsuite/simd_conversions.wast fail on ppc64le

3 participants