Documentation
¶
Overview ¶
Package crc32 is a pure-Go, SIMD-accelerated drop-in replacement for the standard library's hash/crc32. It computes bit-identical CRC-32 checksums but, for the IEEE polynomial, folds the bulk of the input with the host's carryless-multiply unit instead of the scalar table:
arm64 PMULL / PMULL2 (gated on cpu.ARM64.HasPMULL, and always on
GOOS=darwin, where every host is Apple Silicon
with FEAT_PMULL but the cpu package does not probe
the HWCAP)
Every other GOARCH (amd64, ppc64le, s390x, riscv64, loong64, ...) defers to the standard library's own CRC-32 kernel, which is already hardware-assisted on several of those targets — the amd64 path is a PCLMULQDQ fold (AVX-512 VPCLMULQDQ where available), and ppc64le/s390x are hardware-assisted too, so there is nothing to beat there. The hand-written kernel here targets arm64, whose standard-library IEEE path is a latency-bound serial CRC32X.
Only the IEEE polynomial is folded by the kernel; Castagnoli, Koopman and any custom polynomial transparently use the standard library. The result always equals hash/crc32 exactly — bit for bit, on every architecture and for every polynomial. There is no cgo, no GOEXPERIMENT and no assembler intrinsic requirement: a plain `go build` produces the accelerated binary.
The API mirrors hash/crc32 exactly, so it can be swapped in by changing only the import path.
Index ¶
Constants ¶
const ( // IEEE is by far and away the most common CRC-32 polynomial. Used by // ethernet (IEEE 802.3), v.42, fddi, gzip, zip, png, ... IEEE = stdcrc32.IEEE // Castagnoli's polynomial, used in iSCSI. Has better error detection // characteristics than IEEE. Castagnoli = stdcrc32.Castagnoli // Koopman's polynomial. Also has better error detection characteristics // than IEEE. Koopman = stdcrc32.Koopman )
Predefined polynomials (identical to hash/crc32).
const Size = 4
The size of a CRC-32 checksum in bytes.
Variables ¶
var IEEETable = stdcrc32.IEEETable
IEEETable is the table for the IEEE polynomial.
Functions ¶
func Checksum ¶
Checksum returns the CRC-32 checksum of data using the polynomial represented by the Table.
func ChecksumIEEE ¶
ChecksumIEEE returns the CRC-32 checksum of data using the IEEE polynomial.
func New ¶
New creates a new hash.Hash32 computing the CRC-32 checksum using the polynomial represented by the Table. Its Sum method lays the value out in big-endian byte order. The returned Hash32 also implements encoding.BinaryMarshaler and encoding.BinaryUnmarshaler.
The hash uses the SIMD-accelerated kernel for its Write path when the table is the IEEE polynomial.
