Skip to content
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
2 changes: 1 addition & 1 deletion src/libraries/System.Private.CoreLib/src/System/Byte.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ static bool IBinaryInteger<byte>.TryReadBigEndian(ReadOnlySpan<byte> source, boo
}

// We only have 1-byte so read it directly
result = Unsafe.Add(ref MemoryMarshal.GetReference(source), source.Length - sizeof(byte));
result = source[source.Length - sizeof(byte)];
}

value = result;
Expand Down
13 changes: 2 additions & 11 deletions src/libraries/System.Private.CoreLib/src/System/Char.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1254,24 +1254,15 @@ static bool IBinaryInteger<char>.TryReadBigEndian(ReadOnlySpan<byte> source, boo
return false;
}

ref byte sourceRef = ref MemoryMarshal.GetReference(source);

if (source.Length >= sizeof(char))
{
sourceRef = ref Unsafe.Add(ref sourceRef, source.Length - sizeof(char));

// We have at least 2 bytes, so just read the ones we need directly
result = Unsafe.ReadUnaligned<char>(ref sourceRef);

if (BitConverter.IsLittleEndian)
{
result = BinaryPrimitives.ReverseEndianness(result);
}
result = (char)BinaryPrimitives.ReadUInt16BigEndian(source.Slice(source.Length - sizeof(char)));
Comment thread
EgorBo marked this conversation as resolved.
}
else
{
// We only have 1-byte so read it directly
result = (char)sourceRef;
result = (char)source[0];
}
}

Expand Down
13 changes: 2 additions & 11 deletions src/libraries/System.Private.CoreLib/src/System/Int128.cs
Original file line number Diff line number Diff line change
Expand Up @@ -803,19 +803,10 @@ static bool IBinaryInteger<Int128>.TryReadBigEndian(ReadOnlySpan<byte> source, b
}
}

ref byte sourceRef = ref MemoryMarshal.GetReference(source);

if (source.Length >= Size)
{
sourceRef = ref Unsafe.Add(ref sourceRef, source.Length - Size);

// We have at least 16 bytes, so just read the ones we need directly
result = Unsafe.ReadUnaligned<Int128>(ref sourceRef);

if (BitConverter.IsLittleEndian)
{
result = BinaryPrimitives.ReverseEndianness(result);
}
result = BinaryPrimitives.ReadInt128BigEndian(source.Slice(source.Length - Size));
}
Comment thread
EgorBo marked this conversation as resolved.
else
{
Expand All @@ -826,7 +817,7 @@ static bool IBinaryInteger<Int128>.TryReadBigEndian(ReadOnlySpan<byte> source, b
for (int i = 0; i < source.Length; i++)
{
result <<= 8;
result |= Unsafe.Add(ref sourceRef, i);
result |= source[i];
}

if (!isUnsigned)
Expand Down
15 changes: 3 additions & 12 deletions src/libraries/System.Private.CoreLib/src/System/Int16.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,29 +351,20 @@ static bool IBinaryInteger<short>.TryReadBigEndian(ReadOnlySpan<byte> source, bo
}
}

ref byte sourceRef = ref MemoryMarshal.GetReference(source);

if (source.Length >= sizeof(short))
{
sourceRef = ref Unsafe.Add(ref sourceRef, source.Length - sizeof(short));

// We have at least 2 bytes, so just read the ones we need directly
result = Unsafe.ReadUnaligned<short>(ref sourceRef);

if (BitConverter.IsLittleEndian)
{
result = BinaryPrimitives.ReverseEndianness(result);
}
result = BinaryPrimitives.ReadInt16BigEndian(source.Slice(source.Length - sizeof(short)));
Comment thread
EgorBo marked this conversation as resolved.
}
else if (isUnsigned)
{
// We only have 1-byte so read it directly
result = sourceRef;
result = source[0];
}
else
{
// We only have 1-byte so read it directly with sign extension
result = (sbyte)sourceRef;
result = (sbyte)source[0];
}
}

Expand Down
13 changes: 2 additions & 11 deletions src/libraries/System.Private.CoreLib/src/System/Int32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,19 +372,10 @@ static bool IBinaryInteger<int>.TryReadBigEndian(ReadOnlySpan<byte> source, bool
}
}

ref byte sourceRef = ref MemoryMarshal.GetReference(source);

