Source file src/simd/archsimd/maskrange_128.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  //go:build goexperiment.simd && (arm64 || wasm)
     6  
     7  package archsimd
     8  
     9  import "math/bits"
    10  
    11  // All returns true when all positions in mask x are true.
    12  //
    13  // Emulated
    14  func (x Mask8x16) All() bool {
    15  	word := x.ToInt8x16().ToBits().ReshapeToUint64s()
    16  	a0 := word.GetElem(0)
    17  	b0 := word.GetElem(1)
    18  	return (a0&b0)&0x0101010101010101 == 0x0101010101010101
    19  }
    20  
    21  // Any returns true when any position in mask x is true.
    22  //
    23  // Emulated
    24  func (x Mask8x16) Any() bool {
    25  	word := x.ToInt8x16().ToBits().ReshapeToUint64s()
    26  	a0 := word.GetElem(0)
    27  	b0 := word.GetElem(1)
    28  	return (a0|b0)&0x0101010101010101 != 0
    29  }
    30  
    31  // None returns true when no positions in mask x are set.
    32  //
    33  // Emulated
    34  func (x Mask8x16) None() bool {
    35  	word := x.ToInt8x16().ToBits().ReshapeToUint64s()
    36  	a0 := word.GetElem(0)
    37  	b0 := word.GetElem(1)
    38  	return (a0|b0)&0x0101010101010101 == 0
    39  }
    40  
    41  // All returns true when all positions in mask x are true.
    42  //
    43  // Emulated
    44  func (x Mask16x8) All() bool {
    45  	word := x.ToInt16x8().ToBits().ReshapeToUint64s()
    46  	a0 := word.GetElem(0)
    47  	b0 := word.GetElem(1)
    48  	return (a0&b0)&0x0001000100010001 == 0x0001000100010001
    49  }
    50  
    51  // Any returns true when any position in mask x is true.
    52  //
    53  // Emulated
    54  func (x Mask16x8) Any() bool {
    55  	word := x.ToInt16x8().ToBits().ReshapeToUint64s()
    56  	a0 := word.GetElem(0)
    57  	b0 := word.GetElem(1)
    58  	return (a0|b0)&0x0001000100010001 != 0
    59  }
    60  
    61  // None returns true when no positions in mask x are set.
    62  //
    63  // Emulated
    64  func (x Mask16x8) None() bool {
    65  	word := x.ToInt16x8().ToBits().ReshapeToUint64s()
    66  	a0 := word.GetElem(0)
    67  	b0 := word.GetElem(1)
    68  	return (a0|b0)&0x0001000100010001 == 0
    69  }
    70  
    71  // All returns true when all positions in mask x are true.
    72  //
    73  // Emulated
    74  func (x Mask32x4) All() bool {
    75  	word := x.ToInt32x4().ToBits().ReshapeToUint64s()
    76  	a0 := word.GetElem(0)
    77  	b0 := word.GetElem(1)
    78  	return (a0&b0)&0x0000000100000001 == 0x0000000100000001
    79  }
    80  
    81  // Any returns true when any position in mask x is true.
    82  //
    83  // Emulated
    84  func (x Mask32x4) Any() bool {
    85  	word := x.ToInt32x4().ToBits().ReshapeToUint64s()
    86  	a0 := word.GetElem(0)
    87  	b0 := word.GetElem(1)
    88  	return (a0|b0)&0x0000000100000001 != 0
    89  }
    90  
    91  // None returns true when no positions in mask x are set.
    92  //
    93  // Emulated
    94  func (x Mask32x4) None() bool {
    95  	word := x.ToInt32x4().ToBits().ReshapeToUint64s()
    96  	a0 := word.GetElem(0)
    97  	b0 := word.GetElem(1)
    98  	return (a0|b0)&0x0000000100000001 == 0
    99  }
   100  
   101  // All returns true when all positions in mask x are true.
   102  //
   103  // Emulated
   104  func (x Mask64x2) All() bool {
   105  	word := x.ToInt64x2().ToBits()
   106  	a0 := word.GetElem(0)
   107  	b0 := word.GetElem(1)
   108  	return a0 != 0 && b0 != 0
   109  }
   110  
   111  // Any returns true when any position in mask x is true.
   112  //
   113  // Emulated
   114  func (x Mask64x2) Any() bool {
   115  	word := x.ToInt64x2().ToBits()
   116  	a0 := word.GetElem(0)
   117  	b0 := word.GetElem(1)
   118  	return a0 != 0 || b0 != 0
   119  }
   120  
   121  // None returns true when no positions in mask x are set.
   122  //
   123  // Emulated
   124  func (x Mask64x2) None() bool {
   125  	word := x.ToInt64x2().ToBits()
   126  	a0 := word.GetElem(0)
   127  	b0 := word.GetElem(1)
   128  	return a0 == 0 && b0 == 0
   129  }
   130  
   131  // TrailingZeros returns the number of low-order false (zero) elements in mask
   132  // x. If the mask is entirely false, it returns 16.
   133  //
   134  // Emulated
   135  func (x Mask8x16) TrailingZeros() int {
   136  	word := x.ToInt8x16().ToBits().ReshapeToUint64s()
   137  	a0 := word.GetElem(0)
   138  	a := a0 & 0x0101010101010101
   139  	lane := bits.TrailingZeros64(a)
   140  	if lane < 64 {
   141  		return lane >> 3
   142  	}
   143  	a0 = word.GetElem(1)
   144  	a = a0 & 0x0101010101010101
   145  	lane = bits.TrailingZeros64(a)
   146  	return lane>>3 + 8
   147  }
   148  
   149  // TrailingZeros returns the number of low-order false (zero) elements in mask
   150  // x. If the mask is entirely false, it returns 8.
   151  //
   152  // Emulated
   153  func (x Mask16x8) TrailingZeros() int {
   154  	word := x.ToInt16x8().ToBits().ReshapeToUint64s()
   155  	a0 := word.GetElem(0)
   156  	a := a0 & 0x0001000100010001
   157  	lane := bits.TrailingZeros64(a)
   158  	if lane < 64 {
   159  		return lane >> 4
   160  	}
   161  	a0 = word.GetElem(1)
   162  	a = a0 & 0x0001000100010001
   163  	lane = bits.TrailingZeros64(a)
   164  	return lane>>4 + 4
   165  }
   166  
   167  // TrailingZeros returns the number of low-order false (zero) elements in mask
   168  // x. If the mask is entirely false, it returns 4.
   169  //
   170  // Emulated
   171  func (x Mask32x4) TrailingZeros() int {
   172  	word := x.ToInt32x4().ToBits().ReshapeToUint64s()
   173  	a0 := word.GetElem(0)
   174  	a := a0 & 0x0000000100000001
   175  	lane := bits.TrailingZeros64(a)
   176  	if lane < 64 {
   177  		return lane >> 5
   178  	}
   179  	a0 = word.GetElem(1)
   180  	a = a0 & 0x0000000100000001
   181  	lane = bits.TrailingZeros64(a)
   182  	return lane>>5 + 2
   183  }
   184  
   185  // TrailingZeros returns the number of low-order false (zero) elements in mask
   186  // x. If the mask is entirely false, it returns 2.
   187  //
   188  // Emulated
   189  func (x Mask64x2) TrailingZeros() int {
   190  	word := x.ToInt64x2().ToBits()
   191  	if word.GetElem(0) != 0 {
   192  		return 0
   193  	}
   194  	if word.GetElem(1) != 0 {
   195  		return 1
   196  	}
   197  	return 2
   198  }
   199  

View as plain text