checksum_amd64.go 731 B

1234567891011121314151617181920212223
  1. package checksum
  2. import (
  3. "golang.org/x/sys/cpu"
  4. gvisorchecksum "gvisor.dev/gvisor/pkg/tcpip/checksum"
  5. )
  6. //go:noescape
  7. func checksumAVX2(buf []byte, initial uint16) uint16
  8. var hasAVX2 = cpu.X86.HasAVX2
  9. // Checksum computes the RFC 1071 ones-complement sum of buf, seeded with
  10. // initial. It is a drop-in replacement for gvisor's checksum.Checksum that
  11. // dispatches to a hand-written AVX2 routine on amd64 CPUs that support it,
  12. // falling back to gvisor's pure-Go implementation otherwise. The result
  13. // matches gvisor's bit-for-bit for any buffer length and initial seed.
  14. func Checksum(buf []byte, initial uint16) uint16 {
  15. if hasAVX2 {
  16. return checksumAVX2(buf, initial)
  17. }
  18. return gvisorchecksum.Checksum(buf, initial)
  19. }