1//go:build !noasm && gc && amd64 && !arm64
 2// +build !noasm,gc,amd64,!arm64
 3
 4package sha1cd
 5
 6import (
 7	"runtime"
 8
 9	"github.com/klauspost/cpuid/v2"
10	shared "github.com/pjbgf/sha1cd/internal"
11)
12
13var hasSHANI = (runtime.GOARCH == "amd64" &&
14	cpuid.CPU.Supports(cpuid.AVX) &&
15	cpuid.CPU.Supports(cpuid.SHA) &&
16	cpuid.CPU.Supports(cpuid.SSE3) &&
17	cpuid.CPU.Supports(cpuid.SSE4))
18
19// blockAMD64 hashes the message p into the current state in h.
20// Both m1 and cs are used to store intermediate results which are used by the collision detection logic.
21//
22//go:noescape
23func blockAMD64(h []uint32, p []byte, m1 []uint32, cs [][5]uint32)
24
25func block(dig *digest, p []byte) {
26	if forceGeneric || !hasSHANI {
27		blockGeneric(dig, p)
28		return
29	}
30
31	m1 := [shared.Rounds]uint32{}
32	cs := [shared.PreStepState][shared.WordBuffers]uint32{}
33
34	for len(p) >= shared.Chunk {
35		// The assembly code only supports processing a block at a time,
36		// so adjust the chunk accordingly.
37		chunk := p[:shared.Chunk]
38
39		blockAMD64(dig.h[:], chunk, m1[:], cs[:])
40		rectifyCompressionState(&m1, &cs)
41
42		col := checkCollision(&m1, &cs, &dig.h)
43		if col {
44			dig.col = true
45
46			blockAMD64(dig.h[:], chunk, m1[:], cs[:])
47			blockAMD64(dig.h[:], chunk, m1[:], cs[:])
48		}
49
50		p = p[shared.Chunk:]
51	}
52}