| 1 |
// Copyright 2025 Don MacAskill. Licensed under MIT or Apache-2.0. |
| 2 |
|
| 3 |
#![allow(dead_code)] |
| 4 |
|
| 5 |
use crate::traits::{CrcCalculator, CrcWidth}; |
| 6 |
use crate::{arch, cache, CrcAlgorithm, CrcParams}; |
| 7 |
|
| 8 |
/// CRC-16 width implementation |
| 9 |
#[derive(Clone, Copy)] |
| 10 |
pub struct Width16; |
| 11 |
|
| 12 |
impl CrcWidth for Width16 { |
| 13 |
const WIDTH: u32 = 16; |
| 14 |
type Value = u16; |
| 15 |
} |
| 16 |
|
| 17 |
/// CRC-32 width implementation |
| 18 |
#[derive(Clone, Copy)] |
| 19 |
pub struct Width32; |
| 20 |
|
| 21 |
impl CrcWidth for Width32 { |
| 22 |
const WIDTH: u32 = 32; |
| 23 |
type Value = u32; |
| 24 |
} |
| 25 |
|
| 26 |
/// CRC-64 width implementation |
| 27 |
#[derive(Clone, Copy)] |
| 28 |
pub struct Width64; |
| 29 |
|
| 30 |
impl CrcWidth for Width64 { |
| 31 |
const WIDTH: u32 = 64; |
| 32 |
type Value = u64; |
| 33 |
} |
| 34 |
|
| 35 |
/// CRC State wrapper to manage the SIMD operations and reflection mode |
| 36 |
#[derive(Debug, Clone, Copy)] |
| 37 |
pub struct CrcState<T> { |
| 38 |
pub value: T, |
| 39 |
pub reflected: bool, |
| 40 |
} |
| 41 |
|
| 42 |
pub(crate) struct Calculator {} |
| 43 |
|
| 44 |
impl CrcCalculator for Calculator { |
| 45 |
#[inline(always)] |
| 46 |
fn calculate(state: u64, data: &[u8], params: &CrcParams) -> u64 { |
| 47 |
unsafe { arch::update(state, data, params) } |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
impl CrcParams { |
| 52 |
/// Creates custom CRC parameters for a given set of Rocksoft CRC parameters. |
| 53 |
/// |
| 54 |
/// Uses an internal cache to avoid regenerating folding keys for identical parameter sets. |
| 55 |
/// The first call with a given set of parameters will generate and cache the keys, while |
| 56 |
/// subsequent calls with the same parameters will use the cached keys for optimal performance. |
| 57 |
/// |
| 58 |
/// Does not support mis-matched refin/refout parameters, so both must be true or both false. |
| 59 |
/// |
| 60 |
/// Rocksoft parameters for lots of variants: https://reveng.sourceforge.io/crc-catalogue/all.htm |
| 61 |
pub fn new( |
| 62 |
name: &'static str, |
| 63 |
width: u8, |
| 64 |
poly: u64, |
| 65 |
init: u64, |
| 66 |
reflected: bool, |
| 67 |
xorout: u64, |
| 68 |
check: u64, |
| 69 |
) -> Self { |
| 70 |
let keys_array = cache::get_or_generate_keys(width, poly, reflected); |
| 71 |
let keys = crate::CrcKeysStorage::from_keys_fold_256(keys_array); |
| 72 |
|
| 73 |
// Validate width is supported |
| 74 |
if width != 16 && width != 32 && width != 64 { |
| 75 |
panic!("Unsupported width: {width}"); |
| 76 |
} |
| 77 |
|
| 78 |
// For reflected CRC-16, bit-reverse the init value for the SIMD algorithm |
| 79 |
let init_algorithm = if width == 16 && reflected { |
| 80 |
(init as u16).reverse_bits() as u64 |
| 81 |
} else { |
| 82 |
init |
| 83 |
}; |
| 84 |
|
| 85 |
Self { |
| 86 |
algorithm: CrcAlgorithm::CrcCustom, |
| 87 |
name, |
| 88 |
width, |
| 89 |
poly, |
| 90 |
init, |
| 91 |
init_algorithm, |
| 92 |
refin: reflected, |
| 93 |
refout: reflected, |
| 94 |
xorout, |
| 95 |
check, |
| 96 |
keys, |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
/// Gets a key at the specified index, returning 0 if out of bounds. |
| 101 |
/// This provides safe access regardless of internal key storage format. |
| 102 |
#[inline(always)] |
| 103 |
pub fn get_key(&self, index: usize) -> u64 { |
| 104 |
self.keys.get_key(index) |
| 105 |
} |
| 106 |
|
| 107 |
/// Gets a key at the specified index, returning None if out of bounds. |
| 108 |
/// This provides optional key access for cases where bounds checking is needed. |
| 109 |
#[inline(always)] |
| 110 |
pub fn get_key_checked(&self, index: usize) -> Option<u64> { |
| 111 |
if index < self.keys.key_count() { |
| 112 |
Some(self.keys.get_key(index)) |
| 113 |
} else { |
| 114 |
None |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
/// Returns the number of keys available in this CrcParams instance. |
| 119 |
#[inline(always)] |
| 120 |
pub fn key_count(&self) -> usize { |
| 121 |
self.keys.key_count() |
| 122 |
} |
| 123 |
} |