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

     1  // Copyright 2025 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 && (amd64 || arm64 || wasm)
     6  
     7  package simd_test
     8  
     9  import (
    10  	"simd/archsimd"
    11  	"testing"
    12  )
    13  
    14  func TestPartInt8x16(t *testing.T) {
    15  	Do(t, 16, func(a, c []int8) {
    16  		u, _ := archsimd.LoadInt8x16Part(a)
    17  		u.Store(c)
    18  	})
    19  }
    20  
    21  func TestPartUint8x16(t *testing.T) {
    22  	a := []uint8{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
    23  	b := []uint8{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
    24  	for i := 16; i >= 0; i-- {
    25  		u, _ := archsimd.LoadUint8x16Part(a[:i])
    26  		c := make([]uint8, 32, 32)
    27  		u.Store(c)
    28  		checkSlices(t, c, b)
    29  		if i > 0 {
    30  			b[i-1] = 0
    31  		}
    32  	}
    33  }
    34  
    35  func TestPartInt16x8(t *testing.T) {
    36  	a := []int16{1, 2, 3, 4, 5, 6, 7, 8}
    37  	b := []int16{1, 2, 3, 4, 5, 6, 7, 8}
    38  	for i := 8; i >= 0; i-- {
    39  		u, _ := archsimd.LoadInt16x8Part(a[:i])
    40  		c := make([]int16, 16, 16)
    41  		u.Store(c)
    42  		checkSlices(t, c, b)
    43  		if i > 0 {
    44  			b[i-1] = 0
    45  		}
    46  	}
    47  }
    48  
    49  func TestSlicesPartStoreInt8x16(t *testing.T) {
    50  	a := []int8{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
    51  	b := []int8{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
    52  	for i := 16; i >= 0; i-- {
    53  		v := archsimd.LoadInt8x16(a)
    54  		c := make([]int8, 32, 32)
    55  		v.StorePart(c[:i])
    56  		checkSlices(t, c, b)
    57  		if i > 0 {
    58  			b[i-1] = 0
    59  		}
    60  	}
    61  }
    62  
    63  func TestSlicesPartStoreInt16x8(t *testing.T) {
    64  	a := []int16{1, 2, 3, 4, 5, 6, 7, 8}
    65  	b := []int16{1, 2, 3, 4, 5, 6, 7, 8}
    66  	for i := 8; i >= 0; i-- {
    67  		v := archsimd.LoadInt16x8(a)
    68  		c := make([]int16, 32, 32)
    69  		v.StorePart(c[:i])
    70  		checkSlices(t, c, b)
    71  		if i > 0 {
    72  			b[i-1] = 0
    73  		}
    74  	}
    75  }
    76  
    77  func TestSlicesPartStoreUint8x16(t *testing.T) {
    78  	a := []uint8{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
    79  	b := []uint8{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
    80  	for i := 16; i >= 0; i-- {
    81  		v := archsimd.LoadUint8x16(a)
    82  		c := make([]uint8, 32, 32)
    83  		v.StorePart(c[:i])
    84  		checkSlices(t, c, b)
    85  		if i > 0 {
    86  			b[i-1] = 0
    87  		}
    88  	}
    89  }
    90  
    91  func TestPartInt32(t *testing.T) {
    92  	// 32x4
    93  	L := 4
    94  	c := []int32{1, 2, 3, 4, 5, -1, -1, -1, -1}
    95  	a := c[:L+1]
    96  	for i := range a {
    97  		// Test the load first
    98  		// e is a partial slice.
    99  		e := a[i:]
   100  		v, _ := archsimd.LoadInt32x4Part(e)
   101  		// d contains what a ought to contain
   102  		d := make([]int32, L)
   103  		for j := 0; j < len(e) && j < len(d); j++ {
   104  			d[j] = e[j]
   105  		}
   106  
   107  		b := make([]int32, L)
   108  		v.Store(b)
   109  		// test the load
   110  		checkSlices(t, d, b)
   111  
   112  		// Test the store
   113  		f := make([]int32, L+1)
   114  		for i := range f {
   115  			f[i] = 99
   116  		}
   117  
   118  		v.StorePart(f[:len(e)])
   119  		if len(e) < len(b) {
   120  			checkSlices(t, f, b[:len(e)])
   121  		} else {
   122  			checkSlices(t, f, b)
   123  		}
   124  		for i := len(e); i < len(f); i++ {
   125  			if f[i] != 99 {
   126  				t.Errorf("StorePart altered f[%d], expected 99, saw %d", i, f[i])
   127  			}
   128  		}
   129  	}
   130  }
   131  
   132  func TestPartFloat64(t *testing.T) {
   133  	// 64x2
   134  	L := 2
   135  	c := []float64{1, 2, 3, 86, 86, 86, 86}
   136  	a := c[:L+1]
   137  	for i := range a {
   138  		// Test the load first
   139  		// e is a partial slice.
   140  		e := a[i:]
   141  		v, _ := archsimd.LoadFloat64x2Part(e)
   142  		// d contains what a ought to contain
   143  		d := make([]float64, L)
   144  		for j := 0; j < len(e) && j < len(d); j++ {
   145  			d[j] = e[j]
   146  		}
   147  
   148  		b := make([]float64, L)
   149  		v.Store(b)
   150  		// test the load
   151  		checkSlices(t, d, b)
   152  
   153  		// Test the store
   154  		f := make([]float64, L+1)
   155  		for i := range f {
   156  			f[i] = 99
   157  		}
   158  
   159  		v.StorePart(f[:len(e)])
   160  		if len(e) < len(b) {
   161  			checkSlices(t, f, b[:len(e)])
   162  		} else {
   163  			checkSlices(t, f, b)
   164  		}
   165  		for i := len(e); i < len(f); i++ {
   166  			if f[i] != 99 {
   167  				t.Errorf("StorePart altered f[%d], expected 99, saw %v", i, f[i])
   168  			}
   169  		}
   170  	}
   171  }
   172  

View as plain text