peridot_math/bitops.rs
1//! Bit operation utils
2
3use std::ops::{Add, BitAnd, Not, Sub};
4
5use crate::One;
6
7/// computes round up of `x` to the nearest multiple of `a`, specialized for `a` is a power of 2
8#[inline(always)]
9pub fn round_up_pow2n<T>(x: T, a: T) -> T
10where
11 T: BitAnd<T, Output = T>
12 + Copy
13 + Not<Output = T>
14 + One
15 + Sub<T, Output = T>
16 + Add<T, Output = T>,
17{
18 let a1 = a - T::ONE;
19 (x + a1) & !a1
20}
21
22/// computes round up of `x` to the nearest multiple of `a`, specialized for `a` is a power of 2 (u64)
23#[inline(always)]
24pub const fn round_up_pow2n_u64(x: u64, a: u64) -> u64 {
25 (x + (a - 1)) & !(a - 1)
26}