Source file src/simd/tofrom_amd64_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.simdstable && amd64
     6  
     7  package simd_test
     8  
     9  import (
    10  	"fmt"
    11  	"internal/testenv"
    12  	"simd"
    13  	"simd/archsimd"
    14  	"testing"
    15  )
    16  
    17  func doubleViaArchAMD64(x simd.Int32s) simd.Int32s {
    18  	switch a := x.ToArch().(type) {
    19  	case archsimd.Int32x4:
    20  		return simd.Int32sFromArch(a.Add(a))
    21  	case archsimd.Int32x8:
    22  		return simd.Int32sFromArch(a.Add(a))
    23  	case archsimd.Int32x16:
    24  		return simd.Int32sFromArch(a.Add(a))
    25  	default:
    26  		panic("unexpected ToArch type")
    27  	}
    28  }
    29  
    30  func TestToFromArchAMD64(t *testing.T) {
    31  	if simd.Emulated() {
    32  		t.Skip("Emulated() is true")
    33  	}
    34  	s := make([]int32, 16)
    35  	for i := range s {
    36  		s[i] = int32(i + 1)
    37  	}
    38  	x := simd.LoadInt32s(s)
    39  	got := make([]int32, x.Len())
    40  	doubleViaArchAMD64(x).Store(got)
    41  	for i := range got {
    42  		if got[i] != 2*s[i] {
    43  			t.Fatalf("lane %d = %d, want %d", i, got[i], 2*s[i])
    44  		}
    45  	}
    46  
    47  	// Without optimization, the conversions to and from the arch types
    48  	// allocate.
    49  	testenv.SkipIfOptimizationOff(t)
    50  	var sink simd.Int32s
    51  	allocs := testing.AllocsPerRun(100, func() {
    52  		sink = doubleViaArchAMD64(x)
    53  	})
    54  	_ = sink
    55  	if allocs != 0 {
    56  		t.Errorf("doubleViaArchAMD64 allocated %v times per run, want 0", allocs)
    57  	}
    58  }
    59  
    60  func fromArch128(v archsimd.Int32x4) simd.Int32s { return simd.Int32sFromArch(v) }
    61  func fromArch256(v archsimd.Int32x8) simd.Int32s { return simd.Int32sFromArch(v) }
    62  
    63  func TestFromArchWrongWidthPanic(t *testing.T) {
    64  	if simd.Emulated() {
    65  		t.Skip("Emulated() is true; covered by TestFromArchEmulatedPanic")
    66  	}
    67  	bits := simd.VectorBits()
    68  	if bits == 128 {
    69  		want := fmt.Sprintf("simd.Int32sFromArch: cannot convert from archsimd.Int32x8 when simd.VectorBits() is %d", bits)
    70  		expectFromArchPanic(t, func() { _ = fromArch256(archsimd.Int32x8{}) }, want)
    71  	} else {
    72  		want := fmt.Sprintf("simd.Int32sFromArch: cannot convert from archsimd.Int32x4 when simd.VectorBits() is %d", bits)
    73  		expectFromArchPanic(t, func() { _ = fromArch128(archsimd.Int32x4{}) }, want)
    74  	}
    75  }
    76  

View as plain text