fn body_offset(n: usize, stride: isize) -> usize {
n + if n == 0 || stride > 0 {
0
} else {
(n - 1) * stride.unsigned_abs()
}
}
fn strided_buffer(n: usize, stride: isize, j: usize) -> Col<Scalar> {
let start = body_offset(n, stride) as isize;
let body = if n == 0 { 1 } else { (n - 1) * stride.unsigned_abs() + 1 };
Col::<Scalar>::from_fn(2 * n + body, |i| {
let off = i as isize - start;
let k = off / stride;
if n > 0 && off % stride == 0 && k >= 0 && k < n as isize {
integer(k as usize, j)
} else {
nan()
}
})
}
fn strided_row(x: &Col<Scalar>, n: usize, stride: isize) -> RowRef<'_, Scalar> {
unsafe {
RowRef::from_raw_parts(x.as_ptr().add(body_offset(n, stride)), n, stride)
}
}
fn strided_col(x: &Col<Scalar>, n: usize, stride: isize) -> ColRef<'_, Scalar> {
unsafe {
ColRef::from_raw_parts(x.as_ptr().add(body_offset(n, stride)), n, stride)
}
}
#[test]
fn dot_matches_the_source_stride_sweep() {
for n in [0, 1, 2, 4, 7] {
for (incx, incy) in [(1, 1), (2, -2), (-2, 1), (-1, -2)] {
for (name, conj_lhs, conj_rhs) in [
("dot", Conj::No, Conj::No),
("dotc", Conj::Yes, Conj::No),
("dot conj rhs", Conj::No, Conj::Yes),
("dot conj both", Conj::Yes, Conj::Yes),
] {
if Scalar::IS_REAL
&& (conj_lhs == Conj::Yes || conj_rhs == Conj::Yes)
{
continue;
}
let x = strided_buffer(n, incx, 1);
let y = strided_buffer(n, incy, 2);
let got = inner_prod(
strided_row(&x, n, incx),
conj_lhs,
strided_col(&y, n, incy),
conj_rhs,
);
let want = (0..n).fold(zero(), |acc, k| {
acc + conjugated(integer(k, 1), conj_lhs)
* conjugated(integer(k, 2), conj_rhs)
});
crate::assert!(
same_number(got, want),
"{name} n={n} incx={incx} incy={incy} \
got={:?} want={:?}",
real(&got),
real(&want)
);
}
}
}
}
#[test]
fn dot_reads_every_referenced_entry() {
let n = 4;
for stride in [1isize, 2, -2] {
for at in 0..n {
for (conj_lhs, conj_rhs) in [
(Conj::No, Conj::No),
(Conj::Yes, Conj::No),
(Conj::No, Conj::Yes),
(Conj::Yes, Conj::Yes),
] {
if Scalar::IS_REAL
&& (conj_lhs == Conj::Yes || conj_rhs == Conj::Yes)
{
continue;
}
for poison_lhs in [true, false] {
let mut x = strided_buffer(n, stride, 1);
let mut y = strided_buffer(n, 1, 2);
let dot = |x: &Col<Scalar>, y: &Col<Scalar>| {
inner_prod(
strided_row(x, n, stride),
conj_lhs,
strided_col(y, n, 1),
conj_rhs,
)
};
let finite = |v: &Scalar| {
real(v).is_finite() && imag(v).is_finite()
};
let label = format!(
"stride={stride} at={at} poison_lhs={poison_lhs} \
conj_lhs={conj_lhs:?} conj_rhs={conj_rhs:?}"
);
crate::assert!(
finite(&dot(&x, &y)),
"{label}: the sentinel is not referred to"
);
let buf_stride = if poison_lhs { stride } else { 1 };
let referred = (body_offset(n, buf_stride) as isize
+ at as isize * buf_stride)
as usize;
let poison = |x: &mut Col<Scalar>,
y: &mut Col<Scalar>,
v: Scalar| {
if poison_lhs {
*x.as_mut().get_mut(referred) = v;
} else {
*y.as_mut().get_mut(referred) = v;
}
};
poison(&mut x, &mut y, nan());
crate::assert!(
real(&dot(&x, &y)).is_nan(),
"{label}: a NaN in a referred entry did not reach the \
sum"
);
poison(&mut x, &mut y, from_f64(f64::INFINITY));
crate::assert!(
!finite(&dot(&x, &y)),
"{label}: an infinity in a referred entry did not \
reach the sum"
);
}
}
}
}
}