Kirika/audio/packetizer/flac_crc_nocgo.go
DataHoarder ee96281c55
All checks were successful
continuous-integration/drone/push Build is passing
Optimize FLAC packetizer performance (20-15s -> 4s)
2022-07-27 11:43:29 +02:00

78 lines
1.7 KiB
Go

//go:build !cgo
package packetizer
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Predefined polynomials.
const (
crc8ATM = 0x07 // x^8 + x^2 + x + 1
)
// crc8Table is a 256-word table representing the polynomial for efficient
// processing.
type crc8Table [256]uint8
// crc8ATMTable is the table for the crc8ATM polynomial.
var crc8ATMTable = func(poly uint8) (table *crc8Table) {
table = new(crc8Table)
for i := range table {
crc := uint8(i)
for j := 0; j < 8; j++ {
if crc&0x80 != 0 {
crc = crc<<1 ^ poly
} else {
crc <<= 1
}
}
table[i] = crc
}
return table
}(crc8ATM)
// flacCrc8 returns the CRC-8 checksum of data using the crc8ATM polynomial.
func flacCrc8(data []byte) uint8 {
var crc uint8
for _, v := range data {
crc = crc8ATMTable[crc^v]
}
return crc
}
// Predefined polynomials.
const (
crc16IBM = 0x8005 // x^16 + x^15 + x^2 + x^0
)
// crc16Table is a 256-word table representing the polynomial for efficient
// processing.
type crc16Table [256]uint16
// crc16IBMTable is the table for the crc16IBM polynomial.
var crc16IBMTable = func(poly uint16) (table *crc16Table) {
table = new(crc16Table)
for i := range table {
crc := uint16(i << 8)
for j := 0; j < 8; j++ {
if crc&0x8000 != 0 {
crc = crc<<1 ^ poly
} else {
crc <<= 1
}
}
table[i] = crc
}
return table
}(crc16IBM)
// flacCrc16 returns the CRC-16 checksum of data using the crc16IBM polynomial.
func flacCrc16(data []byte) uint16 {
var crc uint16
for _, v := range data {
crc = crc<<8 ^ crc16IBMTable[crc>>8^uint16(v)]
}
return crc
}