Source file src/simd/archsimd/maskrange_amd64.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
     6  
     7  package archsimd
     8  
     9  import "math/bits"
    10  
    11  // 128-bit masks
    12  
    13  // All returns true when all positions in mask x are true.
    14  //
    15  // Emulated, CPU Feature: AVX
    16  func (x Mask8x16) All() bool {
    17  	return x.ToBits() == 0xffff
    18  }
    19  
    20  // Any returns true when any position in mask x is true.
    21  //
    22  // Emulated, CPU Feature: AVX
    23  func (x Mask8x16) Any() bool {
    24  	return x.ToBits() != 0
    25  }
    26  
    27  // None returns true when no positions in mask x are set.
    28  //
    29  // Emulated, CPU Feature: AVX
    30  func (x Mask8x16) None() bool {
    31  	return x.ToBits() == 0
    32  }
    33  
    34  // All returns true when all positions in mask x are true.
    35  //
    36  // Emulated, CPU Feature: AVX
    37  func (x Mask16x8) All() bool {
    38  	return x.ToInt16x8().AsInt8x16().asMask().ToBits()&0x5555 == 0x5555
    39  }
    40  
    41  // Any returns true when any position in mask x is true.
    42  //
    43  // Emulated, CPU Feature: AVX
    44  func (x Mask16x8) Any() bool {
    45  	return x.ToInt16x8().AsInt8x16().asMask().ToBits()&0x5555 != 0
    46  }
    47  
    48  // None returns true when no positions in mask x are set.
    49  //
    50  // Emulated, CPU Feature: AVX
    51  func (x Mask16x8) None() bool {
    52  	return x.ToInt16x8().AsInt8x16().asMask().ToBits()&0x5555 == 0
    53  }
    54  
    55  // All returns true when all positions in mask x are true.
    56  //
    57  // Emulated, CPU Feature: AVX
    58  func (x Mask32x4) All() bool {
    59  	return x.ToBits()&0x0f == 0x0f
    60  }
    61  
    62  // Any returns true when any position in mask x is true.
    63  //
    64  // Emulated, CPU Feature: AVX
    65  func (x Mask32x4) Any() bool {
    66  	return x.ToBits()&0x0f != 0
    67  }
    68  
    69  // None returns true when no positions in mask x are set.
    70  //
    71  // Emulated, CPU Feature: AVX
    72  func (x Mask32x4) None() bool {
    73  	return x.ToBits()&0x0f == 0
    74  }
    75  
    76  // All returns true when all positions in mask x are true.
    77  //
    78  // Emulated, CPU Feature: AVX
    79  func (x Mask64x2) All() bool {
    80  	return x.ToBits()&0x03 == 0x03
    81  }
    82  
    83  // Any returns true when any position in mask x is true.
    84  //
    85  // Emulated, CPU Feature: AVX
    86  func (x Mask64x2) Any() bool {
    87  	return x.ToBits()&0x03 != 0
    88  }
    89  
    90  // None returns true when no positions in mask x are set.
    91  //
    92  // Emulated, CPU Feature: AVX
    93  func (x Mask64x2) None() bool {
    94  	return x.ToBits()&0x03 == 0
    95  }
    96  
    97  // 256-bit masks
    98  
    99  // All returns true when all positions in mask x are true.
   100  //
   101  // Emulated, CPU Feature: AVX2
   102  func (x Mask8x32) All() bool {
   103  	return x.ToBits() == 0xffffffff
   104  }
   105  
   106  // Any returns true when any position in mask x is true.
   107  //
   108  // Emulated, CPU Feature: AVX2
   109  func (x Mask8x32) Any() bool {
   110  	return x.ToBits() != 0
   111  }
   112  
   113  // None returns true when no positions in mask x are set.
   114  //
   115  // Emulated, CPU Feature: AVX2
   116  func (x Mask8x32) None() bool {
   117  	return x.ToBits() == 0
   118  }
   119  
   120  // All returns true when all positions in mask x are true.
   121  //
   122  // Emulated, CPU Feature: AVX2
   123  func (x Mask16x16) All() bool {
   124  	return x.ToInt16x16().AsInt8x32().asMask().ToBits()&0x55555555 == 0x55555555
   125  }
   126  
   127  // Any returns true when any position in mask x is true.
   128  //
   129  // Emulated, CPU Feature: AVX2
   130  func (x Mask16x16) Any() bool {
   131  	return x.ToInt16x16().AsInt8x32().asMask().ToBits()&0x55555555 != 0
   132  }
   133  
   134  // None returns true when no positions in mask x are set.
   135  //
   136  // Emulated, CPU Feature: AVX2
   137  func (x Mask16x16) None() bool {
   138  	return x.ToInt16x16().AsInt8x32().asMask().ToBits()&0x55555555 == 0
   139  }
   140  
   141  // All returns true when all positions in mask x are true.
   142  //
   143  // Emulated, CPU Feature: AVX2
   144  func (x Mask32x8) All() bool {
   145  	return x.ToBits() == 0xff
   146  }
   147  
   148  // Any returns true when any position in mask x is true.
   149  //
   150  // Emulated, CPU Feature: AVX2
   151  func (x Mask32x8) Any() bool {
   152  	return x.ToBits() != 0
   153  }
   154  
   155  // None returns true when no positions in mask x are set.
   156  //
   157  // Emulated, CPU Feature: AVX2
   158  func (x Mask32x8) None() bool {
   159  	return x.ToBits() == 0
   160  }
   161  
   162  // All returns true when all positions in mask x are true.
   163  //
   164  // Emulated, CPU Feature: AVX2
   165  func (x Mask64x4) All() bool {
   166  	return x.ToBits()&0x0f == 0x0f
   167  }
   168  
   169  // Any returns true when any position in mask x is true.
   170  //
   171  // Emulated, CPU Feature: AVX2
   172  func (x Mask64x4) Any() bool {
   173  	return x.ToBits()&0x0f != 0
   174  }
   175  
   176  // None returns true when no positions in mask x are set.
   177  //
   178  // Emulated, CPU Feature: AVX2
   179  func (x Mask64x4) None() bool {
   180  	return x.ToBits()&0x0f == 0
   181  }
   182  
   183  // 512-bit masks
   184  
   185  // All returns true when all positions in mask x are true.
   186  //
   187  // Emulated, CPU Feature: AVX512
   188  func (x Mask8x64) All() bool {
   189  	return ^x.ToBits() == 0
   190  }
   191  
   192  // Any returns true when any position in mask x is true.
   193  //
   194  // Emulated, CPU Feature: AVX512
   195  func (x Mask8x64) Any() bool {
   196  	return x.ToBits() != 0
   197  }
   198  
   199  // None returns true when no positions in mask x are set.
   200  //
   201  // Emulated, CPU Feature: AVX512
   202  func (x Mask8x64) None() bool {
   203  	return x.ToBits() == 0
   204  }
   205  
   206  // All returns true when all positions in mask x are true.
   207  //
   208  // Emulated, CPU Feature: AVX512
   209  func (x Mask16x32) All() bool {
   210  	return ^x.ToBits() == 0
   211  }
   212  
   213  // Any returns true when any position in mask x is true.
   214  //
   215  // Emulated, CPU Feature: AVX512
   216  func (x Mask16x32) Any() bool {
   217  	return x.ToBits() != 0
   218  }
   219  
   220  // None returns true when no positions in mask x are set.
   221  //
   222  // Emulated, CPU Feature: AVX512
   223  func (x Mask16x32) None() bool {
   224  	return x.ToBits() == 0
   225  }
   226  
   227  // All returns true when all positions in mask x are true.
   228  //
   229  // Emulated, CPU Feature: AVX512
   230  func (x Mask32x16) All() bool {
   231  	return ^x.ToBits() == 0
   232  }
   233  
   234  // Any returns true when any position in mask x is true.
   235  //
   236  // Emulated, CPU Feature: AVX512
   237  func (x Mask32x16) Any() bool {
   238  	return x.ToBits() != 0
   239  }
   240  
   241  // None returns true when no positions in mask x are set.
   242  //
   243  // Emulated, CPU Feature: AVX512
   244  func (x Mask32x16) None() bool {
   245  	return x.ToBits() == 0
   246  }
   247  
   248  // All returns true when all positions in mask x are true.
   249  //
   250  // Emulated, CPU Feature: AVX512
   251  func (x Mask64x8) All() bool {
   252  	return ^x.ToBits() == 0
   253  }
   254  
   255  // Any returns true when any position in mask x is true.
   256  //
   257  // Emulated, CPU Feature: AVX512
   258  func (x Mask64x8) Any() bool {
   259  	return x.ToBits() != 0
   260  }
   261  
   262  // None returns true when no positions in mask x are set.
   263  //
   264  // Emulated, CPU Feature: AVX512
   265  func (x Mask64x8) None() bool {
   266  	return x.ToBits() == 0
   267  }
   268  
   269  // TrailingZeros returns the number of low-order false (zero) elements in mask
   270  // x. If the mask is entirely false, it returns 16.
   271  //
   272  // Emulated, CPU Feature: AVX
   273  func (x Mask8x16) TrailingZeros() int {
   274  	word := uint64(x.ToBits())
   275  	lane := bits.TrailingZeros64(word)
   276  	if lane >= 16 {
   277  		return 16
   278  	}
   279  	return lane
   280  }
   281  
   282  // TrailingZeros returns the number of low-order false (zero) elements in mask
   283  // x. If the mask is entirely false, it returns 32.
   284  //
   285  // Emulated, CPU Feature: AVX2
   286  func (x Mask8x32) TrailingZeros() int {
   287  	word := uint64(x.ToBits())
   288  	lane := bits.TrailingZeros64(word)
   289  	if lane >= 32 {
   290  		return 32
   291  	}
   292  	return lane
   293  }
   294  
   295  // TrailingZeros returns the number of low-order false (zero) elements in mask
   296  // x. If the mask is entirely false, it returns 64.
   297  //
   298  // Emulated, CPU Feature: AVX512
   299  func (x Mask8x64) TrailingZeros() int {
   300  	word := uint64(x.ToBits())
   301  	lane := bits.TrailingZeros64(word)
   302  	if lane >= 64 {
   303  		return 64
   304  	}
   305  	return lane
   306  }
   307  
   308  // TrailingZeros returns the number of low-order false (zero) elements in mask
   309  // x. If the mask is entirely false, it returns 8.
   310  //
   311  // Emulated, CPU Feature: AVX
   312  func (x Mask16x8) TrailingZeros() int {
   313  	word := uint64(x.ToInt16x8().AsInt8x16().asMask().ToBits() & 0x5555)
   314  	lane := bits.TrailingZeros64(word) >> 1
   315  	if lane >= 8 {
   316  		return 8
   317  	}
   318  	return lane
   319  }
   320  
   321  // TrailingZeros returns the number of low-order false (zero) elements in mask
   322  // x. If the mask is entirely false, it returns 16.
   323  //
   324  // Emulated, CPU Feature: AVX2
   325  func (x Mask16x16) TrailingZeros() int {
   326  	word := uint64(x.ToInt16x16().AsInt8x32().asMask().ToBits() & 0x55555555)
   327  	lane := bits.TrailingZeros64(word) >> 1
   328  	if lane >= 16 {
   329  		return 16
   330  	}
   331  	return lane
   332  }
   333  
   334  // TrailingZeros returns the number of low-order false (zero) elements in mask
   335  // x. If the mask is entirely false, it returns 32.
   336  //
   337  // Emulated, CPU Feature: AVX512
   338  func (x Mask16x32) TrailingZeros() int {
   339  	word := uint64(x.ToBits())
   340  	lane := bits.TrailingZeros64(word)
   341  	if lane >= 32 {
   342  		return 32
   343  	}
   344  	return lane
   345  }
   346  
   347  // TrailingZeros returns the number of low-order false (zero) elements in mask
   348  // x. If the mask is entirely false, it returns 4.
   349  //
   350  // Emulated, CPU Feature: AVX
   351  func (x Mask32x4) TrailingZeros() int {
   352  	word := uint64(x.ToBits())
   353  	lane := bits.TrailingZeros64(word)
   354  	if lane >= 4 {
   355  		return 4
   356  	}
   357  	return lane
   358  }
   359  
   360  // TrailingZeros returns the number of low-order false (zero) elements in mask
   361  // x. If the mask is entirely false, it returns 8.
   362  //
   363  // Emulated, CPU Feature: AVX2
   364  func (x Mask32x8) TrailingZeros() int {
   365  	word := uint64(x.ToBits())
   366  	lane := bits.TrailingZeros64(word)
   367  	if lane >= 8 {
   368  		return 8
   369  	}
   370  	return lane
   371  }
   372  
   373  // TrailingZeros returns the number of low-order false (zero) elements in mask
   374  // x. If the mask is entirely false, it returns 16.
   375  //
   376  // Emulated, CPU Feature: AVX512
   377  func (x Mask32x16) TrailingZeros() int {
   378  	word := uint64(x.ToBits())
   379  	lane := bits.TrailingZeros64(word)
   380  	if lane >= 16 {
   381  		return 16
   382  	}
   383  	return lane
   384  }
   385  
   386  // TrailingZeros returns the number of low-order false (zero) elements in mask
   387  // x. If the mask is entirely false, it returns 2.
   388  //
   389  // Emulated, CPU Feature: AVX
   390  func (x Mask64x2) TrailingZeros() int {
   391  	word := uint64(x.ToBits())
   392  	lane := bits.TrailingZeros64(word)
   393  	if lane >= 2 {
   394  		return 2
   395  	}
   396  	return lane
   397  }
   398  
   399  // TrailingZeros returns the number of low-order false (zero) elements in mask
   400  // x. If the mask is entirely false, it returns 4.
   401  //
   402  // Emulated, CPU Feature: AVX2
   403  func (x Mask64x4) TrailingZeros() int {
   404  	word := uint64(x.ToBits())
   405  	lane := bits.TrailingZeros64(word)
   406  	if lane >= 4 {
   407  		return 4
   408  	}
   409  	return lane
   410  }
   411  
   412  // TrailingZeros returns the number of low-order false (zero) elements in mask
   413  // x. If the mask is entirely false, it returns 8.
   414  //
   415  // Emulated, CPU Feature: AVX512
   416  func (x Mask64x8) TrailingZeros() int {
   417  	word := uint64(x.ToBits())
   418  	lane := bits.TrailingZeros64(word)
   419  	if lane >= 8 {
   420  		return 8
   421  	}
   422  	return lane
   423  }
   424  

View as plain text