Skip to content

Commit

Permalink
Switch to Rust 1.73.0's builtin div_ceil function
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Gunnerson <[email protected]>
  • Loading branch information
chenxiaolong committed May 24, 2024
1 parent f479fe1 commit d47c14a
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 18 deletions.
6 changes: 3 additions & 3 deletions avbroot/src/format/fec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ impl Fec {
return Err(Error::UnsupportedParity(parity));
}

let blocks = util::div_ceil(file_size, u64::from(block_size));
let rounds = util::div_ceil(blocks, u64::from(rs_k));
let blocks = file_size.div_ceil(u64::from(block_size));
let rounds = blocks.div_ceil(u64::from(rs_k));

// Check upfront so we don't need to do checked multiplication later.
rounds
Expand Down Expand Up @@ -225,7 +225,7 @@ impl Fec {
let end_block = if range.end % block_size == 0 {
range.end / block_size
} else {
util::div_ceil(range.end, block_size)
range.end.div_ceil(block_size)
};

for block in start_block..end_block {
Expand Down
4 changes: 2 additions & 2 deletions avbroot/src/format/hashtree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl HashTree {
let mut level_size = image_size;

while level_size > u64::from(self.block_size) {
let blocks = util::div_ceil(level_size, u64::from(self.block_size));
let blocks = level_size.div_ceil(u64::from(self.block_size));
level_size = blocks
.checked_mul(digest_size as u64)
.and_then(|s| padding::round(s, u64::from(self.block_size)))
Expand Down Expand Up @@ -124,7 +124,7 @@ impl HashTree {
let end_block = if range.end % block_size == 0 {
range.end / block_size
} else {
util::div_ceil(range.end, block_size)
range.end.div_ceil(block_size)
};

result.push(start_block..end_block);
Expand Down
4 changes: 2 additions & 2 deletions avbroot/src/format/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ pub fn compress_image(
});
}

let chunks_total = util::div_ceil(file_size, CHUNK_SIZE);
let chunks_total = file_size.div_ceil(CHUNK_SIZE);
let mut bytes_compressed = 0;
let mut context_uncompressed = Context::new(&ring::digest::SHA256);
let mut operations = vec![];
Expand Down Expand Up @@ -1059,7 +1059,7 @@ pub fn compress_modified_image(
return Err(Error::ExtentsNotInOrder);
}

let groups_total = util::div_ceil(operations.len(), OPERATION_GROUP);
let groups_total = operations.len().div_ceil(OPERATION_GROUP);
let mut bytes_compressed = 0;
let mut context_uncompressed = Context::new(&ring::digest::SHA256);
let mut modified_operations = vec![];
Expand Down
2 changes: 1 addition & 1 deletion avbroot/src/patch/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub fn patch_system_image(
return Err(Error::NoHashTreeDescriptor);
};

let num_chunks = util::div_ceil(footer.original_image_size, CHUNK_SIZE);
let num_chunks = footer.original_image_size.div_ceil(CHUNK_SIZE);
trace!("Parallel heuristics search for otacerts.zip with {num_chunks} chunks");

let modified_ranges = (0..num_chunks)
Expand Down
10 changes: 0 additions & 10 deletions avbroot/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,6 @@ pub fn parent_path(path: &Path) -> &Path {
Path::new(".")
}

/// Since Rust's built-in .div_ceil() is still nightly-only.
pub fn div_ceil<T: PrimInt>(dividend: T, divisor: T) -> T {
dividend / divisor
+ if dividend % divisor != T::zero() {
T::one()
} else {
T::zero()
}
}

/// Sort and merge overlapping intervals.
pub fn merge_overlapping<T>(sections: &[Range<T>]) -> Vec<Range<T>>
where
Expand Down

0 comments on commit d47c14a

Please sign in to comment.