if (source.Length >= sizeof(int))
{
sourceRef = ref Unsafe.Add(ref sourceRef, source.Length - sizeof(int));

// We have at least 4 bytes, so just read the ones we need directly
result = Unsafe.ReadUnaligned<int>(ref sourceRef);

if (BitConverter.IsLittleEndian)
{
result = BinaryPrimitives.ReverseEndianness(result);
}
result = BinaryPrimitives.ReadInt32BigEndian(source.Slice(source.Length - sizeof(int)));
Comment thread
EgorBo marked this conversation as resolved.
}
else
{
Expand All @@ -395,7 +386,7 @@ static bool IBinaryInteger<int>.TryReadBigEndian(ReadOnlySpan<byte> source, bool
for (int i = 0; i < source.Length; i++)
{
result <<= 8;
result |= Unsafe.Add(ref sourceRef, i);
result |= source[i];
}

if (!isUnsigned)
Expand Down
13 changes: 2 additions & 11 deletions src/libraries/System.Private.CoreLib/src/System/Int64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,19 +369,10 @@ static bool IBinaryInteger<long>.TryReadBigEndian(ReadOnlySpan<byte> source, boo
}
}

ref byte sourceRef = ref MemoryMarshal.GetReference(source);

if (source.Length >= sizeof(long))
{
sourceRef = ref Unsafe.Add(ref sourceRef, source.Length - sizeof(long));

// We have at least 8 bytes, so just read the ones we need directly
result = Unsafe.ReadUnaligned<long>(ref sourceRef);

if (BitConverter.IsLittleEndian)
{
result = BinaryPrimitives.ReverseEndianness(result);
}
result = BinaryPrimitives.ReadInt64BigEndian(source.Slice(source.Length - sizeof(long)));
Comment thread
EgorBo marked this conversation as resolved.
}
else
{
Expand All @@ -392,7 +383,7 @@ static bool IBinaryInteger<long>.TryReadBigEndian(ReadOnlySpan<byte> source, boo
for (int i = 0; i < source.Length; i++)
{
result <<= 8;
result |= Unsafe.Add(ref sourceRef, i);
result |= source[i];
}

if (!isUnsigned)
Expand Down
13 changes: 2 additions & 11 deletions src/libraries/System.Private.CoreLib/src/System/IntPtr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -392,19 +392,10 @@ static bool IBinaryInteger<nint>.TryReadBigEndian(ReadOnlySpan<byte> source, boo
}
}

ref byte sourceRef = ref MemoryMarshal.GetReference(source);

if (source.Length >= sizeof(nint_t))
{
sourceRef = ref Unsafe.Add(ref sourceRef, source.Length - sizeof(nint_t));

// We have at least 4/8 bytes, so just read the ones we need directly
result = Unsafe.ReadUnaligned<nint>(ref sourceRef);

if (BitConverter.IsLittleEndian)
{
result = BinaryPrimitives.ReverseEndianness(result);
}
result = BinaryPrimitives.ReadIntPtrBigEndian(source.Slice(source.Length - sizeof(nint_t)));
}
Comment thread
EgorBo marked this conversation as resolved.
else
{
Expand All @@ -415,7 +406,7 @@ static bool IBinaryInteger<nint>.TryReadBigEndian(ReadOnlySpan<byte> source, boo
for (int i = 0; i < source.Length; i++)
{
result <<= 8;
result |= Unsafe.Add(ref sourceRef, i);
result |= source[i];
}

if (!isUnsigned)
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/System.Private.CoreLib/src/System/SByte.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ static bool IBinaryInteger<sbyte>.TryReadBigEndian(ReadOnlySpan<byte> source, bo
}

// We only have 1-byte so read it directly
result = (sbyte)Unsafe.Add(ref MemoryMarshal.GetReference(source), source.Length - sizeof(sbyte));
result = (sbyte)source[source.Length - sizeof(sbyte)];
}

value = result;
Expand Down
13 changes: 2 additions & 11 deletions src/libraries/System.Private.CoreLib/src/System/UInt128.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1129,19 +1129,10 @@ static bool IBinaryInteger<UInt128>.TryReadBigEndian(ReadOnlySpan<byte> source,
return false;
}

ref byte sourceRef = ref MemoryMarshal.GetReference(source);

if (source.Length >= Size)
{
sourceRef = ref Unsafe.Add(ref sourceRef, source.Length - Size);

// We have at least 16 bytes, so just read the ones we need directly
result = Unsafe.ReadUnaligned<UInt128>(ref sourceRef);

if (BitConverter.IsLittleEndian)
{
result = BinaryPrimitives.ReverseEndianness(result);
}
result = BinaryPrimitives.ReadUInt128BigEndian(source.Slice(source.Length - Size));
Comment thread
EgorBo marked this conversation as resolved.
}
else
{
Expand All @@ -1152,7 +1143,7 @@ static bool IBinaryInteger<UInt128>.TryReadBigEndian(ReadOnlySpan<byte> source,
for (int i = 0; i < source.Length; i++)
{
result <<= 8;
result |= Unsafe.Add(ref sourceRef, i);
result |= source[i];
}
}
}
Expand Down
13 changes: 2 additions & 11 deletions src/libraries/System.Private.CoreLib/src/System/UInt16.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,24 +318,15 @@ static bool IBinaryInteger<ushort>.TryReadBigEndian(ReadOnlySpan<byte> source, b
return false;
}

ref byte sourceRef = ref MemoryMarshal.GetReference(source);

if (source.Length >= sizeof(ushort))
{
sourceRef = ref Unsafe.Add(ref sourceRef, source.Length - sizeof(ushort));

// We have at least 2 bytes, so just read the ones we need directly
result = Unsafe.ReadUnaligned<ushort>(ref sourceRef);

if (BitConverter.IsLittleEndian)
{
result = BinaryPrimitives.ReverseEndianness(result);
}
result = BinaryPrimitives.ReadUInt16BigEndian(source.Slice(source.Length - sizeof(ushort)));
Comment thread
EgorBo marked this conversation as resolved.
}
else
{
// We only have 1-byte so read it directly
result = sourceRef;
result = source[0];
}
}

Expand Down
13 changes: 2 additions & 11 deletions src/libraries/System.Private.CoreLib/src/System/UInt32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,19 +365,10 @@ static bool IBinaryInteger<uint>.TryReadBigEndian(ReadOnlySpan<byte> source, boo
return false;
}

ref byte sourceRef = ref MemoryMarshal.GetReference(source);

if (source.Length >= sizeof(uint))
{
sourceRef = ref Unsafe.Add(ref sourceRef, source.Length - sizeof(uint));

// We have at least 4 bytes, so just read the ones we need directly
result = Unsafe.ReadUnaligned<uint>(ref sourceRef);

if (BitConverter.IsLittleEndian)
{
result = BinaryPrimitives.ReverseEndianness(result);
}
result = BinaryPrimitives.ReadUInt32BigEndian(source.Slice(source.Length - sizeof(uint)));
Comment thread
EgorBo marked this conversation as resolved.
}
else
{
Expand All @@ -388,7 +379,7 @@ static bool IBinaryInteger<uint>.TryReadBigEndian(ReadOnlySpan<byte> source, boo
for (int i = 0; i < source.Length; i++)
{
result <<= 8;
result |= Unsafe.Add(ref sourceRef, i);
result |= source[i];
}
}
}
Expand Down
13 changes: 2 additions & 11 deletions src/libraries/System.Private.CoreLib/src/System/UInt64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,19 +372,10 @@ static bool IBinaryInteger<ulong>.TryReadBigEndian(ReadOnlySpan<byte> source, bo
return false;
}

ref byte sourceRef = ref MemoryMarshal.GetReference(source);

if (source.Length >= sizeof(ulong))
{
sourceRef = ref Unsafe.Add(ref sourceRef, source.Length - sizeof(ulong));

// We have at least 8 bytes, so just read the ones we need directly
result = Unsafe.ReadUnaligned<ulong>(ref sourceRef);

if (BitConverter.IsLittleEndian)
{
result = BinaryPrimitives.ReverseEndianness(result);
}
result = BinaryPrimitives.ReadUInt64BigEndian(source.Slice(source.Length - sizeof(ulong)));
Comment thread
EgorBo marked this conversation as resolved.
}
else
{
Expand All @@ -395,7 +386,7 @@ static bool IBinaryInteger<ulong>.TryReadBigEndian(ReadOnlySpan<byte> source, bo
for (int i = 0; i < source.Length; i++)
{
result <<= 8;
result |= Unsafe.Add(ref sourceRef, i);
result |= source[i];
}
}
}
Expand Down
13 changes: 2 additions & 11 deletions src/libraries/System.Private.CoreLib/src/System/UIntPtr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,19 +367,10 @@ static bool IBinaryInteger<nuint>.TryReadBigEndian(ReadOnlySpan<byte> source, bo
return false;
}

ref byte sourceRef = ref MemoryMarshal.GetReference(source);

if (source.Length >= sizeof(nuint_t))
{
sourceRef = ref Unsafe.Add(ref sourceRef, source.Length - sizeof(nuint_t));

// We have at least 4/8 bytes, so just read the ones we need directly
result = Unsafe.ReadUnaligned<nuint>(ref sourceRef);

if (BitConverter.IsLittleEndian)
{
result = BinaryPrimitives.ReverseEndianness(result);
}
result = BinaryPrimitives.ReadUIntPtrBigEndian(source.Slice(source.Length - sizeof(nuint_t)));
}
Comment thread
EgorBo marked this conversation as resolved.
else
{
Expand All @@ -390,7 +381,7 @@ static bool IBinaryInteger<nuint>.TryReadBigEndian(ReadOnlySpan<byte> source, bo
for (int i = 0; i < source.Length; i++)
{
result <<= 8;
result |= Unsafe.Add(ref sourceRef, i);
result |= source[i];
}
}
}
Expand Down
Loading