Skip to content

Preserve signed zeros in VQSort's two-value partition - #3543

Merged
copybara-service[bot] merged 2 commits into
google:masterfrom
LittlePyx:fix/vqsort-two-value-signed-zeros
Nov 11, 2026
Merged

copybara-service[bot] merged 2 commits into
google:masterfrom
LittlePyx:fix/vqsort-two-value-signed-zeros

Conversation

@LittlePyx

Copy link
Copy Markdown
Contributor

VQSort's two-value partition fills the array with copies of two representative keys. With representatives +0.0 and 1.0, the fill accepts -0.0 as a match for +0.0 and 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.0 and -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 BaseCase directly 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.

@google-cla

google-cla Bot commented Nov 11, 2026

Copy link
Copy Markdown

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 jan-wassenberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thanks for the fix!

@copybara-service
copybara-service Bot merged commit 4c729c7 into google:master Nov 11, 2026
52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants