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

Optimize Span<T>.Fill implementation #51365

Merged
merged 5 commits into from
Apr 17, 2021
Merged
Changes from 1 commit
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
15 changes: 12 additions & 3 deletions src/libraries/System.Private.CoreLib/src/System/SpanHelpers.T.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ public static void Fill<T>(ref T refData, nuint numElements, T value)
// Early checks to see if it's even possible to vectorize - JIT will turn these checks into consts.
// - T cannot contain references (GC can't track references in vectors)
// - Vectorization must be hardware-accelerated
// - T's size must not exceed the vector's size and must be a whole power of 2
// - T's size must not exceed the vector's size
// - T's size must be a whole power of 2

if (RuntimeHelpers.IsReferenceOrContainsReferences<T>()) { goto CannotVectorize; }
if (!Vector.IsHardwareAccelerated) { goto CannotVectorize; }
if (Unsafe.SizeOf<T>() > Vector<byte>.Count) { goto CannotVectorize; }
if ((Unsafe.SizeOf<T>() & (Unsafe.SizeOf<T>() - 1)) != 0) { goto CannotVectorize; } // power of 2 check
if (!BitOperations.IsPow2(Unsafe.SizeOf<T>())) { goto CannotVectorize; }

if (numElements >= (uint)(Vector<byte>.Count / Unsafe.SizeOf<T>()))
{
Expand Down Expand Up @@ -71,7 +72,15 @@ public static void Fill<T>(ref T refData, nuint numElements, T value)
}
else if (Unsafe.SizeOf<T>() == 32)
GrabYourPitchforks marked this conversation as resolved.
Show resolved Hide resolved
{
vector = Unsafe.As<T, Vector256<byte>>(ref tmp).AsVector();
if (Vector<byte>.Count == 32)
{
vector = Unsafe.As<T, Vector256<byte>>(ref tmp).AsVector();
}
else
GrabYourPitchforks marked this conversation as resolved.
Show resolved Hide resolved
{
Debug.Fail("Vector<T> isn't 256 bits in size?");
tannergooding marked this conversation as resolved.
Show resolved Hide resolved
goto CannotVectorize;
}
}
else
{
Expand Down