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.simdstable && 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.) With
    21  // GOAMD64=v4, the compiler resolves the feature checks statically.
    22  
    23  // The 256-bit emulations first narrow within each 128-bit group, which leaves
    24  // the results in the low 64 bits of each group and zeros in the high 64 bits,
    25  // and then use gatherLo64 to move the results of the upper group down.
    26  
    27  var gatherLo64Idx = [8]uint32{0, 1, 4, 5, 2, 3, 6, 7}
    28  
    29  // gatherLo64 returns the elements of x in the order 0, 2, 1, 3.
    30  func gatherLo64(x Uint64x4) Uint64x4 {
    31  	return x.ReshapeToUint32s().LookupMod(LoadUint32x8Array(&gatherLo64Idx)).ReshapeToUint64s()
    32  }
    33  
    34  // Byte permutations for truncation. -1 produces a zero.
    35  var (
    36  	trunc16To8Idx  = [16]int8{0, 2, 4, 6, 8, 10, 12, 14, -1, -1, -1, -1, -1, -1, -1, -1}
    37  	trunc32To16Idx = [16]int8{0, 1, 4, 5, 8, 9, 12, 13, -1, -1, -1, -1, -1, -1, -1, -1}
    38  
    39  	trunc16To8GroupedIdx = [32]int8{
    40  		0, 2, 4, 6, 8, 10, 12, 14, -1, -1, -1, -1, -1, -1, -1, -1,
    41  		0, 2, 4, 6, 8, 10, 12, 14, -1, -1, -1, -1, -1, -1, -1, -1,
    42  	}
    43  	trunc32To16GroupedIdx = [32]int8{
    44  		0, 1, 4, 5, 8, 9, 12, 13, -1, -1, -1, -1, -1, -1, -1, -1,
    45  		0, 1, 4, 5, 8, 9, 12, 13, -1, -1, -1, -1, -1, -1, -1, -1,
    46  	}
    47  )
    48  
    49  // Saturation bounds.
    50  var (
    51  	maxUint8x8  = [8]uint16{maxUint8, maxUint8, maxUint8, maxUint8, maxUint8, maxUint8, maxUint8, maxUint8}
    52  	maxUint16x4 = [4]uint32{maxUint16, maxUint16, maxUint16, maxUint16}
    53  	maxInt32x2  = [2]int64{maxInt32, maxInt32}
    54  	minInt32x2  = [2]int64{minInt32, minInt32}
    55  	maxUint32x2 = [2]int64{maxUint32, maxUint32}
    56  	maxUint8x16 = [16]uint16{maxUint8, maxUint8, maxUint8, maxUint8, maxUint8, maxUint8, maxUint8, maxUint8, maxUint8, maxUint8, maxUint8, maxUint8, maxUint8, maxUint8, maxUint8, maxUint8}
    57  	maxUint16x8 = [8]uint32{maxUint16, maxUint16, maxUint16, maxUint16, maxUint16, maxUint16, maxUint16, maxUint16}
    58  	maxInt32x4  = [4]int64{maxInt32, maxInt32, maxInt32, maxInt32}
    59  	minInt32x4  = [4]int64{minInt32, minInt32, minInt32, minInt32}
    60  	maxUint32x4 = [4]int64{maxUint32, maxUint32, maxUint32, maxUint32}
    61  )
    62  

View as plain text