Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
2 changes: 0 additions & 2 deletions internal/kernels/asmgen/amd64/gen.go
Original file line number Diff line number Diff line change
@@ -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).
//
Expand Down
18 changes: 8 additions & 10 deletions internal/kernels/asmgen/arm64/gen.go
Original file line number Diff line number Diff line change
@@ -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).
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions internal/kernels/asmgen/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions internal/kernels/asmgen/go.sum
Original file line number Diff line number Diff line change
@@ -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=
40 changes: 25 additions & 15 deletions overflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
Loading