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 testUint8sCompare(t *testing.T, cmp func(_, _ archsimd.Uint8s) archsimd.Mask8s, want func(_, _ uint8) bool) {
    63  	var z archsimd.Uint8s
    64  	testSVECompare(t, uint8s, 1, z.Len(), archsimd.LoadUint8s, cmp, archsimd.Mask8s.Store, want)
    65  }
    66  
    67  func testFloat32sCompare(t *testing.T, cmp func(_, _ archsimd.Float32s) archsimd.Mask32s, want func(_, _ float32) bool) {
    68  	var z archsimd.Float32s
    69  	testSVECompare(t, float32s, 4, z.Len(), archsimd.LoadFloat32s, cmp, archsimd.Mask32s.Store, want)
    70  }
    71  
    72  func testFloat64sCompare(t *testing.T, cmp func(_, _ archsimd.Float64s) archsimd.Mask64s, want func(_, _ float64) bool) {
    73  	var z archsimd.Float64s
    74  	testSVECompare(t, float64s, 8, z.Len(), archsimd.LoadFloat64s, cmp, archsimd.Mask64s.Store, want)
    75  }
    76  
    77  func TestGreaterSVE(t *testing.T) {
    78  	if !archsimd.ARM64.SVE() {
    79  		t.Skip("no sve")
    80  	}
    81  	testInt8sCompare(t, archsimd.Int8s.Greater, func(a, b int8) bool { return a > b })
    82  	testInt16sCompare(t, archsimd.Int16s.Greater, func(a, b int16) bool { return a > b })
    83  	testInt32sCompare(t, archsimd.Int32s.Greater, func(a, b int32) bool { return a > b })
    84  	testInt64sCompare(t, archsimd.Int64s.Greater, func(a, b int64) bool { return a > b })
    85  	testUint8sCompare(t, archsimd.Uint8s.Greater, func(a, b uint8) bool { return a > b })
    86  	testFloat32sCompare(t, archsimd.Float32s.Greater, func(a, b float32) bool { return a > b })
    87  	testFloat64sCompare(t, archsimd.Float64s.Greater, func(a, b float64) bool { return a > b })
    88  }
    89  
    90  // TestMaskStoreLoadPanicSVE checks that the exported mask memory APIs panic when
    91  // the bits slice is too short to hold the whole predicate.
    92  func TestMaskStoreLoadPanicSVE(t *testing.T) {
    93  	if !archsimd.ARM64.SVE() {
    94  		t.Skip("no sve")
    95  	}
    96  	var z archsimd.Int8s
    97  	m := z.Greater(z)
    98  	mustPanic(t, "Store short", func() { m.Store(nil) })
    99  	mustPanic(t, "LoadMask8s short", func() { archsimd.LoadMask8s(nil) })
   100  	// A slice long enough for the whole predicate must not panic.
   101  	bits := make([]uint16, sveMaskUint16s)
   102  	m.Store(bits)
   103  	archsimd.LoadMask8s(bits)
   104  }
   105  
   106  func TestEqualSVE(t *testing.T) {
   107  	if !archsimd.ARM64.SVE() {
   108  		t.Skip("no sve")
   109  	}
   110  	testInt8sCompare(t, archsimd.Int8s.Equal, func(a, b int8) bool { return a == b })
   111  	testUint8sCompare(t, archsimd.Uint8s.Equal, func(a, b uint8) bool { return a == b })
   112  	testFloat64sCompare(t, archsimd.Float64s.Equal, func(a, b float64) bool { return a == b })
   113  }
   114  
   115  func TestNotEqualSVE(t *testing.T) {
   116  	if !archsimd.ARM64.SVE() {
   117  		t.Skip("no sve")
   118  	}
   119  	testInt8sCompare(t, archsimd.Int8s.NotEqual, func(a, b int8) bool { return a != b })
   120  	testUint8sCompare(t, archsimd.Uint8s.NotEqual, func(a, b uint8) bool { return a != b })
   121  	testFloat64sCompare(t, archsimd.Float64s.NotEqual, func(a, b float64) bool { return a != b })
   122  }
   123  
   124  func TestGreaterEqualSVE(t *testing.T) {
   125  	if !archsimd.ARM64.SVE() {
   126  		t.Skip("no sve")
   127  	}
   128  	testInt8sCompare(t, archsimd.Int8s.GreaterEqual, func(a, b int8) bool { return a >= b })
   129  	testUint8sCompare(t, archsimd.Uint8s.GreaterEqual, func(a, b uint8) bool { return a >= b })
   130  	testFloat64sCompare(t, archsimd.Float64s.GreaterEqual, func(a, b float64) bool { return a >= b })
   131  }
   132  

View as plain text