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

Adjust the usage of ReverseEndianness in BinaryPrimitives #69063

Merged
merged 1 commit into from
May 11, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,9 @@ public static Half ReadHalfBigEndian(ReadOnlySpan<byte> source)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static short ReadInt16BigEndian(ReadOnlySpan<byte> source)
{
short result = MemoryMarshal.Read<short>(source);
if (BitConverter.IsLittleEndian)
{
result = ReverseEndianness(result);
}
return result;
return BitConverter.IsLittleEndian ?
ReverseEndianness(MemoryMarshal.Read<short>(source)) :
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JIT is reordering the IR so that the ReverseEndianness occurs directly over the Unsafe.ReadUnaligned and emitting just movbe reg, [addr]? The throw helpers/checks and inlining boundaries aren't getting in the way here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thats the case. We end up generating:

mov      rax, bword ptr [rcx]
mov      ecx, dword ptr [rcx+8]
cmp      ecx, 4
jl       SHORT G_M53413_IG04
movbe    eax, dword ptr [rax]

MemoryMarshal.Read<short>(source);
}

/// <summary>
Expand All @@ -62,12 +59,9 @@ public static short ReadInt16BigEndian(ReadOnlySpan<byte> source)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int ReadInt32BigEndian(ReadOnlySpan<byte> source)
{
int result = MemoryMarshal.Read<int>(source);
if (BitConverter.IsLittleEndian)
{
result = ReverseEndianness(result);
}
return result;
return BitConverter.IsLittleEndian ?
ReverseEndianness(MemoryMarshal.Read<int>(source)) :
MemoryMarshal.Read<int>(source);
}

/// <summary>
Expand All @@ -76,12 +70,9 @@ public static int ReadInt32BigEndian(ReadOnlySpan<byte> source)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long ReadInt64BigEndian(ReadOnlySpan<byte> source)
{
long result = MemoryMarshal.Read<long>(source);
if (BitConverter.IsLittleEndian)
{
result = ReverseEndianness(result);
}
return result;
return BitConverter.IsLittleEndian ?
ReverseEndianness(MemoryMarshal.Read<long>(source)) :
MemoryMarshal.Read<long>(source);
}

/// <summary>
Expand All @@ -108,12 +99,9 @@ public static float ReadSingleBigEndian(ReadOnlySpan<byte> source)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ushort ReadUInt16BigEndian(ReadOnlySpan<byte> source)
{
ushort result = MemoryMarshal.Read<ushort>(source);
if (BitConverter.IsLittleEndian)
{
result = ReverseEndianness(result);
}
return result;
return BitConverter.IsLittleEndian ?
ReverseEndianness(MemoryMarshal.Read<ushort>(source)) :
MemoryMarshal.Read<ushort>(source);
}

/// <summary>
Expand All @@ -123,12 +111,9 @@ public static ushort ReadUInt16BigEndian(ReadOnlySpan<byte> source)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint ReadUInt32BigEndian(ReadOnlySpan<byte> source)
{
uint result = MemoryMarshal.Read<uint>(source);
if (BitConverter.IsLittleEndian)
{
result = ReverseEndianness(result);
}
return result;
return BitConverter.IsLittleEndian ?
ReverseEndianness(MemoryMarshal.Read<uint>(source)) :
MemoryMarshal.Read<uint>(source);
}

