1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-31 13:07:46 +00:00

cksum: use while loops instead of unroll!

This commit is contained in:
Thomas Queiroz 2021-10-23 23:25:22 -03:00
parent 21a2d0ce40
commit 1f15b8fce4
No known key found for this signature in database
GPG key ID: 229D2DDF7ECA5F8F

View file

@ -25,60 +25,15 @@ const NAME: &str = "cksum";
const SYNTAX: &str = "[OPTIONS] [FILE]..."; const SYNTAX: &str = "[OPTIONS] [FILE]...";
const SUMMARY: &str = "Print CRC and size for each file"; const SUMMARY: &str = "Print CRC and size for each file";
// this is basically a hack to get "loops" to work on Rust 1.33. Once we update to Rust 1.46 or
// greater, we can just use while loops
macro_rules! unroll {
(256, |$i:ident| $s:expr) => {{
unroll!(@ 32, 0 * 32, $i, $s);
unroll!(@ 32, 1 * 32, $i, $s);
unroll!(@ 32, 2 * 32, $i, $s);
unroll!(@ 32, 3 * 32, $i, $s);
unroll!(@ 32, 4 * 32, $i, $s);
unroll!(@ 32, 5 * 32, $i, $s);
unroll!(@ 32, 6 * 32, $i, $s);
unroll!(@ 32, 7 * 32, $i, $s);
}};
(8, |$i:ident| $s:expr) => {{
unroll!(@ 8, 0, $i, $s);
}};
(@ 32, $start:expr, $i:ident, $s:expr) => {{
unroll!(@ 8, $start + 0 * 8, $i, $s);
unroll!(@ 8, $start + 1 * 8, $i, $s);
unroll!(@ 8, $start + 2 * 8, $i, $s);
unroll!(@ 8, $start + 3 * 8, $i, $s);
}};
(@ 8, $start:expr, $i:ident, $s:expr) => {{
unroll!(@ 4, $start, $i, $s);
unroll!(@ 4, $start + 4, $i, $s);
}};
(@ 4, $start:expr, $i:ident, $s:expr) => {{
unroll!(@ 2, $start, $i, $s);
unroll!(@ 2, $start + 2, $i, $s);
}};
(@ 2, $start:expr, $i:ident, $s:expr) => {{
unroll!(@ 1, $start, $i, $s);
unroll!(@ 1, $start + 1, $i, $s);
}};
(@ 1, $start:expr, $i:ident, $s:expr) => {{
let $i = $start;
let _ = $s;
}};
}
const fn generate_crc_table() -> [u32; CRC_TABLE_LEN] { const fn generate_crc_table() -> [u32; CRC_TABLE_LEN] {
let mut table = [0; CRC_TABLE_LEN]; let mut table = [0; CRC_TABLE_LEN];
// NOTE: works on Rust 1.46 let mut i = 0;
//let mut i = 0; while i < CRC_TABLE_LEN {
//while i < CRC_TABLE_LEN {
// table[i] = crc_entry(i as u8) as u32;
//
// i += 1;
//}
unroll!(256, |i| {
table[i] = crc_entry(i as u8) as u32; table[i] = crc_entry(i as u8) as u32;
});
i += 1;
}
table table
} }
@ -86,19 +41,8 @@ const fn generate_crc_table() -> [u32; CRC_TABLE_LEN] {
const fn crc_entry(input: u8) -> u32 { const fn crc_entry(input: u8) -> u32 {
let mut crc = (input as u32) << 24; let mut crc = (input as u32) << 24;
// NOTE: this does not work on Rust 1.33, but *does* on 1.46 let mut i = 0;
//let mut i = 0; while i < 8 {
//while i < 8 {
// if crc & 0x8000_0000 != 0 {
// crc <<= 1;
// crc ^= 0x04c1_1db7;
// } else {
// crc <<= 1;
// }
//
// i += 1;
//}
unroll!(8, |_i| {
let if_condition = crc & 0x8000_0000; let if_condition = crc & 0x8000_0000;
let if_body = (crc << 1) ^ 0x04c1_1db7; let if_body = (crc << 1) ^ 0x04c1_1db7;
let else_body = crc << 1; let else_body = crc << 1;
@ -108,7 +52,8 @@ const fn crc_entry(input: u8) -> u32 {
let condition_table = [else_body, if_body]; let condition_table = [else_body, if_body];
crc = condition_table[(if_condition != 0) as usize]; crc = condition_table[(if_condition != 0) as usize];
}); i += 1;
}
crc crc
} }