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 MemoryMarshal.Cast in a few places #99835

Merged
merged 4 commits into from
Mar 19, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

#pragma warning disable CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type

namespace System.Numerics.Tensors
{
/// <summary>Performs primitive tensor operations over spans of memory.</summary>
Expand All @@ -23,7 +25,21 @@
}

/// <summary>Throws an <see cref="OverflowException"/> for trying to negate the minimum value of a two-complement value.</summary>
internal static void ThrowNegateTwosCompOverflow() => throw new OverflowException(SR.Overflow_NegateTwosCompNum);
private static void ThrowNegateTwosCompOverflow() => throw new OverflowException(SR.Overflow_NegateTwosCompNum);

/// <summary>Creates a span of <typeparamref name="TTo"/> from a <typeparamref name="TTo"/> when they're the same type.</summary>
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
private static unsafe Span<TTo> Rename<TFrom, TTo>(Span<TFrom> span) // MemoryMarshal.Cast, but without constraints or validation
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
{
Debug.Assert(sizeof(TFrom) == sizeof(TTo));
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
return MemoryMarshal.CreateSpan(ref Unsafe.As<TFrom, TTo>(ref MemoryMarshal.GetReference(span)), span.Length);

Check failure on line 34 in src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.Helpers.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop (Build linux-x64 debug Libraries_AllConfigurations)

src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.Helpers.cs#L34

src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.Helpers.cs(34,34): error CS0117: (NETCORE_ENGINEERING_TELEMETRY=Build) 'MemoryMarshal' does not contain a definition for 'CreateSpan'

Check failure on line 34 in src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.Helpers.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop (Build linux-x64 debug Libraries_AllConfigurations)

src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.Helpers.cs#L34

src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.Helpers.cs(34,34): error CS0117: (NETCORE_ENGINEERING_TELEMETRY=Build) 'MemoryMarshal' does not contain a definition for 'CreateSpan'
jkotas marked this conversation as resolved.
Show resolved Hide resolved
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>Creates a span of <typeparamref name="TTo"/> from a <typeparamref name="TTo"/> when they're the same type.</summary>
private static unsafe ReadOnlySpan<TTo> Rename<TFrom, TTo>(ReadOnlySpan<TFrom> span) // MemoryMarshal.Cast, but without constraints or validation
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
{
Debug.Assert(sizeof(TFrom) == sizeof(TTo));
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<TFrom, TTo>(ref MemoryMarshal.GetReference(span)), span.Length);

Check failure on line 41 in src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.Helpers.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop (Build linux-x64 debug Libraries_AllConfigurations)

src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.Helpers.cs#L41

src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.Helpers.cs(41,34): error CS0117: (NETCORE_ENGINEERING_TELEMETRY=Build) 'MemoryMarshal' does not contain a definition for 'CreateReadOnlySpan'

Check failure on line 41 in src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.Helpers.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop (Build linux-x64 debug Libraries_AllConfigurations)

src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.Helpers.cs#L41

src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.Helpers.cs(41,34): error CS0117: (NETCORE_ENGINEERING_TELEMETRY=Build) 'MemoryMarshal' does not contain a definition for 'CreateReadOnlySpan'
}

/// <summary>Mask used to handle alignment elements before vectorized handling of the input.</summary>
/// <remarks>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1247,12 +1247,5 @@ static void VectorizedSmall8(ref TInput xRef, ref TOutput dRef, nuint remainder)
}
}
}

/// <summary>Creates a span of <typeparamref name="TTo"/> from a <typeparamref name="TTo"/> when they're the same type.</summary>
private static unsafe Span<TTo> Rename<TFrom, TTo>(Span<TFrom> span)
{
Debug.Assert(sizeof(TFrom) == sizeof(TTo));
return MemoryMarshal.CreateSpan(ref Unsafe.As<TFrom, TTo>(ref MemoryMarshal.GetReference(span)), span.Length);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -643,13 +643,6 @@ static Vector512<uint> SingleToHalfAsWidenedUInt32(Vector512<float> value)
}
}

/// <summary>Creates a span of <typeparamref name="TTo"/> from a <typeparamref name="TTo"/> when they're the same type.</summary>
private static unsafe ReadOnlySpan<TTo> Rename<TFrom, TTo>(ReadOnlySpan<TFrom> span)
{
Debug.Assert(sizeof(TFrom) == sizeof(TTo));
return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<TFrom, TTo>(ref MemoryMarshal.GetReference(span)), span.Length);
}

