Repository navigation
Preserve signed zeros in VQSort's two-value partition - #3543
Merged
copybara-service[bot] merged 2 commits intoNov 11, 2026
Merged
copybara-service[bot] merged 2 commits into
copybara-service[bot] merged 2 commits into
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
jan-wassenberg
approved these changes
Nov 11, 2026
jan-wassenberg
left a comment
Member
There was a problem hiding this comment.
LGTM, thanks for the fix!
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
VQSort's two-value partition fills the array with copies of two representative keys. With representatives
+0.0and1.0, the fill accepts-0.0as a match for+0.0and can overwrite it. The result is still sorted, but it no longer contains the same floating-point values bit for bit. Related: numpy/numpy#33286.Both scan directions now require a bitwise match before replacing a floating-point key with a representative. If neither representative matches, the sort falls back to regular partitioning. The ordering comparison is unchanged. The pivot-selection assertion also needs to allow a numeric tie between zeros: with a pivot of
1.0, the other two keys can be+0.0and-0.0. They have distinct bit patterns, but neither sorts before the other.Validation. The regression test places one negative zero at the end of each scan direction and checks that it survives, at lengths 128 and 129. It fails before the fix and passes afterward. On NEON under QEMU, the 128-double reproducer changed the input bit patterns in 120/128 ascending runs and 77/128 descending runs; after the fix, all runs preserved them in both release and debug builds. Direct partition checks also passed on native AVX2, SVE2 (128-bit) and SVE (256-bit) under QEMU, and AVX-512 under Intel SDE.
AVX2 performance. On a Ryzen 7 5800H with GCC 13.3, the isolated float32 reverse fill took 5.4–8.3% longer on two-value inputs across lengths 128, 4,096, and 65,536 in two rounds of paired measurements. For end-to-end sorts of random float32 and float64 inputs at the same lengths, median time ranged from 1.4% faster to 1.1% slower. These are measurements on one AVX2 machine; ARM performance was not measured.
AVX-512 limitation. On the Skylake-X target under Intel SDE, the 128-double input fits entirely in the short-array sorting network. Calling
BaseCasedirectly produced the same output as the public sort: of 40 input negative zeros, 2 remained in ascending order and 20 in descending order, both before and after this patch. That path does not call the two-value partition and remains unfixed.Codex assisted with writing and testing the patch and drafting this PR description.