From f80f9afc52567eb1f9fb1a3d9e4a5b75472fda89 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sun, 4 Oct 2026 11:40:56 +0200 Subject: [PATCH] test: compile and run the suite on 32-bit targets; pin the asm generator overflow_test.go wrote its overflowing shapes as 1<<62 and 1<<40 literals, which do not compile where int is 32 bits, so `go test` failed to build on linux/386, arm, mips and mipsle. The extents are now derived from bits.UintSize, so the same products overflow on both word sizes; the suite passes on real 386 hardware. Nothing caught this because CI never ran a 32-bit target. Three lanes now cover it: - arch 386: GOARCH=386 natively on the amd64 runner (not qemu-i386). - arch arm (qemu): a 32-bit ARM lane beside the existing qemu ones. - compile tests on every target: `go test -c` for each `go tool dist list` pair (`go vet` for android/ios, which cannot link without cgo). Checked with a planted 1<<62 literal, which both modes refuse. The asm generators carried //go:build ignore, so `go mod tidy` saw no import of go-asmgen and dropped the requirement. Renovate's "update to v0.9.0" (#6) did exactly that, leaving CI to resolve whatever version -mod=mod found. The tag is gone (each generator is the only file of its own main package), and go-asmgen is pinned at v0.10.0 again, which regenerates the committed .s byte for byte. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 49 ++++++++++++++++++++++++++++ internal/kernels/asmgen/amd64/gen.go | 2 -- internal/kernels/asmgen/arm64/gen.go | 18 +++++----- internal/kernels/asmgen/go.mod | 2 ++ internal/kernels/asmgen/go.sum | 2 ++ overflow_test.go | 40 ++++++++++++++--------- 6 files changed, 86 insertions(+), 27 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4ea7997..68a6d6b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -115,6 +115,8 @@ jobs: - { goarch: loong64, qemu: qemu-loongarch64-static } - { goarch: ppc64le, qemu: qemu-ppc64le-static } - { goarch: s390x, qemu: qemu-s390x-static } + # 32-bit: int is 32 bits, so shape/size overflow paths differ. + - { goarch: arm, qemu: qemu-arm-static } steps: - uses: actions/checkout@v7 - uses: actions/setup-go@v7 @@ -125,3 +127,50 @@ jobs: run: | GOOS=linux GOARCH=${{ matrix.goarch }} go vet ./... GOARCH=${{ matrix.goarch }} go test -exec=${{ matrix.qemu }} ./... + + # 386 runs natively on the amd64 runner (the kernel executes 32-bit x86 + # binaries), not under qemu-i386, whose emulation loses x87/SSE state across + # preemption. It is the second 32-bit word-size lane, beside arm. + arch-386: + name: arch 386 + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v7 + - uses: actions/setup-go@v7 + with: + go-version: stable + - name: Vet + test (GOARCH=386) + run: | + GOARCH=386 go vet ./... + GOARCH=386 go test ./... + + # Every GOOS/GOARCH pair Go supports must at least compile the library's + # tests: a test that does not compile on a target is a test + # that never runs there, and nothing else would notice (a 1<<62 literal + # once broke every 32-bit target this way). Execution is the lanes above. + cross-compile: + name: compile tests on every target + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v7 + - uses: actions/setup-go@v7 + with: + go-version: stable + - name: go test -c (go vet on android/ios) for each go tool dist list pair + run: | + fail=0 + for t in $(go tool dist list); do + os=${t%/*}; arch=${t#*/} + # android/ios refuse to build without a cgo linker, even for vet; + # with cgo nominally on and CC=false, vet type-checks the package + # and its tests (there is no import "C" to compile). + case $os in android|ios) mode=vet ;; *) mode=test ;; esac + for p in $(go list ./...); do + if [ $mode = test ]; then + out=$(CGO_ENABLED=0 GOOS=$os GOARCH=$arch go test -c -o /dev/null "$p" 2>&1) || { echo "::error::$t $p: $out"; fail=1; } + else + out=$(CGO_ENABLED=1 CC=false GOOS=$os GOARCH=$arch go vet "$p" 2>&1) || { echo "::error::$t $p: $out"; fail=1; } + fi + done + done + exit $fail diff --git a/internal/kernels/asmgen/amd64/gen.go b/internal/kernels/asmgen/amd64/gen.go index 6ce0899..5fe92c6 100644 --- a/internal/kernels/asmgen/amd64/gen.go +++ b/internal/kernels/asmgen/amd64/gen.go @@ -1,5 +1,3 @@ -//go:build ignore - // Command gen produces sum_amd64.s, the SSE2 float64 kernels, via go-asmgen. // Run with: go run gen.go (or `go generate` from the kernels package). // diff --git a/internal/kernels/asmgen/arm64/gen.go b/internal/kernels/asmgen/arm64/gen.go index 479b431..2a7b8f8 100644 --- a/internal/kernels/asmgen/arm64/gen.go +++ b/internal/kernels/asmgen/arm64/gen.go @@ -1,5 +1,3 @@ -//go:build ignore - // Command gen produces sum_arm64.s, the NEON float64 sum-reduction kernel, via // go-asmgen. Run with: go run gen.go (or `go generate` from the kernels // package). @@ -10,14 +8,14 @@ // floating-point grouping. So we hand-vectorize with NEON and multiple // accumulators, the same parallelism numpy uses inside one core. // -// sumNEON(a *float64, n int) float64 -// Sums a[0..n) with FOUR independent V.D2 accumulators (8 float64 lanes), -// then folds the lanes pairwise and adds a scalar tail. Floating-point -// addition is not associative, so this lane-parallel grouping can differ -// from a strictly sequential sum by a few ULP — exactly the trade-off -// numpy's own pairwise/SIMD summation makes. It is therefore NOT held -// bit-identical to the scalar oracle; the kernels package validates it to a -// tight relative tolerance against the oracle and against numpy. +// sumNEON(a *float64, n int) float64 +// Sums a[0..n) with FOUR independent V.D2 accumulators (8 float64 lanes), +// then folds the lanes pairwise and adds a scalar tail. Floating-point +// addition is not associative, so this lane-parallel grouping can differ +// from a strictly sequential sum by a few ULP — exactly the trade-off +// numpy's own pairwise/SIMD summation makes. It is therefore NOT held +// bit-identical to the scalar oracle; the kernels package validates it to a +// tight relative tolerance against the oracle and against numpy. // // (A max-reduction SIMD kernel is intentionally not shipped: NEON FMAX's NaN // propagation differs from the scalar `if v > m` oracle, so a bit-identical diff --git a/internal/kernels/asmgen/go.mod b/internal/kernels/asmgen/go.mod index a2b318e..9e9cec2 100644 --- a/internal/kernels/asmgen/go.mod +++ b/internal/kernels/asmgen/go.mod @@ -5,3 +5,5 @@ module github.com/go-ndarray/ndarray/internal/kernels/asmgen go 1.26.4 + +require github.com/go-asmgen/asmgen v0.10.0 diff --git a/internal/kernels/asmgen/go.sum b/internal/kernels/asmgen/go.sum index e69de29..21b2ac1 100644 --- a/internal/kernels/asmgen/go.sum +++ b/internal/kernels/asmgen/go.sum @@ -0,0 +1,2 @@ +github.com/go-asmgen/asmgen v0.10.0 h1:LyCKpGRcug2ynAyOKHwxYxVgd4sNd9S2Oy9IFOR9lTQ= +github.com/go-asmgen/asmgen v0.10.0/go.mod h1:Hj29fXioLRe9oSnQ18SuDBiOcCbu8Qwxba6/5TMr4+Y= diff --git a/overflow_test.go b/overflow_test.go index 195b2ba..ba60619 100644 --- a/overflow_test.go +++ b/overflow_test.go @@ -3,46 +3,56 @@ package ndarray import ( "errors" "math" + "math/bits" "testing" ) +// Shape extents chosen relative to the word size, so the same products +// overflow int on 32- and 64-bit targets alike (a literal 1<<62 does not even +// compile where int is 32 bits). +const ( + quarterWord = 1 << (bits.UintSize - 2) // x4 wraps to exactly 0 + byteBreaker = 1 << (bits.UintSize - 5) // x8 elements fit; x8 bytes do not + halfSquare = 1 << (bits.UintSize/2 + 4) +) + // TestShapeSizeOverflow checks that a shape whose element count overflows int -// is rejected instead of wrapping. Before the fix prod() wrapped silently: -// New(1<<62, 4) returned an array of Size 0, and FromData/Reshape accepted -// (1<<62+1, 4) for 4 elements because the product wrapped to exactly 4, after +// is rejected instead of wrapping. Before the fix prod() wrapped silently (on +// 64-bit): New(1<<62, 4) returned an array of Size 0, and FromData/Reshape +// accepted (1<<62+1, 4) for 4 elements because the product wrapped to 4, after // which At(1, 0) panicked. numpy raises "array is too big" for all of these. func TestShapeSizeOverflow(t *testing.T) { huge := [][]int{ - {1 << 62, 4}, - {1<<62 + 1, 4}, - {1 << 59, 8}, + {quarterWord, 4}, + {quarterWord + 1, 4}, + {byteBreaker, 8}, {math.MaxInt, 2}, - {0, 1 << 62, 1 << 62}, // numpy rejects this too, despite the zero + {0, quarterWord, quarterWord}, // numpy rejects this too, despite the zero } for _, s := range huge { if _, err := New(s...); !errors.Is(err, ErrShapeMismatch) { t.Errorf("New(%v) err = %v, want ErrShapeMismatch", s, err) } } - if _, err := FromData([]float64{1, 2, 3, 4}, 1<<62+1, 4); !errors.Is(err, ErrShapeMismatch) { + if _, err := FromData([]float64{1, 2, 3, 4}, quarterWord+1, 4); !errors.Is(err, ErrShapeMismatch) { t.Errorf("FromData wrapped shape err = %v", err) } a := mustArr(t, ok(Arange(0, 4, 1))) - if _, err := a.Reshape(1<<62+1, 4); !errors.Is(err, ErrShapeMismatch) { + if _, err := a.Reshape(quarterWord+1, 4); !errors.Is(err, ErrShapeMismatch) { t.Errorf("Reshape wrapped shape err = %v", err) } - if _, err := a.Reshape(-1, 1<<62+1, 4); !errors.Is(err, ErrShapeMismatch) { + if _, err := a.Reshape(-1, quarterWord+1, 4); !errors.Is(err, ErrShapeMismatch) { t.Errorf("Reshape wrapped shape with -1 err = %v", err) } // Two valid shapes can broadcast to an unrepresentable one. - col := &Array{data: []float64{1}, shape: []int{1 << 40, 1}, strides: []int{0, 0}} - row := &Array{data: []float64{1}, shape: []int{1, 1 << 40}, strides: []int{0, 0}} + col := &Array{data: []float64{1}, shape: []int{halfSquare, 1}, strides: []int{0, 0}} + row := &Array{data: []float64{1}, shape: []int{1, halfSquare}, strides: []int{0, 0}} if _, err := col.Add(row); !errors.Is(err, ErrShapeMismatch) { - t.Errorf("broadcast to (2^40, 2^40) err = %v", err) + t.Errorf("broadcast to (%d, %d) err = %v", halfSquare, halfSquare, err) } // Large but representable shapes are still fine. - if _, err := New(0, 1<<40); err != nil { - t.Errorf("New(0, 1<<40) err = %v", err) + if _, err := New(0, halfSquare); err != nil { + t.Errorf("New(0, %d) err = %v", halfSquare, err) } }