Source file src/simd/archsimd/internal/simd_test/compare_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 || wasm || arm64)
     6  
     7  package simd_test
     8  
     9  import (
    10  	"simd/archsimd"
    11  	"testing"
    12  )
    13  
    14  func TestLess(t *testing.T) {
    15  	testFloat32x4Compare(t, archsimd.Float32x4.Less, lessSlice[float32])
    16  	testFloat64x2Compare(t, archsimd.Float64x2.Less, lessSlice[float64])
    17  
    18  	testInt16x8Compare(t, archsimd.Int16x8.Less, lessSlice[int16])
    19  	testInt32x4Compare(t, archsimd.Int32x4.Less, lessSlice[int32])
    20  	testInt64x2Compare(t, archsimd.Int64x2.Less, lessSlice[int64])
    21  	testInt8x16Compare(t, archsimd.Int8x16.Less, lessSlice[int8])
    22  
    23  	testUint16x8Compare(t, archsimd.Uint16x8.Less, lessSlice[uint16])
    24  	testUint32x4Compare(t, archsimd.Uint32x4.Less, lessSlice[uint32])
    25  	testUint64x2Compare(t, archsimd.Uint64x2.Less, lessSlice[uint64])
    26  	testUint8x16Compare(t, archsimd.Uint8x16.Less, lessSlice[uint8])
    27  }
    28  
    29  func TestLessEqual(t *testing.T) {
    30  	testFloat32x4Compare(t, archsimd.Float32x4.LessEqual, lessEqualSlice[float32])
    31  	testFloat64x2Compare(t, archsimd.Float64x2.LessEqual, lessEqualSlice[float64])
    32  
    33  	testInt16x8Compare(t, archsimd.Int16x8.LessEqual, lessEqualSlice[int16])
    34  	testInt32x4Compare(t, archsimd.Int32x4.LessEqual, lessEqualSlice[int32])
    35  	testInt64x2Compare(t, archsimd.Int64x2.LessEqual, lessEqualSlice[int64])
    36  	testInt8x16Compare(t, archsimd.Int8x16.LessEqual, lessEqualSlice[int8])
    37  
    38  	testUint16x8Compare(t, archsimd.Uint16x8.LessEqual, lessEqualSlice[uint16])
    39  	testUint32x4Compare(t, archsimd.Uint32x4.LessEqual, lessEqualSlice[uint32])
    40  	testUint64x2Compare(t, archsimd.Uint64x2.LessEqual, lessEqualSlice[uint64])
    41  	testUint8x16Compare(t, archsimd.Uint8x16.LessEqual, lessEqualSlice[uint8])
    42  }
    43  
    44  func TestGreater(t *testing.T) {
    45  	testFloat32x4Compare(t, archsimd.Float32x4.Greater, greaterSlice[float32])
    46  	testFloat64x2Compare(t, archsimd.Float64x2.Greater, greaterSlice[float64])
    47  
    48  	testInt16x8Compare(t, archsimd.Int16x8.Greater, greaterSlice[int16])
    49  	testInt32x4Compare(t, archsimd.Int32x4.Greater, greaterSlice[int32])
    50  	testInt64x2Compare(t, archsimd.Int64x2.Greater, greaterSlice[int64])
    51  	testInt8x16Compare(t, archsimd.Int8x16.Greater, greaterSlice[int8])
    52  
    53  	testUint16x8Compare(t, archsimd.Uint16x8.Greater, greaterSlice[uint16])
    54  	testUint32x4Compare(t, archsimd.Uint32x4.Greater, greaterSlice[uint32])
    55  	testUint64x2Compare(t, archsimd.Uint64x2.Greater, greaterSlice[uint64])
    56  	testUint8x16Compare(t, archsimd.Uint8x16.Greater, greaterSlice[uint8])
    57  }
    58  
    59  func TestGreaterEqual(t *testing.T) {
    60  	testFloat32x4Compare(t, archsimd.Float32x4.GreaterEqual, greaterEqualSlice[float32])
    61  	testFloat64x2Compare(t, archsimd.Float64x2.GreaterEqual, greaterEqualSlice[float64])
    62  
    63  	testInt16x8Compare(t, archsimd.Int16x8.GreaterEqual, greaterEqualSlice[int16])
    64  	testInt32x4Compare(t, archsimd.Int32x4.GreaterEqual, greaterEqualSlice[int32])
    65  	testInt64x2Compare(t, archsimd.Int64x2.GreaterEqual, greaterEqualSlice[int64])
    66  	testInt8x16Compare(t, archsimd.Int8x16.GreaterEqual, greaterEqualSlice[int8])
    67  
    68  	testUint16x8Compare(t, archsimd.Uint16x8.GreaterEqual, greaterEqualSlice[uint16])
    69  	testUint32x4Compare(t, archsimd.Uint32x4.GreaterEqual, greaterEqualSlice[uint32])
    70  	testUint64x2Compare(t, archsimd.Uint64x2.GreaterEqual, greaterEqualSlice[uint64])
    71  	testUint8x16Compare(t, archsimd.Uint8x16.GreaterEqual, greaterEqualSlice[uint8])
    72  }
    73  
    74  func TestEqual(t *testing.T) {
    75  	testFloat32x4Compare(t, archsimd.Float32x4.Equal, equalSlice[float32])
    76  	testFloat64x2Compare(t, archsimd.Float64x2.Equal, equalSlice[float64])
    77  
    78  	testInt16x8Compare(t, archsimd.Int16x8.Equal, equalSlice[int16])
    79  	testInt32x4Compare(t, archsimd.Int32x4.Equal, equalSlice[int32])
    80  	testInt64x2Compare(t, archsimd.Int64x2.Equal, equalSlice[int64])
    81  	testInt8x16Compare(t, archsimd.Int8x16.Equal, equalSlice[int8])
    82  
    83  	testUint16x8Compare(t, archsimd.Uint16x8.Equal, equalSlice[uint16])
    84  	testUint32x4Compare(t, archsimd.Uint32x4.Equal, equalSlice[uint32])
    85  	testUint64x2Compare(t, archsimd.Uint64x2.Equal, equalSlice[uint64])
    86  	testUint8x16Compare(t, archsimd.Uint8x16.Equal, equalSlice[uint8])
    87  }
    88  
    89  func TestNotEqual(t *testing.T) {
    90  	testFloat32x4Compare(t, archsimd.Float32x4.NotEqual, notEqualSlice[float32])
    91  	testFloat64x2Compare(t, archsimd.Float64x2.NotEqual, notEqualSlice[float64])
    92  
    93  	testInt16x8Compare(t, archsimd.Int16x8.NotEqual, notEqualSlice[int16])
    94  	testInt32x4Compare(t, archsimd.Int32x4.NotEqual, notEqualSlice[int32])
    95  	testInt64x2Compare(t, archsimd.Int64x2.NotEqual, notEqualSlice[int64])
    96  	testInt8x16Compare(t, archsimd.Int8x16.NotEqual, notEqualSlice[int8])
    97  
    98  	testUint16x8Compare(t, archsimd.Uint16x8.NotEqual, notEqualSlice[uint16])
    99  	testUint32x4Compare(t, archsimd.Uint32x4.NotEqual, notEqualSlice[uint32])
   100  	testUint64x2Compare(t, archsimd.Uint64x2.NotEqual, notEqualSlice[uint64])
   101  	testUint8x16Compare(t, archsimd.Uint8x16.NotEqual, notEqualSlice[uint8])
   102  }
   103  
   104  // Not yet implemented for WASM
   105  // func TestIsNaN(t *testing.T) {
   106  // 	testFloat32x4UnaryCompare(t, archsimd.Float32x4.IsNaN, isNaNSlice[float32])
   107  // 	testFloat64x2UnaryCompare(t, archsimd.Float64x2.IsNaN, isNaNSlice[float64])
   108  
   109  // 	// Test x.IsNaN().Or(y.IsNaN()), which is optimized to VCMPP(S|D) $3, x, y.
   110  // 	want32 := mapCompare(func(x, y float32) bool { return x != x || y != y })
   111  // 	want64 := mapCompare(func(x, y float64) bool { return x != x || y != y })
   112  // 	testFloat32x4Compare(t,
   113  // 		func(x, y archsimd.Float32x4) archsimd.Mask32x4 {
   114  // 			return x.IsNaN().Or(y.IsNaN())
   115  // 		}, want32)
   116  // 	testFloat64x2Compare(t,
   117  // 		func(x, y archsimd.Float64x2) archsimd.Mask64x2 {
   118  // 			return x.IsNaN().Or(y.IsNaN())
   119  // 		}, want64)
   120  // }
   121  
   122  func TestMasked(t *testing.T) {
   123  	// Test Masked for Int8x16
   124  	forSlicePair(t, int8s, 16, func(x, y []int8) bool {
   125  		t.Helper()
   126  		a := archsimd.LoadInt8x16(x)
   127  		mask := archsimd.LoadInt8x16(y).Greater(archsimd.Int8x16{}) // mask: y > 0
   128  		g := make([]int8, 16)
   129  		a.Masked(mask).Store(g)
   130  		w := make([]int8, 16)
   131  		for i := range w {
   132  			if y[i] > 0 {
   133  				w[i] = x[i]
   134  			}
   135  		}
   136  		return checkSlicesLogInput(t, g, w, 0.0, func() { t.Helper(); t.Logf("x=%v, y=%v", x, y) })
   137  	})
   138  
   139  	// Test Masked for Float64x2
   140  	forSlicePair(t, float64s, 2, func(x, y []float64) bool {
   141  		t.Helper()
   142  		a := archsimd.LoadFloat64x2(x)
   143  		mask := archsimd.LoadFloat64x2(y).Greater(archsimd.Float64x2{}) // mask: y > 0
   144  		g := make([]float64, 2)
   145  		a.Masked(mask).Store(g)
   146  		w := make([]float64, 2)
   147  		for i := range w {
   148  			if y[i] > 0 {
   149  				w[i] = x[i]
   150  			}
   151  		}
   152  		return checkSlicesLogInput(t, g, w, 0.0, func() { t.Helper(); t.Logf("x=%v, y=%v", x, y) })
   153  	})
   154  }
   155  
   156  func TestIfElse(t *testing.T) {
   157  	// Test Merge for Int8x16
   158  	forSliceTriple(t, int8s, 16, func(x, y, m []int8) bool {
   159  		t.Helper()
   160  		a := archsimd.LoadInt8x16(x)
   161  		b := archsimd.LoadInt8x16(y)
   162  		mask := archsimd.LoadInt8x16(m).Greater(archsimd.Int8x16{}) // mask: m > 0
   163  		g := make([]int8, 16)
   164  		a.IfElse(mask, b).Store(g)
   165  		w := make([]int8, 16)
   166  		for i := range w {
   167  			if m[i] > 0 {
   168  				w[i] = x[i]
   169  			} else {
   170  				w[i] = y[i]
   171  			}
   172  		}
   173  		return checkSlicesLogInput(t, g, w, 0.0, func() { t.Helper(); t.Logf("x=%v, y=%v, m=%v", x, y, m) })
   174  	})
   175  
   176  	// Test Merge for Float32x4
   177  	forSliceTriple(t, float32s, 4, func(x, y, m []float32) bool {
   178  		t.Helper()
   179  		a := archsimd.LoadFloat32x4(x)
   180  		b := archsimd.LoadFloat32x4(y)
   181  		mask := archsimd.LoadFloat32x4(m).Greater(archsimd.Float32x4{}) // mask: m > 0
   182  		g := make([]float32, 4)
   183  		a.IfElse(mask, b).Store(g)
   184  		w := make([]float32, 4)
   185  		for i := range w {
   186  			if m[i] > 0 {
   187  				w[i] = x[i]
   188  			} else {
   189  				w[i] = y[i]
   190  			}
   191  		}
   192  		return checkSlicesLogInput(t, g, w, 0.0, func() { t.Helper(); t.Logf("x=%v, y=%v, m=%v", x, y, m) })
   193  	})
   194  }
   195  

View as plain text