Source file src/simd/tofrom_arch_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 || arm64 || wasm)
     6  
     7  package simd_test
     8  
     9  import (
    10  	"fmt"
    11  	"simd"
    12  	"simd/archsimd"
    13  	"strings"
    14  	"testing"
    15  )
    16  
    17  // These platforms all support Int32x4 and Mask32x4.
    18  
    19  func roundTripInt32s(x simd.Int32s) simd.Int32s {
    20  	if a, ok := x.ToArch().(archsimd.Int32x4); ok {
    21  		return simd.Int32sFromArch(a)
    22  	}
    23  	return x
    24  }
    25  
    26  func roundTripMask32s(m simd.Mask32s) simd.Mask32s {
    27  	if a, ok := m.ToArch().(archsimd.Mask32x4); ok {
    28  		return simd.Mask32sFromArch(a)
    29  	}
    30  	return m
    31  }
    32  
    33  func TestToFromArch128(t *testing.T) {
    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  	roundTripInt32s(x).Store(got)
    41  	for i := range got {
    42  		if got[i] != s[i] {
    43  			t.Fatalf("roundTripInt32s lane %d = %d, want %d", i, got[i], s[i])
    44  		}
    45  	}
    46  
    47  	zero := simd.BroadcastInt32s(0)
    48  	m := x.Greater(zero)
    49  	if roundTripMask32s(m).String() != m.String() {
    50  		t.Fatalf("roundTripMask32s mismatch: got %s, want %s", roundTripMask32s(m), m)
    51  	}
    52  }
    53  
    54  func TestFromArchEmulatedPanic(t *testing.T) {
    55  	if !simd.Emulated() {
    56  		t.Skip("simd is not emulated; GODEBUG=simd=0 forces emulation")
    57  	}
    58  
    59  	callFromArch128 := func() simd.Int32s {
    60  		return simd.Int32sFromArch(archsimd.Int32x4{})
    61  	}
    62  	expectFromArchPanic(t, func() { _ = callFromArch128() },
    63  		"simd.Int32sFromArch: cannot convert from archsimd.Int32x4 when simd.Emulated() is true")
    64  }
    65  
    66  func expectFromArchPanic(t *testing.T, fn func(), want string) {
    67  	t.Helper()
    68  	defer func() {
    69  		r := recover()
    70  		if r == nil {
    71  			t.Fatalf("expected panic %q, did not panic", want)
    72  		}
    73  		got := fmt.Sprint(r)
    74  		if strings.Contains(got, "bridge.") {
    75  			t.Errorf("panic %q leaks internal bridge package", got)
    76  		}
    77  		if got != want {
    78  			t.Errorf("panic = %q, want %q", got, want)
    79  		}
    80  	}()
    81  	fn()
    82  }
    83  

View as plain text