Source file src/simd/internal/spec/basic.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  //specgen:category Basics
     6  
     7  package spec
     8  
     9  import (
    10  	"fmt"
    11  	"strings"
    12  )
    13  
    14  // Len returns the number of elements in x.
    15  func Len[E EltOrMask, W Width](x Vec[E, W]) int {
    16  	return x.len()
    17  }
    18  
    19  // GetElem returns the i'th element x. If i >= {{lanes .x "x"}}, it panics.
    20  //
    21  //	z = x[i]
    22  func GetElem[E Elt, W Width](x Vec[E, W], i uint8) (z E) {
    23  	return x[i]
    24  }
    25  
    26  // SetElem returns x with its i'th element set to y. If i >= {{lanes .x "x"}}, it panics.
    27  //
    28  //	z = x
    29  //	z[i] = y
    30  func SetElem[E Elt, W Width](x Vec[E, W], i uint8, y E) (z Vec[E, W]) {
    31  	z = makeVec[E, W]()
    32  	copy(z, x)
    33  	z[i] = y
    34  	return z
    35  }
    36  
    37  // String returns x in the form "{x[0],x[1],...}".
    38  func String[E Nums, W Width](x Vec[E, W]) string {
    39  	var buf strings.Builder
    40  	buf.WriteByte('{')
    41  	for i, val := range x {
    42  		if i > 0 {
    43  			buf.WriteByte(',')
    44  		}
    45  		fmt.Fprint(&buf, val)
    46  	}
    47  	buf.WriteByte('}')
    48  	return buf.String()
    49  }
    50  
    51  // StringMask returns x in the form "{x[0],x[1],...}", where 0 means false and 1
    52  // means true.
    53  //
    54  //specgen:name String
    55  func StringMask[E MaskElt, W Width](x Vec[E, W]) string {
    56  	var buf strings.Builder
    57  	buf.WriteByte('{')
    58  	for i, val := range x {
    59  		if i > 0 {
    60  			buf.WriteByte(',')
    61  		}
    62  		if val == 0 {
    63  			buf.WriteByte('0')
    64  		} else {
    65  			buf.WriteByte('1')
    66  		}
    67  	}
    68  	buf.WriteByte('}')
    69  	return buf.String()
    70  }
    71  

View as plain text