-
Notifications
You must be signed in to change notification settings - Fork 953
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move align_to functions to wgpu_types crate
- Loading branch information
Showing
12 changed files
with
74 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//! Utilitary math functions. | ||
/// | ||
/// Aligns a `value` to an `alignment`. | ||
/// | ||
/// Returns the first number greater than or equal to `value` that is also a | ||
/// multiple of `alignment`. If `value` is already a multiple of `alignment`, | ||
/// `value` will be returned. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// # use wgpu_types::math::align_to; | ||
/// assert_eq!(align_to(253, 16), 256); | ||
/// assert_eq!(align_to(256, 16), 256); | ||
/// assert_eq!(align_to(0, 16), 0); | ||
/// ``` | ||
/// | ||
pub fn align_to(value: u32, alignment: u32) -> u32 { | ||
if alignment.is_power_of_two() { | ||
let mask = alignment - 1; | ||
(value + mask) & !mask | ||
} else { | ||
match value % alignment { | ||
0 => value, | ||
other => value - other + alignment, | ||
} | ||
} | ||
} | ||
|
||
/// | ||
/// Aligns a `value` to an `alignment`. | ||
/// | ||
/// Returns the first number greater than or equal to `value` that is also a | ||
/// multiple of `alignment`. If `value` is already a multiple of `alignment`, | ||
/// `value` will be returned. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// # use wgpu_types::math::align_to_u64; | ||
/// assert_eq!(align_to_u64(253, 16), 256); | ||
/// assert_eq!(align_to_u64(256, 16), 256); | ||
/// assert_eq!(align_to_u64(0, 16), 0); | ||
/// ``` | ||
/// | ||
pub fn align_to_u64(value: u64, alignment: u64) -> u64 { | ||
if alignment.is_power_of_two() { | ||
let mask = alignment - 1; | ||
(value + mask) & !mask | ||
} else { | ||
match value % alignment { | ||
0 => value, | ||
other => value - other + alignment, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters