diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs index d849a0d58c75ed..2d829fa6f831d2 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs @@ -128,11 +128,12 @@ public static unsafe Span Cast(Span span) int toLength; if (fromSize == toSize) { - // Special case for same size types - `(ulong)fromLength * (ulong)fromSize / (ulong)toSize` - // should be optimized to just `length` but the JIT doesn't do that today. - toLength = (int)fromLength; + // Special case for same size types + // should be optimized without this but the JIT doesn't do that today. + return Unsafe.BitCast, Span>(span); } - else if (fromSize == 1) + + if (fromSize == 1) { // Special case for byte sized TFrom - `(ulong)fromLength * (ulong)fromSize / (ulong)toSize` // becomes `(ulong)fromLength / (ulong)toSize` but the JIT can't narrow it down to `int` @@ -183,11 +184,12 @@ public static unsafe ReadOnlySpan Cast(ReadOnlySpan span int toLength; if (fromSize == toSize) { - // Special case for same size types - `(ulong)fromLength * (ulong)fromSize / (ulong)toSize` - // should be optimized to just `length` but the JIT doesn't do that today. - toLength = (int)fromLength; + // Special case for same size types + // should be optimized without this but the JIT doesn't do that today. + return Unsafe.BitCast, ReadOnlySpan>(span); } - else if (fromSize == 1) + + if (fromSize == 1) { // Special case for byte sized TFrom - `(ulong)fromLength * (ulong)fromSize / (ulong)toSize` // becomes `(ulong)fromLength / (ulong)toSize` but the JIT can't narrow it down to `int`