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

Use Unsafe.BitCast in SpanHelpers.Fill #111091

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
16 changes: 8 additions & 8 deletions src/libraries/System.Private.CoreLib/src/System/Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1513,31 +1513,31 @@ public static unsafe int IndexOf<T>(T[] array, T value, int startIndex, int coun
{
int result = SpanHelpers.IndexOfValueType(
ref Unsafe.Add(ref Unsafe.As<T, byte>(ref MemoryMarshal.GetArrayDataReference(array)), startIndex),
Unsafe.As<T, byte>(ref value),
Unsafe.BitCast<T, byte>(value),
count);
return (result >= 0 ? startIndex : 0) + result;
}
else if (sizeof(T) == sizeof(short))
{
int result = SpanHelpers.IndexOfValueType(
ref Unsafe.Add(ref Unsafe.As<T, short>(ref MemoryMarshal.GetArrayDataReference(array)), startIndex),
Unsafe.As<T, short>(ref value),
Unsafe.BitCast<T, short>(value),
count);
return (result >= 0 ? startIndex : 0) + result;
}
else if (sizeof(T) == sizeof(int))
{
int result = SpanHelpers.IndexOfValueType(
ref Unsafe.Add(ref Unsafe.As<T, int>(ref MemoryMarshal.GetArrayDataReference(array)), startIndex),
Unsafe.As<T, int>(ref value),
Unsafe.BitCast<T, int>(value),
count);
return (result >= 0 ? startIndex : 0) + result;
}
else if (sizeof(T) == sizeof(long))
{
int result = SpanHelpers.IndexOfValueType(
ref Unsafe.Add(ref Unsafe.As<T, long>(ref MemoryMarshal.GetArrayDataReference(array)), startIndex),
Unsafe.As<T, long>(ref value),
Unsafe.BitCast<T, long>(value),
count);
return (result >= 0 ? startIndex : 0) + result;
}
Expand Down Expand Up @@ -1758,7 +1758,7 @@ public static unsafe int LastIndexOf<T>(T[] array, T value, int startIndex, int
int endIndex = startIndex - count + 1;
int result = SpanHelpers.LastIndexOfValueType(
ref Unsafe.Add(ref Unsafe.As<T, byte>(ref MemoryMarshal.GetArrayDataReference(array)), endIndex),
Unsafe.As<T, byte>(ref value),
Unsafe.BitCast<T, byte>(value),
count);

return (result >= 0 ? endIndex : 0) + result;
Expand All @@ -1768,7 +1768,7 @@ ref Unsafe.Add(ref Unsafe.As<T, byte>(ref MemoryMarshal.GetArrayDataReference(ar
int endIndex = startIndex - count + 1;
int result = SpanHelpers.LastIndexOfValueType(
ref Unsafe.Add(ref Unsafe.As<T, short>(ref MemoryMarshal.GetArrayDataReference(array)), endIndex),
Unsafe.As<T, short>(ref value),
Unsafe.BitCast<T, short>(value),
count);

return (result >= 0 ? endIndex : 0) + result;
Expand All @@ -1778,7 +1778,7 @@ ref Unsafe.Add(ref Unsafe.As<T, short>(ref MemoryMarshal.GetArrayDataReference(a
int endIndex = startIndex - count + 1;
int result = SpanHelpers.LastIndexOfValueType(
ref Unsafe.Add(ref Unsafe.As<T, int>(ref MemoryMarshal.GetArrayDataReference(array)), endIndex),
Unsafe.As<T, int>(ref value),
Unsafe.BitCast<T, int>(value),
count);

return (result >= 0 ? endIndex : 0) + result;
Expand All @@ -1788,7 +1788,7 @@ ref Unsafe.Add(ref Unsafe.As<T, int>(ref MemoryMarshal.GetArrayDataReference(arr
int endIndex = startIndex - count + 1;
int result = SpanHelpers.LastIndexOfValueType(
ref Unsafe.Add(ref Unsafe.As<T, long>(ref MemoryMarshal.GetArrayDataReference(array)), endIndex),
Unsafe.As<T, long>(ref value),
Unsafe.BitCast<T, long>(value),
count);

return (result >= 0 ? endIndex : 0) + result;
Expand Down
17 changes: 8 additions & 9 deletions src/libraries/System.Private.CoreLib/src/System/SpanHelpers.T.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,33 @@ 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));
tannergooding marked this conversation as resolved.
Show resolved Hide resolved
}
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)));
tannergooding marked this conversation as resolved.
Show resolved Hide resolved
}
else if (sizeof(T) == 16)
{
Vector128<byte> vec128 = Unsafe.As<T, Vector128<byte>>(ref tmp);
Vector128<byte> vec128 = Unsafe.BitCast<T, Vector128<byte>>(value);
if (Vector<byte>.Count == 16)
{
vector = vec128.AsVector();
Expand All @@ -75,7 +74,7 @@ public static unsafe void Fill<T>(ref T refData, nuint numElements, T value)
{
if (Vector<byte>.Count == 32)
{
vector = Unsafe.As<T, Vector256<byte>>(ref tmp).AsVector();
vector = Unsafe.BitCast<T, Vector256<byte>>(value).AsVector();
}
else
{
Expand Down