Skip to content

Commit 35d62fe

Browse files
committed
Switch CrcParams to use CrcKeysStorage
- [x] 3. Phase 3: Switch CrcParams to use CrcKeysStorage - [x] 3.1 Update CrcParams struct definition - Change keys field from [u64; 23] to CrcKeysStorage - Update CrcParams accessor methods to delegate to CrcKeysStorage - Remove temporary delegation methods added in Phase 1 - _Requirements: 5.3_ - [x] 3.2 Update all CRC32 const definitions - Update src/crc32/consts.rs to use CrcKeysStorage::from_keys_fold_256() - Modify all CRC32_* const definitions to use new key storage format - Ensure all existing key arrays are properly wrapped - _Requirements: 1.2, 2.1_ - [x] 3.3 Update all CRC64 const definitions - Update src/crc64/consts.rs to use CrcKeysStorage::from_keys_fold_256() - Modify all CRC64_* const definitions to use new key storage format - Ensure all existing key arrays are properly wrapped - _Requirements: 1.2, 2.1_ - [x] 3.4 Update get-custom-params binary output - Modify src/bin/get-custom-params.rs to output CrcKeysStorage format - Update output template to use CrcKeysStorage::from_keys_fold_256() - Test that generated const definitions compile and work correctly - _Requirements: 6.1, 6.2, 6.3_ - [x] 3.5 Update cache system for new CrcParams structure - Modify src/cache.rs to work with CrcKeysStorage-based CrcParams - Update CrcParams::new() method to use new key storage format - Ensure cache functionality remains intact after structural changes - _Requirements: 2.3, 5.3_
1 parent d726a6a commit 35d62fe

File tree

9 files changed

+108
-54
lines changed

9 files changed

+108
-54
lines changed

.kiro/specs/future-proof-crc-params/tasks.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,32 +31,32 @@
3131
- Document that performance remains identical to direct array access
3232
- _Requirements: 2.2, 4.4_
3333

34-
- [ ] 3. Phase 3: Switch CrcParams to use CrcKeysStorage
35-
- [ ] 3.1 Update CrcParams struct definition
34+
- [x] 3. Phase 3: Switch CrcParams to use CrcKeysStorage
35+
- [x] 3.1 Update CrcParams struct definition
3636
- Change keys field from [u64; 23] to CrcKeysStorage
3737
- Update CrcParams accessor methods to delegate to CrcKeysStorage
3838
- Remove temporary delegation methods added in Phase 1
3939
- _Requirements: 5.3_
4040

41-
- [ ] 3.2 Update all CRC32 const definitions
41+
- [x] 3.2 Update all CRC32 const definitions
4242
- Update src/crc32/consts.rs to use CrcKeysStorage::from_keys_fold_256()
4343
- Modify all CRC32_* const definitions to use new key storage format
4444
- Ensure all existing key arrays are properly wrapped
4545
- _Requirements: 1.2, 2.1_
4646

47-
- [ ] 3.3 Update all CRC64 const definitions
47+
- [x] 3.3 Update all CRC64 const definitions
4848
- Update src/crc64/consts.rs to use CrcKeysStorage::from_keys_fold_256()
4949
- Modify all CRC64_* const definitions to use new key storage format
5050
- Ensure all existing key arrays are properly wrapped
5151
- _Requirements: 1.2, 2.1_
5252

53-
- [ ] 3.4 Update get-custom-params binary output
53+
- [x] 3.4 Update get-custom-params binary output
5454
- Modify src/bin/get-custom-params.rs to output CrcKeysStorage format
5555
- Update output template to use CrcKeysStorage::from_keys_fold_256()
5656
- Test that generated const definitions compile and work correctly
5757
- _Requirements: 6.1, 6.2, 6.3_
5858

59-
- [ ] 3.5 Update cache system for new CrcParams structure
59+
- [x] 3.5 Update cache system for new CrcParams structure
6060
- Modify src/cache.rs to work with CrcKeysStorage-based CrcParams
6161
- Update CrcParams::new() method to use new key storage format
6262
- Ensure cache functionality remains intact after structural changes

