cel_runtime/memory.rs
1/// Aligns an index to the specified alignment boundary.
2/// Returns the next aligned position that satisfies the alignment requirement.
3///
4/// # Panics
5///
6/// Panics (in debug configuration) if the alignment is not a power of two.
7///
8#[must_use]
9pub const fn align_index(align: usize, index: usize) -> usize {
10 debug_assert!(align.is_power_of_two());
11 (index + align - 1) & !(align - 1)
12}
13
14#[cfg(test)]
15mod tests {
16 use super::*;
17
18 #[test]
19 fn test_align_index() {
20 assert_eq!(align_index(16, 0), 0);
21 assert_eq!(align_index(16, 1), 16);
22 assert_eq!(align_index(16, 15), 16);
23 assert_eq!(align_index(16, 16), 16);
24 assert_eq!(align_index(16, 17), 32);
25 }
26}