diff --git a/src/libraries/System.Private.CoreLib/src/System/Byte.cs b/src/libraries/System.Private.CoreLib/src/System/Byte.cs index 331d28cf1d4304..1506b303ccb818 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Byte.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Byte.cs @@ -322,7 +322,7 @@ static bool IBinaryInteger.TryReadBigEndian(ReadOnlySpan 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; diff --git a/src/libraries/System.Private.CoreLib/src/System/Char.cs b/src/libraries/System.Private.CoreLib/src/System/Char.cs index 42078fa8e05d94..5d2c003ba3e66f 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Char.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Char.cs @@ -1254,24 +1254,15 @@ static bool IBinaryInteger.TryReadBigEndian(ReadOnlySpan 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(ref sourceRef); - - if (BitConverter.IsLittleEndian) - { - result = BinaryPrimitives.ReverseEndianness(result); - } + result = (char)BinaryPrimitives.ReadUInt16BigEndian(source.Slice(source.Length - sizeof(char))); } else { // We only have 1-byte so read it directly - result = (char)sourceRef; + result = (char)source[0]; } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Int128.cs b/src/libraries/System.Private.CoreLib/src/System/Int128.cs index 9387db8636ef71..bd701587102b13 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Int128.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Int128.cs @@ -803,19 +803,10 @@ static bool IBinaryInteger.TryReadBigEndian(ReadOnlySpan 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(ref sourceRef); - - if (BitConverter.IsLittleEndian) - { - result = BinaryPrimitives.ReverseEndianness(result); - } + result = BinaryPrimitives.ReadInt128BigEndian(source.Slice(source.Length - Size)); } else { @@ -826,7 +817,7 @@ static bool IBinaryInteger.TryReadBigEndian(ReadOnlySpan source, b for (int i = 0; i < source.Length; i++) { result <<= 8; - result |= Unsafe.Add(ref sourceRef, i); + result |= source[i]; } if (!isUnsigned) diff --git a/src/libraries/System.Private.CoreLib/src/System/Int16.cs b/src/libraries/System.Private.CoreLib/src/System/Int16.cs index c6312f3334c01a..5c30e0c071f256 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Int16.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Int16.cs @@ -351,29 +351,20 @@ static bool IBinaryInteger.TryReadBigEndian(ReadOnlySpan 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(ref sourceRef); - - if (BitConverter.IsLittleEndian) - { - result = BinaryPrimitives.ReverseEndianness(result); - } + result = BinaryPrimitives.ReadInt16BigEndian(source.Slice(source.Length - sizeof(short))); } 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]; } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Int32.cs b/src/libraries/System.Private.CoreLib/src/System/Int32.cs index b48c681f4b0105..17856d445c48f7 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Int32.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Int32.cs @@ -372,19 +372,10 @@ static bool IBinaryInteger.TryReadBigEndian(ReadOnlySpan 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(ref sourceRef); - - if (BitConverter.IsLittleEndian) - { - result = BinaryPrimitives.ReverseEndianness(result); - } + result = BinaryPrimitives.ReadInt32BigEndian(source.Slice(source.Length - sizeof(int))); } else { @@ -395,7 +386,7 @@ static bool IBinaryInteger.TryReadBigEndian(ReadOnlySpan source, bool for (int i = 0; i < source.Length; i++) { result <<= 8; - result |= Unsafe.Add(ref sourceRef, i); + result |= source[i]; } if (!isUnsigned) diff --git a/src/libraries/System.Private.CoreLib/src/System/Int64.cs b/src/libraries/System.Private.CoreLib/src/System/Int64.cs index 18c343fdac1808..ad2b5baa1f3a5a 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Int64.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Int64.cs @@ -369,19 +369,10 @@ static bool IBinaryInteger.TryReadBigEndian(ReadOnlySpan 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(ref sourceRef); - - if (BitConverter.IsLittleEndian) - { - result = BinaryPrimitives.ReverseEndianness(result); - } + result = BinaryPrimitives.ReadInt64BigEndian(source.Slice(source.Length - sizeof(long))); } else { @@ -392,7 +383,7 @@ static bool IBinaryInteger.TryReadBigEndian(ReadOnlySpan source, boo for (int i = 0; i < source.Length; i++) { result <<= 8; - result |= Unsafe.Add(ref sourceRef, i); + result |= source[i]; } if (!isUnsigned) diff --git a/src/libraries/System.Private.CoreLib/src/System/IntPtr.cs b/src/libraries/System.Private.CoreLib/src/System/IntPtr.cs index cd6f768526f722..86dce7a2210d2d 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IntPtr.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IntPtr.cs @@ -392,19 +392,10 @@ static bool IBinaryInteger.TryReadBigEndian(ReadOnlySpan 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(ref sourceRef); - - if (BitConverter.IsLittleEndian) - { - result = BinaryPrimitives.ReverseEndianness(result); - } + result = BinaryPrimitives.ReadIntPtrBigEndian(source.Slice(source.Length - sizeof(nint_t))); } else { @@ -415,7 +406,7 @@ static bool IBinaryInteger.TryReadBigEndian(ReadOnlySpan source, boo for (int i = 0; i < source.Length; i++) { result <<= 8; - result |= Unsafe.Add(ref sourceRef, i); + result |= source[i]; } if (!isUnsigned) diff --git a/src/libraries/System.Private.CoreLib/src/System/SByte.cs b/src/libraries/System.Private.CoreLib/src/System/SByte.cs index c22e77da475053..b39aae69c21a35 100644 --- a/src/libraries/System.Private.CoreLib/src/System/SByte.cs +++ b/src/libraries/System.Private.CoreLib/src/System/SByte.cs @@ -355,7 +355,7 @@ static bool IBinaryInteger.TryReadBigEndian(ReadOnlySpan 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; diff --git a/src/libraries/System.Private.CoreLib/src/System/UInt128.cs b/src/libraries/System.Private.CoreLib/src/System/UInt128.cs index ea72d8d8e5ccb8..6d176fee895b65 100644 --- a/src/libraries/System.Private.CoreLib/src/System/UInt128.cs +++ b/src/libraries/System.Private.CoreLib/src/System/UInt128.cs @@ -1129,19 +1129,10 @@ static bool IBinaryInteger.TryReadBigEndian(ReadOnlySpan 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(ref sourceRef); - - if (BitConverter.IsLittleEndian) - { - result = BinaryPrimitives.ReverseEndianness(result); - } + result = BinaryPrimitives.ReadUInt128BigEndian(source.Slice(source.Length - Size)); } else { @@ -1152,7 +1143,7 @@ static bool IBinaryInteger.TryReadBigEndian(ReadOnlySpan source, for (int i = 0; i < source.Length; i++) { result <<= 8; - result |= Unsafe.Add(ref sourceRef, i); + result |= source[i]; } } } diff --git a/src/libraries/System.Private.CoreLib/src/System/UInt16.cs b/src/libraries/System.Private.CoreLib/src/System/UInt16.cs index c477fcaaf56ad5..103616960bebd6 100644 --- a/src/libraries/System.Private.CoreLib/src/System/UInt16.cs +++ b/src/libraries/System.Private.CoreLib/src/System/UInt16.cs @@ -318,24 +318,15 @@ static bool IBinaryInteger.TryReadBigEndian(ReadOnlySpan 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(ref sourceRef); - - if (BitConverter.IsLittleEndian) - { - result = BinaryPrimitives.ReverseEndianness(result); - } + result = BinaryPrimitives.ReadUInt16BigEndian(source.Slice(source.Length - sizeof(ushort))); } else { // We only have 1-byte so read it directly - result = sourceRef; + result = source[0]; } } diff --git a/src/libraries/System.Private.CoreLib/src/System/UInt32.cs b/src/libraries/System.Private.CoreLib/src/System/UInt32.cs index dc3282ad146444..64257211699f67 100644 --- a/src/libraries/System.Private.CoreLib/src/System/UInt32.cs +++ b/src/libraries/System.Private.CoreLib/src/System/UInt32.cs @@ -365,19 +365,10 @@ static bool IBinaryInteger.TryReadBigEndian(ReadOnlySpan 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(ref sourceRef); - - if (BitConverter.IsLittleEndian) - { - result = BinaryPrimitives.ReverseEndianness(result); - } + result = BinaryPrimitives.ReadUInt32BigEndian(source.Slice(source.Length - sizeof(uint))); } else { @@ -388,7 +379,7 @@ static bool IBinaryInteger.TryReadBigEndian(ReadOnlySpan source, boo for (int i = 0; i < source.Length; i++) { result <<= 8; - result |= Unsafe.Add(ref sourceRef, i); + result |= source[i]; } } } diff --git a/src/libraries/System.Private.CoreLib/src/System/UInt64.cs b/src/libraries/System.Private.CoreLib/src/System/UInt64.cs index 8abc0f1b5a4a9d..40331540c63fdd 100644 --- a/src/libraries/System.Private.CoreLib/src/System/UInt64.cs +++ b/src/libraries/System.Private.CoreLib/src/System/UInt64.cs @@ -372,19 +372,10 @@ static bool IBinaryInteger.TryReadBigEndian(ReadOnlySpan 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(ref sourceRef); - - if (BitConverter.IsLittleEndian) - { - result = BinaryPrimitives.ReverseEndianness(result); - } + result = BinaryPrimitives.ReadUInt64BigEndian(source.Slice(source.Length - sizeof(ulong))); } else { @@ -395,7 +386,7 @@ static bool IBinaryInteger.TryReadBigEndian(ReadOnlySpan source, bo for (int i = 0; i < source.Length; i++) { result <<= 8; - result |= Unsafe.Add(ref sourceRef, i); + result |= source[i]; } } } diff --git a/src/libraries/System.Private.CoreLib/src/System/UIntPtr.cs b/src/libraries/System.Private.CoreLib/src/System/UIntPtr.cs index fe1f01e314ab58..e9cf4ab7950807 100644 --- a/src/libraries/System.Private.CoreLib/src/System/UIntPtr.cs +++ b/src/libraries/System.Private.CoreLib/src/System/UIntPtr.cs @@ -367,19 +367,10 @@ static bool IBinaryInteger.TryReadBigEndian(ReadOnlySpan 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(ref sourceRef); - - if (BitConverter.IsLittleEndian) - { - result = BinaryPrimitives.ReverseEndianness(result); - } + result = BinaryPrimitives.ReadUIntPtrBigEndian(source.Slice(source.Length - sizeof(nuint_t))); } else { @@ -390,7 +381,7 @@ static bool IBinaryInteger.TryReadBigEndian(ReadOnlySpan source, bo for (int i = 0; i < source.Length; i++) { result <<= 8; - result |= Unsafe.Add(ref sourceRef, i); + result |= source[i]; } } }