Source file src/simd/archsimd/internal/simd_test/preempt_amd64_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.simd && amd64
     6  
     7  package simd_test
     8  
     9  import (
    10  	"fmt"
    11  	"internal/cpu"
    12  	"internal/testenv"
    13  	"os"
    14  	"os/exec"
    15  	"runtime"
    16  	"simd/archsimd"
    17  	"strings"
    18  	"sync"
    19  	"sync/atomic"
    20  	"testing"
    21  )
    22  
    23  func TestAsyncPreemptSIMD(t *testing.T) {
    24  	if runtime.GOOS == "plan9" {
    25  		t.Skip("asynchronous preemption not supported on plan9")
    26  	}
    27  	if strings.Contains(os.Getenv("GODEBUG"), "asyncpreemptoff=1") {
    28  		t.Skip("asynchronous preemption disabled via GODEBUG")
    29  	}
    30  	if os.Getenv("GO_TEST_PREEMPT_XSAVEC_OFF") == "1" && runtime.GOOS != "windows" && cpu.X86.HasXSAVEC {
    31  		t.Fatalf("expected cpu.X86.HasXSAVEC == false under GODEBUG=cpu.xsavec=off")
    32  	}
    33  
    34  	t.Run("Y", testAsyncPreemptY)
    35  	t.Run("Z", testAsyncPreemptZ)
    36  	t.Run("XHiWithMask", testAsyncPreemptXHiWithMask)
    37  	t.Run("Mask", testAsyncPreemptMask)
    38  	t.Run("ZAndMask", testAsyncPreemptZAndMask)
    39  	t.Run("MixedCleanDirty", testAsyncPreemptMixedCleanDirty)
    40  
    41  	if os.Getenv("GO_TEST_PREEMPT_XSAVEC_OFF") == "" {
    42  		t.Run("xsavec_off", func(t *testing.T) {
    43  			testenv.MustHaveExec(t)
    44  			cmd := exec.Command(testenv.Executable(t), "-test.run=^TestAsyncPreemptSIMD$")
    45  			if testing.Short() {
    46  				cmd.Args = append(cmd.Args, "-test.short")
    47  			}
    48  			env := os.Environ()
    49  			godebug := os.Getenv("GODEBUG")
    50  			if godebug != "" {
    51  				godebug += ",cpu.xsavec=off"
    52  			} else {
    53  				godebug = "cpu.xsavec=off"
    54  			}
    55  			cmd.Env = append(env, "GO_TEST_PREEMPT_XSAVEC_OFF=1", "GODEBUG="+godebug)
    56  			out, err := cmd.CombinedOutput()
    57  			if err != nil {
    58  				t.Fatalf("TestAsyncPreemptSIMD with GODEBUG=%s failed: %v\n%s", godebug, err, out)
    59  			}
    60  		})
    61  	}
    62  }
    63  
    64  type preemptWorker struct {
    65  	progress atomic.Uint64
    66  	run      func(stop *atomic.Uint32, progress *atomic.Uint64) error
    67  }
    68  
    69  func runPreemptWorkers(t *testing.T, workers []*preemptWorker) {
    70  	t.Helper()
    71  	// Use 2 Ps so multiple workers share Ms and overwrite each other's vector
    72  	// and opmask registers while parked, exercising both save/restore and
    73  	// clean-to-dirty zeroing transitions.
    74  	defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(2))
    75  
    76  	var stop atomic.Uint32
    77  	errs := make([]error, len(workers))
    78  	var wg sync.WaitGroup
    79  	for i, w := range workers {
    80  		wg.Add(1)
    81  		go func(i int, w *preemptWorker) {
    82  			defer wg.Done()
    83  			errs[i] = w.run(&stop, &w.progress)
    84  		}(i, w)
    85  	}
    86  
    87  	numGCs := 8
    88  	if testing.Short() {
    89  		numGCs = 4
    90  	}
    91  	for range numGCs {
    92  		// Wait until every worker has advanced since the previous GC so we
    93  		// exercise multiple distinct save/restore cycles per worker.
    94  		for _, w := range workers {
    95  			prev := w.progress.Load()
    96  			for w.progress.Load() == prev {
    97  				runtime.Gosched()
    98  			}
    99  		}
   100  		runtime.GC()
   101  	}
   102  
   103  	stop.Store(1)
   104  	wg.Wait()
   105  
   106  	for i, err := range errs {
   107  		if err != nil {
   108  			t.Errorf("worker %d: %v", i, err)
   109  		}
   110  	}
   111  }
   112  
   113  func testAsyncPreemptY(t *testing.T) {
   114  	if !archsimd.X86.AVX2() {
   115  		t.Skip("requires AVX2")
   116  	}
   117  	runPreemptWorkers(t, []*preemptWorker{
   118  		{run: func(stop *atomic.Uint32, progress *atomic.Uint64) error {
   119  			return runYLoop(stop, progress, 0x1111_0000_0000_0001)
   120  		}},
   121  		{run: func(stop *atomic.Uint32, progress *atomic.Uint64) error {
   122  			return runYLoop(stop, progress, 0x2222_0000_0000_0003)
   123  		}},
   124  	})
   125  }
   126  
   127  func testAsyncPreemptZ(t *testing.T) {
   128  	if !archsimd.X86.AVX512() {
   129  		t.Skip("requires AVX-512")
   130  	}
   131  	runPreemptWorkers(t, []*preemptWorker{
   132  		{run: func(stop *atomic.Uint32, progress *atomic.Uint64) error {
   133  			return runZLoop(stop, progress, 0x3333_0000_0000_0001)
   134  		}},
   135  		{run: func(stop *atomic.Uint32, progress *atomic.Uint64) error {
   136  			return runZLoop(stop, progress, 0x4444_0000_0000_0005)
   137  		}},
   138  	})
   139  }
   140  
   141  func testAsyncPreemptXHiWithMask(t *testing.T) {
   142  	if !archsimd.X86.AVX512() {
   143  		t.Skip("requires AVX-512")
   144  	}
   145  	runPreemptWorkers(t, []*preemptWorker{
   146  		{run: func(stop *atomic.Uint32, progress *atomic.Uint64) error {
   147  			return runX28MaskedLoop(stop, progress, 0x5555_0000_0000_0001, 0b11)
   148  		}},
   149  		{run: func(stop *atomic.Uint32, progress *atomic.Uint64) error {
   150  			return runX28MaskedLoop(stop, progress, 0x6666_0000_0000_0007, 0b01)
   151  		}},
   152  	})
   153  }
   154  
   155  func testAsyncPreemptMask(t *testing.T) {
   156  	if !archsimd.X86.AVX512() {
   157  		t.Skip("requires AVX-512")
   158  	}
   159  	runPreemptWorkers(t, []*preemptWorker{
   160  		{run: func(stop *atomic.Uint32, progress *atomic.Uint64) error {
   161  			return runMaskLoop(stop, progress, 0x1234_5678_9abc_def0)
   162  		}},
   163  		{run: func(stop *atomic.Uint32, progress *atomic.Uint64) error {
   164  			return runMaskLoop(stop, progress, 0xfedc_ba98_7654_3210)
   165  		}},
   166  	})
   167  }
   168  
   169  func testAsyncPreemptZAndMask(t *testing.T) {
   170  	if !archsimd.X86.AVX512() {
   171  		t.Skip("requires AVX-512")
   172  	}
   173  	runPreemptWorkers(t, []*preemptWorker{
   174  		{run: func(stop *atomic.Uint32, progress *atomic.Uint64) error {
   175  			return runZAndMaskLoop(stop, progress, 0x7777_0000_0000_0001, 0xaa55_cc33_f00f_9669)
   176  		}},
   177  		{run: func(stop *atomic.Uint32, progress *atomic.Uint64) error {
   178  			return runZAndMaskLoop(stop, progress, 0x8888_0000_0000_0009, 0x55aa_33cc_0ff0_6996)
   179  		}},
   180  	})
   181  }
   182  
   183  func testAsyncPreemptMixedCleanDirty(t *testing.T) {
   184  	if !archsimd.X86.AVX2() {
   185  		t.Skip("requires AVX2")
   186  	}
   187  	workers := []*preemptWorker{
   188  		{run: func(stop *atomic.Uint32, progress *atomic.Uint64) error {
   189  			return runX12Loop(stop, progress, 0x9999_0000_0000_0001)
   190  		}},
   191  		{run: func(stop *atomic.Uint32, progress *atomic.Uint64) error {
   192  			return runYLoop(stop, progress, 0xaaaa_0000_0000_0003)
   193  		}},
   194  	}
   195  	if archsimd.X86.AVX512() {
   196  		workers = append(workers,
   197  			&preemptWorker{run: func(stop *atomic.Uint32, progress *atomic.Uint64) error {
   198  				return runZAndMaskLoop(stop, progress, 0xbbbb_0000_0000_0005, 0xc3c3_a5a5_5a5a_3c3c)
   199  			}},
   200  		)
   201  	}
   202  	runPreemptWorkers(t, workers)
   203  }
   204  
   205  func makeInit2(seed uint64, n int) [][2]int64 {
   206  	inits := make([][2]int64, n)
   207  	for i := range n {
   208  		for j := range 2 {
   209  			inits[i][j] = int64(seed) + int64(i*2+j)*0x1000_0001 + int64(j+1)<<40
   210  		}
   211  	}
   212  	return inits
   213  }
   214  
   215  func makeInit4(seed uint64, n int) [][4]int64 {
   216  	inits := make([][4]int64, n)
   217  	for i := range n {
   218  		for j := range 4 {
   219  			inits[i][j] = int64(seed) + int64(i*4+j)*0x1000_0001 + int64(j+1)<<40
   220  		}
   221  	}
   222  	return inits
   223  }
   224  
   225  func makeInit8(seed uint64, n int) [][8]int64 {
   226  	inits := make([][8]int64, n)
   227  	for i := range n {
   228  		for j := range 8 {
   229  			inits[i][j] = int64(seed) + int64(i*8+j)*0x1000_0001 + int64(j+1)<<40
   230  		}
   231  	}
   232  	return inits
   233  }
   234  
   235  // runX12Loop keeps 13 Int64x2 values (12 accumulators + delta) live in X0-X14
   236  // with no YMM/ZMM/K instructions.
   237  //
   238  //go:noinline
   239  func runX12Loop(stop *atomic.Uint32, progress *atomic.Uint64, seed uint64) error {
   240  	inits := makeInit2(seed, 12)
   241  	deltaArr := [2]int64{1, 3}
   242  	d := archsimd.LoadInt64x2Array(&deltaArr)
   243  
   244  	v0 := archsimd.LoadInt64x2Array(&inits[0])
   245  	v1 := archsimd.LoadInt64x2Array(&inits[1])
   246  	v2 := archsimd.LoadInt64x2Array(&inits[2])
   247  	v3 := archsimd.LoadInt64x2Array(&inits[3])
   248  	v4 := archsimd.LoadInt64x2Array(&inits[4])
   249  	v5 := archsimd.LoadInt64x2Array(&inits[5])
   250  	v6 := archsimd.LoadInt64x2Array(&inits[6])
   251  	v7 := archsimd.LoadInt64x2Array(&inits[7])
   252  	v8 := archsimd.LoadInt64x2Array(&inits[8])
   253  	v9 := archsimd.LoadInt64x2Array(&inits[9])
   254  	v10 := archsimd.LoadInt64x2Array(&inits[10])
   255  	v11 := archsimd.LoadInt64x2Array(&inits[11])
   256  
   257  	var iters int64
   258  	for stop.Load() == 0 {
   259  		iters++
   260  		v0 = v0.Add(d)
   261  		v1 = v1.Add(d)
   262  		v2 = v2.Add(d)
   263  		v3 = v3.Add(d)
   264  		v4 = v4.Add(d)
   265  		v5 = v5.Add(d)
   266  		v6 = v6.Add(d)
   267  		v7 = v7.Add(d)
   268  		v8 = v8.Add(d)
   269  		v9 = v9.Add(d)
   270  		v10 = v10.Add(d)
   271  		v11 = v11.Add(d)
   272  		if iters&1023 == 0 {
   273  			progress.Store(uint64(iters))
   274  		}
   275  	}
   276  
   277  	var got [12][2]int64
   278  	v0.StoreArray(&got[0])
   279  	v1.StoreArray(&got[1])
   280  	v2.StoreArray(&got[2])
   281  	v3.StoreArray(&got[3])
   282  	v4.StoreArray(&got[4])
   283  	v5.StoreArray(&got[5])
   284  	v6.StoreArray(&got[6])
   285  	v7.StoreArray(&got[7])
   286  	v8.StoreArray(&got[8])
   287  	v9.StoreArray(&got[9])
   288  	v10.StoreArray(&got[10])
   289  	v11.StoreArray(&got[11])
   290  
   291  	for i := range got {
   292  		for j := range 2 {
   293  			want := inits[i][j] + iters*deltaArr[j]
   294  			if got[i][j] != want {
   295  				return fmt.Errorf("X12 reg %d lane %d after %d iters: got %#x, want %#x", i, j, iters, got[i][j], want)
   296  			}
   297  		}
   298  	}
   299  	return nil
   300  }
   301  
   302  // runYLoop keeps 14 Int64x4 values (13 accumulators + delta) live in Y0-Y14.
   303  //
   304  //go:noinline
   305  func runYLoop(stop *atomic.Uint32, progress *atomic.Uint64, seed uint64) error {
   306  	inits := makeInit4(seed, 13)
   307  	deltaArr := [4]int64{1, 3, 5, 7}
   308  	d := archsimd.LoadInt64x4Array(&deltaArr)
   309  
   310  	v0 := archsimd.LoadInt64x4Array(&inits[0])
   311  	v1 := archsimd.LoadInt64x4Array(&inits[1])
   312  	v2 := archsimd.LoadInt64x4Array(&inits[2])
   313  	v3 := archsimd.LoadInt64x4Array(&inits[3])
   314  	v4 := archsimd.LoadInt64x4Array(&inits[4])
   315  	v5 := archsimd.LoadInt64x4Array(&inits[5])
   316  	v6 := archsimd.LoadInt64x4Array(&inits[6])
   317  	v7 := archsimd.LoadInt64x4Array(&inits[7])
   318  	v8 := archsimd.LoadInt64x4Array(&inits[8])
   319  	v9 := archsimd.LoadInt64x4Array(&inits[9])
   320  	v10 := archsimd.LoadInt64x4Array(&inits[10])
   321  	v11 := archsimd.LoadInt64x4Array(&inits[11])
   322  	v12 := archsimd.LoadInt64x4Array(&inits[12])
   323  
   324  	var iters int64
   325  	for stop.Load() == 0 {
   326  		iters++
   327  		v0 = v0.Add(d)
   328  		v1 = v1.Add(d)
   329  		v2 = v2.Add(d)
   330  		v3 = v3.Add(d)
   331  		v4 = v4.Add(d)
   332  		v5 = v5.Add(d)
   333  		v6 = v6.Add(d)
   334  		v7 = v7.Add(d)
   335  		v8 = v8.Add(d)
   336  		v9 = v9.Add(d)
   337  		v10 = v10.Add(d)
   338  		v11 = v11.Add(d)
   339  		v12 = v12.Add(d)
   340  		if iters&1023 == 0 {
   341  			progress.Store(uint64(iters))
   342  		}
   343  	}
   344  
   345  	var got [13][4]int64
   346  	v0.StoreArray(&got[0])
   347  	v1.StoreArray(&got[1])
   348  	v2.StoreArray(&got[2])
   349  	v3.StoreArray(&got[3])
   350  	v4.StoreArray(&got[4])
   351  	v5.StoreArray(&got[5])
   352  	v6.StoreArray(&got[6])
   353  	v7.StoreArray(&got[7])
   354  	v8.StoreArray(&got[8])
   355  	v9.StoreArray(&got[9])
   356  	v10.StoreArray(&got[10])
   357  	v11.StoreArray(&got[11])
   358  	v12.StoreArray(&got[12])
   359  
   360  	for i := range got {
   361  		for j := range 4 {
   362  			want := inits[i][j] + iters*deltaArr[j]
   363  			if got[i][j] != want {
   364  				return fmt.Errorf("Y reg %d lane %d after %d iters: got %#x, want %#x", i, j, iters, got[i][j], want)
   365  			}
   366  		}
   367  	}
   368  	return nil
   369  }
   370  
   371  // runZLoop keeps 31 Int64x8 values (30 accumulators + delta) live across
   372  // all allocatable Z registers (Z0-Z14 and Z16-Z31).
   373  //
   374  //go:noinline
   375  func runZLoop(stop *atomic.Uint32, progress *atomic.Uint64, seed uint64) error {
   376  	if !archsimd.X86.AVX512() {
   377  		return nil
   378  	}
   379  	inits := makeInit8(seed, 30)
   380  	deltaArr := [8]int64{1, 3, 5, 7, 11, 13, 17, 19}
   381  	d := archsimd.LoadInt64x8Array(&deltaArr)
   382  
   383  	v0 := archsimd.LoadInt64x8Array(&inits[0])
   384  	v1 := archsimd.LoadInt64x8Array(&inits[1])
   385  	v2 := archsimd.LoadInt64x8Array(&inits[2])
   386  	v3 := archsimd.LoadInt64x8Array(&inits[3])
   387  	v4 := archsimd.LoadInt64x8Array(&inits[4])
   388  	v5 := archsimd.LoadInt64x8Array(&inits[5])
   389  	v6 := archsimd.LoadInt64x8Array(&inits[6])
   390  	v7 := archsimd.LoadInt64x8Array(&inits[7])
   391  	v8 := archsimd.LoadInt64x8Array(&inits[8])
   392  	v9 := archsimd.LoadInt64x8Array(&inits[9])
   393  	v10 := archsimd.LoadInt64x8Array(&inits[10])
   394  	v11 := archsimd.LoadInt64x8Array(&inits[11])
   395  	v12 := archsimd.LoadInt64x8Array(&inits[12])
   396  	v13 := archsimd.LoadInt64x8Array(&inits[13])
   397  	v14 := archsimd.LoadInt64x8Array(&inits[14])
   398  	v15 := archsimd.LoadInt64x8Array(&inits[15])
   399  	v16 := archsimd.LoadInt64x8Array(&inits[16])
   400  	v17 := archsimd.LoadInt64x8Array(&inits[17])
   401  	v18 := archsimd.LoadInt64x8Array(&inits[18])
   402  	v19 := archsimd.LoadInt64x8Array(&inits[19])
   403  	v20 := archsimd.LoadInt64x8Array(&inits[20])
   404  	v21 := archsimd.LoadInt64x8Array(&inits[21])
   405  	v22 := archsimd.LoadInt64x8Array(&inits[22])
   406  	v23 := archsimd.LoadInt64x8Array(&inits[23])
   407  	v24 := archsimd.LoadInt64x8Array(&inits[24])
   408  	v25 := archsimd.LoadInt64x8Array(&inits[25])
   409  	v26 := archsimd.LoadInt64x8Array(&inits[26])
   410  	v27 := archsimd.LoadInt64x8Array(&inits[27])
   411  	v28 := archsimd.LoadInt64x8Array(&inits[28])
   412  	v29 := archsimd.LoadInt64x8Array(&inits[29])
   413  
   414  	var iters int64
   415  	for stop.Load() == 0 {
   416  		iters++
   417  		v0 = v0.Add(d)
   418  		v1 = v1.Add(d)
   419  		v2 = v2.Add(d)
   420  		v3 = v3.Add(d)
   421  		v4 = v4.Add(d)
   422  		v5 = v5.Add(d)
   423  		v6 = v6.Add(d)
   424  		v7 = v7.Add(d)
   425  		v8 = v8.Add(d)
   426  		v9 = v9.Add(d)
   427  		v10 = v10.Add(d)
   428  		v11 = v11.Add(d)
   429  		v12 = v12.Add(d)
   430  		v13 = v13.Add(d)
   431  		v14 = v14.Add(d)
   432  		v15 = v15.Add(d)
   433  		v16 = v16.Add(d)
   434  		v17 = v17.Add(d)
   435  		v18 = v18.Add(d)
   436  		v19 = v19.Add(d)
   437  		v20 = v20.Add(d)
   438  		v21 = v21.Add(d)
   439  		v22 = v22.Add(d)
   440  		v23 = v23.Add(d)
   441  		v24 = v24.Add(d)
   442  		v25 = v25.Add(d)
   443  		v26 = v26.Add(d)
   444  		v27 = v27.Add(d)
   445  		v28 = v28.Add(d)
   446  		v29 = v29.Add(d)
   447  		if iters&1023 == 0 {
   448  			progress.Store(uint64(iters))
   449  		}
   450  	}
   451  
   452  	var got [30][8]int64
   453  	v0.StoreArray(&got[0])
   454  	v1.StoreArray(&got[1])
   455  	v2.StoreArray(&got[2])
   456  	v3.StoreArray(&got[3])
   457  	v4.StoreArray(&got[4])
   458  	v5.StoreArray(&got[5])
   459  	v6.StoreArray(&got[6])
   460  	v7.StoreArray(&got[7])
   461  	v8.StoreArray(&got[8])
   462  	v9.StoreArray(&got[9])
   463  	v10.StoreArray(&got[10])
   464  	v11.StoreArray(&got[11])
   465  	v12.StoreArray(&got[12])
   466  	v13.StoreArray(&got[13])
   467  	v14.StoreArray(&got[14])
   468  	v15.StoreArray(&got[15])
   469  	v16.StoreArray(&got[16])
   470  	v17.StoreArray(&got[17])
   471  	v18.StoreArray(&got[18])
   472  	v19.StoreArray(&got[19])
   473  	v20.StoreArray(&got[20])
   474  	v21.StoreArray(&got[21])
   475  	v22.StoreArray(&got[22])
   476  	v23.StoreArray(&got[23])
   477  	v24.StoreArray(&got[24])
   478  	v25.StoreArray(&got[25])
   479  	v26.StoreArray(&got[26])
   480  	v27.StoreArray(&got[27])
   481  	v28.StoreArray(&got[28])
   482  	v29.StoreArray(&got[29])
   483  
   484  	for i := range got {
   485  		for j := range 8 {
   486  			want := inits[i][j] + iters*deltaArr[j]
   487  			if got[i][j] != want {
   488  				return fmt.Errorf("Z reg %d lane %d after %d iters: got %#x, want %#x", i, j, iters, got[i][j], want)
   489  			}
   490  		}
   491  	}
   492  	return nil
   493  }
   494  
   495  // runX28MaskedLoop uses EVEX 128-bit masked adds to keep 29 Int64x2 values live
   496  // across X0-X14 and X16-X31 (Hi16_ZMM) along with an opmask register while
   497  // YMM_Hi128 and ZMM_Hi256 remain in INIT state.
   498  //
   499  //go:noinline
   500  func runX28MaskedLoop(stop *atomic.Uint32, progress *atomic.Uint64, seed uint64, maskBits uint8) error {
   501  	if !archsimd.X86.AVX512() {
   502  		return nil
   503  	}
   504  	inits := makeInit2(seed, 28)
   505  	deltaArr := [2]int64{1, 3}
   506  	d := archsimd.LoadInt64x2Array(&deltaArr)
   507  	m := archsimd.Mask64x2FromBits(maskBits)
   508  
   509  	v0 := archsimd.LoadInt64x2Array(&inits[0])
   510  	v1 := archsimd.LoadInt64x2Array(&inits[1])
   511  	v2 := archsimd.LoadInt64x2Array(&inits[2])
   512  	v3 := archsimd.LoadInt64x2Array(&inits[3])
   513  	v4 := archsimd.LoadInt64x2Array(&inits[4])
   514  	v5 := archsimd.LoadInt64x2Array(&inits[5])
   515  	v6 := archsimd.LoadInt64x2Array(&inits[6])
   516  	v7 := archsimd.LoadInt64x2Array(&inits[7])
   517  	v8 := archsimd.LoadInt64x2Array(&inits[8])
   518  	v9 := archsimd.LoadInt64x2Array(&inits[9])
   519  	v10 := archsimd.LoadInt64x2Array(&inits[10])
   520  	v11 := archsimd.LoadInt64x2Array(&inits[11])
   521  	v12 := archsimd.LoadInt64x2Array(&inits[12])
   522  	v13 := archsimd.LoadInt64x2Array(&inits[13])
   523  	v14 := archsimd.LoadInt64x2Array(&inits[14])
   524  	v15 := archsimd.LoadInt64x2Array(&inits[15])
   525  	v16 := archsimd.LoadInt64x2Array(&inits[16])
   526  	v17 := archsimd.LoadInt64x2Array(&inits[17])
   527  	v18 := archsimd.LoadInt64x2Array(&inits[18])
   528  	v19 := archsimd.LoadInt64x2Array(&inits[19])
   529  	v20 := archsimd.LoadInt64x2Array(&inits[20])
   530  	v21 := archsimd.LoadInt64x2Array(&inits[21])
   531  	v22 := archsimd.LoadInt64x2Array(&inits[22])
   532  	v23 := archsimd.LoadInt64x2Array(&inits[23])
   533  	v24 := archsimd.LoadInt64x2Array(&inits[24])
   534  	v25 := archsimd.LoadInt64x2Array(&inits[25])
   535  	v26 := archsimd.LoadInt64x2Array(&inits[26])
   536  	v27 := archsimd.LoadInt64x2Array(&inits[27])
   537  
   538  	var iters int64
   539  	for stop.Load() == 0 {
   540  		iters++
   541  		v0 = v0.Add(d).Merge(v0, m)
   542  		v1 = v1.Add(d).Merge(v1, m)
   543  		v2 = v2.Add(d).Merge(v2, m)
   544  		v3 = v3.Add(d).Merge(v3, m)
   545  		v4 = v4.Add(d).Merge(v4, m)
   546  		v5 = v5.Add(d).Merge(v5, m)
   547  		v6 = v6.Add(d).Merge(v6, m)
   548  		v7 = v7.Add(d).Merge(v7, m)
   549  		v8 = v8.Add(d).Merge(v8, m)
   550  		v9 = v9.Add(d).Merge(v9, m)
   551  		v10 = v10.Add(d).Merge(v10, m)
   552  		v11 = v11.Add(d).Merge(v11, m)
   553  		v12 = v12.Add(d).Merge(v12, m)
   554  		v13 = v13.Add(d).Merge(v13, m)
   555  		v14 = v14.Add(d).Merge(v14, m)
   556  		v15 = v15.Add(d).Merge(v15, m)
   557  		v16 = v16.Add(d).Merge(v16, m)
   558  		v17 = v17.Add(d).Merge(v17, m)
   559  		v18 = v18.Add(d).Merge(v18, m)
   560  		v19 = v19.Add(d).Merge(v19, m)
   561  		v20 = v20.Add(d).Merge(v20, m)
   562  		v21 = v21.Add(d).Merge(v21, m)
   563  		v22 = v22.Add(d).Merge(v22, m)
   564  		v23 = v23.Add(d).Merge(v23, m)
   565  		v24 = v24.Add(d).Merge(v24, m)
   566  		v25 = v25.Add(d).Merge(v25, m)
   567  		v26 = v26.Add(d).Merge(v26, m)
   568  		v27 = v27.Add(d).Merge(v27, m)
   569  		if iters&1023 == 0 {
   570  			progress.Store(uint64(iters))
   571  		}
   572  	}
   573  
   574  	var got [28][2]int64
   575  	v0.StoreArray(&got[0])
   576  	v1.StoreArray(&got[1])
   577  	v2.StoreArray(&got[2])
   578  	v3.StoreArray(&got[3])
   579  	v4.StoreArray(&got[4])
   580  	v5.StoreArray(&got[5])
   581  	v6.StoreArray(&got[6])
   582  	v7.StoreArray(&got[7])
   583  	v8.StoreArray(&got[8])
   584  	v9.StoreArray(&got[9])
   585  	v10.StoreArray(&got[10])
   586  	v11.StoreArray(&got[11])
   587  	v12.StoreArray(&got[12])
   588  	v13.StoreArray(&got[13])
   589  	v14.StoreArray(&got[14])
   590  	v15.StoreArray(&got[15])
   591  	v16.StoreArray(&got[16])
   592  	v17.StoreArray(&got[17])
   593  	v18.StoreArray(&got[18])
   594  	v19.StoreArray(&got[19])
   595  	v20.StoreArray(&got[20])
   596  	v21.StoreArray(&got[21])
   597  	v22.StoreArray(&got[22])
   598  	v23.StoreArray(&got[23])
   599  	v24.StoreArray(&got[24])
   600  	v25.StoreArray(&got[25])
   601  	v26.StoreArray(&got[26])
   602  	v27.StoreArray(&got[27])
   603  
   604  	for i := range got {
   605  		for j := range 2 {
   606  			want := inits[i][j]
   607  			if maskBits&(1<<j) != 0 {
   608  				want += iters * deltaArr[j]
   609  			}
   610  			if got[i][j] != want {
   611  				return fmt.Errorf("X28 reg %d lane %d after %d iters: got %#x, want %#x", i, j, iters, got[i][j], want)
   612  			}
   613  		}
   614  	}
   615  	return nil
   616  }
   617  
   618  // runMaskLoop keeps all 7 allocatable 64-bit opmask registers (K1-K7) live
   619  // across masked 512-bit byte vector adds, checking every bit of every mask.
   620  //
   621  //go:noinline
   622  func runMaskLoop(stop *atomic.Uint32, progress *atomic.Uint64, seed uint64) error {
   623  	if !archsimd.X86.AVX512() {
   624  		return nil
   625  	}
   626  	mBits := [7]uint64{
   627  		seed ^ 0xaaaa_aaaa_aaaa_aaaa,
   628  		seed ^ 0x5555_5555_5555_5555,
   629  		seed ^ 0xcccc_cccc_cccc_cccc,
   630  		seed ^ 0x3333_3333_3333_3333,
   631  		seed ^ 0xf0f0_f0f0_f0f0_f0f0,
   632  		seed ^ 0x0f0f_0f0f_0f0f_0f0f,
   633  		seed ^ 0x9696_6969_a5a5_5a5a,
   634  	}
   635  	m0 := archsimd.Mask8x64FromBits(mBits[0])
   636  	m1 := archsimd.Mask8x64FromBits(mBits[1])
   637  	m2 := archsimd.Mask8x64FromBits(mBits[2])
   638  	m3 := archsimd.Mask8x64FromBits(mBits[3])
   639  	m4 := archsimd.Mask8x64FromBits(mBits[4])
   640  	m5 := archsimd.Mask8x64FromBits(mBits[5])
   641  	m6 := archsimd.Mask8x64FromBits(mBits[6])
   642  
   643  	var inits [7][64]int8
   644  	for i := range inits {
   645  		for j := range 64 {
   646  			inits[i][j] = int8(seed + uint64(i*64+j)*7)
   647  		}
   648  	}
   649  	var deltaArr [64]int8
   650  	for j := range 64 {
   651  		deltaArr[j] = int8(2*j + 1) // odd so every lane changes every iteration
   652  	}
   653  	d := archsimd.LoadInt8x64Array(&deltaArr)
   654  
   655  	v0 := archsimd.LoadInt8x64Array(&inits[0])
   656  	v1 := archsimd.LoadInt8x64Array(&inits[1])
   657  	v2 := archsimd.LoadInt8x64Array(&inits[2])
   658  	v3 := archsimd.LoadInt8x64Array(&inits[3])
   659  	v4 := archsimd.LoadInt8x64Array(&inits[4])
   660  	v5 := archsimd.LoadInt8x64Array(&inits[5])
   661  	v6 := archsimd.LoadInt8x64Array(&inits[6])
   662  
   663  	var iters uint64
   664  	for stop.Load() == 0 {
   665  		iters++
   666  		v0 = v0.Add(d).Merge(v0, m0)
   667  		v1 = v1.Add(d).Merge(v1, m1)
   668  		v2 = v2.Add(d).Merge(v2, m2)
   669  		v3 = v3.Add(d).Merge(v3, m3)
   670  		v4 = v4.Add(d).Merge(v4, m4)
   671  		v5 = v5.Add(d).Merge(v5, m5)
   672  		v6 = v6.Add(d).Merge(v6, m6)
   673  		if iters&1023 == 0 {
   674  			progress.Store(iters)
   675  		}
   676  	}
   677  
   678  	var got [7][64]int8
   679  	v0.StoreArray(&got[0])
   680  	v1.StoreArray(&got[1])
   681  	v2.StoreArray(&got[2])
   682  	v3.StoreArray(&got[3])
   683  	v4.StoreArray(&got[4])
   684  	v5.StoreArray(&got[5])
   685  	v6.StoreArray(&got[6])
   686  
   687  	for i := range got {
   688  		for j := range 64 {
   689  			want := inits[i][j]
   690  			if mBits[i]&(1<<uint(j)) != 0 {
   691  				want += int8(iters) * deltaArr[j]
   692  			}
   693  			if got[i][j] != want {
   694  				return fmt.Errorf("Mask reg %d lane %d after %d iters: got %#x, want %#x", i, j, iters, uint8(got[i][j]), uint8(want))
   695  			}
   696  		}
   697  	}
   698  	gotMasks := [7]uint64{
   699  		m0.ToBits(),
   700  		m1.ToBits(),
   701  		m2.ToBits(),
   702  		m3.ToBits(),
   703  		m4.ToBits(),
   704  		m5.ToBits(),
   705  		m6.ToBits(),
   706  	}
   707  	for i := range gotMasks {
   708  		if gotMasks[i] != mBits[i] {
   709  			return fmt.Errorf("K reg %d after %d iters: got %#016x, want %#016x", i, iters, gotMasks[i], mBits[i])
   710  		}
   711  	}
   712  	return nil
   713  }
   714  
   715  // runZAndMaskLoop keeps Z0-Z14, Z16-Z31, and K1-K7 live simultaneously.
   716  //
   717  //go:noinline
   718  func runZAndMaskLoop(stop *atomic.Uint32, progress *atomic.Uint64, seed uint64, maskSeed uint64) error {
   719  	if !archsimd.X86.AVX512() {
   720  		return nil
   721  	}
   722  	inits := makeInit8(seed, 28)
   723  	deltaArr := [8]int64{1, 3, 5, 7, 11, 13, 17, 19}
   724  	d := archsimd.LoadInt64x8Array(&deltaArr)
   725  
   726  	mBits := [7]uint8{
   727  		uint8(maskSeed>>0) ^ 0xaa,
   728  		uint8(maskSeed>>8) ^ 0x55,
   729  		uint8(maskSeed>>16) ^ 0xcc,
   730  		uint8(maskSeed>>24) ^ 0x33,
   731  		uint8(maskSeed>>32) ^ 0xf0,
   732  		uint8(maskSeed>>40) ^ 0x0f,
   733  		uint8(maskSeed>>48) ^ 0x96,
   734  	}
   735  	m0 := archsimd.Mask64x8FromBits(mBits[0])
   736  	m1 := archsimd.Mask64x8FromBits(mBits[1])
   737  	m2 := archsimd.Mask64x8FromBits(mBits[2])
   738  	m3 := archsimd.Mask64x8FromBits(mBits[3])
   739  	m4 := archsimd.Mask64x8FromBits(mBits[4])
   740  	m5 := archsimd.Mask64x8FromBits(mBits[5])
   741  	m6 := archsimd.Mask64x8FromBits(mBits[6])
   742  
   743  	v0 := archsimd.LoadInt64x8Array(&inits[0])
   744  	v1 := archsimd.LoadInt64x8Array(&inits[1])
   745  	v2 := archsimd.LoadInt64x8Array(&inits[2])
   746  	v3 := archsimd.LoadInt64x8Array(&inits[3])
   747  	v4 := archsimd.LoadInt64x8Array(&inits[4])
   748  	v5 := archsimd.LoadInt64x8Array(&inits[5])
   749  	v6 := archsimd.LoadInt64x8Array(&inits[6])
   750  	v7 := archsimd.LoadInt64x8Array(&inits[7])
   751  	v8 := archsimd.LoadInt64x8Array(&inits[8])
   752  	v9 := archsimd.LoadInt64x8Array(&inits[9])
   753  	v10 := archsimd.LoadInt64x8Array(&inits[10])
   754  	v11 := archsimd.LoadInt64x8Array(&inits[11])
   755  	v12 := archsimd.LoadInt64x8Array(&inits[12])
   756  	v13 := archsimd.LoadInt64x8Array(&inits[13])
   757  	v14 := archsimd.LoadInt64x8Array(&inits[14])
   758  	v15 := archsimd.LoadInt64x8Array(&inits[15])
   759  	v16 := archsimd.LoadInt64x8Array(&inits[16])
   760  	v17 := archsimd.LoadInt64x8Array(&inits[17])
   761  	v18 := archsimd.LoadInt64x8Array(&inits[18])
   762  	v19 := archsimd.LoadInt64x8Array(&inits[19])
   763  	v20 := archsimd.LoadInt64x8Array(&inits[20])
   764  	v21 := archsimd.LoadInt64x8Array(&inits[21])
   765  	v22 := archsimd.LoadInt64x8Array(&inits[22])
   766  	v23 := archsimd.LoadInt64x8Array(&inits[23])
   767  	v24 := archsimd.LoadInt64x8Array(&inits[24])
   768  	v25 := archsimd.LoadInt64x8Array(&inits[25])
   769  	v26 := archsimd.LoadInt64x8Array(&inits[26])
   770  	v27 := archsimd.LoadInt64x8Array(&inits[27])
   771  
   772  	var iters int64
   773  	for stop.Load() == 0 {
   774  		iters++
   775  		v0 = v0.Add(d).Merge(v0, m0)
   776  		v1 = v1.Add(d).Merge(v1, m1)
   777  		v2 = v2.Add(d).Merge(v2, m2)
   778  		v3 = v3.Add(d).Merge(v3, m3)
   779  		v4 = v4.Add(d).Merge(v4, m4)
   780  		v5 = v5.Add(d).Merge(v5, m5)
   781  		v6 = v6.Add(d).Merge(v6, m6)
   782  		v7 = v7.Add(d).Merge(v7, m0)
   783  		v8 = v8.Add(d).Merge(v8, m1)
   784  		v9 = v9.Add(d).Merge(v9, m2)
   785  		v10 = v10.Add(d).Merge(v10, m3)
   786  		v11 = v11.Add(d).Merge(v11, m4)
   787  		v12 = v12.Add(d).Merge(v12, m5)
   788  		v13 = v13.Add(d).Merge(v13, m6)
   789  		v14 = v14.Add(d).Merge(v14, m0)
   790  		v15 = v15.Add(d).Merge(v15, m1)
   791  		v16 = v16.Add(d).Merge(v16, m2)
   792  		v17 = v17.Add(d).Merge(v17, m3)
   793  		v18 = v18.Add(d).Merge(v18, m4)
   794  		v19 = v19.Add(d).Merge(v19, m5)
   795  		v20 = v20.Add(d).Merge(v20, m6)
   796  		v21 = v21.Add(d).Merge(v21, m0)
   797  		v22 = v22.Add(d).Merge(v22, m1)
   798  		v23 = v23.Add(d).Merge(v23, m2)
   799  		v24 = v24.Add(d).Merge(v24, m3)
   800  		v25 = v25.Add(d).Merge(v25, m4)
   801  		v26 = v26.Add(d).Merge(v26, m5)
   802  		v27 = v27.Add(d).Merge(v27, m6)
   803  		if iters&1023 == 0 {
   804  			progress.Store(uint64(iters))
   805  		}
   806  	}
   807  
   808  	var got [28][8]int64
   809  	v0.StoreArray(&got[0])
   810  	v1.StoreArray(&got[1])
   811  	v2.StoreArray(&got[2])
   812  	v3.StoreArray(&got[3])
   813  	v4.StoreArray(&got[4])
   814  	v5.StoreArray(&got[5])
   815  	v6.StoreArray(&got[6])
   816  	v7.StoreArray(&got[7])
   817  	v8.StoreArray(&got[8])
   818  	v9.StoreArray(&got[9])
   819  	v10.StoreArray(&got[10])
   820  	v11.StoreArray(&got[11])
   821  	v12.StoreArray(&got[12])
   822  	v13.StoreArray(&got[13])
   823  	v14.StoreArray(&got[14])
   824  	v15.StoreArray(&got[15])
   825  	v16.StoreArray(&got[16])
   826  	v17.StoreArray(&got[17])
   827  	v18.StoreArray(&got[18])
   828  	v19.StoreArray(&got[19])
   829  	v20.StoreArray(&got[20])
   830  	v21.StoreArray(&got[21])
   831  	v22.StoreArray(&got[22])
   832  	v23.StoreArray(&got[23])
   833  	v24.StoreArray(&got[24])
   834  	v25.StoreArray(&got[25])
   835  	v26.StoreArray(&got[26])
   836  	v27.StoreArray(&got[27])
   837  
   838  	for i := range got {
   839  		mb := mBits[i%len(mBits)]
   840  		for j := range 8 {
   841  			want := inits[i][j]
   842  			if mb&(1<<uint(j)) != 0 {
   843  				want += iters * deltaArr[j]
   844  			}
   845  			if got[i][j] != want {
   846  				return fmt.Errorf("Z+Mask Z reg %d lane %d after %d iters: got %#x, want %#x", i, j, iters, got[i][j], want)
   847  			}
   848  		}
   849  	}
   850  
   851  	mGot := [7]uint8{
   852  		m0.ToBits(),
   853  		m1.ToBits(),
   854  		m2.ToBits(),
   855  		m3.ToBits(),
   856  		m4.ToBits(),
   857  		m5.ToBits(),
   858  		m6.ToBits(),
   859  	}
   860  	for i := range mGot {
   861  		if mGot[i] != mBits[i] {
   862  			return fmt.Errorf("Z+Mask K reg %d after %d iters: got %#02x, want %#02x", i, iters, mGot[i], mBits[i])
   863  		}
   864  	}
   865  	return nil
   866  }
   867  

View as plain text