/// <summary>
Expand All @@ -138,12 +123,9 @@ public static uint ReadUInt32BigEndian(ReadOnlySpan<byte> source)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong ReadUInt64BigEndian(ReadOnlySpan<byte> source)
{
ulong result = MemoryMarshal.Read<ulong>(source);
if (BitConverter.IsLittleEndian)
{
result = ReverseEndianness(result);
}
return result;
return BitConverter.IsLittleEndian ?
ReverseEndianness(MemoryMarshal.Read<ulong>(source)) :
MemoryMarshal.Read<ulong>(source);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,9 @@ public static Half ReadHalfLittleEndian(ReadOnlySpan<byte> source)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static short ReadInt16LittleEndian(ReadOnlySpan<byte> source)
{
short result = MemoryMarshal.Read<short>(source);
if (!BitConverter.IsLittleEndian)
{
result = ReverseEndianness(result);
}
return result;
return !BitConverter.IsLittleEndian ?
ReverseEndianness(MemoryMarshal.Read<short>(source)) :
MemoryMarshal.Read<short>(source);
}

/// <summary>
Expand All @@ -62,12 +59,9 @@ public static short ReadInt16LittleEndian(ReadOnlySpan<byte> source)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int ReadInt32LittleEndian(ReadOnlySpan<byte> source)
{
int result = MemoryMarshal.Read<int>(source);
if (!BitConverter.IsLittleEndian)
{
result = ReverseEndianness(result);
}
return result;
return !BitConverter.IsLittleEndian ?
ReverseEndianness(MemoryMarshal.Read<int>(source)) :
MemoryMarshal.Read<int>(source);
}

/// <summary>
Expand All @@ -76,12 +70,9 @@ public static int ReadInt32LittleEndian(ReadOnlySpan<byte> source)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long ReadInt64LittleEndian(ReadOnlySpan<byte> source)
{
long result = MemoryMarshal.Read<long>(source);
if (!BitConverter.IsLittleEndian)
{
result = ReverseEndianness(result);
}
return result;
return !BitConverter.IsLittleEndian ?
ReverseEndianness(MemoryMarshal.Read<long>(source)) :
MemoryMarshal.Read<long>(source);
}

/// <summary>
Expand All @@ -108,12 +99,9 @@ public static float ReadSingleLittleEndian(ReadOnlySpan<byte> source)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ushort ReadUInt16LittleEndian(ReadOnlySpan<byte> source)
{
ushort result = MemoryMarshal.Read<ushort>(source);
if (!BitConverter.IsLittleEndian)
{
result = ReverseEndianness(result);
}
return result;
return !BitConverter.IsLittleEndian ?
ReverseEndianness(MemoryMarshal.Read<ushort>(source)) :
MemoryMarshal.Read<ushort>(source);
}

/// <summary>
Expand All @@ -123,12 +111,9 @@ public static ushort ReadUInt16LittleEndian(ReadOnlySpan<byte> source)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint ReadUInt32LittleEndian(ReadOnlySpan<byte> source)
{
uint result = MemoryMarshal.Read<uint>(source);
if (!BitConverter.IsLittleEndian)
{
result = ReverseEndianness(result);
}
return result;
return !BitConverter.IsLittleEndian ?
ReverseEndianness(MemoryMarshal.Read<uint>(source)) :
MemoryMarshal.Read<uint>(source);
}

/// <summary>
Expand All @@ -138,12 +123,9 @@ public static uint ReadUInt32LittleEndian(ReadOnlySpan<byte> source)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong ReadUInt64LittleEndian(ReadOnlySpan<byte> source)
{
ulong result = MemoryMarshal.Read<ulong>(source);
if (!BitConverter.IsLittleEndian)
{
result = ReverseEndianness(result);
}
return result;
return !BitConverter.IsLittleEndian ?
ReverseEndianness(MemoryMarshal.Read<ulong>(source)) :
MemoryMarshal.Read<ulong>(source);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,13 @@ public static void WriteInt16BigEndian(Span<byte> destination, short value)
{
if (BitConverter.IsLittleEndian)
{
value = ReverseEndianness(value);
short tmp = ReverseEndianness(value);
MemoryMarshal.Write(destination, ref tmp);
}
else
{
MemoryMarshal.Write(destination, ref value);
}
MemoryMarshal.Write(destination, ref value);
}

/// <summary>
Expand All @@ -75,9 +79,13 @@ public static void WriteInt32BigEndian(Span<byte> destination, int value)
{
if (BitConverter.IsLittleEndian)
{
value = ReverseEndianness(value);
int tmp = ReverseEndianness(value);
MemoryMarshal.Write(destination, ref tmp);
}
else
{
MemoryMarshal.Write(destination, ref value);
}
MemoryMarshal.Write(destination, ref value);
}

/// <summary>
Expand All @@ -88,9 +96,13 @@ public static void WriteInt64BigEndian(Span<byte> destination, long value)
{
if (BitConverter.IsLittleEndian)
{
value = ReverseEndianness(value);
long tmp = ReverseEndianness(value);
MemoryMarshal.Write(destination, ref tmp);
}
else
{
MemoryMarshal.Write(destination, ref value);
}
MemoryMarshal.Write(destination, ref value);
}

/// <summary>
Expand Down Expand Up @@ -125,9 +137,13 @@ public static void WriteUInt16BigEndian(Span<byte> destination, ushort value)
{
if (BitConverter.IsLittleEndian)
{
value = ReverseEndianness(value);
ushort tmp = ReverseEndianness(value);
MemoryMarshal.Write(destination, ref tmp);
}
else
{
MemoryMarshal.Write(destination, ref value);
}
MemoryMarshal.Write(destination, ref value);
}

/// <summary>
Expand All @@ -139,9 +155,13 @@ public static void WriteUInt32BigEndian(Span<byte> destination, uint value)
{
if (BitConverter.IsLittleEndian)
{
value = ReverseEndianness(value);
uint tmp = ReverseEndianness(value);
MemoryMarshal.Write(destination, ref tmp);
}
else
{
MemoryMarshal.Write(destination, ref value);
}
MemoryMarshal.Write(destination, ref value);
}

/// <summary>
Expand All @@ -153,9 +173,13 @@ public static void WriteUInt64BigEndian(Span<byte> destination, ulong value)
{
if (BitConverter.IsLittleEndian)
{
value = ReverseEndianness(value);
ulong tmp = ReverseEndianness(value);
MemoryMarshal.Write(destination, ref tmp);
}
else
{
MemoryMarshal.Write(destination, ref value);
}
MemoryMarshal.Write(destination, ref value);
}

/// <summary>
Expand Down Expand Up @@ -209,8 +233,10 @@ public static bool TryWriteInt16BigEndian(Span<byte> destination, short value)
{
if (BitConverter.IsLittleEndian)
{
value = ReverseEndianness(value);
short tmp = ReverseEndianness(value);
return MemoryMarshal.TryWrite(destination, ref tmp);
}

return MemoryMarshal.TryWrite(destination, ref value);
}

Expand All @@ -223,8 +249,10 @@ public static bool TryWriteInt32BigEndian(Span<byte> destination, int value)
{
if (BitConverter.IsLittleEndian)
{
value = ReverseEndianness(value);
int tmp = ReverseEndianness(value);
return MemoryMarshal.TryWrite(destination, ref tmp);
}

return MemoryMarshal.TryWrite(destination, ref value);
}

Expand All @@ -237,8 +265,10 @@ public static bool TryWriteInt64BigEndian(Span<byte> destination, long value)
{
if (BitConverter.IsLittleEndian)
{
value = ReverseEndianness(value);
long tmp = ReverseEndianness(value);
return MemoryMarshal.TryWrite(destination, ref tmp);
}

return MemoryMarshal.TryWrite(destination, ref value);
}

Expand Down Expand Up @@ -273,8 +303,10 @@ public static bool TryWriteUInt16BigEndian(Span<byte> destination, ushort value)
{
if (BitConverter.IsLittleEndian)
{
value = ReverseEndianness(value);
ushort tmp = ReverseEndianness(value);
return MemoryMarshal.TryWrite(destination, ref tmp);
}

return MemoryMarshal.TryWrite(destination, ref value);
}

Expand All @@ -288,8 +320,10 @@ public static bool TryWriteUInt32BigEndian(Span<byte> destination, uint value)
{
if (BitConverter.IsLittleEndian)
{
value = ReverseEndianness(value);
uint tmp = ReverseEndianness(value);
return MemoryMarshal.TryWrite(destination, ref tmp);
}

return MemoryMarshal.TryWrite(destination, ref value);
}

Expand All @@ -303,8 +337,10 @@ public static bool TryWriteUInt64BigEndian(Span<byte> destination, ulong value)
{
if (BitConverter.IsLittleEndian)
{
value = ReverseEndianness(value);
ulong tmp = ReverseEndianness(value);
return MemoryMarshal.TryWrite(destination, ref tmp);
}

return MemoryMarshal.TryWrite(destination, ref value);
}
}
Expand Down
Loading