Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Issue 6657 - dotProduct overload for small fixed size arrays #6990

Merged
merged 1 commit into from
May 3, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions std/numeric.d
Original file line number Diff line number Diff line change
Expand Up @@ -1774,6 +1774,24 @@ dotProduct(F1, F2)(in F1[] avector, in F2[] bvector)
return sum0;
}

/// ditto
F dotProduct(F, uint N)(const ref scope F[N] a, const ref scope F[N] b)
if (N <= 16)
{
F sum0 = 0;
F sum1 = 0;
static foreach (i; 0 .. N / 2)
{
sum0 += a[i*2] * b[i*2];
sum1 += a[i*2+1] * b[i*2+1];
}
static if (N % 2 == 1)
{
sum0 += a[N-1] * b[N-1];
}
return sum0 + sum1;
}

@system unittest
{
// @system due to dotProduct and assertCTFEable
Expand All @@ -1785,6 +1803,13 @@ dotProduct(F1, F2)(in F1[] avector, in F2[] bvector)
T[] b = [ 4.0, 6.0, ];
assert(dotProduct(a, b) == 16);
assert(dotProduct([1, 3, -5], [4, -2, -1]) == 3);
// Test with fixed-length arrays.
T[2] c = [ 1.0, 2.0, ];
T[2] d = [ 4.0, 6.0, ];
assert(dotProduct(c, d) == 16);
T[3] e = [1, 3, -5];
T[3] f = [4, -2, -1];
assert(dotProduct(e, f) == 3);
}}

// Make sure the unrolled loop codepath gets tested.
Expand Down