Source file src/simd/pkginternal_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
     6  
     7  package simd_test
     8  
     9  import (
    10  	"simd"
    11  	"simd/internal/test_helpers"
    12  	"testing"
    13  )
    14  
    15  func TestConcatSelectedConstant64(t *testing.T) {
    16  	a := make([]int64, 2)
    17  	x := simd.LoadInt64x2Slice([]int64{4, 5})
    18  	y := simd.LoadInt64x2Slice([]int64{6, 7})
    19  	z := x.ExportTestConcatSelectedConstant(0b10, y)
    20  	z.StoreSlice(a)
    21  	test_helpers.CheckSlices[int64](t, a, []int64{4, 7})
    22  }
    23  
    24  func TestConcatSelectedConstantGrouped64(t *testing.T) {
    25  	a := make([]float64, 4)
    26  	x := simd.LoadFloat64x4Slice([]float64{4, 5, 8, 9})
    27  	y := simd.LoadFloat64x4Slice([]float64{6, 7, 10, 11})
    28  	z := x.ExportTestConcatSelectedConstantGrouped(0b_11_10, y)
    29  	z.StoreSlice(a)
    30  	test_helpers.CheckSlices[float64](t, a, []float64{4, 7, 9, 11})
    31  }
    32  
    33  func TestConcatSelectedConstant32(t *testing.T) {
    34  	a := make([]float32, 4)
    35  	x := simd.LoadFloat32x4Slice([]float32{4, 5, 8, 9})
    36  	y := simd.LoadFloat32x4Slice([]float32{6, 7, 10, 11})
    37  	z := x.ExportTestConcatSelectedConstant(0b_11_01_10_00, y)
    38  	z.StoreSlice(a)
    39  	test_helpers.CheckSlices[float32](t, a, []float32{4, 8, 7, 11})
    40  }
    41  
    42  func TestConcatSelectedConstantGrouped32(t *testing.T) {
    43  	a := make([]uint32, 8)
    44  	x := simd.LoadUint32x8Slice([]uint32{0, 1, 2, 3, 8, 9, 10, 11})
    45  	y := simd.LoadUint32x8Slice([]uint32{4, 5, 6, 7, 12, 13, 14, 15})
    46  	z := x.ExportTestConcatSelectedConstantGrouped(0b_11_01_00_10, y)
    47  	z.StoreSlice(a)
    48  	test_helpers.CheckSlices[uint32](t, a, []uint32{2, 0, 5, 7, 10, 8, 13, 15})
    49  }
    50  
    51  func TestTern(t *testing.T) {
    52  	if !simd.X86.AVX512() {
    53  		t.Skip("This test needs AVX512")
    54  	}
    55  	x := simd.LoadInt32x8Slice([]int32{0, 0, 0, 0, 1, 1, 1, 1})
    56  	y := simd.LoadInt32x8Slice([]int32{0, 0, 1, 1, 0, 0, 1, 1})
    57  	z := simd.LoadInt32x8Slice([]int32{0, 1, 0, 1, 0, 1, 0, 1})
    58  
    59  	foo := func(w simd.Int32x8, k uint8) {
    60  		a := make([]int32, 8)
    61  		w.StoreSlice(a)
    62  		t.Logf("For k=%0b, w=%v", k, a)
    63  		for i, b := range a {
    64  			if (int32(k)>>i)&1 != b {
    65  				t.Errorf("Element %d of stored slice (=%d) did not match corresponding bit in 0b%b",
    66  					i, b, k)
    67  			}
    68  		}
    69  	}
    70  
    71  	foo(x.ExportTestTern(0b1111_0000, y, z), 0b1111_0000)
    72  	foo(x.ExportTestTern(0b1100_1100, y, z), 0b1100_1100)
    73  	foo(x.ExportTestTern(0b1010_1010, y, z), 0b1010_1010)
    74  }
    75  
    76  func TestSelect2x4x32(t *testing.T) {
    77  	for a := range uint8(8) {
    78  		for b := range uint8(8) {
    79  			for c := range uint8(8) {
    80  				for d := range uint8(8) {
    81  					x := simd.LoadInt32x4Slice([]int32{0, 1, 2, 3})
    82  					y := simd.LoadInt32x4Slice([]int32{4, 5, 6, 7})
    83  					z := select2x4x32(x, a, b, c, d, y)
    84  					w := make([]int32, 4, 4)
    85  					z.StoreSlice(w)
    86  					if w[0] != int32(a) || w[1] != int32(b) ||
    87  						w[2] != int32(c) || w[3] != int32(d) {
    88  						t.Errorf("Expected [%d %d %d %d] got %v", a, b, c, d, w)
    89  					}
    90  				}
    91  			}
    92  		}
    93  	}
    94  }
    95  
    96  func TestSelect2x8x32Grouped(t *testing.T) {
    97  	for a := range uint8(8) {
    98  		for b := range uint8(8) {
    99  			for c := range uint8(8) {
   100  				for d := range uint8(8) {
   101  					x := simd.LoadInt32x8Slice([]int32{0, 1, 2, 3, 10, 11, 12, 13})
   102  					y := simd.LoadInt32x8Slice([]int32{4, 5, 6, 7, 14, 15, 16, 17})
   103  					z := select2x8x32Grouped(x, a, b, c, d, y)
   104  					w := make([]int32, 8, 8)
   105  					z.StoreSlice(w)
   106  					if w[0] != int32(a) || w[1] != int32(b) ||
   107  						w[2] != int32(c) || w[3] != int32(d) ||
   108  						w[4] != int32(10+a) || w[5] != int32(10+b) ||
   109  						w[6] != int32(10+c) || w[7] != int32(10+d) {
   110  						t.Errorf("Expected [%d %d %d %d %d %d %d %d] got %v", a, b, c, d, 10+a, 10+b, 10+c, 10+d, w)
   111  					}
   112  				}
   113  			}
   114  		}
   115  	}
   116  }
   117  
   118  // select2x4x32 returns a selection of 4 elements in x and y, numbered
   119  // 0-7, where 0-3 are the four elements of x and 4-7 are the four elements
   120  // of y.
   121  func select2x4x32(x simd.Int32x4, a, b, c, d uint8, y simd.Int32x4) simd.Int32x4 {
   122  	pattern := a>>2 + (b&4)>>1 + (c & 4) + (d&4)<<1
   123  
   124  	a, b, c, d = a&3, b&3, c&3, d&3
   125  
   126  	switch pattern {
   127  	case simd.LLLL:
   128  		return x.ExportTestConcatSelectedConstant(simd.ExportTestCscImm4(a, b, c, d), x)
   129  	case simd.HHHH:
   130  		return y.ExportTestConcatSelectedConstant(simd.ExportTestCscImm4(a, b, c, d), y)
   131  	case simd.LLHH:
   132  		return x.ExportTestConcatSelectedConstant(simd.ExportTestCscImm4(a, b, c, d), y)
   133  	case simd.HHLL:
   134  		return y.ExportTestConcatSelectedConstant(simd.ExportTestCscImm4(a, b, c, d), x)
   135  
   136  	case simd.HLLL:
   137  		z := y.ExportTestConcatSelectedConstant(simd.ExportTestCscImm4(a, a, b, b), x)
   138  		return z.ExportTestConcatSelectedConstant(simd.ExportTestCscImm4(0, 2, c, d), x)
   139  	case simd.LHLL:
   140  		z := x.ExportTestConcatSelectedConstant(simd.ExportTestCscImm4(a, a, b, b), y)
   141  		return z.ExportTestConcatSelectedConstant(simd.ExportTestCscImm4(0, 2, c, d), x)
   142  
   143  	case simd.HLHH:
   144  		z := y.ExportTestConcatSelectedConstant(simd.ExportTestCscImm4(a, a, b, b), x)
   145  		return z.ExportTestConcatSelectedConstant(simd.ExportTestCscImm4(0, 2, c, d), y)
   146  	case simd.LHHH:
   147  		z := x.ExportTestConcatSelectedConstant(simd.ExportTestCscImm4(a, a, b, b), y)
   148  		return z.ExportTestConcatSelectedConstant(simd.ExportTestCscImm4(0, 2, c, d), y)
   149  
   150  	case simd.LLLH:
   151  		z := x.ExportTestConcatSelectedConstant(simd.ExportTestCscImm4(c, c, d, d), y)
   152  		return x.ExportTestConcatSelectedConstant(simd.ExportTestCscImm4(a, b, 0, 2), z)
   153  	case simd.LLHL:
   154  		z := y.ExportTestConcatSelectedConstant(simd.ExportTestCscImm4(c, c, d, d), x)
   155  		return x.ExportTestConcatSelectedConstant(simd.ExportTestCscImm4(a, b, 0, 2), z)
   156  	case simd.HHLH:
   157  		z := x.ExportTestConcatSelectedConstant(simd.ExportTestCscImm4(c, c, d, d), y)
   158  		return y.ExportTestConcatSelectedConstant(simd.ExportTestCscImm4(a, b, 0, 2), z)
   159  	case simd.HHHL:
   160  		z := y.ExportTestConcatSelectedConstant(simd.ExportTestCscImm4(c, c, d, d), x)
   161  		return y.ExportTestConcatSelectedConstant(simd.ExportTestCscImm4(a, b, 0, 2), z)
   162  
   163  	case simd.LHLH:
   164  		z := x.ExportTestConcatSelectedConstant(simd.ExportTestCscImm4(a, c, b, d), y)
   165  		return z.ExportTestConcatSelectedConstant(0b11_01_10_00 /* =simd.ExportTestCscImm4(0, 2, 1, 3) */, z)
   166  	case simd.HLHL:
   167  		z := x.ExportTestConcatSelectedConstant(simd.ExportTestCscImm4(b, d, a, c), y)
   168  		return z.ExportTestConcatSelectedConstant(0b01_11_00_10 /* =simd.ExportTestCscImm4(2, 0, 3, 1) */, z)
   169  	case simd.HLLH:
   170  		z := x.ExportTestConcatSelectedConstant(simd.ExportTestCscImm4(b, c, a, d), y)
   171  		return z.ExportTestConcatSelectedConstant(0b11_01_00_10 /* =simd.ExportTestCscImm4(2, 0, 1, 3) */, z)
   172  	case simd.LHHL:
   173  		z := x.ExportTestConcatSelectedConstant(simd.ExportTestCscImm4(a, d, b, c), y)
   174  		return z.ExportTestConcatSelectedConstant(0b01_11_10_00 /* =simd.ExportTestCscImm4(0, 2, 3, 1) */, z)
   175  	}
   176  	panic("missing case, switch should be exhaustive")
   177  }
   178  
   179  // select2x8x32Grouped returns a pair of selection of 4 elements in x and y,
   180  // numbered 0-7, where 0-3 are the four elements of x's two groups (lower and
   181  // upper 128 bits) and 4-7 are the four elements of y's two groups.
   182  
   183  func select2x8x32Grouped(x simd.Int32x8, a, b, c, d uint8, y simd.Int32x8) simd.Int32x8 {
   184  	// selections as being expressible in the ExportTestConcatSelectedConstant pattern,
   185  	// or not. Classification is by H and L, where H is a selection from 4-7
   186  	// and L is a selection from 0-3.
   187  	// simd.LLHH -> CSC(x,y, a, b, c&3, d&3)
   188  	// simd.HHLL -> CSC(y,x, a&3, b&3, c, d)
   189  	// simd.LLLL -> CSC(x,x, a, b, c, d)
   190  	// simd.HHHH -> CSC(y,y, a&3, b&3, c&3, d&3)
   191  
   192  	// simd.LLLH -> z = CSC(x, y, c, c, d&3, d&3); CSC(x, z, a, b, 0, 2)
   193  	// simd.LLHL -> z = CSC(x, y, c&3, c&3, d, d); CSC(x, z, a, b, 0, 2)
   194  	// simd.HHLH -> z = CSC(x, y, c, c, d&3, d&3); CSC(y, z, a&3, b&3, 0, 2)
   195  	// simd.HHHL -> z = CSC(x, y, c&3, c&3, d, d); CSC(y, z, a&3, b&3, 0, 2)
   196  
   197  	// simd.LHLL -> z = CSC(x, y, a, a, b&3, b&3); CSC(z, x, 0, 2, c, d)
   198  	// etc
   199  
   200  	// simd.LHLH -> z = CSC(x, y, a, c, b&3, d&3); CSC(z, z, 0, 2, 1, 3)
   201  	// simd.HLHL -> z = CSC(x, y, b, d, a&3, c&3); CSC(z, z, 2, 0, 3, 1)
   202  
   203  	pattern := a>>2 + (b&4)>>1 + (c & 4) + (d&4)<<1
   204  
   205  	a, b, c, d = a&3, b&3, c&3, d&3
   206  
   207  	switch pattern {
   208  	case simd.LLLL:
   209  		return x.ExportTestConcatSelectedConstantGrouped(simd.ExportTestCscImm4(a, b, c, d), x)
   210  	case simd.HHHH:
   211  		return y.ExportTestConcatSelectedConstantGrouped(simd.ExportTestCscImm4(a, b, c, d), y)
   212  	case simd.LLHH:
   213  		return x.ExportTestConcatSelectedConstantGrouped(simd.ExportTestCscImm4(a, b, c, d), y)
   214  	case simd.HHLL:
   215  		return y.ExportTestConcatSelectedConstantGrouped(simd.ExportTestCscImm4(a, b, c, d), x)
   216  
   217  	case simd.HLLL:
   218  		z := y.ExportTestConcatSelectedConstantGrouped(simd.ExportTestCscImm4(a, a, b, b), x)
   219  		return z.ExportTestConcatSelectedConstantGrouped(simd.ExportTestCscImm4(0, 2, c, d), x)
   220  	case simd.LHLL:
   221  		z := x.ExportTestConcatSelectedConstantGrouped(simd.ExportTestCscImm4(a, a, b, b), y)
   222  		return z.ExportTestConcatSelectedConstantGrouped(simd.ExportTestCscImm4(0, 2, c, d), x)
   223  
   224  	case simd.HLHH:
   225  		z := y.ExportTestConcatSelectedConstantGrouped(simd.ExportTestCscImm4(a, a, b, b), x)
   226  		return z.ExportTestConcatSelectedConstantGrouped(simd.ExportTestCscImm4(0, 2, c, d), y)
   227  	case simd.LHHH:
   228  		z := x.ExportTestConcatSelectedConstantGrouped(simd.ExportTestCscImm4(a, a, b, b), y)
   229  		return z.ExportTestConcatSelectedConstantGrouped(simd.ExportTestCscImm4(0, 2, c, d), y)
   230  
   231  	case simd.LLLH:
   232  		z := x.ExportTestConcatSelectedConstantGrouped(simd.ExportTestCscImm4(c, c, d, d), y)
   233  		return x.ExportTestConcatSelectedConstantGrouped(simd.ExportTestCscImm4(a, b, 0, 2), z)
   234  	case simd.LLHL:
   235  		z := y.ExportTestConcatSelectedConstantGrouped(simd.ExportTestCscImm4(c, c, d, d), x)
   236  		return x.ExportTestConcatSelectedConstantGrouped(simd.ExportTestCscImm4(a, b, 0, 2), z)
   237  	case simd.HHLH:
   238  		z := x.ExportTestConcatSelectedConstantGrouped(simd.ExportTestCscImm4(c, c, d, d), y)
   239  		return y.ExportTestConcatSelectedConstantGrouped(simd.ExportTestCscImm4(a, b, 0, 2), z)
   240  	case simd.HHHL:
   241  		z := y.ExportTestConcatSelectedConstantGrouped(simd.ExportTestCscImm4(c, c, d, d), x)
   242  		return y.ExportTestConcatSelectedConstantGrouped(simd.ExportTestCscImm4(a, b, 0, 2), z)
   243  
   244  	case simd.LHLH:
   245  		z := x.ExportTestConcatSelectedConstantGrouped(simd.ExportTestCscImm4(a, c, b, d), y)
   246  		return z.ExportTestConcatSelectedConstantGrouped(0b11_01_10_00 /* =simd.ExportTestCscImm4(0, 2, 1, 3) */, z)
   247  	case simd.HLHL:
   248  		z := x.ExportTestConcatSelectedConstantGrouped(simd.ExportTestCscImm4(b, d, a, c), y)
   249  		return z.ExportTestConcatSelectedConstantGrouped(0b01_11_00_10 /* =simd.ExportTestCscImm4(2, 0, 3, 1) */, z)
   250  	case simd.HLLH:
   251  		z := x.ExportTestConcatSelectedConstantGrouped(simd.ExportTestCscImm4(b, c, a, d), y)
   252  		return z.ExportTestConcatSelectedConstantGrouped(0b11_01_00_10 /* =simd.ExportTestCscImm4(2, 0, 1, 3) */, z)
   253  	case simd.LHHL:
   254  		z := x.ExportTestConcatSelectedConstantGrouped(simd.ExportTestCscImm4(a, d, b, c), y)
   255  		return z.ExportTestConcatSelectedConstantGrouped(0b01_11_10_00 /* =simd.ExportTestCscImm4(0, 2, 3, 1) */, z)
   256  	}
   257  	panic("missing case, switch should be exhaustive")
   258  }
   259  

View as plain text