Source file src/simd/archsimd/internal/simd_test/ternlog_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.simdstable && amd64
     6  
     7  package simd_test
     8  
     9  import (
    10  	"simd/archsimd"
    11  	"testing"
    12  )
    13  
    14  //go:noinline
    15  func f(a, b, c archsimd.Int32x16) archsimd.Int32x16 {
    16  	return a.AndNot(b).Xor(c) // a &^ b ^ c, fused into one VPTERNLOGD
    17  }
    18  
    19  func TestTernLogAndnot(t *testing.T) {
    20  	if !archsimd.X86.AVX512() {
    21  		t.Skip("needs AVX-512")
    22  		return
    23  	}
    24  	A := int32(0b1100)
    25  	B := int32(0b1010)
    26  	C := int32(0)
    27  	a := archsimd.BroadcastInt32x16(A)
    28  	b := archsimd.BroadcastInt32x16(B)
    29  	c := archsimd.BroadcastInt32x16(C)
    30  	var r [16]int32
    31  	f(a, b, c).StoreArray(&r)
    32  	want := A &^ B
    33  	got := r[0]
    34  	if want != got {
    35  		t.Errorf("got %04b, want %04b", got, want)
    36  	}
    37  }
    38  
    39  //go:noinline
    40  func ternLoadD128(x, y, z *[4]int32) archsimd.Int32x4 {
    41  	if !archsimd.X86.AVX512() {
    42  		return archsimd.Int32x4{}
    43  	}
    44  	a, b, c := archsimd.LoadInt32x4Array(x), archsimd.LoadInt32x4Array(y), archsimd.LoadInt32x4Array(z)
    45  	return a.AndNot(b).Xor(c)
    46  }
    47  
    48  //go:noinline
    49  func ternLoadD256(x, y, z *[8]int32) archsimd.Int32x8 {
    50  	if !archsimd.X86.AVX512() {
    51  		return archsimd.Int32x8{}
    52  	}
    53  	a, b, c := archsimd.LoadInt32x8Array(x), archsimd.LoadInt32x8Array(y), archsimd.LoadInt32x8Array(z)
    54  	return a.AndNot(b).Xor(c)
    55  }
    56  
    57  //go:noinline
    58  func ternLoadD512(x, y, z *[16]int32) archsimd.Int32x16 {
    59  	a, b, c := archsimd.LoadInt32x16Array(x), archsimd.LoadInt32x16Array(y), archsimd.LoadInt32x16Array(z)
    60  	return a.AndNot(b).Xor(c)
    61  }
    62  
    63  //go:noinline
    64  func ternLoadQ128(x, y, z *[2]int64) archsimd.Int64x2 {
    65  	if !archsimd.X86.AVX512() {
    66  		return archsimd.Int64x2{}
    67  	}
    68  	a, b, c := archsimd.LoadInt64x2Array(x), archsimd.LoadInt64x2Array(y), archsimd.LoadInt64x2Array(z)
    69  	return a.AndNot(b).Xor(c)
    70  }
    71  
    72  //go:noinline
    73  func ternLoadQ256(x, y, z *[4]int64) archsimd.Int64x4 {
    74  	if !archsimd.X86.AVX512() {
    75  		return archsimd.Int64x4{}
    76  	}
    77  	a, b, c := archsimd.LoadInt64x4Array(x), archsimd.LoadInt64x4Array(y), archsimd.LoadInt64x4Array(z)
    78  	return a.AndNot(b).Xor(c)
    79  }
    80  
    81  //go:noinline
    82  func ternLoadQ512(x, y, z *[8]int64) archsimd.Int64x8 {
    83  	a, b, c := archsimd.LoadInt64x8Array(x), archsimd.LoadInt64x8Array(y), archsimd.LoadInt64x8Array(z)
    84  	return a.AndNot(b).Xor(c)
    85  }
    86  
    87  var (
    88  	ternLoadGlobX, ternLoadGlobY, ternLoadGlobZ [16]int32
    89  )
    90  
    91  //go:noinline
    92  func ternLoadD512Global() {
    93  	a := archsimd.LoadInt32x16Array(&ternLoadGlobX)
    94  	b := archsimd.LoadInt32x16Array(&ternLoadGlobY)
    95  	c := archsimd.LoadInt32x16Array(&ternLoadGlobZ)
    96  	a.Xor(b).Xor(c).StoreArray(&ternLoadGlobX)
    97  }
    98  
    99  func TestTernLogMergedLoad(t *testing.T) {
   100  	if !archsimd.X86.AVX512() {
   101  		t.Skip("needs AVX-512")
   102  		return
   103  	}
   104  	// Distinct 8-bit truth-table inputs covering all 8 (a, b, c) bit combinations.
   105  	const (
   106  		A32 int32 = 0b11110000
   107  		B32 int32 = 0b11001100
   108  		C32 int32 = 0b10101010
   109  		A64 int64 = 0b11110000
   110  		B64 int64 = 0b11001100
   111  		C64 int64 = 0b10101010
   112  	)
   113  	want32 := (A32 &^ B32) ^ C32
   114  	want64 := (A64 &^ B64) ^ C64
   115  
   116  	var x4, y4, z4, r4 [4]int32
   117  	for i := range x4 {
   118  		x4[i], y4[i], z4[i] = A32, B32, C32
   119  	}
   120  	ternLoadD128(&x4, &y4, &z4).StoreArray(&r4)
   121  	if r4[0] != want32 {
   122  		t.Errorf("ternLoadD128: got %08b, want %08b", r4[0], want32)
   123  	}
   124  
   125  	var x8, y8, z8, r8 [8]int32
   126  	for i := range x8 {
   127  		x8[i], y8[i], z8[i] = A32, B32, C32
   128  	}
   129  	ternLoadD256(&x8, &y8, &z8).StoreArray(&r8)
   130  	if r8[0] != want32 {
   131  		t.Errorf("ternLoadD256: got %08b, want %08b", r8[0], want32)
   132  	}
   133  
   134  	var x16, y16, z16, r16 [16]int32
   135  	for i := range x16 {
   136  		x16[i], y16[i], z16[i] = A32, B32, C32
   137  	}
   138  	ternLoadD512(&x16, &y16, &z16).StoreArray(&r16)
   139  	if r16[0] != want32 {
   140  		t.Errorf("ternLoadD512: got %08b, want %08b", r16[0], want32)
   141  	}
   142  
   143  	var qx2, qy2, qz2, qr2 [2]int64
   144  	for i := range qx2 {
   145  		qx2[i], qy2[i], qz2[i] = A64, B64, C64
   146  	}
   147  	ternLoadQ128(&qx2, &qy2, &qz2).StoreArray(&qr2)
   148  	if qr2[0] != want64 {
   149  		t.Errorf("ternLoadQ128: got %08b, want %08b", qr2[0], want64)
   150  	}
   151  
   152  	var qx4, qy4, qz4, qr4 [4]int64
   153  	for i := range qx4 {
   154  		qx4[i], qy4[i], qz4[i] = A64, B64, C64
   155  	}
   156  	ternLoadQ256(&qx4, &qy4, &qz4).StoreArray(&qr4)
   157  	if qr4[0] != want64 {
   158  		t.Errorf("ternLoadQ256: got %08b, want %08b", qr4[0], want64)
   159  	}
   160  
   161  	var qx8, qy8, qz8, qr8 [8]int64
   162  	for i := range qx8 {
   163  		qx8[i], qy8[i], qz8[i] = A64, B64, C64
   164  	}
   165  	ternLoadQ512(&qx8, &qy8, &qz8).StoreArray(&qr8)
   166  	if qr8[0] != want64 {
   167  		t.Errorf("ternLoadQ512: got %08b, want %08b", qr8[0], want64)
   168  	}
   169  
   170  	for i := range ternLoadGlobX {
   171  		ternLoadGlobX[i], ternLoadGlobY[i], ternLoadGlobZ[i] = A32, B32, C32
   172  	}
   173  	ternLoadD512Global()
   174  	if wantGlob := A32 ^ B32 ^ C32; ternLoadGlobX[0] != wantGlob {
   175  		t.Errorf("ternLoadD512Global: got %08b, want %08b", ternLoadGlobX[0], wantGlob)
   176  	}
   177  }
   178  

View as plain text