Skip to content

Replace Unsafe.As with Unsafe.BitCast in SpanHelpers.Fill #116221

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

Merged
merged 3 commits into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/libraries/System.Memory/tests/Span/Fill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ public static void FillWithRecognizedType()
0x1111111111111111, 0x2222222222222222, 0x3333333333333333, 0x4444444444444444,
0x5555555555555555, 0x6666666666666666, 0x7777777777777777, 0x8888888888888888)); // 512-bit struct, no SIMD
RunTest<MyRefContainingStruct>(new("Hello world!")); // struct contains refs, no SIMD
RunTest<int?>(42); // nullable type

static void RunTest<T>(T value)
{
Expand Down
45 changes: 13 additions & 32 deletions src/libraries/System.Private.CoreLib/src/System/SpanHelpers.T.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,74 +29,55 @@ public static unsafe void Fill<T>(ref T refData, nuint numElements, T value)
{
// We have enough data for at least one vectorized write.

T tmp = value; // Avoid taking address of the "value" argument. It would regress performance of the loops below.
Vector<byte> vector;

if (sizeof(T) == 1)
{
vector = new Vector<byte>(Unsafe.As<T, byte>(ref tmp));
vector = new Vector<byte>(Unsafe.BitCast<T, byte>(value));
}
else if (sizeof(T) == 2)
{
vector = (Vector<byte>)(new Vector<ushort>(Unsafe.As<T, ushort>(ref tmp)));
vector = (Vector<byte>)new Vector<ushort>(Unsafe.BitCast<T, ushort>(value));
}
else if (sizeof(T) == 4)
{
// special-case float since it's already passed in a SIMD reg
vector = (typeof(T) == typeof(float))
? (Vector<byte>)(new Vector<float>((float)(object)tmp!))
: (Vector<byte>)(new Vector<uint>(Unsafe.As<T, uint>(ref tmp)));
? (Vector<byte>)new Vector<float>(Unsafe.BitCast<T, float>(value))
: (Vector<byte>)new Vector<uint>(Unsafe.BitCast<T, uint>(value));
}
else if (sizeof(T) == 8)
{
// special-case double since it's already passed in a SIMD reg
vector = (typeof(T) == typeof(double))
? (Vector<byte>)(new Vector<double>((double)(object)tmp!))
: (Vector<byte>)(new Vector<ulong>(Unsafe.As<T, ulong>(ref tmp)));
? (Vector<byte>)new Vector<double>(Unsafe.BitCast<T, double>(value))
: (Vector<byte>)new Vector<ulong>(Unsafe.BitCast<T, ulong>(value));
}
else if (sizeof(T) == 16)
else if (sizeof(T) == Vector<byte>.Count)
{
Vector128<byte> vec128 = Unsafe.As<T, Vector128<byte>>(ref tmp);
if (Vector<byte>.Count == 16)
{
vector = vec128.AsVector();
}
else if (Vector<byte>.Count == 32)
{
vector = Vector256.Create(vec128).AsVector();
}
else if (Vector<byte>.Count == 64)
{
vector = Vector512.Create(vec128).AsVector();
}
else
{
Debug.Fail("Vector<T> is unexpected size.");
goto CannotVectorize;
}
vector = Unsafe.BitCast<T, Vector<byte>>(value);
}
else if (sizeof(T) == 32)
else if (sizeof(T) == 16)
{
Vector256<byte> vec256 = Unsafe.As<T, Vector256<byte>>(ref tmp);
if (Vector<byte>.Count == 32)
{
vector = vec256.AsVector();
vector = Vector256.Create(Unsafe.BitCast<T, Vector128<byte>>(value)).AsVector();
}
else if (Vector<byte>.Count == 64)
{
vector = Vector512.Create(vec256).AsVector();
vector = Vector512.Create(Unsafe.BitCast<T, Vector128<byte>>(value)).AsVector();
}
else
{
Debug.Fail("Vector<T> is unexpected size.");
goto CannotVectorize;
}
}
else if (sizeof(T) == 64)
else if (sizeof(T) == 32)
{
if (Vector<byte>.Count == 64)
{
vector = Unsafe.As<T, Vector512<byte>>(ref tmp).AsVector();
vector = Vector512.Create(Unsafe.BitCast<T, Vector256<byte>>(value)).AsVector();
}
else
{
Expand Down
Loading