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

Update loops in CpuMath to be more efficient #1177

Merged
merged 4 commits into from
Oct 31, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 5 additions & 3 deletions src/Microsoft.ML.CpuMath/AvxIntrinsics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ internal static class AvxIntrinsics

private const int Vector256Alignment = 32;

private const int destinationEnd = pDstEnd - 4;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is pDstEnd defined, I wouldn't expect this could be a constant....


[MethodImplAttribute(MethodImplOptions.AggressiveInlining)]
private static bool HasCompatibleAlignment(AlignedArray alignedArray)
{
Expand Down Expand Up @@ -431,7 +433,7 @@ public static unsafe void AddScalarU(float scalar, Span<float> dst)

Vector128<float> scalarVector128 = Sse.SetAllVector128(scalar);

if (pDstCurrent + 4 <= pDstEnd)
if (pDstCurrent <= destinationEnd)
{
Vector128<float> dstVector = Sse.LoadVector128(pDstCurrent);
dstVector = Sse.Add(dstVector, scalarVector128);
Expand Down Expand Up @@ -517,7 +519,7 @@ public static unsafe void ScaleSrcU(float scale, Span<float> src, Span<float> ds

Vector128<float> scaleVector128 = Sse.SetAllVector128(scale);

if (pDstCurrent + 4 <= pDstEnd)
if (pDstCurrent <= destinationEnd)
{
Vector128<float> srcVector = Sse.LoadVector128(pSrcCurrent);
srcVector = Sse.Multiply(srcVector, scaleVector128);
Expand Down Expand Up @@ -563,7 +565,7 @@ public static unsafe void ScaleAddU(float a, float b, Span<float> dst)
Vector128<float> a128 = Sse.SetAllVector128(a);
Vector128<float> b128 = Sse.SetAllVector128(b);

if (pDstCurrent + 4 <= pDstEnd)
if (pDstCurrent <= destinationEnd)
{
Vector128<float> dstVector = Sse.LoadVector128(pDstCurrent);
dstVector = Sse.Add(dstVector, b128);
Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.ML.CpuMath/SseIntrinsics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ public static unsafe void AddScalarU(float scalar, Span<float> dst)

Vector128<float> scalarVector = Sse.SetAllVector128(scalar);

while (pDstCurrent + 4 <= pDstEnd)
while (pDstCurrent <= pDstEnd - 4)
Copy link
Member

@tannergooding tannergooding Oct 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you update the SSE case as well?

{
Vector128<float> dstVector = Sse.LoadVector128(pDstCurrent);
dstVector = Sse.Add(dstVector, scalarVector);
Expand Down Expand Up @@ -479,7 +479,7 @@ public static unsafe void ScaleSrcU(float scale, Span<float> src, Span<float> ds

Vector128<float> scaleVector = Sse.SetAllVector128(scale);

while (pDstCurrent + 4 <= pDstEnd)
while (pDstCurrent <= pDstEnd - 4)
{
Vector128<float> srcVector = Sse.LoadVector128(pSrcCurrent);
srcVector = Sse.Multiply(srcVector, scaleVector);
Expand Down Expand Up @@ -512,7 +512,7 @@ public static unsafe void ScaleAddU(float a, float b, Span<float> dst)
Vector128<float> aVector = Sse.SetAllVector128(a);
Vector128<float> bVector = Sse.SetAllVector128(b);

while (pDstCurrent + 4 <= pDstEnd)
while (pDstCurrent <= pDstEnd - 4)
{
Vector128<float> dstVector = Sse.LoadVector128(pDstCurrent);
dstVector = Sse.Add(dstVector, bVector);
Expand Down