Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure max_rows & max_cols are power of 2 #100

Merged
merged 2 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion kate/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "kate"
version = "0.9.1"
version = "0.9.2"
authors = ["Denis Ermolin <[email protected]>"]
edition = "2021"
license = "Apache-2.0"
Expand Down
8 changes: 8 additions & 0 deletions kate/src/com.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ pub enum Error {
ExtendedGridDomainSizeInvalid(usize),
IndexOutOfRange,
ConversionFailed,
InvalidMaxRows,
InvalidMaxCols,
}

impl From<TryFromIntError> for Error {
Expand Down Expand Up @@ -227,6 +229,12 @@ pub fn get_block_dimensions<const CHUNK_SIZE: usize>(
let () = USizeSafeCastToU32::<CHUNK_SIZE>::OK;
let chunk_size_u32 = unsafe { NonZeroU32::new_unchecked(CHUNK_SIZE as u32) };

// SAFETY: `max_rows` and `max_cols` must be a power of 2.
// Otherwise block dimensions will be one row short, as the
// row count equals `total_cells / max_cols1`.
ensure!(max_rows.0.is_power_of_two(), Error::InvalidMaxRows);
ensure!(max_cols.0.is_power_of_two(), Error::InvalidMaxCols);

let max_block_dimensions =
BlockDimensions::new(max_rows, max_cols, chunk_size_u32).ok_or(Error::BlockTooBig)?;
let max_block_dimensions_size = max_block_dimensions.size();
Expand Down
Loading