Source file src/simd/internal/spec/comparison.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 Comparison
     6  
     7  package spec
     8  
     9  import "math"
    10  
    11  func bool2mask[E MaskElt](b bool) E {
    12  	if b {
    13  		return ^E(0)
    14  	}
    15  	return 0
    16  }
    17  
    18  // Equal returns a mask indicating which elements of x and y are equal.
    19  //
    20  //specgen:require zN=xN
    21  func Equal[E EltOrMask, W Width, zE MaskElt](x, y Vec[E, W]) (z Vec[zE, W]) {
    22  	return map2[E, W, zE, W](x, y, func(x, y E) zE {
    23  		return bool2mask[zE](x == y)
    24  	})
    25  }
    26  
    27  // NotEqual returns a mask indicating which elements of x and y are not equal.
    28  //
    29  //specgen:require zN=xN
    30  func NotEqual[E EltOrMask, W Width, zE MaskElt](x, y Vec[E, W]) (z Vec[zE, W]) {
    31  	return map2[E, W, zE, W](x, y, func(x, y E) zE {
    32  		return bool2mask[zE](x != y)
    33  	})
    34  }
    35  
    36  // Less returns a mask indicating which elements of x are less than y.
    37  //
    38  //	z[i] = x[i] < y[i]
    39  //
    40  //specgen:require zN=xN
    41  func Less[E Elt, W Width, zE MaskElt](x, y Vec[E, W]) (z Vec[zE, W]) {
    42  	return map2[E, W, zE, W](x, y, func(x, y E) zE {
    43  		return bool2mask[zE](x < y)
    44  	})
    45  }
    46  
    47  // LessEqual returns a mask indicating which elements of x are less than or
    48  // equal to y.
    49  //
    50  //	z[i] = x[i] <= y[i]
    51  //
    52  //specgen:require zN=xN
    53  func LessEqual[E Elt, W Width, zE MaskElt](x, y Vec[E, W]) (z Vec[zE, W]) {
    54  	return map2[E, W, zE, W](x, y, func(x, y E) zE {
    55  		return bool2mask[zE](x <= y)
    56  	})
    57  }
    58  
    59  // Greater returns a mask indicating which elements of x are greater than y.
    60  //
    61  //	z[i] = x[i] > y[i]
    62  //
    63  //specgen:require zN=xN
    64  func Greater[E Elt, W Width, zE MaskElt](x, y Vec[E, W]) (z Vec[zE, W]) {
    65  	return map2[E, W, zE, W](x, y, func(x, y E) zE {
    66  		return bool2mask[zE](x > y)
    67  	})
    68  }
    69  
    70  // GreaterEqual returns a mask indicating which elements of x are greater than
    71  // or equal to y.
    72  //
    73  //	z[i] = x[i] >= y[i]
    74  //
    75  //specgen:require zN=xN
    76  func GreaterEqual[E Elt, W Width, zE MaskElt](x, y Vec[E, W]) (z Vec[zE, W]) {
    77  	return map2[E, W, zE, W](x, y, func(x, y E) zE {
    78  		return bool2mask[zE](x >= y)
    79  	})
    80  }
    81  
    82  // ToMask returns a mask indicating which elements of x are non-zero.
    83  //
    84  //	z[i] = x[i] != 0
    85  //
    86  //specgen:require zN=xN
    87  func ToMask[E Elt, W Width, zE MaskElt](x Vec[E, W]) (z Vec[zE, W]) {
    88  	return map1[E, W, zE, W](x, func(x E) zE {
    89  		return bool2mask[zE](x != 0)
    90  	})
    91  }
    92  
    93  // IsNan returns a mask indicating which elements of x are NaN.
    94  //
    95  //	z[i] = math.IsNaN(x[i])
    96  //
    97  //specgen:require zN=xN
    98  func IsNaN[E Floats, W Width, zE MaskElt](x Vec[E, W]) (z Vec[zE, W]) {
    99  	return map1[E, W, zE, W](x, func(x E) zE {
   100  		return bool2mask[zE](math.IsNaN(float64(x)))
   101  	})
   102  }
   103  

View as plain text