From c0f2a869bc0d730af2c38a3e7c14f0e3ad847c13 Mon Sep 17 00:00:00 2001
From: Frank Wessels <fwessels@xs4all.nl>
Date: Wed, 5 Jun 2024 19:42:17 -0700
Subject: [PATCH] Sync up go generate code in _gen

---
 _gen/gen.go | 46 +++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 41 insertions(+), 5 deletions(-)

diff --git a/_gen/gen.go b/_gen/gen.go
index b842907..f42cdec 100644
--- a/_gen/gen.go
+++ b/_gen/gen.go
@@ -121,12 +121,48 @@ import (
 )
 
 `)
+	avx2funcs := string(`	fAvx2       = galMulSlicesAvx2
+	fAvx2Xor    = galMulSlicesAvx2Xor
+`)
+
+	hasCodeGenImpl := string(`	return &fAvx2, &fAvx2Xor, codeGen && pshufb && r.o.useAVX2 &&
+	byteCount >= codeGenMinSize && inputs+outputs >= codeGenMinShards &&
+	inputs <= codeGenMaxInputs && outputs <= codeGenMaxOutputs`)
+
+	if !pshufb {
+		avx2funcs = ""
+		hasCodeGenImpl = `	return nil, nil, false // no code generation for generic case (only GFNI cases)	`
+	}
+
 	w.WriteString(fmt.Sprintf(`const (
-avx2CodeGen = true
-maxAvx2Inputs = %d
-maxAvx2Outputs = %d
-minAvx2Size = 64
-)`, inputMax, outputMax))
+codeGen              = true
+codeGenMaxGoroutines = 8
+codeGenMaxInputs     = %d
+codeGenMaxOutputs    = %d
+minCodeGenSize       = 64
+)
+
+var (
+%s	fGFNI       = galMulSlicesGFNI
+	fGFNIXor    = galMulSlicesGFNIXor
+	fAvxGFNI    = galMulSlicesAvxGFNI
+	fAvxGFNIXor = galMulSlicesAvxGFNIXor
+)
+
+func (r *reedSolomon) hasCodeGen(byteCount int, inputs, outputs int) (_, _ *func(matrix []byte, in, out [][]byte, start, stop int) int, ok bool) {
+%s
+}
+
+func (r *reedSolomon) canGFNI(byteCount int, inputs, outputs int) (_, _ *func(matrix []uint64, in, out [][]byte, start, stop int) int, ok bool) {
+	if r.o.useAvx512GFNI {
+		return &fGFNI, &fGFNIXor, codeGen &&
+			byteCount >= codeGenMinSize && inputs+outputs >= codeGenMinShards &&
+			inputs <= codeGenMaxInputs && outputs <= codeGenMaxOutputs
+	}
+	return &fAvxGFNI, &fAvxGFNIXor, codeGen && r.o.useAvxGNFI &&
+		byteCount >= codeGenMinSize && inputs+outputs >= codeGenMinShards &&
+		inputs <= codeGenMaxInputs && outputs <= codeGenMaxOutputs
+}`, inputMax, outputMax, avx2funcs, hasCodeGenImpl))
 
 	if !pshufb {
 		w.WriteString("\n\nfunc galMulSlicesAvx2(matrix []byte, in, out [][]byte, start, stop int) int { panic(`no pshufb`)}\n")