Source file src/simd/internal/spec/loadstore.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  package spec
     6  
     7  // Most of these are top-level functions, not methods, because their first
     8  // argument is not a Vec.
     9  
    10  // BroadcastZ returns a vector with the input x assigned to all elements of the
    11  // result.
    12  //
    13  //specgen:name Broadcast{z}
    14  func BroadcastZ[E Elt, W Width](x E) (z Vec[E, W]) {
    15  	z = makeVec[E, W]()
    16  	for i := range z {
    17  		z[i] = x
    18  	}
    19  	return z
    20  }
    21  
    22  // LoadZ loads a slice into a vector. If len(s) is less than the number of
    23  // elements in the vector, it panics.
    24  //
    25  //specgen:name Load{z}
    26  func LoadZ[E Elt, W Width](s []E) (z Vec[E, W]) {
    27  	z = makeVec[E, W]()
    28  	_ = s[:z.len()]
    29  	copy(z, s)
    30  	return z
    31  }
    32  
    33  // LoadZArray loads an array into a vector.
    34  //
    35  //specgen:name Load{z}Array
    36  func LoadZArray[E Elt, W FixedWidth](x *Array[E, W]) (z Vec[E, W]) {
    37  	z = makeVec[E, W]()
    38  	if len(*x) != z.len() {
    39  		panic("bad array size")
    40  	}
    41  	copy(z, *x)
    42  	return z
    43  }
    44  
    45  // LoadZPart loads a slice into a vector and returns the vector and the number
    46  // of elements loaded from s. If len(s) is less than the number of elements in
    47  // the vector, the remaining vector elements will be zero-filled.
    48  //
    49  //specgen:name Load{z}Part
    50  func LoadZPart[E Elt, W Width](s []E) (z Vec[E, W], n int) {
    51  	z = makeVec[E, W]()
    52  	n = copy(z, s)
    53  	return z, n
    54  }
    55  
    56  // Store stores the elements of x into a slice. If len(s) is less than x.Len(),
    57  // it panics.
    58  func Store[E Elt, W Width](x Vec[E, W], s []E) {
    59  	_ = s[:x.len()]
    60  	copy(s, x)
    61  }
    62  
    63  // StoreArray stores the elements of x to an array.
    64  func StoreArray[E Elt, W FixedWidth](x Vec[E, W], y *Array[E, W]) {
    65  	if x.len() != len(*y) {
    66  		panic("bad array size")
    67  	}
    68  	copy(*y, x)
    69  }
    70  
    71  // StoreArrayMasked stores the masked elements of x to an array. It does not
    72  // modify elements of y that are false in the mask.
    73  //
    74  //specgen:require maskN=xN
    75  func StoreArrayMasked[E Elt, W FixedWidth, mE MaskElt](x Vec[E, W], y *Array[E, W], mask Vec[mE, W]) {
    76  	for i, elt := range x {
    77  		if mask[i] != 0 {
    78  			(*y)[i] = elt
    79  		}
    80  	}
    81  }
    82  
    83  // StorePart stores at most len(s) elements of x into s and returns the number
    84  // of elements stored.
    85  func StorePart[E Elt, W Width](x Vec[E, W], s []E) int {
    86  	return copy(s, x)
    87  }
    88  

View as plain text