cpu: add X86.HasVPCLMULQDQ and GODEBUG=cpu.vpclmulqdq

VPCLMULQDQ on 256-bit vectors needs only AVX, but this package only
reported it for 512-bit vectors, as HasAVX512VPCLMULQDQ. Add
HasVPCLMULQDQ for the 256-bit form, as internal/cpu has.

Add the "vpclmulqdq" GODEBUG option to control it. As in internal/cpu,
turning it off also turns off HasAVX512VPCLMULQDQ, since the same CPUID
bit reports both. Without the option, a program that links both
packages, such as the go command, reports an unknown CPU feature when
GODEBUG=cpu.vpclmulqdq=off is set.

Change-Id: Ifb26554b6253cf9afa299fee92a2758df5f1b7b2
Reviewed-on: https://go-review.googlesource.com/c/sys/+/865983
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
Auto-Submit: Austin Clements <austin@google.com>
diff --git a/cpu/cpu.go b/cpu/cpu.go
index 422b139..da31f11 100644
--- a/cpu/cpu.go
+++ b/cpu/cpu.go
@@ -79,6 +79,7 @@
 	HasAVXIFMA          bool // Advanced vector extension Integer Fused Multiply Add
 	HasAVXVNNI          bool // Advanced vector extension Vector Neural Network Instructions
 	HasAVXVNNIInt8      bool // Advanced vector extension Vector Neural Network Int8 instructions
+	HasVPCLMULQDQ       bool // Vector carry-less multiply operations on 256-bit vectors (requires only AVX)
 	HasXSAVE            bool // XSAVE/XRSTOR/XSETBV/XGETBV instructions (hardware support; see also HasOSXSAVE)
 	_                   CacheLinePad
 }
diff --git a/cpu/cpu_netbsd_amd64.go b/cpu/cpu_netbsd_amd64.go
index 4f981b1..3550725 100644
--- a/cpu/cpu_netbsd_amd64.go
+++ b/cpu/cpu_netbsd_amd64.go
@@ -39,6 +39,7 @@
 	X86.HasAVXIFMA = false
 	X86.HasAVXVNNI = false
 	X86.HasAVXVNNIInt8 = false
+	X86.HasVPCLMULQDQ = false
 
 }
 
diff --git a/cpu/cpu_x86.go b/cpu/cpu_x86.go
index 7e6cead..91fc66a 100644
--- a/cpu/cpu_x86.go
+++ b/cpu/cpu_x86.go
@@ -57,6 +57,7 @@
 		{Name: "sse41", Feature: &X86.HasSSE41},
 		{Name: "sse42", Feature: &X86.HasSSE42},
 		{Name: "ssse3", Feature: &X86.HasSSSE3},
+		{Name: "vpclmulqdq", Feature: &X86.HasVPCLMULQDQ},
 		{Name: "avxifma", Feature: &X86.HasAVXIFMA},
 		{Name: "avxvnni", Feature: &X86.HasAVXVNNI},
 		{Name: "avxvnniint8", Feature: &X86.HasAVXVNNIInt8},
@@ -113,7 +114,8 @@
 		cpuid_AVX512_VBMI      = 1 << 1
 		cpuid_AVX512_VBMI2     = 1 << 6
 		cpuid_GFNI             = 1 << 8
-		cpuid_AVX512VPCLMULQDQ = 1 << 10
+		cpuid_VPCLMULQDQ       = 1 << 10 // && AVX => 256-bit VPCLMULQDQ
+		cpuid_AVX512VPCLMULQDQ = 1 << 10 // && AVX512 => 512-bit VPCLMULQDQ
 		cpuid_AVX512_BITALG    = 1 << 12
 
 		// edx bits
@@ -151,6 +153,15 @@
 
 	Initialized = true
 
+	doDerived = func() {
+		// The same CPUID bit reports VPCLMULQDQ on 256-bit vectors and
+		// on 512-bit vectors, and GODEBUG=cpu.vpclmulqdq=off turns off
+		// the 256-bit flag, so the 512-bit flag must follow it.
+		if !X86.HasVPCLMULQDQ {
+			X86.HasAVX512VPCLMULQDQ = false
+		}
+	}
+
 	maxID, _, _, _ := cpuid(0, 0)
 
 	if maxID < 1 {
@@ -213,6 +224,7 @@
 	X86.HasERMS = isSet(ebx7, cpuid_ERMS)
 	X86.HasRDSEED = isSet(ebx7, cpuid_RDSEED)
 	X86.HasADX = isSet(ebx7, cpuid_ADX)
+	X86.HasVPCLMULQDQ = isSet(ecx7, cpuid_VPCLMULQDQ) && X86.HasAVX
 
 	X86.HasAVX512 = isSet(ebx7, cpuid_AVX512F) && osSupportsAVX512 // Because avx-512 foundation is the core required extension
 	if X86.HasAVX512 {