cranelift_codegen/machinst/
helpers.rs

1//! Miscellaneous helpers for machine backends.
2
3use crate::ir::Type;
4use std::ops::{Add, BitAnd, Not, Sub};
5
6/// Returns the size (in bits) of a given type.
7pub fn ty_bits(ty: Type) -> usize {
8    ty.bits() as usize
9}
10
11/// Align a size up to a power-of-two alignment.
12pub(crate) fn align_to<N>(x: N, alignment: N) -> N
13where
14    N: Not<Output = N>
15        + BitAnd<N, Output = N>
16        + Add<N, Output = N>
17        + Sub<N, Output = N>
18        + From<u8>
19        + Copy,
20{
21    let alignment_mask = alignment - 1.into();
22    (x + alignment_mask) & !alignment_mask
23}