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