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

View as plain text