Source file src/simd/internal/spec/conditional.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 Conditional logic
     6  
     7  package spec
     8  
     9  // Masked returns a vector with elements from x where mask is true, and zero
    10  // elsewhere.
    11  //
    12  //	z[i] = if mask[i] { x[i] } else { 0 }
    13  //
    14  //specgen:require maskN=xN
    15  func Masked[E Elt, W Width, mE MaskElt](x Vec[E, W], mask Vec[mE, W]) (z Vec[E, W]) {
    16  	z = makeVec[E, W]()
    17  	for i, xi := range x {
    18  		if mask[i] != 0 {
    19  			z[i] = xi
    20  		}
    21  	}
    22  	return z
    23  }
    24  
    25  // IfElse returns a vector with elements from x where mask is true, and y where
    26  // mask is false.
    27  //
    28  //	z[i] = if mask[i] { x[i] } else { y[i] }
    29  //
    30  //specgen:require maskN=xN
    31  func IfElse[E Elt, W Width, mE MaskElt](x Vec[E, W], mask Vec[mE, W], y Vec[E, W]) (z Vec[E, W]) {
    32  	z = makeVec[E, W]()
    33  	for i, xi := range x {
    34  		if mask[i] != 0 {
    35  			z[i] = xi
    36  		} else {
    37  			z[i] = y[i]
    38  		}
    39  	}
    40  	return z
    41  }
    42  

View as plain text