1 //! Miscellaneous helpers for machine backends.
2 
3 use crate::ir::Type;
4 use core::ops::{Add, BitAnd, Not, Sub};
5 
6 /// Returns the size (in bits) of a given type.
ty_bits(ty: Type) -> usize7 pub fn ty_bits(ty: Type) -> usize {
8     ty.bits() as usize
9 }
10 
11 /// Align a size up to a power-of-two alignment.
align_to<N>(x: N, alignment: N) -> N where N: Not<Output = N> + BitAnd<N, Output = N> + Add<N, Output = N> + Sub<N, Output = N> + From<u8> + Copy,12 pub(crate) fn align_to<N>(x: N, alignment: N) -> N
13 where
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 }
24