Skip to content

Commit

Permalink
poc: semver compatible impl extensions
Browse files Browse the repository at this point in the history
Signed-off-by: Akhil Velagapudi <[email protected]>
  • Loading branch information
akhilles committed Mar 10, 2024
1 parent 5e38fdd commit cd20960
Show file tree
Hide file tree
Showing 28 changed files with 185 additions and 1,286 deletions.
30 changes: 15 additions & 15 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ 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 BLUETOOTH_SLICE16: Crc<u8, Table<16>> = Crc::<u8, Table<16>>::new(&CRC_8_BLUETOOTH);
pub const BLUETOOTH_BYTEWISE: Crc<u8, Table> = Crc::<u8, Table>::new(&CRC_8_BLUETOOTH);
pub const BLUETOOTH_NOLOOKUP: Crc<u8, NoTable> = Crc::<u8, NoTable>::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);
pub const X25_NOLOOKUP: Crc<NoTable<u16>> = Crc::<NoTable<u16>>::new(&CRC_16_IBM_SDLC);
pub const X25_SLICE16: Crc<u16, Table<16>> = Crc::<u16, Table<16>>::new(&CRC_16_IBM_SDLC);
pub const X25_BYTEWISE: Crc<u16, Table> = Crc::<u16, Table>::new(&CRC_16_IBM_SDLC);
pub const X25_NOLOOKUP: Crc<u16, NoTable> = Crc::<u16, NoTable>::new(&CRC_16_IBM_SDLC);
pub const ISCSI: Crc<u32> = Crc::<u32>::new(&CRC_32_ISCSI);
pub const ISCSI_SLICE16: Crc<Slice16<u32>> = Crc::<Slice16<u32>>::new(&CRC_32_ISCSI);
pub const ISCSI_BYTEWISE: Crc<Bytewise<u32>> = Crc::<Bytewise<u32>>::new(&CRC_32_ISCSI);
pub const ISCSI_NOLOOKUP: Crc<NoTable<u32>> = Crc::<NoTable<u32>>::new(&CRC_32_ISCSI);
pub const ISCSI_SLICE16: Crc<u32, Table<16>> = Crc::<u32, Table<16>>::new(&CRC_32_ISCSI);
pub const ISCSI_BYTEWISE: Crc<u32, Table> = Crc::<u32, Table>::new(&CRC_32_ISCSI);
pub const ISCSI_NOLOOKUP: Crc<u32, NoTable> = Crc::<u32, NoTable>::new(&CRC_32_ISCSI);
pub const GSM_40: Crc<u64> = Crc::<u64>::new(&CRC_40_GSM);
pub const ECMA: Crc<u64> = Crc::<u64>::new(&CRC_64_ECMA_182);
pub const ECMA_SLICE16: Crc<Slice16<u64>> = Crc::<Slice16<u64>>::new(&CRC_64_ECMA_182);
pub const ECMA_BYTEWISE: Crc<Bytewise<u64>> = Crc::<Bytewise<u64>>::new(&CRC_64_ECMA_182);
pub const ECMA_NOLOOKUP: Crc<NoTable<u64>> = Crc::<NoTable<u64>>::new(&CRC_64_ECMA_182);
pub const ECMA_SLICE16: Crc<u64, Table<16>> = Crc::<u64, Table<16>>::new(&CRC_64_ECMA_182);
pub const ECMA_BYTEWISE: Crc<u64, Table> = Crc::<u64, Table>::new(&CRC_64_ECMA_182);
pub const ECMA_NOLOOKUP: Crc<u64, NoTable> = Crc::<u64, NoTable>::new(&CRC_64_ECMA_182);
pub const DARC: Crc<u128> = Crc::<u128>::new(&CRC_82_DARC);
pub const DARC_SLICE16: Crc<Slice16<u128>> = Crc::<Slice16<u128>>::new(&CRC_82_DARC);
pub const DARC_BYTEWISE: Crc<Bytewise<u128>> = Crc::<Bytewise<u128>>::new(&CRC_82_DARC);
pub const DARC_NOLOOKUP: Crc<NoTable<u128>> = Crc::<NoTable<u128>>::new(&CRC_82_DARC);
pub const DARC_SLICE16: Crc<u128, Table<16>> = Crc::<u128, Table<16>>::new(&CRC_82_DARC);
pub const DARC_BYTEWISE: Crc<u128, Table> = Crc::<u128, Table>::new(&CRC_82_DARC);
pub const DARC_NOLOOKUP: Crc<u128, NoTable> = Crc::<u128, NoTable>::new(&CRC_82_DARC);

static KB: usize = 1024;

Expand Down
100 changes: 4 additions & 96 deletions src/crc128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::util::crc128;
use crc_catalog::Algorithm;

mod bytewise;
mod default;
mod nolookup;
mod slice16;

