use onemkl_sys::MKL_INT;
use crate::error::{Error, Result};
#[inline]
pub(crate) fn dim_to_mkl_int(value: usize) -> Result<MKL_INT> {
MKL_INT::try_from(value).map_err(|_| Error::DimensionOverflow)
}
#[inline]
pub(crate) fn stride_to_mkl_int(stride: i64) -> Result<MKL_INT> {
MKL_INT::try_from(stride).map_err(|_| Error::DimensionOverflow)
}
#[inline]
pub(crate) fn derive_n(slice_len: usize, inc: i64) -> Result<usize> {
if inc == 0 {
return Err(Error::InvalidArgument("vector stride must be non-zero"));
}
if slice_len == 0 {
return Ok(0);
}
let abs_inc = inc.unsigned_abs() as usize;
Ok((slice_len - 1) / abs_inc + 1)
}
#[inline]
pub(crate) fn check_two_vectors<T, U>(x: &[T], incx: i64, y: &[U], incy: i64) -> Result<usize> {
let nx = derive_n(x.len(), incx)?;
let ny = derive_n(y.len(), incy)?;
if nx != ny {
return Err(Error::InvalidArgument(
"x and y vectors must contain the same number of elements (length / |stride|)",
));
}
Ok(nx)
}
#[inline]
pub(crate) fn vector_min_len(n: usize, inc: i64) -> usize {
if n == 0 {
return 0;
}
1 + (n - 1) * inc.unsigned_abs() as usize
}