src/bin/get-custom-params.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,40 @@ fn main() -> ExitCode {
178178
);
179179

180180
println!();
181-
println!("{:#x?}", params);
181+
println!("// Generated CRC parameters for {}", static_name);
182+
println!(
183+
"pub const {}: CrcParams = CrcParams {{",
184+
static_name
185+
.to_uppercase()
186+
.replace("-", "_")
187+
.replace("/", "_")
188+
);
189+
println!(
190+
" algorithm: CrcAlgorithm::{}Custom,",
191+
if config.width.unwrap() == 32 {
192+
"Crc32"
193+
} else {
194+
"Crc64"
195+
}
196+
);
197+
println!(" name: \"{}\",", static_name);
198+
println!(" width: {},", config.width.unwrap());
199+
println!(" poly: 0x{:x},", config.polynomial.unwrap());
200+
println!(" init: 0x{:x},", config.init.unwrap());
201+
println!(" refin: {},", config.reflected.unwrap());
202+
println!(" refout: {},", config.reflected.unwrap());
203+
println!(" xorout: 0x{:x},", config.xorout.unwrap());
204+
println!(" check: 0x{:x},", config.check.unwrap());
205+
println!(" keys: CrcKeysStorage::from_keys_fold_256([");
206+
207+
// Print the keys array
208+
for i in 0..23 {
209+
let key = params.get_key(i);
210+
println!(" 0x{:016x},", key);
211+
}
212+
213+
println!(" ]),");
214+
println!("}};");
182215
println!();
183216

184217
ExitCode::from(0)

