Skip to main content

Crc32C

Struct Crc32C 

Source
pub struct Crc32C(/* private fields */);
Expand description

Implementation of CRC32C (Castagnoli polynomial) to compute a 32-bit cyclic redundancy check (CRC) using Castagnoli polynomial

§Example

let crc = frozen_core::crc32::Crc32C::default();

let b0: [u8; 8] = *b"12345678";
let b1: [u8; 8] = *b"ABCDEFGH";
let b2: [u8; 8] = *b"abcdefgh";
let b3: [u8; 8] = *b"zyxwvuts";

assert_eq!(crc.crc(&b0), crc.crc(&b0));
assert_eq!(crc.crc_2x([&b0, &b1]),crc.crc_2x([&b0, &b1]));
assert_eq!(crc.crc_4x([&b0, &b1, &b2, &b3]),crc.crc_4x([&b0, &b1, &b2, &b3]));

Implementations§

Source§

impl Crc32C

Source

pub fn new() -> Self

Create a new instance of Crc32C

§CRC backend (Hardware vs Software)

When the new instance is created, we select the fastest available backend,

  • On x86_64 we use sse4.2 SIMD instructions when available
  • On aarch64 we use ARMv8 crc instructions when available
  • Otherwise, fallback to portable software implementation

Hardware acceleration is significantly faster than the software fallback, while both backend producing exact same CRC values

§Example
let crc = frozen_core::crc32::Crc32C::default();

let b0: [u8; 8] = *b"12345678";
let b1: [u8; 8] = *b"ABCDEFGH";
let b2: [u8; 8] = *b"abcdefgh";
let b3: [u8; 8] = *b"zyxwvuts";

assert_eq!(crc.crc(&b0), crc.crc(&b0));
assert_eq!(crc.crc_2x([&b0, &b1]),crc.crc_2x([&b0, &b1]));
assert_eq!(crc.crc_4x([&b0, &b1, &b2, &b3]),crc.crc_4x([&b0, &b1, &b2, &b3]));
Source

pub fn is_hardware_acceleration_available(&self) -> bool

Checks whether hardware acceleration is available at runtime

let crc = frozen_core::crc32::Crc32C::default();

#[cfg(target_arch = "x86_64")]
let expected = std::is_x86_feature_detected!("sse4.2");

#[cfg(target_arch = "aarch64")]
let expected = std::arch::is_aarch64_feature_detected!("crc");

assert_eq!(crc.is_hardware_acceleration_available(), expected);    
Source

pub fn crc(&self, buf: &[u8]) -> u32

Compute CRC32C for given buf to generate 32-bit crc

NOTE: Length of buf must be a multiple of 8

At runtime we choose the fastest available backend,

  • x86_64: sse4.2 when available
  • aarch64: ArmV8 crc instruction when available
  • fallback: software castagnoli impl

All backends produce identical CRC32C values

§Example
let crc = frozen_core::crc32::Crc32C::default();

let b0: [u8; 8] = *b"12345678";
assert_eq!(crc.crc(&b0), crc.crc(&b0));
Source

pub fn crc_2x(&self, buffers: [&[u8]; 2]) -> [u32; 2]

Compute CRC32C for two bufs in parallel using Instruction Level Parallelism to generate 32-bit crc for each buf (mapped one-to-one)

NOTE: Length of each buf must be equal and a multiple of 8

At runtime we choose the fastest available backend,

  • x86_64: sse4.2 when available
  • aarch64: ArmV8 crc instruction when available
  • fallback: software castagnoli impl

All backends produce identical CRC32C values

§Example
let crc = frozen_core::crc32::Crc32C::default();

let b0: [u8; 8] = *b"12345678";
let b1: [u8; 8] = *b"ABCDEFGH";

assert_eq!(crc.crc_2x([&b0, &b1]),crc.crc_2x([&b0, &b1]));
Source

pub fn crc_4x(&self, buffers: [&[u8]; 4]) -> [u32; 4]

Compute CRC32C for four bufs in parallel using Instruction Level Parallelism to generate 32-bit crc for each buf (mapped one-to-one)

NOTE: Length of each buf must be equal and a multiple of 8

At runtime we choose the fastest available backend,

  • x86_64: sse4.2 when available
  • aarch64: ArmV8 crc instruction when available
  • fallback: software castagnoli impl

All backends produce identical CRC32C values

§Example
let crc = frozen_core::crc32::Crc32C::default();

let b0: [u8; 8] = *b"12345678";
let b1: [u8; 8] = *b"ABCDEFGH";
let b2: [u8; 8] = *b"abcdefgh";
let b3: [u8; 8] = *b"zyxwvuts";

assert_eq!(crc.crc_4x([&b0, &b1, &b2, &b3]),crc.crc_4x([&b0, &b1, &b2, &b3]));

Trait Implementations§

Source§

impl Clone for Crc32C

Source§

fn clone(&self) -> Crc32C

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Crc32C

Source§

impl Debug for Crc32C

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Crc32C

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PartialEq for Crc32C

Source§

fn eq(&self, other: &Crc32C) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Crc32C

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.