Expand Down Expand Up @@ -169,100 +168,9 @@ const fn update_slice16(

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

#[test]
fn default_table_size() {
const TABLE_SIZE: usize = core::mem::size_of::<<u128 as Implementation>::Table>();
const BYTES_PER_ENTRY: usize = 16;
#[cfg(all(
feature = "no-table-mem-limit",
feature = "bytewise-mem-limit",
feature = "slice16-mem-limit"
))]
{
const EXPECTED: usize = 0;
let _ = EXPECTED;
const _: () = assert!(EXPECTED == TABLE_SIZE);
}
#[cfg(all(
feature = "no-table-mem-limit",
feature = "bytewise-mem-limit",
not(feature = "slice16-mem-limit")
))]
{
const EXPECTED: usize = 0;
let _ = EXPECTED;
const _: () = assert!(EXPECTED == TABLE_SIZE);
}
#[cfg(all(
feature = "no-table-mem-limit",
not(feature = "bytewise-mem-limit"),
feature = "slice16-mem-limit"
))]
{
const EXPECTED: usize = 0;
let _ = EXPECTED;
const _: () = assert!(EXPECTED == TABLE_SIZE);
}
#[cfg(all(
feature = "no-table-mem-limit",
not(feature = "bytewise-mem-limit"),
not(feature = "slice16-mem-limit")
))]
{
const EXPECTED: usize = 0;
let _ = EXPECTED;
const _: () = assert!(EXPECTED == TABLE_SIZE);
}

#[cfg(all(
not(feature = "no-table-mem-limit"),
feature = "bytewise-mem-limit",
feature = "slice16-mem-limit"
))]
{
const EXPECTED: usize = 256 * BYTES_PER_ENTRY;
let _ = EXPECTED;
const _: () = assert!(EXPECTED == TABLE_SIZE);
}
#[cfg(all(
not(feature = "no-table-mem-limit"),
feature = "bytewise-mem-limit",
not(feature = "slice16-mem-limit")
))]
{
const EXPECTED: usize = 256 * BYTES_PER_ENTRY;
let _ = EXPECTED;
const _: () = assert!(EXPECTED == TABLE_SIZE);
}

#[cfg(all(
not(feature = "no-table-mem-limit"),
not(feature = "bytewise-mem-limit"),
feature = "slice16-mem-limit"
))]
{
const EXPECTED: usize = 256 * 16 * BYTES_PER_ENTRY;
let _ = EXPECTED;
const _: () = assert!(EXPECTED == TABLE_SIZE);
}

#[cfg(all(
not(feature = "no-table-mem-limit"),
not(feature = "bytewise-mem-limit"),
not(feature = "slice16-mem-limit")
))]
{
const EXPECTED: usize = 256 * BYTES_PER_ENTRY;
let _ = EXPECTED;
const _: () = assert!(EXPECTED == TABLE_SIZE);
}
let _ = TABLE_SIZE;
let _ = BYTES_PER_ENTRY;
}

/// Test this optimized version against the well known implementation to ensure correctness
#[test]
fn correctness() {
Expand Down Expand Up @@ -291,9 +199,9 @@ mod test {

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

// Check that doing all at once works as expected
assert_eq!(crc_slice16.checksum(data.as_bytes()), expected);
Expand Down
19 changes: 11 additions & 8 deletions src/crc128/bytewise.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use crate::table::crc128_table;
use crate::{Algorithm, Bytewise, Crc, Digest};
use crate::*;

use super::{finalize, init, update_bytewise};

impl Crc<Bytewise<u128>> {
impl Crc<u128, Table> {
pub const fn new(algorithm: &'static Algorithm<u128>) -> Self {
let table = crc128_table(algorithm.width, algorithm.poly, algorithm.refin);
Self { algorithm, table }
Self {
algorithm,
data: [table],
}
}

pub const fn checksum(&self, bytes: &[u8]) -> u128 {
Expand All @@ -16,10 +19,10 @@ impl Crc<Bytewise<u128>> {
}

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

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

Expand All @@ -28,14 +31,14 @@ impl Crc<Bytewise<u128>> {
/// 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: u128) -> Digest<Bytewise<u128>> {
pub const fn digest_with_initial(&self, initial: u128) -> Digest<u128, Table> {
let value = init(self.algorithm, initial);
Digest::new(self, value)
}
}

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

Expand Down
126 changes: 0 additions & 126 deletions src/crc128/default.rs

This file was deleted.

15 changes: 7 additions & 8 deletions src/crc128/nolookup.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::{Algorithm, Crc, Digest, NoTable};

use super::{finalize, init, update_nolookup};
use crate::*;

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

Expand All @@ -20,7 +19,7 @@ impl Crc<NoTable<u128>> {
update_nolookup(crc, self.algorithm, bytes)
}

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

Expand All @@ -29,14 +28,14 @@ impl Crc<NoTable<u128>> {
/// 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: u128) -> Digest<NoTable<u128>> {
pub const fn digest_with_initial(&self, initial: u128) -> Digest<u128, NoTable> {
let value = init(self.algorithm, initial);
Digest::new(self, value)
}
}

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

Expand Down
Loading

0 comments on commit cd20960

Please sign in to comment.