src/cache.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,7 +1378,7 @@ mod tests {
13781378
let _refout: bool = params.refout;
13791379
let _xorout: u64 = params.xorout;
13801380
let _check: u64 = params.check;
1381-
let _keys: [u64; 23] = params.keys;
1381+
let _keys: [u64; 23] = params.keys.to_keys_array_23();
13821382

13831383
// Verify the algorithm is set correctly based on width
13841384
match params.width {
@@ -1498,12 +1498,12 @@ mod tests {
14981498
assert_eq!(params64_max.poly, 0xFFFFFFFFFFFFFFFF);
14991499

15001500
// Verify all instances have valid 23-element key arrays
1501-
assert_eq!(params_min_poly.keys.len(), 23);
1502-
assert_eq!(params_max_poly.keys.len(), 23);
1503-
assert_eq!(params_reflected.keys.len(), 23);
1504-
assert_eq!(params_normal.keys.len(), 23);
1505-
assert_eq!(params64_min.keys.len(), 23);
1506-
assert_eq!(params64_max.keys.len(), 23);
1501+
assert_eq!(params_min_poly.keys.key_count(), 23);
1502+
assert_eq!(params_max_poly.keys.key_count(), 23);
1503+
assert_eq!(params_reflected.keys.key_count(), 23);
1504+
assert_eq!(params_normal.keys.key_count(), 23);
1505+
assert_eq!(params64_min.keys.key_count(), 23);
1506+
assert_eq!(params64_max.keys.key_count(), 23);
15071507
}
15081508

15091509
#[test]

src/crc32/consts.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub const CRC32_AIXM: CrcParams = CrcParams {
2525
refout: CRC_32_AIXM.refout, // false
2626
xorout: CRC_32_AIXM.xorout as u64,
2727
check: CRC_32_AIXM.check as u64,
28-
keys: KEYS_814141AB_FORWARD,
28+
keys: crate::CrcKeysStorage::from_keys_fold_256(KEYS_814141AB_FORWARD),
2929
};
3030

3131
// width=32 poly=0xf4acfb13 init=0xffffffff refin=true refout=true xorout=0xffffffff check=0x1697d06a residue=0x904cddbf name="CRC-32/AUTOSAR"
@@ -39,7 +39,7 @@ pub const CRC32_AUTOSAR: CrcParams = CrcParams {
3939
refout: CRC_32_AUTOSAR.refout, // true
4040
xorout: CRC_32_AUTOSAR.xorout as u64,
4141
check: CRC_32_AUTOSAR.check as u64,
42-
keys: KEYS_F4ACFB13_REFLECTED,
42+
keys: crate::CrcKeysStorage::from_keys_fold_256(KEYS_F4ACFB13_REFLECTED),
4343
};
4444

4545
// width=32 poly=0xa833982b init=0xffffffff refin=true refout=true xorout=0xffffffff check=0x87315576 residue=0x45270551 name="CRC-32/BASE91-D"
@@ -53,7 +53,7 @@ pub const CRC32_BASE91_D: CrcParams = CrcParams {
5353
refout: CRC_32_BASE91_D.refout, // true
5454
xorout: CRC_32_BASE91_D.xorout as u64,
5555
check: CRC_32_BASE91_D.check as u64,
56-
keys: KEYS_A833982B_REFLECTED,
56+
keys: crate::CrcKeysStorage::from_keys_fold_256(KEYS_A833982B_REFLECTED),
5757
};
5858

5959
// width=32 poly=0x04c11db7 init=0xffffffff refin=false refout=false xorout=0xffffffff check=0xfc891918 residue=0xc704dd7b name="CRC-32/BZIP2"
@@ -67,7 +67,7 @@ pub const CRC32_BZIP2: CrcParams = CrcParams {
6767
refout: CRC_32_BZIP2.refout, // false
6868
xorout: CRC_32_BZIP2.xorout as u64,
6969
check: CRC_32_BZIP2.check as u64,
70-
keys: KEYS_04C11DB7_FORWARD,
70+
keys: crate::CrcKeysStorage::from_keys_fold_256(KEYS_04C11DB7_FORWARD),
7171
};
7272

7373
// width=32 poly=0x8001801b init=0x00000000 refin=true refout=true xorout=0x00000000 check=0x6ec2edc4 residue=0x00000000 name="CRC-32/CD-ROM-EDC"
@@ -81,7 +81,7 @@ pub const CRC32_CD_ROM_EDC: CrcParams = CrcParams {
8181
refout: CRC_32_CD_ROM_EDC.refout, // true
8282
xorout: CRC_32_CD_ROM_EDC.xorout as u64,
8383
check: CRC_32_CD_ROM_EDC.check as u64,
84-
keys: KEYS_8001801B_REFLECTED,
84+
keys: crate::CrcKeysStorage::from_keys_fold_256(KEYS_8001801B_REFLECTED),
8585
};
8686

8787
// width=32 poly=0x04c11db7 init=0x00000000 refin=false refout=false xorout=0xffffffff check=0x765e7680 residue=0xc704dd7b name="CRC-32/CKSUM"
@@ -95,7 +95,7 @@ pub const CRC32_CKSUM: CrcParams = CrcParams {
9595
refout: CRC_32_CKSUM.refout, // false
9696
xorout: CRC_32_CKSUM.xorout as u64,
9797
check: CRC_32_CKSUM.check as u64,
98-
keys: KEYS_04C11DB7_FORWARD,
98+
keys: crate::CrcKeysStorage::from_keys_fold_256(KEYS_04C11DB7_FORWARD),
9999
};
100100

101101
// width=32 poly=0x1edc6f41 init=0xffffffff refin=true refout=true xorout=0xffffffff check=0xe3069283 residue=0xb798b438 name="CRC-32/ISCSI"
@@ -109,7 +109,7 @@ pub const CRC32_ISCSI: CrcParams = CrcParams {
109109
refout: CRC_32_ISCSI.refout, // true
110110
xorout: CRC_32_ISCSI.xorout as u64,
111111
check: CRC_32_ISCSI.check as u64,
112-
keys: KEYS_1EDC6F41_REFLECTED,
112+
keys: crate::CrcKeysStorage::from_keys_fold_256(KEYS_1EDC6F41_REFLECTED),
113113
};
114114

115115
// width=32 poly=0x04c11db7 init=0xffffffff refin=true refout=true xorout=0xffffffff check=0xcbf43926 residue=0xdebb20e3 name="CRC-32/ISO-HDLC"
@@ -123,7 +123,7 @@ pub const CRC32_ISO_HDLC: CrcParams = CrcParams {
123123
refout: CRC_32_ISO_HDLC.refout, // true
124124
xorout: CRC_32_ISO_HDLC.xorout as u64,
125125
check: CRC_32_ISO_HDLC.check as u64,
126-
keys: KEYS_04C11DB7_REFLECTED,
126+
keys: crate::CrcKeysStorage::from_keys_fold_256(KEYS_04C11DB7_REFLECTED),
127127
};
128128

129129
// width=32 poly=0x04c11db7 init=0xffffffff refin=true refout=true xorout=0x00000000 check=0x340bc6d9 residue=0x00000000 name="CRC-32/JAMCRC"
@@ -137,7 +137,7 @@ pub const CRC32_JAMCRC: CrcParams = CrcParams {
137137
refout: CRC_32_JAMCRC.refout, // true
138138
xorout: CRC_32_JAMCRC.xorout as u64,
139139
check: CRC_32_JAMCRC.check as u64,
140-
keys: KEYS_04C11DB7_REFLECTED,
140+
keys: crate::CrcKeysStorage::from_keys_fold_256(KEYS_04C11DB7_REFLECTED),
141141
};
142142

143143
// width=32 poly=0x741b8cd7 init=0xffffffff refin=true refout=true xorout=0x00000000 check=0xd2c22f51 residue=0x00000000 name="CRC-32/MEF"
@@ -151,7 +151,7 @@ pub const CRC32_MEF: CrcParams = CrcParams {
151151
refout: CRC_32_MEF.refout, // true
152152
xorout: CRC_32_MEF.xorout as u64,
153153
check: CRC_32_MEF.check as u64,
154-
keys: KEYS_741B8CD7_REFLECTED,
154+
keys: crate::CrcKeysStorage::from_keys_fold_256(KEYS_741B8CD7_REFLECTED),
155155
};
156156

157157
// width=32 poly=0x04c11db7 init=0xffffffff refin=false refout=false xorout=0x00000000 check=0x0376e6e7 residue=0x00000000 name="CRC-32/MPEG-2"
@@ -165,7 +165,7 @@ pub const CRC32_MPEG_2: CrcParams = CrcParams {
165165
refout: CRC_32_MPEG_2.refout, // false
166166
xorout: CRC_32_MPEG_2.xorout as u64,
167167
check: CRC_32_MPEG_2.check as u64,
168-
keys: KEYS_04C11DB7_FORWARD,
168+
keys: crate::CrcKeysStorage::from_keys_fold_256(KEYS_04C11DB7_FORWARD),
169169
};
170170

171171
// width=32 poly=0x000000af init=0x00000000 refin=false refout=false xorout=0x00000000 check=0xbd0be338 residue=0x00000000 name="CRC-32/XFER"
@@ -179,7 +179,7 @@ pub const CRC32_XFER: CrcParams = CrcParams {
179179
refout: CRC_32_XFER.refout, // false
180180
xorout: CRC_32_XFER.xorout as u64,
181181
check: CRC_32_XFER.check as u64,
182-
keys: KEYS_000000AF_FORWARD,
182+
keys: crate::CrcKeysStorage::from_keys_fold_256(KEYS_000000AF_FORWARD),
183183
};
184184

185185
// CRC-32/AIXM

src/crc64/consts.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub const CRC64_ECMA_182: CrcParams = CrcParams {
1818
refout: CRC_64_ECMA_182.refout, // false
1919
xorout: CRC_64_ECMA_182.xorout,
2020
check: CRC_64_ECMA_182.check,
21-
keys: KEYS_42F0E1EBA9EA3693_FORWARD,
21+
keys: crate::CrcKeysStorage::from_keys_fold_256(KEYS_42F0E1EBA9EA3693_FORWARD),
2222
};
2323

2424
// width=64 poly=0x000000000000001b init=0xffffffffffffffff refin=true refout=true xorout=0xffffffffffffffff check=0xb90956c775a41001 residue=0x5300000000000000 name="CRC-64/GO-ISO"
@@ -32,7 +32,7 @@ pub const CRC64_GO_ISO: CrcParams = CrcParams {
3232
refout: CRC_64_GO_ISO.refout, // true
3333
xorout: CRC_64_GO_ISO.xorout,
3434
check: CRC_64_GO_ISO.check,
35-
keys: KEYS_000000000000001B_REFLECTED,
35+
keys: crate::CrcKeysStorage::from_keys_fold_256(KEYS_000000000000001B_REFLECTED),
3636
};
3737

3838
// width=64 poly=0x259c84cba6426349 init=0xffffffffffffffff refin=true refout=true xorout=0x0000000000000000 check=0x75d4b74f024eceea residue=0x0000000000000000 name="CRC-64/MS"
@@ -46,7 +46,7 @@ pub const CRC64_MS: CrcParams = CrcParams {
4646
refout: CRC_64_MS.refout, // true
4747
xorout: CRC_64_MS.xorout,
4848
check: CRC_64_MS.check,
49-
keys: KEYS_259C84CBA6426349_REFLECTED,
49+
keys: crate::CrcKeysStorage::from_keys_fold_256(KEYS_259C84CBA6426349_REFLECTED),
5050
};
5151

5252
// https://reveng.sourceforge.io/crc-catalogue/all.htm#crc.cat.crc-64-nvme
@@ -61,7 +61,7 @@ pub const CRC64_NVME: CrcParams = CrcParams {
6161
refout: CRC_64_NVME.refout, // true
6262
xorout: CRC_64_NVME.xorout,
6363
check: CRC_64_NVME.check,
64-
keys: KEYS_AD93D23594C93659_REFLECTED,
64+
keys: crate::CrcKeysStorage::from_keys_fold_256(KEYS_AD93D23594C93659_REFLECTED),
6565
};
6666

6767
// width=64 poly=0xad93d23594c935a9 init=0x0000000000000000 refin=true refout=true xorout=0x0000000000000000 check=0xe9c6d914c4b8d9ca residue=0x0000000000000000 name="CRC-64/REDIS"
@@ -75,7 +75,7 @@ pub const CRC64_REDIS: CrcParams = CrcParams {
7575
refout: CRC_64_REDIS.refout, // true
7676
xorout: CRC_64_REDIS.xorout,
7777
check: CRC_64_REDIS.check,
78-
keys: KEYS_AD93D23594C935A9_REFLECTED,
78+
keys: crate::CrcKeysStorage::from_keys_fold_256(KEYS_AD93D23594C935A9_REFLECTED),
7979
};
8080

8181
// width=64 poly=0x42f0e1eba9ea3693 init=0xffffffffffffffff refin=false refout=false xorout=0xffffffffffffffff check=0x62ec59e3f1a4f00a residue=0xfcacbebd5931a992 name="CRC-64/WE"
@@ -89,7 +89,7 @@ pub const CRC64_WE: CrcParams = CrcParams {
8989
refout: CRC_64_WE.refout, // false
9090
xorout: CRC_64_WE.xorout,
9191
check: CRC_64_WE.check,
92-
keys: KEYS_42F0E1EBA9EA3693_FORWARD,
92+
keys: crate::CrcKeysStorage::from_keys_fold_256(KEYS_42F0E1EBA9EA3693_FORWARD),
9393
};
9494

9595
// width=64 poly=0x42f0e1eba9ea3693 init=0xffffffffffffffff refin=true refout=true xorout=0xffffffffffffffff check=0x995dc9bbdf1939fa residue=0x49958c9abd7d353f name="CRC-64/XZ"
@@ -103,7 +103,7 @@ pub const CRC64_XZ: CrcParams = CrcParams {
103103
refout: CRC_64_XZ.refout, // true
104104
xorout: CRC_64_XZ.xorout,
105105
check: CRC_64_XZ.check,
106-
keys: KEYS_42F0E1EBA9EA3693_REFLECTED,
106+
keys: crate::CrcKeysStorage::from_keys_fold_256(KEYS_42F0E1EBA9EA3693_REFLECTED),
107107
};
108108

109109
// CRC-64/MS

src/ffi.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl From<CrcFastParams> for CrcParams {
100100
refout: value.refout,
101101
xorout: value.xorout,
102102
check: value.check,
103-
keys: value.keys,
103+
keys: crate::CrcKeysStorage::from_keys_fold_256(value.keys),
104104
}
105105
}
106106
}
@@ -368,7 +368,7 @@ pub extern "C" fn crc_fast_get_custom_params(
368368
refout: params.refout,
369369
xorout: params.xorout,
370370
check: params.check,
371-
keys: params.keys,
371+
keys: params.keys.to_keys_array_23(),
372372
}
373373
}
374374

