use crate::dtype::DType;
use crate::error::Result;
use crate::runtime::Runtime;
use crate::tensor::Tensor;
use std::borrow::Cow;
#[derive(Debug)]
pub struct LaneOperand<'a, R: Runtime> {
pub tensor: Cow<'a, Tensor<R>>,
pub step: u32,
}
impl<R: Runtime<DType = DType>> LaneOperand<'_, R> {
pub fn addr(&self) -> u64 {
self.tensor.ptr()
}
}
pub fn lane_operand<'a, R, F>(
value: &'a Tensor<R>,
shape: &[usize],
materialize: F,
) -> Result<LaneOperand<'a, R>>
where
R: Runtime<DType = DType>,
F: FnOnce(&Tensor<R>) -> Result<Tensor<R>>,
{
if value.numel() == 1 {
return Ok(LaneOperand {
tensor: Cow::Borrowed(value),
step: 0,
});
}
if value.shape() == shape && value.is_contiguous() {
return Ok(LaneOperand {
tensor: Cow::Borrowed(value),
step: 1,
});
}
let view = value.broadcast_to(shape)?;
Ok(LaneOperand {
tensor: Cow::Owned(materialize(&view)?),
step: 1,
})
}
pub fn lane_count(shape: &[usize]) -> usize {
shape.iter().product()
}