Source file src/simd/archsimd/internal/simd_test/shift_128_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 && (amd64 || wasm || arm64)
     6  
     7  package simd_test
     8  
     9  import (
    10  	"runtime"
    11  	"simd/archsimd"
    12  	"testing"
    13  )
    14  
    15  func TestRotateAllLeft(t *testing.T) {
    16  	x := uint8(0x81)
    17  	if y := rotl(x, 1); y != 3 {
    18  		t.Errorf("Expected 3, got 0x%x", y)
    19  	}
    20  	if y := rotl(x, 7); y != 0xc0 {
    21  		t.Errorf("Expected 0xc0, got 0x%x", y)
    22  	}
    23  	if y := rotr(x, 4); y != 0x18 {
    24  		t.Errorf("Expected 0x18, got 0x%x", y)
    25  	}
    26  
    27  	for i := uint64(0); i < 65; i++ {
    28  		testUint64x2Unary(t, curry2(archsimd.Uint64x2.RotateAllLeft, i), rotlOfSlice[uint64](i))
    29  		testUint32x4Unary(t, curry2(archsimd.Uint32x4.RotateAllLeft, i), rotlOfSlice[uint32](i))
    30  		//		testUint16x8Unary(t, curry2(archsimd.Uint16x8.RotateAllLeft, i), rotlOfSlice[uint16](i))
    31  		//		testUint8x16Unary(t, curry2(archsimd.Uint8x16.RotateAllLeft, i), rotlOfSlice[uint8](i))
    32  	}
    33  }
    34  
    35  func TestRotateAllRight(t *testing.T) {
    36  	x := uint8(0x81)
    37  	if y := rotr(x, 1); y != 0xc0 {
    38  		t.Errorf("Expected 0xc0, got 0x%x", y)
    39  	}
    40  	if y := rotr(x, 7); y != 3 {
    41  		t.Errorf("Expected 3, got 0x%x", y)
    42  	}
    43  	if y := rotr(x, 4); y != 0x18 {
    44  		t.Errorf("Expected 0x18, got 0x%x", y)
    45  	}
    46  
    47  	for i := uint64(0); i < 65; i++ {
    48  		testUint64x2Unary(t, curry2(archsimd.Uint64x2.RotateAllRight, i), rotrOfSlice[uint64](i))
    49  		testUint32x4Unary(t, curry2(archsimd.Uint32x4.RotateAllRight, i), rotrOfSlice[uint32](i))
    50  		//		testUint16x8Unary(t, curry2(archsimd.Uint16x8.RotateAllLeft, i), rotlOfSlice[uint16](i))
    51  		//		testUint8x16Unary(t, curry2(archsimd.Uint8x16.RotateAllLeft, i), rotlOfSlice[uint8](i))
    52  	}
    53  }
    54  
    55  func TestShiftAll(t *testing.T) {
    56  	// Test both const and non-const shifts.
    57  	// Test both regular and over-shifts.
    58  
    59  	hide := hideConst[uint64]
    60  
    61  	// ShiftAllLeft
    62  
    63  	testInt32x4Unary(t,
    64  		func(x archsimd.Int32x4) archsimd.Int32x4 { return x.ShiftAllLeft(2) },
    65  		map1(func(x int32) int32 { return x << 2 }))
    66  	testInt32x4Unary(t,
    67  		func(x archsimd.Int32x4) archsimd.Int32x4 { return x.ShiftAllLeft(hide(2)) },
    68  		map1(func(x int32) int32 { return x << hide(2) }))
    69  
    70  	// Ironically, we have to hide the constant in the want function so the
    71  	// compiler doesn't complain about a silly shift.
    72  	testInt32x4Unary(t,
    73  		func(x archsimd.Int32x4) archsimd.Int32x4 { return x.ShiftAllLeft(0x1000) },
    74  		map1(func(x int32) int32 { return x << hide(0x1000) }))
    75  	testInt32x4Unary(t,
    76  		func(x archsimd.Int32x4) archsimd.Int32x4 { return x.ShiftAllLeft(hide(0x1000)) },
    77  		map1(func(x int32) int32 { return x << hide(0x1000) }))
    78  
    79  	testInt16x8ShiftAll(t, archsimd.Int16x8.ShiftAllLeft, shiftAllLeftSlice[int16])
    80  	testInt32x4ShiftAll(t, archsimd.Int32x4.ShiftAllLeft, shiftAllLeftSlice[int32])
    81  	testInt64x2ShiftAll(t, archsimd.Int64x2.ShiftAllLeft, shiftAllLeftSlice[int64])
    82  	testUint16x8ShiftAll(t, archsimd.Uint16x8.ShiftAllLeft, shiftAllLeftSlice[uint16])
    83  	testUint32x4ShiftAll(t, archsimd.Uint32x4.ShiftAllLeft, shiftAllLeftSlice[uint32])
    84  	testUint64x2ShiftAll(t, archsimd.Uint64x2.ShiftAllLeft, shiftAllLeftSlice[uint64])
    85  
    86  	// Signed ShiftAllRight
    87  
    88  	testInt32x4Unary(t,
    89  		func(x archsimd.Int32x4) archsimd.Int32x4 { return x.ShiftAllRight(2) },
    90  		map1(func(x int32) int32 { return x >> 2 }))
    91  	testInt32x4Unary(t,
    92  		func(x archsimd.Int32x4) archsimd.Int32x4 { return x.ShiftAllRight(hide(2)) },
    93  		map1(func(x int32) int32 { return x >> hide(2) }))
    94  
    95  	testInt32x4Unary(t,
    96  		func(x archsimd.Int32x4) archsimd.Int32x4 { return x.ShiftAllRight(0x1000) },
    97  		map1(func(x int32) int32 { return x >> hide(0x1000) }))
    98  	testInt32x4Unary(t,
    99  		func(x archsimd.Int32x4) archsimd.Int32x4 { return x.ShiftAllRight(hide(0x1000)) },
   100  		map1(func(x int32) int32 { return x >> hide(0x1000) }))
   101  
   102  	testInt16x8ShiftAll(t, archsimd.Int16x8.ShiftAllRight, shiftAllRightSlice[int16])
   103  	testInt32x4ShiftAll(t, archsimd.Int32x4.ShiftAllRight, shiftAllRightSlice[int32])
   104  	if runtime.GOARCH != "amd64" || archsimd.X86.AVX512() {
   105  		testInt64x2ShiftAll(t, archsimd.Int64x2.ShiftAllRight, shiftAllRightSlice[int64])
   106  	}
   107  
   108  	// Unsigned ShiftAllRight
   109  
   110  	testUint32x4Unary(t,
   111  		func(x archsimd.Uint32x4) archsimd.Uint32x4 { return x.ShiftAllRight(2) },
   112  		map1(func(x uint32) uint32 { return x >> 2 }))
   113  	testUint32x4Unary(t,
   114  		func(x archsimd.Uint32x4) archsimd.Uint32x4 { return x.ShiftAllRight(hide(2)) },
   115  		map1(func(x uint32) uint32 { return x >> hide(2) }))
   116  
   117  	testUint32x4Unary(t,
   118  		func(x archsimd.Uint32x4) archsimd.Uint32x4 { return x.ShiftAllRight(0x1000) },
   119  		map1(func(x uint32) uint32 { return x >> hide(0x1000) }))
   120  	testUint32x4Unary(t,
   121  		func(x archsimd.Uint32x4) archsimd.Uint32x4 { return x.ShiftAllRight(hide(0x1000)) },
   122  		map1(func(x uint32) uint32 { return x >> hide(0x1000) }))
   123  
   124  	testUint16x8ShiftAll(t, archsimd.Uint16x8.ShiftAllRight, shiftAllRightSlice[uint16])
   125  	testUint32x4ShiftAll(t, archsimd.Uint32x4.ShiftAllRight, shiftAllRightSlice[uint32])
   126  	testUint64x2ShiftAll(t, archsimd.Uint64x2.ShiftAllRight, shiftAllRightSlice[uint64])
   127  }
   128  
   129  var testShiftAllAmts = []uint64{0, 1, 3, 7, 15, 31, 63, 128, 1024}
   130  

View as plain text