Source file src/simd/internal/spec/masks.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 Mask
     6  
     7  package spec
     8  
     9  // UintN represents a Go uintN type. An argument x of type UintN introduces a
    10  // constraint variable named xN that must be resolved to the bit width (8, 16,
    11  // 32, or 64). Widths smaller than 8 are rounded up to 8. Widths larger than 64
    12  // are not allowed.
    13  //
    14  // This is used by mask operations that convert between bits in a uintN type and
    15  // elements in a mask.
    16  //
    17  // This type is known to specgen.
    18  type UintN uint64
    19  
    20  // MaskFromBits constructs a mask from a bitmap value. If bit i of y is set,
    21  // then mask element i of the result is set.
    22  //
    23  //specgen:name {z}FromBits
    24  //specgen:require x=uint{zL}
    25  func MaskFromBits[E MaskElt, W FixedWidth](x UintN) (z Vec[E, W]) {
    26  	z = makeVec[E, W]()
    27  	for i := range z {
    28  		if x&(1<<i) != 0 {
    29  			z[i] = 1
    30  		}
    31  	}
    32  	return z
    33  }
    34  
    35  // MaskToBits constructs a bitmap from mask x, where bit i is set if mask
    36  // element i is set.
    37  //
    38  //specgen:name ToBits
    39  //specgen:require z=uint{xL}
    40  func MaskToBits[E MaskElt, W FixedWidth](x Vec[E, W]) (z UintN) {
    41  	for i, elt := range x {
    42  		if elt != 0 {
    43  			z |= 1 << i
    44  		}
    45  	}
    46  	return z
    47  }
    48  
    49  // MaskToZ converts the mask to a vector, where element i is set to ^0 (all bits
    50  // set, e.g., -1) if mask element i is "true".
    51  //
    52  //specgen:name To{z}
    53  //specgen:require z=Int{xN}x{xL}
    54  func MaskToZ[E MaskElt, W Width, zE Ints](x Vec[E, W]) (z Vec[zE, W]) {
    55  	z = makeVec[zE, W]()
    56  	for i, val := range x {
    57  		if val != 0 {
    58  			z[i] = ^0
    59  		}
    60  	}
    61  	return z
    62  }
    63  

View as plain text