updated crc algorithm to a more efficient implementation

This commit is contained in:
2026-03-25 13:54:20 +01:00
parent 5ab61e77d6
commit 6b82ddcd74
+7 -10
View File
@@ -332,7 +332,7 @@ pub fn check_hemming(data: u32, hemming: u8) -> Result<(), HemmingCorrectionValu
/// CRC-6 calculation using polynomial 0x21 (MSB first)
///
/// This function calculates a 6-bit CRC for 26 bits of data.
/// This function calculates a 6-bit CRC for a u32 value.
/// The polynomial used is 0x21 in the crc world polynoms
/// are named after the bit pattern with implicit assumption
/// of the trailing 1, thus 0x21 become 0x43 referring to
@@ -349,18 +349,15 @@ pub fn check_hemming(data: u32, hemming: u8) -> Result<(), HemmingCorrectionValu
/// - All double-bit errors
/// - Random errors with a probability of 2^-6 = 0.015625 of going undetected
pub fn calc_crc6(data: u32) -> u8 {
let mut working_data: u32 = data << 6;
let mut polynom: u32 = 0x43 << 25;
let mut mask: u32 = 0x40 << 25;
while working_data >= 0x40 {
if working_data & mask != 0 {
working_data ^= polynom;
let mut crc: u8 = 0x00;
for pos in 0..32 {
if ((data >> (31-pos)) & 0x1) ^ ((crc >> 5) & 0x1) as u32 != 0 {
crc = ((crc << 1) ^ 0x03) & 0x3F;
} else {
polynom >>= 1;
mask >>= 1;
crc = (crc << 1) & 0x3F;
}
}
(working_data & 0x3f) as u8
crc
}
/// CRC-6 checking