use crate::error::{Error, Result};
use crate::runtime::Runtime;
use crate::tensor::Tensor;
#[non_exhaustive]
pub struct PackedPlane<'a, R: Runtime> {
pub words: &'a Tensor<R>,
pub width: usize,
}
impl<R: Runtime> Clone for PackedPlane<'_, R> {
fn clone(&self) -> Self {
*self
}
}
impl<R: Runtime> Copy for PackedPlane<'_, R> {}
impl<R: Runtime> std::fmt::Debug for PackedPlane<'_, R> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("PackedPlane")
.field("shape", &self.words.shape())
.field("width", &self.width)
.finish()
}
}
impl<'a, R: Runtime> PackedPlane<'a, R> {
pub fn new(words: &'a Tensor<R>, width: usize) -> Self {
Self { words, width }
}
pub fn row_words(row_bytes: usize) -> usize {
row_bytes.div_ceil(4)
}
pub fn words_shape(rows: usize, pitch_bytes: usize) -> Result<[usize; 2]> {
if !pitch_bytes.is_multiple_of(4) {
return Err(Error::InvalidArgument {
arg: "pitch_bytes",
reason: format!(
"row pitch {pitch_bytes} bytes is not a whole number of 32-bit words; \
align the pitch to 4 bytes, for example {}",
pitch_bytes.div_ceil(4) * 4
),
});
}
Ok([rows, pitch_bytes / 4])
}
}