Skip to content

Commit 259418b

Browse files
committed
Minor codegen improvements
1 parent cad806c commit 259418b

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

Microsoft.Toolkit.HighPerformance/Helpers/Internals/RuntimeHelpers.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,24 @@ internal static class RuntimeHelpers
3232
/// <returns>The converted length for the specified argument and types.</returns>
3333
[Pure]
3434
[MethodImpl(MethodImplOptions.AggressiveInlining)]
35-
public static int ConvertLength<TFrom, TTo>(int length)
35+
public static unsafe int ConvertLength<TFrom, TTo>(int length)
3636
where TFrom : unmanaged
3737
where TTo : unmanaged
3838
{
39-
uint fromSize = (uint)Unsafe.SizeOf<TFrom>();
40-
uint toSize = (uint)Unsafe.SizeOf<TTo>();
41-
uint fromLength = (uint)length;
42-
int toLength;
43-
44-
if (fromSize == toSize)
39+
if (sizeof(TFrom) == sizeof(TTo))
4540
{
46-
toLength = (int)fromLength;
41+
return length;
4742
}
48-
else if (fromSize == 1)
43+
else if (sizeof(TFrom) == 1)
4944
{
50-
toLength = (int)(fromLength / toSize);
45+
return length / sizeof(TTo);
5146
}
5247
else
5348
{
54-
ulong toLengthUInt64 = (ulong)fromLength * fromSize / toSize;
49+
ulong targetLength = (ulong)(uint)length * (uint)sizeof(TFrom) / (uint)sizeof(TTo);
5550

56-
toLength = checked((int)toLengthUInt64);
51+
return checked((int)targetLength);
5752
}
58-
59-
return toLength;
6053
}
6154

6255
/// <summary>

0 commit comments

Comments
 (0)