src/lib.rs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,16 +165,14 @@ pub enum CrcAlgorithm {
165165
/// Internal storage for CRC folding keys that can accommodate different array sizes.
166166
/// This enum allows future expansion to support larger folding distances while maintaining
167167
/// backwards compatibility with existing const definitions.
168-
#[derive(Clone, Copy, Debug)]
169-
#[allow(dead_code)] // Used in Phase 3 of migration
170-
enum CrcKeysStorage {
168+
#[derive(Clone, Copy, Debug, PartialEq)]
169+
pub enum CrcKeysStorage {
171170
/// Current 23-key format for existing algorithms (supports up to 256-byte folding distances)
172171
KeysFold256([u64; 23]),
173172
/// Future 25-key format for potential expanded folding distances (testing purposes only)
174173
KeysFutureTest([u64; 25]),
175174
}
176175

177-
#[allow(dead_code)] // Used in Phase 3 of migration
178176
impl CrcKeysStorage {
179177
/// Safe key access with bounds checking. Returns 0 for out-of-bounds indices.
180178
#[inline(always)]
@@ -214,9 +212,38 @@ impl CrcKeysStorage {
214212

215213
/// Const constructor for 25-key arrays (future expansion testing).
216214
#[inline(always)]
215+
#[allow(dead_code)] // Reserved for future expansion
217216
const fn from_keys_fold_future_test(keys: [u64; 25]) -> Self {
218217
CrcKeysStorage::KeysFutureTest(keys)
219218
}
219+
220+
/// Extracts keys as a [u64; 23] array for FFI compatibility.
221+
/// For variants with more than 23 keys, only the first 23 are returned.
222+
/// For variants with fewer keys, remaining slots are filled with 0.
223+
#[inline(always)]
224+
pub fn to_keys_array_23(self) -> [u64; 23] {
225+
match self {
226+
CrcKeysStorage::KeysFold256(keys) => keys,
227+
CrcKeysStorage::KeysFutureTest(keys) => {
228+
let mut result = [0u64; 23];
229+
result.copy_from_slice(&keys[..23]);
230+
result
231+
}
232+
}
233+
}
234+
}
235+
236+
// Implement PartialEq between CrcKeysStorage and [u64; 23] for test compatibility
237+
impl PartialEq<[u64; 23]> for CrcKeysStorage {
238+
fn eq(&self, other: &[u64; 23]) -> bool {
239+
self.to_keys_array_23() == *other
240+
}
241+
}
242+
243+
impl PartialEq<CrcKeysStorage> for [u64; 23] {
244+
fn eq(&self, other: &CrcKeysStorage) -> bool {
245+
*self == other.to_keys_array_23()
246+
}
220247
}
221248

222249
/// Parameters for CRC computation, including polynomial, initial value, and other settings.
@@ -231,7 +258,7 @@ pub struct CrcParams {
231258
pub refout: bool,
232259
pub xorout: u64,
233260
pub check: u64,
234-
pub keys: [u64; 23],
261+
pub keys: CrcKeysStorage,
235262
}
236263

237264
/// Type alias for a function pointer that represents a CRC calculation function.
@@ -1217,7 +1244,7 @@ mod lib {
12171244
refout: true,
12181245
xorout: 0xFFFFFFFF,
12191246
check: 0xCBF43926,
1220-
keys: test_keys,
1247+
keys: CrcKeysStorage::from_keys_fold_256(test_keys),
12211248
};
12221249

12231250
// Test valid key access

0 commit comments

Comments
 (0)