ports/alif: Use direct CRC calculation for unaligned bytes

Replace lookup table with bit-by-bit polynomial calculation for
the remaining 1-3 bytes. Direct calculation is faster than table
lookup for such small amounts due to avoiding cache misses and
memory indirection overhead that dominates when processing only
1-3 bytes.

Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
This commit is contained in:
iabdalkader 2025-09-26 19:39:12 +02:00
parent 0612fc9768
commit 75a2a7a2d5
2 changed files with 10 additions and 6 deletions

View File

@ -60,7 +60,7 @@ static const uint16_t crc16_table[256] = {
};
// CRC32 lookup table for polynomial 0xFA567D89
const uint32_t crc32_table[256] = {
static const uint32_t crc32_table[256] = {
0x00000000, 0xFA567D89, 0x0EFA869B, 0xF4ACFB12, 0x1DF50D36, 0xE7A370BF, 0x130F8BAD, 0xE959F624,
0x3BEA1A6C, 0xC1BC67E5, 0x35109CF7, 0xCF46E17E, 0x261F175A, 0xDC496AD3, 0x28E591C1, 0xD2B3EC48,
0x77D434D8, 0x8D824951, 0x792EB243, 0x8378CFCA, 0x6A2139EE, 0x90774467, 0x64DBBF75, 0x9E8DC2FC,

View File

@ -28,8 +28,6 @@
#define CRC0 ((CRC_Type *) CRC0_BASE)
static bool crc32_initialized = false;
extern const uint32_t crc32_table[256];
static void crc_calculate(CRC_Type *crc, const void *buf, uint32_t len, uint32_t *value);
static void omv_crc32_init(void) {
@ -89,14 +87,20 @@ static void crc_calculate(CRC_Type *crc, const void *buf, uint32_t len, uint32_t
*value = crc->CRC_OUT;
}
// Process remaining bytes using software CRC with lookup table
// Process remaining bytes using software CRC calculation
if (remainder > 0) {
uint32_t result = *value;
const uint8_t *remaining_data = data + aligned_len;
for (uint32_t i = 0; i < remainder; i++) {
uint8_t index = (result >> 24) ^ remaining_data[i];
result = (result << 8) ^ crc32_table[index];
result ^= (uint32_t) remaining_data[i] << 24;
for (int bit = 0; bit < 8; bit++) {
if (result & 0x80000000) {
result = (result << 1) ^ OMV_CRC32_POLY;
} else {
result <<= 1;
}
}
}
*value = result;
}