nak: Add a util library

Currently, all we do here is implement div_ceil() and next_multiple_of()
because these helpers are super useful but currently an experimental
Rust feature.  This should be a drop-in for the feature so we can delete
it once the feature stabilizes with little to no effect.

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/24998>
This commit is contained in:
Faith Ekstrand 2023-01-30 20:53:16 -06:00 committed by Marge Bot
parent 4e95cb908f
commit 08eb906111
2 changed files with 55 additions and 0 deletions

View file

@ -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::*;

View file

@ -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<Rhs = Self> {
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<Rhs = Self> {
type Output;
fn next_multiple_of(&self, a: Rhs) -> Self::Output;
}
impl<T: Copy + DivCeil<Output = T> + Mul<Output = T>> NextMultipleOf<T> for T {
type Output = T;
fn next_multiple_of(&self, a: T) -> T {
self.div_ceil(a) * a
}
}