diff --git a/SECURITY.md b/SECURITY.md index ca8e41b..dcadfcb 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -66,6 +66,27 @@ standard library's known vulnerabilities; `govulncheck ./...` reports none. ## Audit log +**2026-10-25 (v0.19.1).** A correctness defect, found while reading the +float64 extremum kernels before writing float32 ones. On amd64, `Max`, `Min`, +`MaxAxis` and `MinAxis` over a mix of signed zeros returned the sign of +whichever zero the kernel met last: `[+0, -0]` gave `Max = -0` and +`[-0, +0]` gave `Min = +0`. MAXPD/MINPD return their source operand on a zero +tie. Every other architecture, and the scalar oracle, give `Max = +0` and +`Min = -0`, as Go's max/min order them. + +The tests had meant to cover this. They wrote `-0.0`, which Go's constant +arithmetic makes +0, so no negative zero was ever tested: seven cases in two +files. They now use `math.Copysign(0, -1)`, guarded by a test of the helper; +corrected, they failed on a Zen 3. + +The fix leaves the kernel alone. A zero result is the only one whose sign +can be wrong, and it needs every element <= 0 with a zero among them. The +wrapper then asks the scalar oracle. Combining both operand orders inside +the kernel was also correct, but cost 53-60% on a Zen 3; the wrapper check +costs nothing measurable, +3.5% (6 ns) at n=1024 and ~ elsewhere. +`TestExtremaOfSignedZeros` checks the public API; it fails on v0.19.0 (amd64) +and passes now. + **2026-10-20 (v0.19.0).** `kernels.Sqrt32` follows `Bin32`'s guards: it reslices `src` to `len(dst)` before the call (a short `src` panics in Go, tested), never passes an empty slice to a kernel, and keeps the LASX, V and diff --git a/fidelity_test.go b/fidelity_test.go index 0759411..6edeb5a 100644 --- a/fidelity_test.go +++ b/fidelity_test.go @@ -122,3 +122,34 @@ func TestLog2Accuracy(t *testing.T) { t.Errorf("complex Log2 = %v", z) } } + +// TestExtremaOfSignedZeros: Max is +0 and Min is -0 over a mix of signed +// zeros, whatever their order, as Go's max/min and math.Max/Min order them. +// Until v0.19.1 amd64 returned the sign of whichever zero its kernel met last +// (MAXPD/MINPD return their source operand on a zero tie). The tests had meant +// to cover this but wrote -0.0, which Go's constant arithmetic makes +0. +func TestExtremaOfSignedZeros(t *testing.T) { + nz := math.Copysign(0, -1) + for _, n := range []int{2, 3, 9, 16, 17, 40} { + for first := 0; first < 2; first++ { + d := make([]float64, n) + for i := range d { + if (i+first)%2 == 0 { + d[i] = nz + } + } + a := mustArr(t, ok(FromData(d, n))) + mx, _ := a.Max() + mn, _ := a.Min() + if math.Signbit(mx) || !math.Signbit(mn) { + t.Errorf("n=%d first=%d: Max=%v Min=%v, want 0 and -0", n, first, mx, mn) + } + rows := mustArr(t, ok(a.Reshape(1, n))) + rmx := mustArr(t, ok(rows.MaxAxis(1, false))).materialize()[0] + rmn := mustArr(t, ok(rows.MinAxis(1, false))).materialize()[0] + if math.Signbit(rmx) || !math.Signbit(rmn) { + t.Errorf("n=%d first=%d: MaxAxis=%v MinAxis=%v, want 0 and -0", n, first, rmx, rmn) + } + } + } +} diff --git a/internal/kernels/log_test.go b/internal/kernels/log_test.go index 063ba7a..4d601ae 100644 --- a/internal/kernels/log_test.go +++ b/internal/kernels/log_test.go @@ -126,7 +126,7 @@ func TestLogSpecial(t *testing.T) { // TestLogLoopMatchesLog1: Log's loop and log1 give the same bits everywhere. func TestLogLoopMatchesLog1(t *testing.T) { rng := rand.New(rand.NewSource(4)) - xs := []float64{0, -0.0, 1, 1 - 0x1p-4, 1 + 0x1.09p-4, 0x1p-1022, 5e-324, -3, + xs := []float64{0, negZero, 1, 1 - 0x1p-4, 1 + 0x1.09p-4, 0x1p-1022, 5e-324, -3, math.Inf(1), math.Inf(-1), math.NaN(), math.MaxFloat64} for i := 0; i < 20000; i++ { xs = append(xs, math.Ldexp(rng.Float64()*2-0.5, rng.Intn(2100)-1074), 1+(rng.Float64()-0.5)*0.3) diff --git a/internal/kernels/reduce_amd64.go b/internal/kernels/reduce_amd64.go index 4524233..a378763 100644 --- a/internal/kernels/reduce_amd64.go +++ b/internal/kernels/reduce_amd64.go @@ -37,14 +37,28 @@ func sqrtSIMD(dst, src []float64) { // math.Max-based scalar oracle (Max) and numpy.max bit-for-bit. The SSE2 kernel // accumulates a packed running max with MAXPD and a separate "saw NaN" mask via // CMPUNORDPD, then returns NaN if any lane ever held a NaN. +// +// MAXPD returns its source operand when both inputs are zeros, so the kernel's +// zero is +0 or -0 depending on where the zeros sit. A zero result is the one +// case where that matters, and it needs every element <= 0 with a zero among +// them: the scalar oracle then gives the sign Go's max uses (+0 if any +0). +// Doing the same in the kernel, both operand orders combined at every step, +// cost 53-60% on a Zen 3; this costs nothing on any other result. func maxSIMD(a []float64) float64 { - return maxSSE2(&a[0], len(a)) + if r := maxSSE2(&a[0], len(a)); r != 0 { + return r + } + return Max(a) } // minSIMD returns the NaN-propagating minimum of a (non-empty), the dual of -// maxSIMD via MINPD; bit-identical to the scalar Min oracle and numpy.min. +// maxSIMD via MINPD; bit-identical to the scalar Min oracle and numpy.min. A +// zero result is resolved by the oracle, as in maxSIMD (-0 if any -0). func minSIMD(a []float64) float64 { - return minSSE2(&a[0], len(a)) + if r := minSSE2(&a[0], len(a)); r != 0 { + return r + } + return Min(a) } // addBin/subBin/mulBin/divBin write a[i] OP b[i] into dst[i] with the packed diff --git a/internal/kernels/reduce_simd_test.go b/internal/kernels/reduce_simd_test.go index ca4caa9..0d435ac 100644 --- a/internal/kernels/reduce_simd_test.go +++ b/internal/kernels/reduce_simd_test.go @@ -6,6 +6,18 @@ import ( "testing" ) +// negZero is -0. The literal -0.0 is NOT: Go's constant arithmetic has no +// negative zero, so -0.0 is +0, and the signed-zero cases these tests meant to +// cover were never run until 2026-10-25. +var negZero = math.Copysign(0, -1) + +// TestNegZeroIsNegative guards the helper against that mistake. +func TestNegZeroIsNegative(t *testing.T) { + if !math.Signbit(negZero) { + t.Fatal("negZero is +0") + } +} + // TestSumSIMD asserts the SIMD sum kernel agrees with the scalar oracle to a // tight relative tolerance across many random lengths (covering the vector body, // the lane fold, and the scalar tail of every residue mod 8). The two use a @@ -88,8 +100,8 @@ func TestBinSIMD(t *testing.T) { } // Edge inputs: signed zeros, infinities, NaN, extremes — paired so each op // hits e.g. Inf-Inf=NaN, 0*Inf=NaN, x/0=Inf, 0/0=NaN, -0 handling, etc. - ea := []float64{-0.0, 0.0, 1, -1, math.Inf(1), math.Inf(-1), math.NaN(), 1e308, -1e308, math.MaxFloat64, math.SmallestNonzeroFloat64, 0.0} - eb := []float64{0.0, -0.0, math.Inf(1), math.Inf(-1), math.Inf(1), 1, 2, 1e308, 1e308, 2, 2, 0.0} + ea := []float64{negZero, 0.0, 1, -1, math.Inf(1), math.Inf(-1), math.NaN(), 1e308, -1e308, math.MaxFloat64, math.SmallestNonzeroFloat64, 0.0} + eb := []float64{0.0, negZero, math.Inf(1), math.Inf(-1), math.Inf(1), 1, 2, 1e308, 1e308, 2, 2, 0.0} for _, op := range ops { got := make([]float64, len(ea)) want := make([]float64, len(ea)) @@ -139,7 +151,7 @@ func TestSqrtSIMD(t *testing.T) { } // Edge cases: negatives, zeros, infinities, NaN must match the oracle bitwise. edge := []float64{ - -1, -0.0, 0.0, 1, 4, 2, 1e308, + -1, negZero, 0.0, 1, 4, 2, 1e308, math.Inf(1), math.Inf(-1), math.NaN(), -math.MaxFloat64, math.SmallestNonzeroFloat64, } got := make([]float64, len(edge)) @@ -188,7 +200,7 @@ func TestMaxMinSIMD(t *testing.T) { } } // Infinities and signed zeros bit-match the oracle. - special := []float64{math.Inf(-1), -0.0, 0.0, math.Inf(1), 5, -5} + special := []float64{math.Inf(-1), negZero, 0.0, math.Inf(1), 5, -5} if g, w := maxSIMD(special), Max(special); !bitEq(g, w) { t.Fatalf("special: maxSIMD=%v, Max=%v", g, w) } @@ -263,8 +275,8 @@ func BenchmarkSumSIMD(b *testing.B) { func TestMaxMinSIMDSpecialsInVectorLanes(t *testing.T) { for n := 8; n <= 40; n++ { for pos := 0; pos < n; pos++ { - for _, fill := range []float64{-0.0, 0.0, -1, 1} { - for _, v := range []float64{-0.0, 0.0, math.Inf(1), math.Inf(-1)} { + for _, fill := range []float64{negZero, 0.0, -1, 1} { + for _, v := range []float64{negZero, 0.0, math.Inf(1), math.Inf(-1)} { a := make([]float64, n) for i := range a { a[i] = fill