Repository navigation
fix: amd64 Max/Min over signed zeros depended on element order - #62
Merged
Merged
Conversation
On amd64, Max, Min, MaxAxis and MinAxis over a mix of signed zeros returned the sign of whichever zero the SSE2 kernel met last ([+0, -0] gave Max = -0; [-0, +0] gave Min = +0): MAXPD/MINPD return their source operand on a zero tie. The scalar oracle and every other architecture give Max = +0 and Min = -0, Go's order. The tests meant to cover it but wrote -0.0, which Go's constant arithmetic makes +0: no negative zero was ever tested (7 cases in 2 files). They now use negZero = math.Copysign(0, -1), with a test that it is negative; corrected, they failed on a Zen 3. Fix in the wrapper: a zero result is the only one whose sign can be wrong, and it needs every element <= 0 with a zero among them; the scalar oracle then decides. Taking both operand orders in the kernel was also correct but cost 53-60% on a Zen 3; this costs +3.5% (6 ns) at n=1024 and nothing measurable elsewhere (abbench, 8 rounds). TestExtremaOfSignedZeros (public API, Max/Min/MaxAxis/MinAxis) fails on v0.19.0 on amd64 and passes now. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Defect. On amd64,
Max,Min,MaxAxisandMinAxisover a mix of signed zeros returned the sign of whichever zero the SSE2 kernel met last:Max([+0, −0])Min([−0, +0])MAXPD/MINPD return their source operand when both inputs are zeros. ndarray's contract is the scalar oracle (
math.Max/Min, Go's max/min: −0 < +0), which arm64 FMAX and s390x Java-mode VFMAXDB already follow. NumPy's own result for zeros depends on its implementation and the element order (loops_minmax.dispatch.c.src), so it gives no answer here.Why the tests missed it. They wrote
-0.0. Go's constant arithmetic has no negative zero, so-0.0is +0, and no negative zero was ever tested: seven cases in two files. They now usenegZero = math.Copysign(0, -1), with a test that it is negative. Corrected,TestMaxMinSIMDSpecialsInVectorLanesfailed on a Zen 3 (maxSIMD=-0, Max=0).Fix, in the wrapper (
reduce_amd64.go). A zero result is the only one whose sign can be wrong, and it needs every element ≤ 0 with a zero among them. In that case the scalar oracle decides.Measured. fleettools'
abbenchon Zen 3, 8 rounds, pinned core:MaxcostSumcontrol flatTest.
TestExtremaOfSignedZeroschecks the public API (Max,Min,MaxAxis,MinAxis) for n from 2 to 40, both orders. It fails on v0.19.0 on a Zen 3 and passes with this change. The local coverage gate (covblocks) reports 0 uncovered blocks.SECURITY.mdrecords the defect.A patch release, v0.19.1.
🤖 Generated with Claude Code