Source file src/simd/internal/spec/logical.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 Logical
     6  
     7  package spec
     8  
     9  // And returns the bitwise AND of x and y.
    10  //
    11  //	z[i] = x[i] & y[i]
    12  //
    13  //specgen:commutative
    14  func And[E Ints | Uints | MaskElt, W Width](x, y Vec[E, W]) (z Vec[E, W]) {
    15  	return map2[E, W, E, W](x, y, func(x, y E) E {
    16  		return x & y
    17  	})
    18  }
    19  
    20  // AndNot returns the bitwise AND NOT of x and y.
    21  //
    22  //	z[i] = x[i] &^ y[i]
    23  func AndNot[E Ints | Uints | MaskElt, W Width](x, y Vec[E, W]) (z Vec[E, W]) {
    24  	return map2[E, W, E, W](x, y, func(x, y E) E {
    25  		return x &^ y
    26  	})
    27  }
    28  
    29  // Or returns the bitwise OR of x and y.
    30  //
    31  //	z[i] = x[i] | y[i]
    32  //
    33  //specgen:commutative
    34  func Or[E Ints | Uints | MaskElt, W Width](x, y Vec[E, W]) (z Vec[E, W]) {
    35  	return map2[E, W, E, W](x, y, func(x, y E) E {
    36  		return x | y
    37  	})
    38  }
    39  
    40  // OrNot returns the bitwise OR NOT of x and y.
    41  //
    42  //	z[i] = x[i] | ^y[i]
    43  func OrNot[E Ints | Uints | MaskElt, W Width](x, y Vec[E, W]) (z Vec[E, W]) {
    44  	return map2[E, W, E, W](x, y, func(x, y E) E {
    45  		return x | ^y
    46  	})
    47  }
    48  
    49  // Xor returns the bitwise XOR of x and y.
    50  //
    51  //	z[i] = x[i] ^ y[i]
    52  //
    53  //specgen:commutative
    54  func Xor[E Ints | Uints | MaskElt, W Width](x, y Vec[E, W]) (z Vec[E, W]) {
    55  	return map2[E, W, E, W](x, y, func(x, y E) E {
    56  		return x ^ y
    57  	})
    58  }
    59  
    60  // Not returns the bitwise negation of x.
    61  //
    62  //	z[i] = ^x[i]
    63  func Not[E Ints | Uints | MaskElt, W Width](x Vec[E, W]) (z Vec[E, W]) {
    64  	return map1[E, W, E, W](x, func(x E) E {
    65  		return ^x
    66  	})
    67  }
    68  
    69  // BitSelect returns the bitwise selection of x and y controlled by cond,
    70  // choosing each bit from x where the corresponding bit of cond is 1 and from y
    71  // where it is 0.
    72  //
    73  //	z[i] = (x[i] & cond[i]) | (y[i] &^ cond[i])
    74  //
    75  //specgen:require cond=Uint{xN}x{xL}
    76  func BitSelect[E Ints | Uints, W Width, CondE Uints](x, y Vec[E, W], cond Vec[CondE, W]) (z Vec[E, W]) {
    77  	z = makeVec[E, W]()
    78  	for i := range z {
    79  		z[i] = (x[i] & E(cond[i])) | (y[i] &^ E(cond[i]))
    80  	}
    81  	return z
    82  }
    83  

View as plain text