sha1block_amd64.mx raw

   1  // Copyright 2016 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 !purego
   6  
   7  package sha1
   8  
   9  import (
  10  	"crypto/internal/impl"
  11  	"internal/cpu"
  12  )
  13  
  14  //go:noescape
  15  func blockAVX2(dig *digest, p []byte)
  16  
  17  //go:noescape
  18  func blockSHANI(dig *digest, p []byte)
  19  
  20  var useAVX2 = cpu.X86.HasAVX && cpu.X86.HasAVX2 && cpu.X86.HasBMI1 && cpu.X86.HasBMI2
  21  var useSHANI = cpu.X86.HasAVX && cpu.X86.HasSHA && cpu.X86.HasSSE41 && cpu.X86.HasSSSE3
  22  
  23  func init() {
  24  	impl.Register("sha1", "AVX2", &useAVX2)
  25  	impl.Register("sha1", "SHA-NI", &useSHANI)
  26  }
  27  
  28  func block(dig *digest, p []byte) {
  29  	if useSHANI {
  30  		blockSHANI(dig, p)
  31  	} else if useAVX2 && len(p) >= 256 {
  32  		// blockAVX2 calculates sha1 for 2 block per iteration and also
  33  		// interleaves precalculation for next block. So it may read up-to 192
  34  		// bytes past end of p. We could add checks inside blockAVX2, but this
  35  		// would just turn it into a copy of the old pre-AVX2 amd64 SHA1
  36  		// assembly implementation, so just call blockGeneric instead.
  37  		safeLen := len(p) - 128
  38  		if safeLen%128 != 0 {
  39  			safeLen -= 64
  40  		}
  41  		blockAVX2(dig, p[:safeLen])
  42  		blockGeneric(dig, p[safeLen:])
  43  	} else {
  44  		blockGeneric(dig, p)
  45  	}
  46  }
  47