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

View as plain text