diff --git a/src/nouveau/compiler/nak.rs b/src/nouveau/compiler/nak.rs index a20e9a17d79..19412e8cf89 100644 --- a/src/nouveau/compiler/nak.rs +++ b/src/nouveau/compiler/nak.rs @@ -8,6 +8,7 @@ mod nak_ir; mod nak_opt_copy_prop; mod nak_opt_dce; mod nir; +mod util; use nak_bindings::*; use nak_from_nir::*; diff --git a/src/nouveau/compiler/util.rs b/src/nouveau/compiler/util.rs new file mode 100644 index 00000000000..95e3583583c --- /dev/null +++ b/src/nouveau/compiler/util.rs @@ -0,0 +1,54 @@ +/* + * Copyright © 2022 Collabora, Ltd. + * SPDX-License-Identifier: MIT + */ + +use std::ops::Mul; + +/** Provides div_ceil() + * + * This is a nightly feature so we can't rely on it yet. + * https://github.com/rust-lang/rust/issues/88581 + */ +pub trait DivCeil { + type Output; + + fn div_ceil(&self, a: Rhs) -> Self::Output; +} + +macro_rules! impl_uint_div_ceil { + ($ty: ty) => { + impl DivCeil<$ty> for $ty { + type Output = $ty; + + fn div_ceil(&self, d: $ty) -> $ty { + (*self + (d - 1)) / d + } + } + }; +} + +impl_uint_div_ceil!(u8); +impl_uint_div_ceil!(u16); +impl_uint_div_ceil!(u32); +impl_uint_div_ceil!(u64); +impl_uint_div_ceil!(usize); + +/** Provides next_multiple_of() + * + * This is a nightly feature so we can't rely on it yet. + * https://github.com/rust-lang/rust/issues/88581 + */ +pub trait NextMultipleOf { + type Output; + + fn next_multiple_of(&self, a: Rhs) -> Self::Output; +} + +impl + Mul> NextMultipleOf for T { + type Output = T; + + fn next_multiple_of(&self, a: T) -> T { + self.div_ceil(a) * a + } +}