Skip to content

Commit b79d65d

Browse files
committed
Add a const fn implementation to compute CRC_LOOKUP_ARRAY
For now our MSRV remains below 1.46, but add code to compute the array that can be enabled once the MSRV is increased.
1 parent da902d8 commit b79d65d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/crc.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,31 @@ static CRC_LOOKUP_ARRAY : &'static[u32] = &[
8080
0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668,
8181
0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4];
8282

83+
/*
84+
// Const implementation: TODO adopt it once MSRV > 1.46
85+
static CRC_LOOKUP_ARRAY :&[u32] = &lookup_array();
86+
87+
const fn get_tbl_elem(idx :u32) -> u32 {
88+
let mut r :u32 = idx << 24;
89+
let mut i = 0;
90+
while i < 8 {
91+
r = (r << 1) ^ (-(((r >> 31) & 1) as i32) as u32 & 0x04c11db7);
92+
i += 1;
93+
}
94+
return r;
95+
}
96+
97+
const fn lookup_array() -> [u32; 0x100] {
98+
let mut lup_arr :[u32; 0x100] = [0; 0x100];
99+
let mut i = 0;
100+
while i < 0x100 {
101+
lup_arr[i] = get_tbl_elem(i as u32);
102+
i += 1;
103+
}
104+
lup_arr
105+
}
106+
*/
107+
83108
#[cfg(test)]
84109
pub fn vorbis_crc32(array :&[u8]) -> u32 {
85110
return vorbis_crc32_update(0, array);

0 commit comments

Comments
 (0)