Skip to content

fix: amd64 Max/Min over signed zeros depended on element order - #62

Merged
tannevaled merged 1 commit into
mainfrom
fix/amd64-signed-zero-extrema
Oct 26, 2026
Merged

tannevaled merged 1 commit into
mainfrom
fix/amd64-signed-zero-extrema

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Defect. On amd64, Max, Min, MaxAxis and MinAxis over a mix of signed zeros returned the sign of whichever zero the SSE2 kernel met last:

input amd64 (v0.19.0) every other arch and the scalar oracle
Max([+0, −0]) −0 +0
Min([−0, +0]) +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.0 is +0, and no negative zero was ever tested: seven cases in two files. They now use negZero = math.Copysign(0, -1), with a test that it is negative. Corrected, TestMaxMinSIMDSpecialsInVectorLanes failed 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' abbench on Zen 3, 8 rounds, pinned core:

approach Max cost
combine both operand orders in the kernel (tried, kept out) +53–60%
wrapper check (this PR) +3.5% (6 ns) at n=1024, ~ at 16 Ki, 256 Ki and 4 Mi; Sum control flat

Test. TestExtremaOfSignedZeros checks 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.md records the defect.

A patch release, v0.19.1.

🤖 Generated with Claude Code

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>
@tannevaled
tannevaled merged commit 725ee5d into main Oct 26, 2026
12 checks passed
@tannevaled
tannevaled deleted the fix/amd64-signed-zero-extrema branch October 26, 2026 09:59
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.

1 participant