Source file src/simd/archsimd/internal/simd_test/mulwiden_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 && (arm64 || wasm)
     6  
     7  package simd_test
     8  
     9  import (
    10  	"simd/archsimd"
    11  	"testing"
    12  )
    13  
    14  // TestMulWiden tests x.MulWiden(y) and x.HiToLo().MulWiden(y.HiToLo()). The
    15  // latter compiles to a single instruction that multiplies the high halves of x
    16  // and y, so it's written out in full in each closure to exercise that.
    17  func TestMulWiden(t *testing.T) {
    18  	testMulWiden(t, int8s, 16, archsimd.LoadInt8x16, archsimd.Int16x8.Store,
    19  		archsimd.Int8x16.MulWiden,
    20  		func(x, y archsimd.Int8x16) archsimd.Int16x8 { return x.HiToLo().MulWiden(y.HiToLo()) })
    21  	testMulWiden(t, int16s, 8, archsimd.LoadInt16x8, archsimd.Int32x4.Store,
    22  		archsimd.Int16x8.MulWiden,
    23  		func(x, y archsimd.Int16x8) archsimd.Int32x4 { return x.HiToLo().MulWiden(y.HiToLo()) })
    24  	testMulWiden(t, int32s, 4, archsimd.LoadInt32x4, archsimd.Int64x2.Store,
    25  		archsimd.Int32x4.MulWiden,
    26  		func(x, y archsimd.Int32x4) archsimd.Int64x2 { return x.HiToLo().MulWiden(y.HiToLo()) })
    27  
    28  	testMulWiden(t, uint8s, 16, archsimd.LoadUint8x16, archsimd.Uint16x8.Store,
    29  		archsimd.Uint8x16.MulWiden,
    30  		func(x, y archsimd.Uint8x16) archsimd.Uint16x8 { return x.HiToLo().MulWiden(y.HiToLo()) })
    31  	testMulWiden(t, uint16s, 8, archsimd.LoadUint16x8, archsimd.Uint32x4.Store,
    32  		archsimd.Uint16x8.MulWiden,
    33  		func(x, y archsimd.Uint16x8) archsimd.Uint32x4 { return x.HiToLo().MulWiden(y.HiToLo()) })
    34  	testMulWiden(t, uint32s, 4, archsimd.LoadUint32x4, archsimd.Uint64x2.Store,
    35  		archsimd.Uint32x4.MulWiden,
    36  		func(x, y archsimd.Uint32x4) archsimd.Uint64x2 { return x.HiToLo().MulWiden(y.HiToLo()) })
    37  }
    38  
    39  func testMulWiden[X, Z number, VX, VZ any](t *testing.T, s []X, n int, load func([]X) VX, store func(VZ, []Z), mulWiden, mulWidenHi func(VX, VX) VZ) {
    40  	t.Helper()
    41  	want := func(x, y []X) []Z {
    42  		z := make([]Z, n/2)
    43  		for i := range z {
    44  			z[i] = Z(x[i]) * Z(y[i])
    45  		}
    46  		return z
    47  	}
    48  	forSlicePair(t, s, n, func(x, y []X) bool {
    49  		t.Helper()
    50  		logInput := func() { t.Helper(); t.Logf("x=%v", x); t.Logf("y=%v", y) }
    51  
    52  		got := make([]Z, n/2)
    53  		store(mulWiden(load(x), load(y)), got)
    54  		if !checkSlicesLogInput(t, got, want(x, y), 0.0, logInput) {
    55  			return false
    56  		}
    57  
    58  		store(mulWidenHi(load(x), load(y)), got)
    59  		return checkSlicesLogInput(t, got, want(x[n/2:], y[n/2:]), 0.0, logInput)
    60  	})
    61  }
    62  

View as plain text