Source file src/simd/archsimd/internal/simd_test/compare_sve_arm64_test.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build goexperiment.simd && arm64
     6  
     7  // SVE mask (predicate) tests, in the same utility-function shape as
     8  // compare_amd64_test.go.
     9  
    10  package simd_test
    11  
    12  import (
    13  	"simd/archsimd"
    14  	"testing"
    15  )
    16  
    17  const sveMaskUint16s = sveMaxBytes / 8
    18  
    19  // testSVECompare drives a scalable compare that returns a mask. active is the
    20  // runtime number of live lanes; elemBytes is the element width. Lane i is true
    21  // iff bit i*elemBytes of the stored predicate is set.
    22  func testSVECompare[T number, V, M any](t *testing.T, pool []T, elemBytes, active int,
    23  	load func([]T) V, cmp func(V, V) M, store func(M, []uint16), want func(T, T) bool) {
    24  	t.Helper()
    25  	count := sveMaxBytes / elemBytes
    26  	forSlicePair(t, pool, count, func(x, y []T) bool {
    27  		t.Helper()
    28  		bits := make([]uint16, sveMaskUint16s)
    29  		store(cmp(load(x), load(y)), bits)
    30  		for i := 0; i < active; i++ {
    31  			b := i * elemBytes
    32  			got := bits[b/16]>>uint(b%16)&1 == 1
    33  			if got != want(x[i], y[i]) {
    34  				t.Errorf("lane %d: got %v, want %v (x=%v y=%v)", i, got, want(x[i], y[i]), x[i], y[i])
    35  				return false
    36  			}
    37  		}
    38  		return true
    39  	})
    40  }
    41  
    42  func testInt8sCompare(t *testing.T, cmp func(_, _ archsimd.Int8s) archsimd.Mask8s, want func(_, _ int8) bool) {
    43  	var z archsimd.Int8s
    44  	testSVECompare(t, int8s, 1, z.Len(), archsimd.LoadInt8s, cmp, archsimd.Mask8s.Store, want)
    45  }
    46  
    47  func testInt16sCompare(t *testing.T, cmp func(_, _ archsimd.Int16s) archsimd.Mask16s, want func(_, _ int16) bool) {
    48  	var z archsimd.Int16s
    49  	testSVECompare(t, int16s, 2, z.Len(), archsimd.LoadInt16s, cmp, archsimd.Mask16s.Store, want)
    50  }
    51  
    52  func testInt32sCompare(t *testing.T, cmp func(_, _ archsimd.Int32s) archsimd.Mask32s, want func(_, _ int32) bool) {
    53  	var z archsimd.Int32s
    54  	testSVECompare(t, int32s, 4, z.Len(), archsimd.LoadInt32s, cmp, archsimd.Mask32s.Store, want)
    55  }
    56  
    57  func testInt64sCompare(t *testing.T, cmp func(_, _ archsimd.Int64s) archsimd.Mask64s, want func(_, _ int64) bool) {
    58  	var z archsimd.Int64s
    59  	testSVECompare(t, int64s, 8, z.Len(), archsimd.LoadInt64s, cmp, archsimd.Mask64s.Store, want)
    60  }
    61  
    62  func TestGreaterSVE(t *testing.T) {
    63  	if !archsimd.ARM64.SVE() {
    64  		t.Skip("no sve")
    65  	}
    66  	testInt8sCompare(t, archsimd.Int8s.Greater, func(a, b int8) bool { return a > b })
    67  	testInt16sCompare(t, archsimd.Int16s.Greater, func(a, b int16) bool { return a > b })
    68  	testInt32sCompare(t, archsimd.Int32s.Greater, func(a, b int32) bool { return a > b })
    69  	testInt64sCompare(t, archsimd.Int64s.Greater, func(a, b int64) bool { return a > b })
    70  }
    71  
    72  // TestMaskStoreLoadPanicSVE checks that the exported mask memory APIs panic when
    73  // the bits slice is too short to hold the whole predicate.
    74  func TestMaskStoreLoadPanicSVE(t *testing.T) {
    75  	if !archsimd.ARM64.SVE() {
    76  		t.Skip("no sve")
    77  	}
    78  	var z archsimd.Int8s
    79  	m := z.Greater(z)
    80  	mustPanic(t, "Store short", func() { m.Store(nil) })
    81  	mustPanic(t, "LoadMask8s short", func() { archsimd.LoadMask8s(nil) })
    82  	// A slice long enough for the whole predicate must not panic.
    83  	bits := make([]uint16, sveMaskUint16s)
    84  	m.Store(bits)
    85  	archsimd.LoadMask8s(bits)
    86  }
    87  

View as plain text