use eunomia::NumericElement;
use super::fallback::{
scalar_axpy_rows_batch_fallback, scalar_axpy_rows_fallback, scalar_gemv_strided_fallback,
scalar_gemv_transpose_strided_fallback, scalar_tiled_gemm_fallback,
};
pub trait Scalar: NumericElement {
#[inline]
fn add_slice(a: &[Self], b: &[Self], out: &mut [Self]) {
assert_eq!(a.len(), b.len(), "add_slice: a.len() != b.len()");
assert_eq!(a.len(), out.len(), "add_slice: output length mismatch");
for ((o, &x), &y) in out.iter_mut().zip(a.iter()).zip(b.iter()) {
*o = x + y;
}
}
#[inline]
fn sub_slice(a: &[Self], b: &[Self], out: &mut [Self]) {
assert_eq!(a.len(), b.len(), "sub_slice: a.len() != b.len()");
assert_eq!(a.len(), out.len(), "sub_slice: output length mismatch");
for ((o, &x), &y) in out.iter_mut().zip(a.iter()).zip(b.iter()) {
*o = x - y;
}
}
#[inline]
fn mul_slice(a: &[Self], b: &[Self], out: &mut [Self]) {
assert_eq!(a.len(), b.len(), "mul_slice: a.len() != b.len()");
assert_eq!(a.len(), out.len(), "mul_slice: output length mismatch");
for ((o, &x), &y) in out.iter_mut().zip(a.iter()).zip(b.iter()) {
*o = x * y;
}
}
#[inline]
fn div_slice(a: &[Self], b: &[Self], out: &mut [Self]) {
assert_eq!(a.len(), b.len(), "div_slice: a.len() != b.len()");
assert_eq!(a.len(), out.len(), "div_slice: output length mismatch");
for ((o, &x), &y) in out.iter_mut().zip(a.iter()).zip(b.iter()) {
*o = x / y;
}
}
#[inline]
fn scale_slice(data: &mut [Self], scalar: Self) {
for x in data.iter_mut() {
*x *= scalar;
}
}
#[inline]
fn sum_slice(s: &[Self]) -> Self {
s.iter()
.copied()
.fold(<Self as NumericElement>::ZERO, |acc, x| acc + x)
}
#[inline]
fn dot_slice(a: &[Self], b: &[Self]) -> Self {
assert_eq!(a.len(), b.len(), "dot_slice: a.len() != b.len()");
a.iter()
.copied()
.zip(b.iter().copied())
.fold(<Self as NumericElement>::ZERO, |acc, (x, y)| acc + x * y)
}
#[inline]
fn axpy_slice(alpha: Self, x: &[Self], out: &mut [Self]) {
assert_eq!(x.len(), out.len(), "axpy_slice: x.len() != out.len()");
for (o, &xv) in out.iter_mut().zip(x.iter()) {
*o += alpha * xv;
}
}
#[inline]
fn axpy_rows(
alphas: &[Self],
x: &[Self],
out: &mut [Self],
row_stride: usize,
rows: usize,
cols: usize,
) {
scalar_axpy_rows_fallback(alphas, x, out, row_stride, rows, cols);
}
#[inline]
fn axpy_rows_batch(
alphas: &[Self],
x_panel: &[Self],
out: &mut [Self],
row_stride: usize,
rows: usize,
depth: usize,
cols: usize,
) {
scalar_axpy_rows_batch_fallback(alphas, x_panel, out, row_stride, rows, depth, cols);
}
#[inline]
fn gemv_strided(
a: &[Self],
x: &[Self],
y: &mut [Self],
nrows: usize,
ncols: usize,
lda: usize,
) {
scalar_gemv_strided_fallback(a, x, y, nrows, ncols, lda);
}
#[inline]
fn gemv_transpose_strided(
a: &[Self],
x: &[Self],
y: &mut [Self],
nrows: usize,
ncols: usize,
lda: usize,
) {
scalar_gemv_transpose_strided_fallback(a, x, y, nrows, ncols, lda);
}
#[inline]
fn tiled_gemm(a: &[Self], b: &[Self], c: &mut [Self], m: usize, n: usize, k: usize) {
scalar_tiled_gemm_fallback(a, b, c, m, n, k);
}
#[inline]
fn min_slice(s: &[Self]) -> Self {
s.iter()
.copied()
.fold(<Self as NumericElement>::MAX_VALUE, |acc, x| {
if x < acc {
x
} else {
acc
}
})
}
#[inline]
fn max_slice(s: &[Self]) -> Self {
s.iter()
.copied()
.fold(<Self as NumericElement>::MIN_VALUE, |acc, x| {
if x > acc {
x
} else {
acc
}
})
}
#[inline]
fn jaccard_distance(_a: &[Self], _b: &[Self]) -> Option<f64> {
None
}
#[inline]
fn hamming_distance(_a: &[Self], _b: &[Self]) -> Option<u64> {
None
}
}