Source file src/simd/archsimd/ops_emulated_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 && amd64
     6  
     7  package archsimd
     8  
     9  // Abs returns the absolute values of the elements of x
    10  //
    11  // Emulated, CPU Feature AVX
    12  func (x Float32x4) Abs() Float32x4 {
    13  	mask := BroadcastUint32x4(0x80000000)
    14  	return x.ToBits().AndNot(mask).BitsToFloat32()
    15  }
    16  
    17  // Abs returns the absolute values of the elements of x
    18  //
    19  // Emulated, CPU Feature AVX2
    20  func (x Float32x8) Abs() Float32x8 {
    21  	// mask will have a 1 in the sign bit UNLESS x is NaN
    22  	mask := BroadcastUint32x8(0x80000000)
    23  	return x.ToBits().AndNot(mask).BitsToFloat32()
    24  }
    25  
    26  // Abs returns the absolute values of the elements of x
    27  //
    28  // Emulated, CPU Feature AVX512
    29  func (x Float32x16) Abs() Float32x16 {
    30  	mask := BroadcastUint32x16(0x80000000)
    31  	return x.ToBits().AndNot(mask).BitsToFloat32()
    32  }
    33  
    34  // Abs returns the absolute values of the elements of x
    35  //
    36  // Emulated, CPU Feature AVX
    37  func (x Float64x2) Abs() Float64x2 {
    38  	// mask will have a 1 in the sign bit UNLESS x is NaN
    39  	mask := BroadcastUint64x2(0x8000000000000000)
    40  	return x.ToBits().AndNot(mask).BitsToFloat64()
    41  }
    42  
    43  // Abs returns the absolute values of the elements of x
    44  //
    45  // Emulated, CPU Feature AVX2
    46  func (x Float64x4) Abs() Float64x4 {
    47  	mask := BroadcastUint64x4(0x8000000000000000)
    48  	return x.ToBits().AndNot(mask).BitsToFloat64()
    49  }
    50  
    51  // Abs returns the absolute values of the elements of x
    52  //
    53  // Emulated, CPU Feature AVX512
    54  func (x Float64x8) Abs() Float64x8 {
    55  	mask := BroadcastUint64x8(0x8000000000000000)
    56  	return x.ToBits().AndNot(mask).BitsToFloat64()
    57  }
    58  
    59  // Neg returns the negation of the elements of x
    60  //
    61  // Emulated, CPU Feature AVX
    62  func (x Float32x4) Neg() Float32x4 {
    63  	mask := BroadcastUint32x4(0x80000000)
    64  	return x.ToBits().Xor(mask).BitsToFloat32()
    65  }
    66  
    67  // Neg returns the negation of the elements of x
    68  //
    69  // Emulated, CPU Feature AVX2
    70  func (x Float32x8) Neg() Float32x8 {
    71  	// mask will have a 1 in the sign bit UNLESS x is NaN
    72  	mask := BroadcastUint32x8(0x80000000)
    73  	return x.ToBits().Xor(mask).BitsToFloat32()
    74  }
    75  
    76  // Neg returns the negation of the elements of x
    77  //
    78  // Emulated, CPU Feature AVX512
    79  func (x Float32x16) Neg() Float32x16 {
    80  	mask := BroadcastUint32x16(0x80000000)
    81  	return x.ToBits().Xor(mask).BitsToFloat32()
    82  }
    83  
    84  // Neg returns the negation of the elements of x
    85  //
    86  // Emulated, CPU Feature AVX
    87  func (x Float64x2) Neg() Float64x2 {
    88  	// mask will have a 1 in the sign bit UNLESS x is NaN
    89  	mask := BroadcastUint64x2(0x8000000000000000)
    90  	return x.ToBits().Xor(mask).BitsToFloat64()
    91  }
    92  
    93  // Neg returns the negation of the elements of x
    94  //
    95  // Emulated, CPU Feature AVX2
    96  func (x Float64x4) Neg() Float64x4 {
    97  	mask := BroadcastUint64x4(0x8000000000000000)
    98  	return x.ToBits().Xor(mask).BitsToFloat64()
    99  }
   100  
   101  // Neg returns the negation of the elements of x
   102  //
   103  // Emulated, CPU Feature AVX512
   104  func (x Float64x8) Neg() Float64x8 {
   105  	mask := BroadcastUint64x8(0x8000000000000000)
   106  	return x.ToBits().Xor(mask).BitsToFloat64()
   107  }
   108  
   109  var f0x16 = [16]int8{-1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0}
   110  var f0x32 = [32]int8{-1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0,
   111  	-1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0}
   112  var f0x64 = [64]int8{-1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0,
   113  	-1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0,
   114  	-1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0,
   115  	-1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0}
   116  
   117  // Mul multiplies corresponding elements of two vectors, modulo 2ⁿ.
   118  //
   119  // Emulated, CPU Feature: AVX
   120  func (x Int8x16) Mul(y Int8x16) Int8x16 {
   121  	mask := LoadInt8x16Array(&f0x16)
   122  	mask16 := mask.ToBits().ReshapeToUint16s()
   123  	xe := x.And(mask).ToBits().ReshapeToUint16s()
   124  	xo := x.AndNot(mask).ToBits().ReshapeToUint16s().ShiftAllRight(8)
   125  	ye := y.And(mask).ToBits().ReshapeToUint16s()
   126  	yo := y.AndNot(mask).ToBits().ReshapeToUint16s().ShiftAllRight(8)
   127  	pe := xe.Mul(ye).And(mask16)
   128  	po := xo.Mul(yo).And(mask16).ShiftAllLeft(8)
   129  	return pe.Or(po).ReshapeToUint8s().BitsToInt8()
   130  }
   131  
   132  // Mul multiplies corresponding elements of two vectors, modulo 2ⁿ.
   133  //
   134  // Emulated, CPU Feature: AVX
   135  func (x Uint8x16) Mul(y Uint8x16) Uint8x16 {
   136  	mask := LoadInt8x16Array(&f0x16).ToBits()
   137  	mask16 := mask.ReshapeToUint16s()
   138  	xe := x.And(mask).ReshapeToUint16s()
   139  	xo := x.AndNot(mask).ReshapeToUint16s().ShiftAllRight(8)
   140  	ye := y.And(mask).ReshapeToUint16s()
   141  	yo := y.AndNot(mask).ReshapeToUint16s().ShiftAllRight(8)
   142  	pe := xe.Mul(ye).And(mask16)
   143  	po := xo.Mul(yo).And(mask16).ShiftAllLeft(8)
   144  	return pe.Or(po).ReshapeToUint8s()
   145  }
   146  
   147  // Mul multiplies corresponding elements of two vectors, modulo 2ⁿ.
   148  //
   149  // Emulated, CPU Feature: AVX2
   150  func (x Int8x32) Mul(y Int8x32) Int8x32 {
   151  	mask := LoadInt8x32Array(&f0x32)
   152  	mask16 := mask.ToBits().ReshapeToUint16s()
   153  	xe := x.And(mask).ToBits().ReshapeToUint16s()
   154  	xo := x.AndNot(mask).ToBits().ReshapeToUint16s().ShiftAllRight(8)
   155  	ye := y.And(mask).ToBits().ReshapeToUint16s()
   156  	yo := y.AndNot(mask).ToBits().ReshapeToUint16s().ShiftAllRight(8)
   157  	pe := xe.Mul(ye).And(mask16)
   158  	po := xo.Mul(yo).And(mask16).ShiftAllLeft(8)
   159  	return pe.Or(po).ReshapeToUint8s().BitsToInt8()
   160  }
   161  
   162  // Mul multiplies corresponding elements of two vectors, modulo 2ⁿ.
   163  //
   164  // Emulated, CPU Feature: AVX512
   165  func (x Int8x64) Mul(y Int8x64) Int8x64 {
   166  	mask := LoadInt8x64Array(&f0x64)
   167  	mask16 := mask.ToBits().ReshapeToUint16s()
   168  	xe := x.And(mask).ToBits().ReshapeToUint16s()
   169  	xo := x.AndNot(mask).ToBits().ReshapeToUint16s().ShiftAllRight(8)
   170  	ye := y.And(mask).ToBits().ReshapeToUint16s()
   171  	yo := y.AndNot(mask).ToBits().ReshapeToUint16s().ShiftAllRight(8)
   172  	pe := xe.Mul(ye).And(mask16)
   173  	po := xo.Mul(yo).And(mask16).ShiftAllLeft(8)
   174  	return pe.Or(po).ReshapeToUint8s().BitsToInt8()
   175  }
   176  
   177  // Mul multiplies corresponding elements of two vectors, modulo 2ⁿ.
   178  //
   179  // Emulated, CPU Feature: AVX2
   180  func (x Uint8x32) Mul(y Uint8x32) Uint8x32 {
   181  	mask := LoadInt8x32Array(&f0x32).ToBits()
   182  	mask16 := mask.ReshapeToUint16s()
   183  	xe := x.And(mask).ReshapeToUint16s()
   184  	xo := x.AndNot(mask).ReshapeToUint16s().ShiftAllRight(8)
   185  	ye := y.And(mask).ReshapeToUint16s()
   186  	yo := y.AndNot(mask).ReshapeToUint16s().ShiftAllRight(8)
   187  	pe := xe.Mul(ye).And(mask16)
   188  	po := xo.Mul(yo).And(mask16).ShiftAllLeft(8)
   189  	return pe.Or(po).ReshapeToUint8s()
   190  }
   191  
   192  // Mul multiplies corresponding elements of two vectors, modulo 2ⁿ.
   193  //
   194  // Emulated, CPU Feature: AVX512
   195  func (x Uint8x64) Mul(y Uint8x64) Uint8x64 {
   196  	mask := LoadInt8x64Array(&f0x64).ToBits()
   197  	mask16 := mask.ReshapeToUint16s()
   198  	xe := x.And(mask).ReshapeToUint16s()
   199  	xo := x.AndNot(mask).ReshapeToUint16s().ShiftAllRight(8)
   200  	ye := y.And(mask).ReshapeToUint16s()
   201  	yo := y.AndNot(mask).ReshapeToUint16s().ShiftAllRight(8)
   202  	pe := xe.Mul(ye).And(mask16)
   203  	po := xo.Mul(yo).And(mask16).ShiftAllLeft(8)
   204  	return pe.Or(po).ReshapeToUint8s()
   205  }
   206  

View as plain text