| 2cd40ea1 Andy McCall ↶ |
1 |
|
| 2 |
|
| 3 |
#![allow(dead_code)] |
| 4 |
|
| 5 |
use crate::traits::{CrcCalculator, CrcWidth}; |
| 6 |
use crate::{arch, cache, CrcAlgorithm, CrcParams}; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 13 |
pub struct Algorithm<W> { |
| 14 |
|
| 15 |
|
| 16 |
pub width: u8, |
| 17 |
|
| 18 |
pub poly: W, |
| 19 |
|
| 20 |
|
| 21 |
pub init: W, |
| 22 |
|
| 23 |
|
| 24 |
pub refin: bool, |
| 25 |
|
| 26 |
|
| 27 |
pub refout: bool, |
| 28 |
|
| 29 |
|
| 30 |
pub xorout: W, |
| 31 |
|
| 32 |
|
| 33 |
pub check: W, |
| 34 |
|
| 35 |
|
| 36 |
pub residue: W, |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
#[derive(Clone, Copy)] |
| 41 |
pub struct Width16; |
| 42 |
|
| 43 |
impl CrcWidth for Width16 { |
| 44 |
const WIDTH: u32 = 16; |
| 45 |
type Value = u16; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
#[derive(Clone, Copy)] |
| 50 |
pub struct Width32; |
| 51 |
|
| 52 |
impl CrcWidth for Width32 { |
| 53 |
const WIDTH: u32 = 32; |
| 54 |
type Value = u32; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
#[derive(Clone, Copy)] |
| 59 |
pub struct Width64; |
| 60 |
|
| 61 |
impl CrcWidth for Width64 { |
| 62 |
const WIDTH: u32 = 64; |
| 63 |
type Value = u64; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
#[derive(Debug, Clone, Copy)] |
| 68 |
pub struct CrcState<T> { |
| 69 |
pub value: T, |
| 70 |
pub reflected: bool, |
| 71 |
} |
| 72 |
|
| 73 |
pub(crate) struct Calculator {} |
| 74 |
|
| 75 |
impl CrcCalculator for Calculator { |
| 76 |
#[inline(always)] |
| 77 |
fn calculate(state: u64, data: &[u8], params: &CrcParams) -> u64 { |
| 78 |
unsafe { arch::update(state, data, params) } |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
impl CrcParams { |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
pub fn new( |
| 93 |
name: &'static str, |
| 94 |
width: u8, |
| 95 |
poly: u64, |
| 96 |
init: u64, |
| 97 |
reflected: bool, |
| 98 |
xorout: u64, |
| 99 |
check: u64, |
| 100 |
) -> Self { |
| 101 |
let keys_array = cache::get_or_generate_keys(width, poly, reflected); |
| 102 |
let keys = crate::CrcKeysStorage::from_keys_fold_256(keys_array); |
| 103 |
|
| 104 |
|
| 105 |
if width != 16 && width != 32 && width != 64 { |
| 106 |
panic!("Unsupported width: {width}"); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
let init_algorithm = if width == 16 && reflected { |
| 111 |
(init as u16).reverse_bits() as u64 |
| 112 |
} else { |
| 113 |
init |
| 114 |
}; |
| 115 |
|
| 116 |
Self { |
| 117 |
algorithm: CrcAlgorithm::CrcCustom, |
| 118 |
name, |
| 119 |
width, |
| 120 |
poly, |
| 121 |
init, |
| 122 |
init_algorithm, |
| 123 |
refin: reflected, |
| 124 |
refout: reflected, |
| 125 |
xorout, |
| 126 |
check, |
| 127 |
keys, |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
#[inline(always)] |
| 134 |
pub fn get_key(&self, index: usize) -> u64 { |
| 135 |
self.keys.get_key(index) |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
#[inline(always)] |
| 141 |
pub fn get_key_checked(&self, index: usize) -> Option<u64> { |
| 142 |
if index < self.keys.key_count() { |
| 143 |
Some(self.keys.get_key(index)) |
| 144 |
} else { |
| 145 |
None |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
#[inline(always)] |
| 151 |
pub fn key_count(&self) -> usize { |
| 152 |
self.keys.key_count() |
| 153 |
} |
| 154 |
} |