Source file src/simd/internal/spec/basic_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  package spec
     6  
     7  import (
     8  	"fmt"
     9  	"slices"
    10  	"testing"
    11  )
    12  
    13  func vecOf[E EltOrMask, W Width](xs ...E) Vec[E, W] {
    14  	l := lanes[E, W]()
    15  	if len(xs) != l {
    16  		panic(fmt.Sprintf("got %d elements, want %d", len(xs), l))
    17  	}
    18  	return xs
    19  }
    20  
    21  func TestPreserveTNxL(t *testing.T) {
    22  	x := vecOf[int32, Width128](1, 2, 3, 4)
    23  	y := vecOf[int32, Width128](2, 3, 4, 5)
    24  	want := vecOf[int32, Width128](3, 5, 7, 9)
    25  	z := Add(x, y)
    26  	if !slices.Equal(z, want) {
    27  		t.Fatalf("got %v, want %v", z, want)
    28  	}
    29  }
    30  
    31  func TestPreserveL(t *testing.T) {
    32  	// This operation changes T and N
    33  	x := vecOf[int64, Width256](1, 2, 3, 4)
    34  	want := vecOf[float32, Width128](1, 2, 3, 4)
    35  	z := ConvertToZ[int64, Width256, float32, Width128](x)
    36  	if !slices.Equal(z, want) {
    37  		t.Fatalf("got %v, want %v", z, want)
    38  	}
    39  }
    40  
    41  func TestPreserveNxL(t *testing.T) {
    42  	// This operation changes T
    43  	x := vecOf[int32, Width128](1, 2, 3, 4)
    44  	want := vecOf[float32, Width128](1, 2, 3, 4)
    45  	z := ConvertToZ[int32, Width128, float32, Width128](x)
    46  	if !slices.Equal(z, want) {
    47  		t.Fatalf("got %v, want %v", z, want)
    48  	}
    49  }
    50  
    51  func TestWidthRounding(t *testing.T) {
    52  	// The "natural" result of this is only 64 bits, so it gets rounded up to
    53  	// 128 bits.
    54  	x := vecOf[int64, Width128](1, 2)
    55  	want := vecOf[float32, Width128](1, 2, 0, 0)
    56  	z := ConvertToZ[int64, Width128, float32, Width128](x)
    57  	if !slices.Equal(z, want) {
    58  		t.Fatalf("got %v, want %v", z, want)
    59  	}
    60  }
    61  
    62  func TestPreserveW(t *testing.T) {
    63  	x := vecOf[int32, Width128](1, 2, 3, 4)
    64  	y := vecOf[int32, Width128](2, 3, 4, 5)
    65  	want := vecOf[int64, Width128](1*2+2*3, 3*4+4*5)
    66  	z := DotProductPairs[int32, Width128, int64](x, y)
    67  	if !slices.Equal(z, want) {
    68  		t.Fatalf("got %v, want %v", z, want)
    69  	}
    70  }
    71  

View as plain text