Source file src/simd/archsimd/converts_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  // This file contains helpers for the emulated narrowing integer conversions in
    10  // converts_gen_amd64.go, which tmplgen generates. These are conversions that
    11  // AVX-512 provides as single instructions but that we need without AVX-512:
    12  //
    13  //   - The 2:1 TruncTo and SaturateTo conversions on 128- and 256-bit vectors,
    14  //     which only require AVX and AVX2, respectively.
    15  //   - SaturateToUint from signed integers, which has no AVX-512 instruction,
    16  //     at every size ratio.
    17  //
    18  // On AVX-512 hardware, a single VPMOV instruction does each 2:1 conversion, so
    19  // the emulations use that instead where it saves instructions. (It doesn't for
    20  // the 128-bit truncations, or for saturation that's a single pack.)
    21  //
    22  // TODO: With GOAMD64=v4, the feature checks could be resolved statically.
    23  
    24  // The 256-bit emulations first narrow within each 128-bit group, which leaves
    25  // the results in the low 64 bits of each group and zeros in the high 64 bits,
    26  // and then use gatherLo64 to move the results of the upper group down.
    27  
    28  var gatherLo64Idx = [8]uint32{0, 1, 4, 5, 2, 3, 6, 7}
    29  
    30  // gatherLo64 returns the elements of x in the order 0, 2, 1, 3.
    31  func gatherLo64(x Uint64x4) Uint64x4 {
    32  	return x.ReshapeToUint32s().LookupMod(LoadUint32x8Array(&gatherLo64Idx)).ReshapeToUint64s()
    33  }
    34  
    35  // Byte permutations for truncation. -1 produces a zero.
    36  var (
    37  	trunc16To8Idx  = [16]int8{0, 2, 4, 6, 8, 10, 12, 14, -1, -1, -1, -1, -1, -1, -1, -1}
    38  	trunc32To16Idx = [16]int8{0, 1, 4, 5, 8, 9, 12, 13, -1, -1, -1, -1, -1, -1, -1, -1}
    39  
    40  	trunc16To8GroupedIdx = [32]int8{
    41  		0, 2, 4, 6, 8, 10, 12, 14, -1, -1, -1, -1, -1, -1, -1, -1,
    42  		0, 2, 4, 6, 8, 10, 12, 14, -1, -1, -1, -1, -1, -1, -1, -1,
    43  	}
    44  	trunc32To16GroupedIdx = [32]int8{
    45  		0, 1, 4, 5, 8, 9, 12, 13, -1, -1, -1, -1, -1, -1, -1, -1,
    46  		0, 1, 4, 5, 8, 9, 12, 13, -1, -1, -1, -1, -1, -1, -1, -1,
    47  	}
    48  )
    49  
    50  // Saturation bounds.
    51  var (
    52  	maxUint8x8  = [8]uint16{maxUint8, maxUint8, maxUint8, maxUint8, maxUint8, maxUint8, maxUint8, maxUint8}
    53  	maxUint16x4 = [4]uint32{maxUint16, maxUint16, maxUint16, maxUint16}
    54  	maxInt32x2  = [2]int64{maxInt32, maxInt32}
    55  	minInt32x2  = [2]int64{minInt32, minInt32}
    56  	maxUint32x2 = [2]int64{maxUint32, maxUint32}
    57  	maxUint8x16 = [16]uint16{maxUint8, maxUint8, maxUint8, maxUint8, maxUint8, maxUint8, maxUint8, maxUint8, maxUint8, maxUint8, maxUint8, maxUint8, maxUint8, maxUint8, maxUint8, maxUint8}
    58  	maxUint16x8 = [8]uint32{maxUint16, maxUint16, maxUint16, maxUint16, maxUint16, maxUint16, maxUint16, maxUint16}
    59  	maxInt32x4  = [4]int64{maxInt32, maxInt32, maxInt32, maxInt32}
    60  	minInt32x4  = [4]int64{minInt32, minInt32, minInt32, minInt32}
    61  	maxUint32x4 = [4]int64{maxUint32, maxUint32, maxUint32, maxUint32}
    62  )
    63  

View as plain text