Source file src/simd/archsimd/internal/simd_test/dot_128_test.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 || wasm)
     6  
     7  package simd_test
     8  
     9  import (
    10  	"math"
    11  	"simd/archsimd"
    12  	"testing"
    13  )
    14  
    15  func dotProductPairsSlice(x, y []int16) []int32 {
    16  	z := make([]int32, len(x)/2)
    17  	for i := range z {
    18  		// Products of int16s always fit in an int32, but their sum can wrap.
    19  		z[i] = int32(x[2*i])*int32(y[2*i]) + int32(x[2*i+1])*int32(y[2*i+1])
    20  	}
    21  	return z
    22  }
    23  
    24  func TestDotProductPairs(t *testing.T) {
    25  	forSlicePair(t, int16s, 8, func(x, y []int16) bool {
    26  		t.Helper()
    27  		got := make([]int32, 4)
    28  		archsimd.LoadInt16x8(x).DotProductPairs(archsimd.LoadInt16x8(y)).Store(got)
    29  		return checkSlicesLogInput(t, got, dotProductPairsSlice(x, y), 0.0, func() { t.Helper(); t.Logf("x=%v", x); t.Logf("y=%v", y) })
    30  	})
    31  
    32  	// The only case that overflows is when all four inputs to a sum are
    33  	// MinInt16, which wraps to MinInt32. Changing any one of them by one
    34  	// gives the largest sum that doesn't overflow.
    35  	m := int16(math.MinInt16)
    36  	x := []int16{m, m, m, m, m, m, 1, 2}
    37  	y := []int16{m, m, m, m, m, m + 1, 3, 4}
    38  	got := make([]int32, 4)
    39  	archsimd.LoadInt16x8(x).DotProductPairs(archsimd.LoadInt16x8(y)).Store(got)
    40  	checkSlices(t, got, []int32{math.MinInt32, math.MinInt32, 1<<31 - 1<<15, 11})
    41  }
    42  

View as plain text