Skip to content

Commit

Permalink
Add crc8 slice-by-16 and no-table implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
KillingSpark authored Feb 11, 2023
1 parent fd3ffc3 commit f8e6e38
Show file tree
Hide file tree
Showing 7 changed files with 372 additions and 55 deletions.
19 changes: 18 additions & 1 deletion benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use crc::*;
use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput};

pub const BLUETOOTH: Crc<u8> = Crc::<u8>::new(&CRC_8_BLUETOOTH);
pub const BLUETOOTH_SLICE16: Crc<Slice16<u8>> = Crc::<Slice16<u8>>::new(&CRC_8_BLUETOOTH);
pub const BLUETOOTH_BYTEWISE: Crc<Bytewise<u8>> = Crc::<Bytewise<u8>>::new(&CRC_8_BLUETOOTH);
pub const BLUETOOTH_NOLOOKUP: Crc<NoTable<u8>> = Crc::<NoTable<u8>>::new(&CRC_8_BLUETOOTH);
pub const X25: Crc<u16> = Crc::<u16>::new(&CRC_16_IBM_SDLC);
pub const X25_SLICE16: Crc<Slice16<u16>> = Crc::<Slice16<u16>>::new(&CRC_16_IBM_SDLC);
pub const X25_BYTEWISE: Crc<Bytewise<u16>> = Crc::<Bytewise<u16>>::new(&CRC_16_IBM_SDLC);
Expand Down Expand Up @@ -35,6 +38,21 @@ fn checksum(c: &mut Criterion) {
.throughput(Throughput::Bytes(size as u64))
.bench_function("baseline", |b| b.iter(|| baseline(black_box(&bytes))));

c.benchmark_group("crc8")
.throughput(Throughput::Bytes(size as u64))
.bench_function("default", |b| {
b.iter(|| BLUETOOTH.checksum(black_box(&bytes)))
})
.bench_function("nolookup", |b| {
b.iter(|| BLUETOOTH_NOLOOKUP.checksum(black_box(&bytes)))
})
.bench_function("bytewise", |b| {
b.iter(|| BLUETOOTH_BYTEWISE.checksum(black_box(&bytes)))
})
.bench_function("slice16", |b| {
b.iter(|| BLUETOOTH_SLICE16.checksum(black_box(&bytes)))
});

c.benchmark_group("crc16")
.throughput(Throughput::Bytes(size as u64))
.bench_function("default", |b| b.iter(|| X25.checksum(black_box(&bytes))))
Expand Down Expand Up @@ -89,7 +107,6 @@ fn checksum(c: &mut Criterion) {

c.benchmark_group("checksum")
.bench_function("crc8", |b| b.iter(|| BLUETOOTH.checksum(black_box(&bytes))))
.bench_function("crc16", |b| b.iter(|| X25.checksum(black_box(&bytes))))
.bench_function("crc40", |b| b.iter(|| GSM_40.checksum(black_box(&bytes))));
}

Expand Down
187 changes: 133 additions & 54 deletions src/crc8.rs
Original file line number Diff line number Diff line change
@@ -1,76 +1,155 @@
use super::{Algorithm, Crc, Digest};
use crate::table::crc8_table;
use crate::util::crc8;
use crc_catalog::Algorithm;

impl Crc<u8> {
pub const fn new(algorithm: &'static Algorithm<u8>) -> Self {
let table = crc8_table(algorithm.width, algorithm.poly, algorithm.refin);
Self { algorithm, table }
}
mod bytewise;
mod default;
mod nolookup;
mod slice16;

pub const fn checksum(&self, bytes: &[u8]) -> u8 {
let mut crc = self.init(self.algorithm.init);
crc = self.update(crc, bytes);
self.finalize(crc)
const fn init(algorithm: &Algorithm<u8>, initial: u8) -> u8 {
if algorithm.refin {
initial.reverse_bits() >> (8u8 - algorithm.width)
} else {
initial << (8u8 - algorithm.width)
}
}

const fn init(&self, initial: u8) -> u8 {
if self.algorithm.refin {
initial.reverse_bits() >> (8u8 - self.algorithm.width)
} else {
initial << (8u8 - self.algorithm.width)
}
const fn finalize(algorithm: &Algorithm<u8>, mut crc: u8) -> u8 {
if algorithm.refin ^ algorithm.refout {
crc = crc.reverse_bits();
}

const fn table_entry(&self, index: u8) -> u8 {
self.table[index as usize]
if !algorithm.refout {
crc >>= 8u8 - algorithm.width;
}
crc ^ algorithm.xorout
}

const fn update(&self, mut crc: u8, bytes: &[u8]) -> u8 {
let mut i = 0;
const fn update_nolookup(mut crc: u8, algorithm: &Algorithm<u8>, bytes: &[u8]) -> u8 {
let poly = if algorithm.refin {
let poly = algorithm.poly.reverse_bits();
poly >> (8u8 - algorithm.width)
} else {
algorithm.poly << (8u8 - algorithm.width)
};

while i < bytes.len() {
crc = self.table_entry(crc ^ bytes[i]);
i += 1;
}
let mut i = 0;

crc
while i < bytes.len() {
crc = crc8(poly, algorithm.refin, crc ^ bytes[i]);
i += 1;
}

const fn finalize(&self, mut crc: u8) -> u8 {
if self.algorithm.refin ^ self.algorithm.refout {
crc = crc.reverse_bits();
}
if !self.algorithm.refout {
crc >>= 8u8 - self.algorithm.width;
}
crc ^ self.algorithm.xorout
}
crc
}

pub const fn digest(&self) -> Digest<u8> {
self.digest_with_initial(self.algorithm.init)
}
const fn update_bytewise(mut crc: u8, table: &[u8; 256], bytes: &[u8]) -> u8 {
let mut i = 0;

/// Construct a `Digest` with a given initial value.
///
/// This overrides the initial value specified by the algorithm.
/// The effects of the algorithm's properties `refin` and `width`
/// are applied to the custom initial value.
pub const fn digest_with_initial(&self, initial: u8) -> Digest<u8> {
let value = self.init(initial);
Digest::new(self, value)
while i < bytes.len() {
crc = table[(crc ^ bytes[i]) as usize];
i += 1;
}

crc
}

impl<'a> Digest<'a, u8> {
const fn new(crc: &'a Crc<u8>, value: u8) -> Self {
Digest { crc, value }
const fn update_slice16(mut crc: u8, table: &[[u8; 256]; 16], bytes: &[u8]) -> u8 {
let len = bytes.len();
let mut i = 0;

while i + 16 < len {
crc = table[0][bytes[i + 15] as usize]
^ table[1][bytes[i + 14] as usize]
^ table[2][bytes[i + 13] as usize]
^ table[3][bytes[i + 12] as usize]
^ table[4][bytes[i + 11] as usize]
^ table[5][bytes[i + 10] as usize]
^ table[6][bytes[i + 9] as usize]
^ table[7][bytes[i + 8] as usize]
^ table[8][bytes[i + 7] as usize]
^ table[9][bytes[i + 6] as usize]
^ table[10][bytes[i + 5] as usize]
^ table[11][bytes[i + 4] as usize]
^ table[12][bytes[i + 3] as usize]
^ table[13][bytes[i + 2] as usize]
^ table[14][bytes[i + 1] as usize]
^ table[15][(bytes[i] ^ crc) as usize];

i += 16;
}

pub fn update(&mut self, bytes: &[u8]) {
self.value = self.crc.update(self.value, bytes);
while i < len {
crc = table[0][(crc ^ bytes[i]) as usize];
i += 1;
}

pub const fn finalize(self) -> u8 {
self.crc.finalize(self.value)
crc
}

#[cfg(test)]
mod test {
use crate::{Bytewise, Crc, NoTable, Slice16};
use crc_catalog::{Algorithm, CRC_8_BLUETOOTH};

/// Test this opitimized version against the well known implementation to ensure correctness
#[test]
fn correctness() {
let data: &[&str] = &[
"",
"1",
"1234",
"123456789",
"0123456789ABCDE",
"01234567890ABCDEFGHIJK",
"01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK01234567890ABCDEFGHIJK",
];

pub const CRC_8_BLUETOOTH_NONREFLEX: Algorithm<u8> = Algorithm {
width: 8,
poly: 0xa7,
init: 0x00,
refin: false,
refout: true,
xorout: 0x00,
check: 0x26,
residue: 0x00,
};

let algs_to_test = [&CRC_8_BLUETOOTH, &CRC_8_BLUETOOTH_NONREFLEX];

for alg in algs_to_test {
for data in data {
let crc_slice16 = Crc::<Slice16<u8>>::new(alg);
let crc_nolookup = Crc::<NoTable<u8>>::new(alg);
let expected = Crc::<Bytewise<u8>>::new(alg).checksum(data.as_bytes());

// Check that doing all at once works as expected
assert_eq!(crc_slice16.checksum(data.as_bytes()), expected);
assert_eq!(crc_nolookup.checksum(data.as_bytes()), expected);

let mut digest = crc_slice16.digest();
digest.update(data.as_bytes());
assert_eq!(digest.finalize(), expected);

let mut digest = crc_nolookup.digest();
digest.update(data.as_bytes());
assert_eq!(digest.finalize(), expected);

// Check that we didn't break updating from multiple sources
if data.len() > 2 {
let data = data.as_bytes();
let data1 = &data[..data.len() / 2];
let data2 = &data[data.len() / 2..];
let mut digest = crc_slice16.digest();
digest.update(data1);
digest.update(data2);
assert_eq!(digest.finalize(), expected);
let mut digest = crc_nolookup.digest();
digest.update(data1);
digest.update(data2);
assert_eq!(digest.finalize(), expected);
}
}
}
}
}
48 changes: 48 additions & 0 deletions src/crc8/bytewise.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use crate::crc8::{finalize, init, update_bytewise};
use crate::table::crc8_table;
use crate::{Algorithm, Bytewise, Crc, Digest};

impl Crc<Bytewise<u8>> {
pub const fn new(algorithm: &'static Algorithm<u8>) -> Self {
let table = crc8_table(algorithm.width, algorithm.poly, algorithm.refin);
Self { algorithm, table }
}

pub const fn checksum(&self, bytes: &[u8]) -> u8 {
let mut crc = init(self.algorithm, self.algorithm.init);
crc = self.update(crc, bytes);
finalize(self.algorithm, crc)
}

const fn update(&self, crc: u8, bytes: &[u8]) -> u8 {
update_bytewise(crc, &self.table, bytes)
}

pub const fn digest(&self) -> Digest<Bytewise<u8>> {
self.digest_with_initial(self.algorithm.init)
}

/// Construct a `Digest` with a given initial value.
///
/// This overrides the initial value specified by the algorithm.
/// The effects of the algorithm's properties `refin` and `width`
/// are applied to the custom initial value.
pub const fn digest_with_initial(&self, initial: u8) -> Digest<Bytewise<u8>> {
let value = init(self.algorithm, initial);
Digest::new(self, value)
}
}

impl<'a> Digest<'a, Bytewise<u8>> {
const fn new(crc: &'a Crc<Bytewise<u8>>, value: u8) -> Self {
Digest { crc, value }
}

pub fn update(&mut self, bytes: &[u8]) {
self.value = self.crc.update(self.value, bytes);
}

pub const fn finalize(self) -> u8 {
finalize(self.crc.algorithm, self.value)
}
}
48 changes: 48 additions & 0 deletions src/crc8/default.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use crate::crc8::{finalize, init, update_bytewise};
use crate::table::crc8_table;
use crate::{Algorithm, Crc, Digest};

impl Crc<u8> {
pub const fn new(algorithm: &'static Algorithm<u8>) -> Self {
let table = crc8_table(algorithm.width, algorithm.poly, algorithm.refin);
Self { algorithm, table }
}

pub const fn checksum(&self, bytes: &[u8]) -> u8 {
let mut crc = init(self.algorithm, self.algorithm.init);
crc = self.update(crc, bytes);
finalize(self.algorithm, crc)
}

const fn update(&self, crc: u8, bytes: &[u8]) -> u8 {
update_bytewise(crc, &self.table, bytes)
}

pub const fn digest(&self) -> Digest<u8> {
self.digest_with_initial(self.algorithm.init)
}

/// Construct a `Digest` with a given initial value.
///
/// This overrides the initial value specified by the algorithm.
/// The effects of the algorithm's properties `refin` and `width`
/// are applied to the custom initial value.
pub const fn digest_with_initial(&self, initial: u8) -> Digest<u8> {
let value = init(self.algorithm, initial);
Digest::new(self, value)
}
}

impl<'a> Digest<'a, u8> {
const fn new(crc: &'a Crc<u8>, value: u8) -> Self {
Digest { crc, value }
}

pub fn update(&mut self, bytes: &[u8]) {
self.value = self.crc.update(self.value, bytes);
}

pub const fn finalize(self) -> u8 {
finalize(self.crc.algorithm, self.value)
}
}
49 changes: 49 additions & 0 deletions src/crc8/nolookup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use crate::crc8::{finalize, init, update_nolookup};
use crate::{Algorithm, Crc, Digest, NoTable};

impl Crc<NoTable<u8>> {
pub const fn new(algorithm: &'static Algorithm<u8>) -> Self {
Self {
algorithm,
table: (),
}
}

pub const fn checksum(&self, bytes: &[u8]) -> u8 {
let mut crc = init(self.algorithm, self.algorithm.init);
crc = self.update(crc, bytes);
finalize(self.algorithm, crc)
}

const fn update(&self, crc: u8, bytes: &[u8]) -> u8 {
update_nolookup(crc, self.algorithm, bytes)
}

pub const fn digest(&self) -> Digest<NoTable<u8>> {
self.digest_with_initial(self.algorithm.init)
}

/// Construct a `Digest` with a given initial value.
///
/// This overrides the initial value specified by the algorithm.
/// The effects of the algorithm's properties `refin` and `width`
/// are applied to the custom initial value.
pub const fn digest_with_initial(&self, initial: u8) -> Digest<NoTable<u8>> {
let value = init(self.algorithm, initial);
Digest::new(self, value)
}
}

impl<'a> Digest<'a, NoTable<u8>> {
const fn new(crc: &'a Crc<NoTable<u8>>, value: u8) -> Self {
Digest { crc, value }
}

pub fn update(&mut self, bytes: &[u8]) {
self.value = self.crc.update(self.value, bytes);
}

pub const fn finalize(self) -> u8 {
finalize(self.crc.algorithm, self.value)
}
}
Loading

0 comments on commit f8e6e38

Please sign in to comment.