| 1 |
# CRC Width-32 Shared Base Refactoring |
| 2 |
|
| 3 |
## Goal |
| 4 |
|
| 5 |
Refactor the CRC-16 and CRC-32 algorithm implementations to share a common base, reducing code duplication and preparing for future CRC width additions (like CRC-8). |
| 6 |
|
| 7 |
## Background |
| 8 |
|
| 9 |
CRC-16 computation is performed by scaling 16-bit values to 32-bit space, using the CRC-32 algorithm infrastructure, and then scaling the result back to 16 bits. This means CRC-16 and CRC-32 share nearly identical implementations for: |
| 10 |
|
| 11 |
- `process_0_to_15()` function (~100 lines each, nearly identical) |
| 12 |
- `load_constants()` - identical constants |
| 13 |
- `fold_16()` - identical logic |
| 14 |
- `fold_width()` - identical logic |
| 15 |
- `barrett_reduction()` - nearly identical (just different final extraction) |
| 16 |
- `create_coefficient()` - identical |
| 17 |
- `perform_final_reduction()` - identical |
| 18 |
- `get_last_bytes_table_ptr()` - identical |
| 19 |
|
| 20 |
## Approach |
| 21 |
|
| 22 |
Create a shared module (`src/crc32/width32_ops.rs`) containing the common 32-bit-space operations that both CRC-16 and CRC-32 use. The Width16 and Width32 trait implementations will delegate to these shared functions, with Width16 performing the final 16-bit extraction where needed. |
| 23 |
|
| 24 |
## Constraints |
| 25 |
|
| 26 |
- Must maintain full backwards compatibility with existing CRC-16, CRC-32, and CRC-64 functionality |
| 27 |
- All existing tests must continue to pass |
| 28 |
- The refactoring should make it easier to add future CRC widths (like CRC-8) |