/// <summary>Gets whether <typeparamref name="T"/> is <see cref="uint"/> or <see cref="nuint"/> if in a 32-bit process.</summary>
private static bool IsUInt32Like<T>() => typeof(T) == typeof(uint) || (IntPtr.Size == 4 && typeof(T) == typeof(nuint));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ public static void Round<T>(ReadOnlySpan<T> x, int digits, MidpointRounding mode
if (typeof(T) == typeof(float))
{
ReadOnlySpan<float> roundPower10Single = [1e0f, 1e1f, 1e2f, 1e3f, 1e4f, 1e5f, 1e6f];
roundPower10 = MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<float, T>(ref MemoryMarshal.GetReference(roundPower10Single)), roundPower10Single.Length);
roundPower10 = Rename<float, T>(roundPower10Single);
}
else if (typeof(T) == typeof(double))
{
Debug.Assert(typeof(T) == typeof(double));
ReadOnlySpan<double> roundPower10Double = [1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15];
roundPower10 = MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<double, T>(ref MemoryMarshal.GetReference(roundPower10Double)), roundPower10Double.Length);
roundPower10 = Rename<double, T>(roundPower10Double);
}
else
{
Expand Down
24 changes: 10 additions & 14 deletions src/libraries/System.Private.CoreLib/src/System/Number.Parsing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -883,16 +883,16 @@ internal static bool SpanStartsWith<TChar>(ReadOnlySpan<TChar> span, ReadOnlySpa
{
if (typeof(TChar) == typeof(char))
{
ReadOnlySpan<char> typedSpan = MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<TChar, char>(ref MemoryMarshal.GetReference(span)), span.Length);
ReadOnlySpan<char> typedValue = MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<TChar, char>(ref MemoryMarshal.GetReference(value)), value.Length);
ReadOnlySpan<char> typedSpan = MemoryMarshal.Cast<TChar, char>(span);
ReadOnlySpan<char> typedValue = MemoryMarshal.Cast<TChar, char>(value);
return typedSpan.StartsWith(typedValue, comparisonType);
}
else
{
Debug.Assert(typeof(TChar) == typeof(byte));

ReadOnlySpan<byte> typedSpan = MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<TChar, byte>(ref MemoryMarshal.GetReference(span)), span.Length);
ReadOnlySpan<byte> typedValue = MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<TChar, byte>(ref MemoryMarshal.GetReference(value)), value.Length);
ReadOnlySpan<byte> typedSpan = MemoryMarshal.Cast<TChar, byte>(span);
ReadOnlySpan<byte> typedValue = MemoryMarshal.Cast<TChar, byte>(value);
return typedSpan.StartsWithUtf8(typedValue, comparisonType);
}
}
Expand All @@ -903,17 +903,13 @@ internal static ReadOnlySpan<TChar> SpanTrim<TChar>(ReadOnlySpan<TChar> span)
{
if (typeof(TChar) == typeof(char))
{
ReadOnlySpan<char> typedSpan = MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<TChar, char>(ref MemoryMarshal.GetReference(span)), span.Length);
ReadOnlySpan<char> result = typedSpan.Trim();
return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<char, TChar>(ref MemoryMarshal.GetReference(result)), result.Length);
return MemoryMarshal.Cast<char, TChar>(MemoryMarshal.Cast<TChar, char>(span).Trim());
}
else
{
Debug.Assert(typeof(TChar) == typeof(byte));

ReadOnlySpan<byte> typedSpan = MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<TChar, byte>(ref MemoryMarshal.GetReference(span)), span.Length);
ReadOnlySpan<byte> result = typedSpan.TrimUtf8();
return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<byte, TChar>(ref MemoryMarshal.GetReference(result)), result.Length);
return MemoryMarshal.Cast<byte, TChar>(MemoryMarshal.Cast<TChar, byte>(span).TrimUtf8());
}
}

Expand All @@ -923,16 +919,16 @@ internal static bool SpanEqualsOrdinalIgnoreCase<TChar>(ReadOnlySpan<TChar> span
{
if (typeof(TChar) == typeof(char))
{
ReadOnlySpan<char> typedSpan = MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<TChar, char>(ref MemoryMarshal.GetReference(span)), span.Length);
ReadOnlySpan<char> typedValue = MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<TChar, char>(ref MemoryMarshal.GetReference(value)), value.Length);
ReadOnlySpan<char> typedSpan = MemoryMarshal.Cast<TChar, char>(span);
ReadOnlySpan<char> typedValue = MemoryMarshal.Cast<TChar, char>(value);
return typedSpan.EqualsOrdinalIgnoreCase(typedValue);
}
else
{
Debug.Assert(typeof(TChar) == typeof(byte));

ReadOnlySpan<byte> typedSpan = MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<TChar, byte>(ref MemoryMarshal.GetReference(span)), span.Length);
ReadOnlySpan<byte> typedValue = MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<TChar, byte>(ref MemoryMarshal.GetReference(value)), value.Length);
ReadOnlySpan<byte> typedSpan = MemoryMarshal.Cast<TChar, byte>(span);
ReadOnlySpan<byte> typedValue = MemoryMarshal.Cast<TChar, byte>(value);
return typedSpan.EqualsOrdinalIgnoreCaseUtf8(typedValue);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ public static Span<TTo> Cast<TFrom, TTo>(Span<TFrom> span)
if (RuntimeHelpers.IsReferenceOrContainsReferences<TTo>())
ThrowHelper.ThrowInvalidTypeWithPointersNotSupported(typeof(TTo));

if (typeof(TFrom) == typeof(TTo))
return new Span<TTo>(ref Unsafe.As<TFrom, TTo>(ref span._reference), span.Length);
stephentoub marked this conversation as resolved.
Show resolved Hide resolved

// Use unsigned integers - unsigned division by constant (especially by power of 2)
// and checked casts are faster and smaller.
uint fromSize = (uint)Unsafe.SizeOf<TFrom>();
Expand Down Expand Up @@ -177,6 +180,9 @@ public static ReadOnlySpan<TTo> Cast<TFrom, TTo>(ReadOnlySpan<TFrom> span)
if (RuntimeHelpers.IsReferenceOrContainsReferences<TTo>())
ThrowHelper.ThrowInvalidTypeWithPointersNotSupported(typeof(TTo));

if (typeof(TFrom) == typeof(TTo))
return new ReadOnlySpan<TTo>(ref Unsafe.As<TFrom, TTo>(ref span._reference), span.Length);

// Use unsigned integers - unsigned division by constant (especially by power of 2)
// and checked casts are faster and smaller.
uint fromSize = (uint)Unsafe.SizeOf<TFrom>();
Expand Down
Loading