Skip to content

Commit bbc4a75

Browse files
Get uniform behaviour across all pairwise_sum implementations
1 parent 9f1c4d2 commit bbc4a75

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/numeric_util.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,25 @@ where
6767
}
6868
partial_sums.push(partial_sum);
6969

70-
pairwise_sum(&partial_sums)
70+
pure_pairwise_sum(&partial_sums)
71+
}
72+
73+
/// An implementation of pairwise summation for a vector slice that never
74+
/// switches to the naive sum algorithm.
75+
fn pure_pairwise_sum<A>(v: &[A]) -> A
76+
where
77+
A: Clone + Add<Output=A> + Zero,
78+
{
79+
let n = v.len();
80+
match n {
81+
0 => A::zero(),
82+
1 => v[0].clone(),
83+
n => {
84+
let mid_index = n / 2;
85+
let (v1, v2) = v.split_at(mid_index);
86+
pure_pairwise_sum(v1) + pure_pairwise_sum(v2)
87+
}
88+
}
7189
}
7290

7391
/// Fold over the manually unrolled `xs` with `f`

0 commit comments

Comments
 (0)