Source file src/simd/testdata/iface/iface.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
     6  
     7  package iface
     8  
     9  import (
    10  	"simd"
    11  )
    12  
    13  // A SIMD-dependent type alias
    14  type MyInt8s = simd.Int8s
    15  
    16  func Generic[T haslen](x int) int {
    17  	var v T
    18  	return x + v.Len()
    19  }
    20  
    21  // VL = Generic[MyInt8s](1) doesn't currently work.
    22  // TODO: automatically transform those initializers into what is done here instead.
    23  var VL int
    24  
    25  func init() {
    26  	VL = Generic[MyInt8s](1)
    27  }
    28  
    29  // A struct dependent on SIMD
    30  type VectorC struct {
    31  	Field simd.Float32s
    32  }
    33  
    34  type Ftype func(x any) any
    35  
    36  var Fvar Ftype
    37  
    38  // A dependent function with a dependent signature
    39  func (v *VectorC) MethodOfSimd() bool {
    40  	return false
    41  }
    42  
    43  func (v VectorC) Data() simd.Float32s {
    44  	return v.Field
    45  }
    46  
    47  func (v VectorC) Foo(x VectorC) VectorC {
    48  	return VectorC{Field: v.Field.Add(x.Field)}
    49  }
    50  
    51  func (v VectorC) Bar(x VectorC) VectorC {
    52  	return VectorC{Field: v.Field.Add(x.Field)}
    53  }
    54  
    55  type Vint interface {
    56  	MethodOfSimd() bool
    57  }
    58  
    59  type haslen interface {
    60  	Len() int
    61  }
    62  
    63  type HasFoo[T any] interface {
    64  	Foo(x T) T
    65  }
    66  
    67  type HasBar interface {
    68  	Bar(x VectorC) VectorC
    69  }
    70  
    71  //go:noinline
    72  func MakeHasFoo[T HasFoo[T]](v T) HasFoo[T] {
    73  	return v
    74  }
    75  
    76  func MakeHasBar(v VectorC) HasBar {
    77  	return v
    78  }
    79  
    80  func VC(x simd.Float32s) VectorC {
    81  	return VectorC{x}
    82  }
    83  
    84  type EmbedBar struct {
    85  	HasBar
    86  }
    87  
    88  type EmbedFoo[T HasFoo[T]] struct {
    89  	HasFoo[T]
    90  }
    91  
    92  func MakeHasEmbedFoo[T HasFoo[T]](v T) EmbedFoo[T] {
    93  	return EmbedFoo[T]{MakeHasFoo[T](v)}
    94  }
    95  
    96  //go:noinline
    97  func MakeHasEmbedBar(v VectorC) EmbedBar {
    98  	return EmbedBar{MakeHasBar(v)}
    99  }
   100  
   101  type HasQux[T any] interface {
   102  	Qux(x T) HasFoo[T]
   103  }
   104  
   105  type Q struct {
   106  	q *Q
   107  }
   108  
   109  func (q *Q) Qux(v VectorC) HasFoo[VectorC] {
   110  	return v
   111  }
   112  

View as plain text