Repository navigation
Evaluate both operands of floating-point <=> exactly once. [GH #520] #519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nv-cmeerw
merged 1 commit into
edgcpp:main
from
jonatanlinden:fix/spaceship-nan-eval-order
Nov 13, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| //allowed_words: NaN | ||
| [GH #520] | ||
| Lowering of <=> and the evaluation of its operands | ||
|
|
||
| For floating-point operands, the lowering of <=> for the C-generating back | ||
| end tested the right operand for a NaN before evaluating the left operand, | ||
| using a short-circuit "||". When the right operand was a NaN, the left | ||
| operand was therefore not evaluated, and its side effects were lost. Also, | ||
| an operand without side effects could be read by the NaN test before the | ||
| other operand was evaluated, and by the comparisons after it. If the other | ||
| operand changed it, the result could then be "equivalent" for a NaN. For | ||
| example, with --c++20: | ||
|
|
||
| #include <compare> | ||
|
|
||
| float x(), y(); | ||
| auto f() { return x() <=> y(); } // Previously x() was not called when | ||
| // y() returned a NaN. | ||
|
|
||
| float g, h(); // h() modifies g. | ||
| auto k() { return h() <=> g; } // Previously could be "equivalent" | ||
| // when h() stores a NaN into g. | ||
|
|
||
| Each operand is now evaluated exactly once, and the NaN test and the | ||
| comparisons use the same operand values. | ||
1 change: 1 addition & 0 deletions
1
tests/tests/regressions/.spaceship-nan-eval-order.rto/default.1.1.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| eccp -DTEST_NUMBER=1 --c++20 --g++ Test_name.c |
1 change: 1 addition & 0 deletions
1
tests/tests/regressions/.spaceship-nan-eval-order.rto/default.1.2.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ./a.out |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| //type:rp | ||
| //options_all:--c++20 --g++ | ||
|
|
||
| // Lowering of <=> must evaluate both operands exactly once, and must compare | ||
| // the same operand values in the NaN test and in the ordering tests. | ||
|
|
||
| #include <compare> | ||
|
|
||
| float nan_value() { return __builtin_nanf(""); } | ||
|
|
||
| int calls; | ||
| float one() { ++calls; return 1.0f; } | ||
|
|
||
| float g; | ||
| float set_g_to_nan() { g = nan_value(); return 0.0f; } | ||
|
|
||
| int main() { | ||
| int failures = 0; | ||
|
|
||
| // The left operand is evaluated when the right operand is a NaN. | ||
| calls = 0; | ||
| if ((one() <=> nan_value()) != std::partial_ordering::unordered) { | ||
| failures |= 1; | ||
| } // if | ||
| if (calls != 1) failures |= 2; | ||
|
|
||
| // The right operand is evaluated when the left operand is a NaN. | ||
| calls = 0; | ||
| if ((nan_value() <=> one()) != std::partial_ordering::unordered) { | ||
| failures |= 4; | ||
| } // if | ||
| if (calls != 1) failures |= 8; | ||
|
|
||
| // g is read either before set_g_to_nan() (less) or after it (unordered), | ||
| // never both. | ||
| g = 1.0f; | ||
| std::partial_ordering r = set_g_to_nan() <=> g; | ||
| if (r != std::partial_ordering::less && | ||
| r != std::partial_ordering::unordered) { | ||
| failures |= 16; | ||
| } // if | ||
| g = 1.0f; | ||
| r = g <=> set_g_to_nan(); | ||
| if (r != std::partial_ordering::greater && | ||
| r != std::partial_ordering::unordered) { | ||
| failures |= 32; | ||
| } // if | ||
|
|
||
| // Finite operands give less, equivalent, and greater, both with operands | ||
| // that are copied to temporaries (one()) and with one that is not (g). | ||
| calls = 0; | ||
| g = 2.0f; | ||
| if ((one() <=> g) != std::partial_ordering::less) failures |= 64; | ||
| if ((one() <=> one()) != std::partial_ordering::equivalent) failures |= 64; | ||
| if ((g <=> one()) != std::partial_ordering::greater) failures |= 64; | ||
| if (calls != 4) failures |= 128; | ||
|
|
||
| return failures; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.