< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 234
Coverable lines: 234
Total lines: 2825
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 102
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
File 1: TryReadDoubleBigEndian(...)100%110%
File 1: TryReadHalfBigEndian(...)100%110%
File 1: TryReadBFloat16BigEndian(...)100%110%
File 1: TryReadInt16BigEndian(...)100%110%
File 1: TryReadInt32BigEndian(...)100%110%
File 1: TryReadInt64BigEndian(...)100%110%
File 1: TryReadInt128BigEndian(...)100%110%
File 1: TryReadIntPtrBigEndian(...)100%110%
File 1: TryReadSingleBigEndian(...)100%110%
File 1: TryReadUInt16BigEndian(...)100%110%
File 1: TryReadUInt32BigEndian(...)100%110%
File 1: TryReadUInt64BigEndian(...)100%110%
File 1: TryReadUInt128BigEndian(...)100%110%
File 1: TryReadUIntPtrBigEndian(...)100%110%
File 2: TryReadDoubleLittleEndian(...)0%220%
File 2: TryReadHalfLittleEndian(...)0%220%
File 2: TryReadBFloat16LittleEndian(...)0%220%
File 2: TryReadInt16LittleEndian(...)100%110%
File 2: TryReadInt32LittleEndian(...)100%110%
File 2: TryReadInt64LittleEndian(...)100%110%
File 2: TryReadInt128LittleEndian(...)100%110%
File 2: TryReadIntPtrLittleEndian(...)100%110%
File 2: TryReadSingleLittleEndian(...)0%220%
File 2: TryReadUInt16LittleEndian(...)100%110%
File 2: TryReadUInt32LittleEndian(...)100%110%
File 2: TryReadUInt64LittleEndian(...)100%110%
File 2: TryReadUInt128LittleEndian(...)100%110%
File 2: TryReadUIntPtrLittleEndian(...)100%110%
File 3: ReverseEndianness(...)100%110%
File 3: ReverseEndianness(...)100%110%
File 3: ReverseEndianness(...)100%110%
File 3: ReverseEndianness(...)100%110%
File 3: ReverseEndianness(...)100%110%
File 3: ReverseEndianness(...)100%110%
File 3: ReverseEndianness(...)100%110%
File 3: ReverseEndianness(...)100%110%
File 3: ReverseEndianness(...)100%110%
File 3: ReverseEndianness(...)100%110%
File 3: ReverseEndianness(...)100%110%
File 3: ReverseEndianness(...)100%110%
File 3: ReverseEndianness(...)100%110%
File 3: ReverseEndianness(...)100%110%
File 3: ReverseEndianness(...)100%110%
File 3: ReverseEndianness(...)100%110%
File 3: ReverseEndianness(...)100%110%
File 3: ReverseEndianness(...)100%110%
File 3: ReverseEndianness(...)100%110%
File 3: ReverseEndianness(...)100%110%
File 3: Reverse(...)100%110%
File 3: Reverse(...)100%110%
File 3: Reverse(...)100%110%
File 3: Reverse(...)100%110%
File 3: Reverse(...)100%110%
File 3: Reverse(...)100%110%
File 3: Reverse(...)100%110%
File 3: Reverse(...)100%110%
File 3: Reverse(...)100%110%
File 3: ReverseEndianness(...)0%28280%
File 3: ReverseEndianness(...)100%110%
File 3: ReverseEndianness(...)0%10100%
File 3: ThrowDestinationTooSmall()100%110%
File 4: WriteDoubleBigEndian(...)100%110%
File 4: WriteHalfBigEndian(...)100%110%
File 4: WriteBFloat16BigEndian(...)100%110%
File 4: WriteInt16BigEndian(...)100%110%
File 4: WriteInt32BigEndian(...)100%110%
File 4: WriteInt64BigEndian(...)100%110%
File 4: WriteInt128BigEndian(...)100%110%
File 4: WriteIntPtrBigEndian(...)100%110%
File 4: WriteSingleBigEndian(...)100%110%
File 4: WriteUInt16BigEndian(...)100%110%
File 4: WriteUInt32BigEndian(...)100%110%
File 4: WriteUInt64BigEndian(...)100%110%
File 4: WriteUInt128BigEndian(...)100%110%
File 4: WriteUIntPtrBigEndian(...)100%110%
File 4: TryWriteDoubleBigEndian(...)100%110%
File 4: TryWriteHalfBigEndian(...)100%110%
File 4: TryWriteBFloat16BigEndian(...)100%110%
File 4: TryWriteInt16BigEndian(...)100%110%
File 4: TryWriteInt32BigEndian(...)100%110%
File 4: TryWriteInt64BigEndian(...)100%110%
File 4: TryWriteInt128BigEndian(...)100%110%
File 4: TryWriteIntPtrBigEndian(...)100%110%
File 4: TryWriteSingleBigEndian(...)100%110%
File 4: TryWriteUInt16BigEndian(...)100%110%
File 4: TryWriteUInt32BigEndian(...)100%110%
File 4: TryWriteUInt64BigEndian(...)100%110%
File 4: TryWriteUInt128BigEndian(...)100%110%
File 4: TryWriteUIntPtrBigEndian(...)100%110%
File 5: WriteDoubleLittleEndian(...)0%220%
File 5: WriteHalfLittleEndian(...)0%220%
File 5: WriteBFloat16LittleEndian(...)0%220%
File 5: WriteInt16LittleEndian(...)0%220%
File 5: WriteInt32LittleEndian(...)0%220%
File 5: WriteInt64LittleEndian(...)0%220%
File 5: WriteInt128LittleEndian(...)0%220%
File 5: WriteIntPtrLittleEndian(...)0%220%
File 5: WriteSingleLittleEndian(...)0%220%
File 5: WriteUInt16LittleEndian(...)0%220%
File 5: WriteUInt32LittleEndian(...)0%220%
File 5: WriteUInt64LittleEndian(...)0%220%
File 5: WriteUInt128LittleEndian(...)0%220%
File 5: WriteUIntPtrLittleEndian(...)0%220%
File 5: TryWriteDoubleLittleEndian(...)0%220%
File 5: TryWriteHalfLittleEndian(...)0%220%
File 5: TryWriteBFloat16LittleEndian(...)0%220%
File 5: TryWriteInt16LittleEndian(...)0%220%
File 5: TryWriteInt32LittleEndian(...)0%220%
File 5: TryWriteInt64LittleEndian(...)0%220%
File 5: TryWriteInt128LittleEndian(...)0%220%
File 5: TryWriteIntPtrLittleEndian(...)0%220%
File 5: TryWriteSingleLittleEndian(...)0%220%
File 5: TryWriteUInt16LittleEndian(...)0%220%
File 5: TryWriteUInt32LittleEndian(...)0%220%
File 5: TryWriteUInt64LittleEndian(...)0%220%
File 5: TryWriteUInt128LittleEndian(...)0%220%
File 5: TryWriteUIntPtrLittleEndian(...)0%220%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Private.CoreLib/src/System/Buffers/Binary/BinaryPrimitives.ReadBigEndian.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Numerics;
 5using System.Runtime.CompilerServices;
 6using System.Runtime.InteropServices;
 7
 8namespace System.Buffers.Binary
 9{
 10    public static partial class BinaryPrimitives
 11    {
 12        /// <summary>
 13        /// Reads a <see cref="double" /> from the beginning of a read-only span of bytes, as big endian.
 14        /// </summary>
 15        /// <param name="source">The read-only span to read.</param>
 16        /// <returns>The big endian value.</returns>
 17        /// <remarks>Reads exactly 8 bytes from the beginning of the span.</remarks>
 18        /// <exception cref="ArgumentOutOfRangeException">
 19        /// <paramref name="source"/> is too small to contain a <see cref="double" />.
 20        /// </exception>
 21        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 22        public static double ReadDoubleBigEndian(ReadOnlySpan<byte> source)
 23        {
 24            return BitConverter.IsLittleEndian ?
 25                BitConverter.Int64BitsToDouble(ReverseEndianness(MemoryMarshal.Read<long>(source))) :
 26                MemoryMarshal.Read<double>(source);
 27        }
 28
 29        /// <summary>
 30        /// Reads a <see cref="Half" /> from the beginning of a read-only span of bytes, as big endian.
 31        /// </summary>
 32        /// <param name="source">The read-only span to read.</param>
 33        /// <returns>The big endian value.</returns>
 34        /// <remarks>Reads exactly 2 bytes from the beginning of the span.</remarks>
 35        /// <exception cref="ArgumentOutOfRangeException">
 36        /// <paramref name="source"/> is too small to contain a <see cref="Half" />.
 37        /// </exception>
 38        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 39        public static Half ReadHalfBigEndian(ReadOnlySpan<byte> source)
 40        {
 41            return BitConverter.IsLittleEndian ?
 42                BitConverter.Int16BitsToHalf(ReverseEndianness(MemoryMarshal.Read<short>(source))) :
 43                MemoryMarshal.Read<Half>(source);
 44        }
 45
 46        /// <summary>
 47        /// Reads a <see cref="BFloat16" /> from the beginning of a read-only span of bytes, as big endian.
 48        /// </summary>
 49        /// <param name="source">The read-only span to read.</param>
 50        /// <returns>The big endian value.</returns>
 51        /// <remarks>Reads exactly 2 bytes from the beginning of the span.</remarks>
 52        /// <exception cref="ArgumentOutOfRangeException">
 53        /// <paramref name="source"/> is too small to contain a <see cref="BFloat16" />.
 54        /// </exception>
 55        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 56        public static BFloat16 ReadBFloat16BigEndian(ReadOnlySpan<byte> source)
 57        {
 58            return BitConverter.IsLittleEndian ?
 59                BitConverter.Int16BitsToBFloat16(ReverseEndianness(MemoryMarshal.Read<short>(source))) :
 60                MemoryMarshal.Read<BFloat16>(source);
 61        }
 62
 63        /// <summary>
 64        /// Reads a <see cref="short" /> from the beginning of a read-only span of bytes, as big endian.
 65        /// </summary>
 66        /// <param name="source">The read-only span to read.</param>
 67        /// <returns>The big endian value.</returns>
 68        /// <remarks>Reads exactly 2 bytes from the beginning of the span.</remarks>
 69        /// <exception cref="ArgumentOutOfRangeException">
 70        /// <paramref name="source"/> is too small to contain a <see cref="short" />.
 71        /// </exception>
 72        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 73        public static short ReadInt16BigEndian(ReadOnlySpan<byte> source)
 74        {
 75            return BitConverter.IsLittleEndian ?
 76                ReverseEndianness(MemoryMarshal.Read<short>(source)) :
 77                MemoryMarshal.Read<short>(source);
 78        }
 79
 80        /// <summary>
 81        /// Reads a <see cref="int" /> from the beginning of a read-only span of bytes, as big endian.
 82        /// </summary>
 83        /// <param name="source">The read-only span to read.</param>
 84        /// <returns>The big endian value.</returns>
 85        /// <remarks>Reads exactly 4 bytes from the beginning of the span.</remarks>
 86        /// <exception cref="ArgumentOutOfRangeException">
 87        /// <paramref name="source"/> is too small to contain a <see cref="int" />.
 88        /// </exception>
 89        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 90        public static int ReadInt32BigEndian(ReadOnlySpan<byte> source)
 91        {
 92            return BitConverter.IsLittleEndian ?
 93                ReverseEndianness(MemoryMarshal.Read<int>(source)) :
 94                MemoryMarshal.Read<int>(source);
 95        }
 96
 97        /// <summary>
 98        /// Reads a <see cref="long" /> from the beginning of a read-only span of bytes, as big endian.
 99        /// </summary>
 100        /// <param name="source">The read-only span to read.</param>
 101        /// <returns>The big endian value.</returns>
 102        /// <remarks>Reads exactly 8 bytes from the beginning of the span.</remarks>
 103        /// <exception cref="ArgumentOutOfRangeException">
 104        /// <paramref name="source"/> is too small to contain a <see cref="long" />.
 105        /// </exception>
 106        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 107        public static long ReadInt64BigEndian(ReadOnlySpan<byte> source)
 108        {
 109            return BitConverter.IsLittleEndian ?
 110                ReverseEndianness(MemoryMarshal.Read<long>(source)) :
 111                MemoryMarshal.Read<long>(source);
 112        }
 113
 114        /// <summary>
 115        /// Reads a <see cref="Int128" /> from the beginning of a read-only span of bytes, as big endian.
 116        /// </summary>
 117        /// <param name="source">The read-only span to read.</param>
 118        /// <returns>The big endian value.</returns>
 119        /// <remarks>Reads exactly 16 bytes from the beginning of the span.</remarks>
 120        /// <exception cref="ArgumentOutOfRangeException">
 121        /// <paramref name="source"/> is too small to contain a <see cref="Int128" />.
 122        /// </exception>
 123        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 124        public static Int128 ReadInt128BigEndian(ReadOnlySpan<byte> source)
 125        {
 126            return BitConverter.IsLittleEndian ?
 127                ReverseEndianness(MemoryMarshal.Read<Int128>(source)) :
 128                MemoryMarshal.Read<Int128>(source);
 129        }
 130
 131        /// <summary>
 132        /// Reads a <see cref="nint" /> from the beginning of a read-only span of bytes, as big endian.
 133        /// </summary>
 134        /// <param name="source">The read-only span to read.</param>
 135        /// <returns>The big endian value.</returns>
 136        /// <remarks>Reads exactly 4 bytes on 32-bit platforms -or- 8 bytes on 64-bit platforms from the beginning of th
 137        /// <exception cref="ArgumentOutOfRangeException">
 138        /// <paramref name="source"/> is too small to contain a <see cref="nint" />.
 139        /// </exception>
 140        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 141        public static nint ReadIntPtrBigEndian(ReadOnlySpan<byte> source)
 142        {
 143            return BitConverter.IsLittleEndian ?
 144                ReverseEndianness(MemoryMarshal.Read<nint>(source)) :
 145                MemoryMarshal.Read<nint>(source);
 146        }
 147
 148        /// <summary>
 149        /// Reads a <see cref="float" /> from the beginning of a read-only span of bytes, as big endian.
 150        /// </summary>
 151        /// <param name="source">The read-only span to read.</param>
 152        /// <returns>The big endian value.</returns>
 153        /// <remarks>Reads exactly 4 bytes from the beginning of the span.</remarks>
 154        /// <exception cref="ArgumentOutOfRangeException">
 155        /// <paramref name="source"/> is too small to contain a <see cref="float" />.
 156        /// </exception>
 157        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 158        public static float ReadSingleBigEndian(ReadOnlySpan<byte> source)
 159        {
 160            return BitConverter.IsLittleEndian ?
 161                BitConverter.Int32BitsToSingle(ReverseEndianness(MemoryMarshal.Read<int>(source))) :
 162                MemoryMarshal.Read<float>(source);
 163        }
 164
 165        /// <summary>
 166        /// Reads a <see cref="ushort" /> from the beginning of a read-only span of bytes, as big endian.
 167        /// </summary>
 168        /// <param name="source">The read-only span to read.</param>
 169        /// <returns>The big endian value.</returns>
 170        /// <remarks>Reads exactly 2 bytes from the beginning of the span.</remarks>
 171        /// <exception cref="ArgumentOutOfRangeException">
 172        /// <paramref name="source"/> is too small to contain a <see cref="ushort" />.
 173        /// </exception>
 174        [CLSCompliant(false)]
 175        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 176        public static ushort ReadUInt16BigEndian(ReadOnlySpan<byte> source)
 177        {
 178            return BitConverter.IsLittleEndian ?
 179                ReverseEndianness(MemoryMarshal.Read<ushort>(source)) :
 180                MemoryMarshal.Read<ushort>(source);
 181        }
 182
 183        /// <summary>
 184        /// Reads a <see cref="uint" /> from the beginning of a read-only span of bytes, as big endian.
 185        /// </summary>
 186        /// <param name="source">The read-only span to read.</param>
 187        /// <returns>The big endian value.</returns>
 188        /// <remarks>Reads exactly 4 bytes from the beginning of the span.</remarks>
 189        /// <exception cref="ArgumentOutOfRangeException">
 190        /// <paramref name="source"/> is too small to contain a <see cref="uint" />.
 191        /// </exception>
 192        [CLSCompliant(false)]
 193        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 194        public static uint ReadUInt32BigEndian(ReadOnlySpan<byte> source)
 195        {
 196            return BitConverter.IsLittleEndian ?
 197                ReverseEndianness(MemoryMarshal.Read<uint>(source)) :
 198                MemoryMarshal.Read<uint>(source);
 199        }
 200
 201        /// <summary>
 202        /// Reads a <see cref="ulong" /> from the beginning of a read-only span of bytes, as big endian.
 203        /// </summary>
 204        /// <param name="source">The read-only span to read.</param>
 205        /// <returns>The big endian value.</returns>
 206        /// <remarks>Reads exactly 8 bytes from the beginning of the span.</remarks>
 207        /// <exception cref="ArgumentOutOfRangeException">
 208        /// <paramref name="source"/> is too small to contain a <see cref="ulong" />.
 209        /// </exception>
 210        [CLSCompliant(false)]
 211        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 212        public static ulong ReadUInt64BigEndian(ReadOnlySpan<byte> source)
 213        {
 214            return BitConverter.IsLittleEndian ?
 215                ReverseEndianness(MemoryMarshal.Read<ulong>(source)) :
 216                MemoryMarshal.Read<ulong>(source);
 217        }
 218
 219        /// <summary>
 220        /// Reads a <see cref="UInt128" /> from the beginning of a read-only span of bytes, as big endian.
 221        /// </summary>
 222        /// <param name="source">The read-only span to read.</param>
 223        /// <returns>The big endian value.</returns>
 224        /// <remarks>Reads exactly 16 bytes from the beginning of the span.</remarks>
 225        /// <exception cref="ArgumentOutOfRangeException">
 226        /// <paramref name="source"/> is too small to contain a <see cref="UInt128" />.
 227        /// </exception>
 228        [CLSCompliant(false)]
 229        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 230        public static UInt128 ReadUInt128BigEndian(ReadOnlySpan<byte> source)
 231        {
 232            return BitConverter.IsLittleEndian ?
 233                ReverseEndianness(MemoryMarshal.Read<UInt128>(source)) :
 234                MemoryMarshal.Read<UInt128>(source);
 235        }
 236
 237        /// <summary>
 238        /// Reads a <see cref="nuint" /> from the beginning of a read-only span of bytes, as big endian.
 239        /// </summary>
 240        /// <param name="source">The read-only span to read.</param>
 241        /// <returns>The big endian value.</returns>
 242        /// <remarks>Reads exactly 4 bytes on 32-bit platforms -or- 8 bytes on 64-bit platforms from the beginning of th
 243        /// <exception cref="ArgumentOutOfRangeException">
 244        /// <paramref name="source"/> is too small to contain a <see cref="nuint" />.
 245        /// </exception>
 246        [CLSCompliant(false)]
 247        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 248        public static nuint ReadUIntPtrBigEndian(ReadOnlySpan<byte> source)
 249        {
 250            return BitConverter.IsLittleEndian ?
 251                ReverseEndianness(MemoryMarshal.Read<nuint>(source)) :
 252                MemoryMarshal.Read<nuint>(source);
 253        }
 254
 255        /// <summary>
 256        /// Reads a <see cref="double" /> from the beginning of a read-only span of bytes, as big endian.
 257        /// </summary>
 258        /// <param name="source">The read-only span of bytes to read.</param>
 259        /// <param name="value">When this method returns, contains the value read out of the read-only span of bytes, as
 260        /// <returns>
 261        /// <see langword="true" /> if the span is large enough to contain a <see cref="double" />; otherwise, <see lang
 262        /// </returns>
 263        /// <remarks>Reads exactly 8 bytes from the beginning of the span.</remarks>
 264        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 265        public static bool TryReadDoubleBigEndian(ReadOnlySpan<byte> source, out double value)
 266        {
 267            if (BitConverter.IsLittleEndian)
 268            {
 0269                bool success = MemoryMarshal.TryRead(source, out long tmp);
 0270                value = BitConverter.Int64BitsToDouble(ReverseEndianness(tmp));
 0271                return success;
 272            }
 273
 274            return MemoryMarshal.TryRead(source, out value);
 275        }
 276
 277        /// <summary>
 278        /// Reads a <see cref="Half" /> from the beginning of a read-only span of bytes, as big endian.
 279        /// </summary>
 280        /// <param name="source">The read-only span of bytes to read.</param>
 281        /// <param name="value">When this method returns, contains the value read out of the read-only span of bytes, as
 282        /// <returns>
 283        /// <see langword="true" /> if the span is large enough to contain a <see cref="Half" />; otherwise, <see langwo
 284        /// </returns>
 285        /// <remarks>Reads exactly 2 bytes from the beginning of the span.</remarks>
 286        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 287        public static bool TryReadHalfBigEndian(ReadOnlySpan<byte> source, out Half value)
 288        {
 289            if (BitConverter.IsLittleEndian)
 290            {
 0291                bool success = MemoryMarshal.TryRead(source, out short tmp);
 0292                value = BitConverter.Int16BitsToHalf(ReverseEndianness(tmp));
 0293                return success;
 294            }
 295
 296            return MemoryMarshal.TryRead(source, out value);
 297        }
 298
 299        /// <summary>
 300        /// Reads a <see cref="BFloat16" /> from the beginning of a read-only span of bytes, as big endian.
 301        /// </summary>
 302        /// <param name="source">The read-only span of bytes to read.</param>
 303        /// <param name="value">When this method returns, contains the value read out of the read-only span of bytes, as
 304        /// <returns>
 305        /// <see langword="true" /> if the span is large enough to contain a <see cref="BFloat16" />; otherwise, <see la
 306        /// </returns>
 307        /// <remarks>Reads exactly 2 bytes from the beginning of the span.</remarks>
 308        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 309        public static bool TryReadBFloat16BigEndian(ReadOnlySpan<byte> source, out BFloat16 value)
 310        {
 311            if (BitConverter.IsLittleEndian)
 312            {
 0313                bool success = MemoryMarshal.TryRead(source, out short tmp);
 0314                value = BitConverter.Int16BitsToBFloat16(ReverseEndianness(tmp));
 0315                return success;
 316            }
 317
 318            return MemoryMarshal.TryRead(source, out value);
 319        }
 320
 321        /// <summary>
 322        /// Reads a <see cref="short" /> from the beginning of a read-only span of bytes, as big endian.
 323        /// </summary>
 324        /// <param name="source">The read-only span of bytes to read.</param>
 325        /// <param name="value">When this method returns, contains the value read out of the read-only span of bytes, as
 326        /// <returns>
 327        /// <see langword="true" /> if the span is large enough to contain a <see cref="short" />; otherwise, <see langw
 328        /// </returns>
 329        /// <remarks>Reads exactly 2 bytes from the beginning of the span.</remarks>
 330        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 331        public static bool TryReadInt16BigEndian(ReadOnlySpan<byte> source, out short value)
 332        {
 333            if (BitConverter.IsLittleEndian)
 334            {
 0335                bool success = MemoryMarshal.TryRead(source, out short tmp);
 0336                value = ReverseEndianness(tmp);
 0337                return success;
 338            }
 339
 340            return MemoryMarshal.TryRead(source, out value);
 341        }
 342
 343        /// <summary>
 344        /// Reads a <see cref="int" /> from the beginning of a read-only span of bytes, as big endian.
 345        /// </summary>
 346        /// <param name="source">The read-only span of bytes to read.</param>
 347        /// <param name="value">When this method returns, contains the value read out of the read-only span of bytes, as
 348        /// <returns>
 349        /// <see langword="true" /> if the span is large enough to contain a <see cref="int" />; otherwise, <see langwor
 350        /// </returns>
 351        /// <remarks>Reads exactly 4 bytes from the beginning of the span.</remarks>
 352        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 353        public static bool TryReadInt32BigEndian(ReadOnlySpan<byte> source, out int value)
 354        {
 355            if (BitConverter.IsLittleEndian)
 356            {
 0357                bool success = MemoryMarshal.TryRead(source, out int tmp);
 0358                value = ReverseEndianness(tmp);
 0359                return success;
 360            }
 361
 362            return MemoryMarshal.TryRead(source, out value);
 363        }
 364
 365        /// <summary>
 366        /// Reads a <see cref="long" /> from the beginning of a read-only span of bytes, as big endian.
 367        /// </summary>
 368        /// <param name="source">The read-only span of bytes to read.</param>
 369        /// <param name="value">When this method returns, contains the value read out of the read-only span of bytes, as
 370        /// <returns>
 371        /// <see langword="true" /> if the span is large enough to contain a <see cref="long" />; otherwise, <see langwo
 372        /// </returns>
 373        /// <remarks>Reads exactly 8 bytes from the beginning of the span.</remarks>
 374        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 375        public static bool TryReadInt64BigEndian(ReadOnlySpan<byte> source, out long value)
 376        {
 377            if (BitConverter.IsLittleEndian)
 378            {
 0379                bool success = MemoryMarshal.TryRead(source, out long tmp);
 0380                value = ReverseEndianness(tmp);
 0381                return success;
 382            }
 383
 384            return MemoryMarshal.TryRead(source, out value);
 385        }
 386
 387        /// <summary>
 388        /// Reads a <see cref="Int128" /> from the beginning of a read-only span of bytes, as big endian.
 389        /// </summary>
 390        /// <param name="source">The read-only span of bytes to read.</param>
 391        /// <param name="value">When this method returns, contains the value read out of the read-only span of bytes, as
 392        /// <returns>
 393        /// <see langword="true" /> if the span is large enough to contain a <see cref="Int128" />; otherwise, <see lang
 394        /// </returns>
 395        /// <remarks>Reads exactly 16 bytes from the beginning of the span.</remarks>
 396        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 397        public static bool TryReadInt128BigEndian(ReadOnlySpan<byte> source, out Int128 value)
 398        {
 399            if (BitConverter.IsLittleEndian)
 400            {
 0401                bool success = MemoryMarshal.TryRead(source, out Int128 tmp);
 0402                value = ReverseEndianness(tmp);
 0403                return success;
 404            }
 405
 406            return MemoryMarshal.TryRead(source, out value);
 407        }
 408
 409        /// <summary>
 410        /// Reads a <see cref="nint" /> from the beginning of a read-only span of bytes, as big endian.
 411        /// </summary>
 412        /// <param name="source">The read-only span of bytes to read.</param>
 413        /// <param name="value">When this method returns, contains the value read out of the read-only span of bytes, as
 414        /// <returns>
 415        /// <see langword="true" /> if the span is large enough to contain a <see cref="nint" />; otherwise, <see langwo
 416        /// </returns>
 417        /// <remarks>Reads exactly 4 bytes on 32-bit platforms -or- 8 bytes on 64-bit platforms from the beginning of th
 418        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 419        public static bool TryReadIntPtrBigEndian(ReadOnlySpan<byte> source, out nint value)
 420        {
 421            if (BitConverter.IsLittleEndian)
 422            {
 0423                bool success = MemoryMarshal.TryRead(source, out nint tmp);
 0424                value = ReverseEndianness(tmp);
 0425                return success;
 426            }
 427
 428            return MemoryMarshal.TryRead(source, out value);
 429        }
 430
 431        /// <summary>
 432        /// Reads a <see cref="float" /> from the beginning of a read-only span of bytes, as big endian.
 433        /// </summary>
 434        /// <param name="source">The read-only span of bytes to read.</param>
 435        /// <param name="value">When this method returns, contains the value read out of the read-only span of bytes, as
 436        /// <returns>
 437        /// <see langword="true" /> if the span is large enough to contain a <see cref="float" />; otherwise, <see langw
 438        /// </returns>
 439        /// <remarks>Reads exactly 4 bytes from the beginning of the span.</remarks>
 440        public static bool TryReadSingleBigEndian(ReadOnlySpan<byte> source, out float value)
 441        {
 442            if (BitConverter.IsLittleEndian)
 443            {
 0444                bool success = MemoryMarshal.TryRead(source, out int tmp);
 0445                value = BitConverter.Int32BitsToSingle(ReverseEndianness(tmp));
 0446                return success;
 447            }
 448
 449            return MemoryMarshal.TryRead(source, out value);
 450        }
 451
 452        /// <summary>
 453        /// Reads a <see cref="ushort" /> from the beginning of a read-only span of bytes, as big endian.
 454        /// </summary>
 455        /// <param name="source">The read-only span of bytes to read.</param>
 456        /// <param name="value">When this method returns, contains the value read out of the read-only span of bytes, as
 457        /// <returns>
 458        /// <see langword="true" /> if the span is large enough to contain a <see cref="ushort" />; otherwise, <see lang
 459        /// </returns>
 460        /// <remarks>Reads exactly 2 bytes from the beginning of the span.</remarks>
 461        [CLSCompliant(false)]
 462        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 463        public static bool TryReadUInt16BigEndian(ReadOnlySpan<byte> source, out ushort value)
 464        {
 465            if (BitConverter.IsLittleEndian)
 466            {
 0467                bool success = MemoryMarshal.TryRead(source, out ushort tmp);
 0468                value = ReverseEndianness(tmp);
 0469                return success;
 470            }
 471
 472            return MemoryMarshal.TryRead(source, out value);
 473        }
 474
 475        /// <summary>
 476        /// Reads a <see cref="uint" /> from the beginning of a read-only span of bytes, as big endian.
 477        /// </summary>
 478        /// <param name="source">The read-only span of bytes to read.</param>
 479        /// <param name="value">When this method returns, contains the value read out of the read-only span of bytes, as
 480        /// <returns>
 481        /// <see langword="true" /> if the span is large enough to contain a <see cref="uint" />; otherwise, <see langwo
 482        /// </returns>
 483        /// <remarks>Reads exactly 4 bytes from the beginning of the span.</remarks>
 484        [CLSCompliant(false)]
 485        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 486        public static bool TryReadUInt32BigEndian(ReadOnlySpan<byte> source, out uint value)
 487        {
 488            if (BitConverter.IsLittleEndian)
 489            {
 0490                bool success = MemoryMarshal.TryRead(source, out uint tmp);
 0491                value = ReverseEndianness(tmp);
 0492                return success;
 493            }
 494
 495            return MemoryMarshal.TryRead(source, out value);
 496        }
 497
 498        /// <summary>
 499        /// Reads a <see cref="ulong" /> from the beginning of a read-only span of bytes, as big endian.
 500        /// </summary>
 501        /// <param name="source">The read-only span of bytes to read.</param>
 502        /// <param name="value">When this method returns, contains the value read out of the read-only span of bytes, as
 503        /// <returns>
 504        /// <see langword="true" /> if the span is large enough to contain a <see cref="ulong" />; otherwise, <see langw
 505        /// </returns>
 506        /// <remarks>Reads exactly 8 bytes from the beginning of the span.</remarks>
 507        [CLSCompliant(false)]
 508        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 509        public static bool TryReadUInt64BigEndian(ReadOnlySpan<byte> source, out ulong value)
 510        {
 511            if (BitConverter.IsLittleEndian)
 512            {
 0513                bool success = MemoryMarshal.TryRead(source, out ulong tmp);
 0514                value = ReverseEndianness(tmp);
 0515                return success;
 516            }
 517
 518            return MemoryMarshal.TryRead(source, out value);
 519        }
 520
 521        /// <summary>
 522        /// Reads a <see cref="UInt128" /> from the beginning of a read-only span of bytes, as big endian.
 523        /// </summary>
 524        /// <param name="source">The read-only span of bytes to read.</param>
 525        /// <param name="value">When this method returns, contains the value read out of the read-only span of bytes, as
 526        /// <returns>
 527        /// <see langword="true" /> if the span is large enough to contain a <see cref="UInt128" />; otherwise, <see lan
 528        /// </returns>
 529        /// <remarks>Reads exactly 16 bytes from the beginning of the span.</remarks>
 530        [CLSCompliant(false)]
 531        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 532        public static bool TryReadUInt128BigEndian(ReadOnlySpan<byte> source, out UInt128 value)
 533        {
 534            if (BitConverter.IsLittleEndian)
 535            {
 0536                bool success = MemoryMarshal.TryRead(source, out UInt128 tmp);
 0537                value = ReverseEndianness(tmp);
 0538                return success;
 539            }
 540
 541            return MemoryMarshal.TryRead(source, out value);
 542        }
 543
 544        /// <summary>
 545        /// Reads a <see cref="nuint" /> from the beginning of a read-only span of bytes, as big endian.
 546        /// </summary>
 547
 548        /// <param name="source">The read-only span of bytes to read.</param>
 549        /// <param name="value">When this method returns, contains the value read out of the read-only span of bytes, as
 550        /// <returns>
 551        /// <see langword="true" /> if the span is large enough to contain a <see cref="nuint" />; otherwise, <see langw
 552        /// </returns>
 553        /// <remarks>Reads exactly 4 bytes on 32-bit platforms -or- 8 bytes on 64-bit platforms from the beginning of th
 554        [CLSCompliant(false)]
 555        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 556        public static bool TryReadUIntPtrBigEndian(ReadOnlySpan<byte> source, out nuint value)
 557        {
 558            if (BitConverter.IsLittleEndian)
 559            {
 0560                bool success = MemoryMarshal.TryRead(source, out nuint tmp);
 0561                value = ReverseEndianness(tmp);
 0562                return success;
 563            }
 564
 565            return MemoryMarshal.TryRead(source, out value);
 566        }
 567    }
 568}
 569

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Private.CoreLib/src/System/Buffers/Binary/BinaryPrimitives.ReadLittleEndian.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Numerics;
 5using System.Runtime.CompilerServices;
 6using System.Runtime.InteropServices;
 7
 8namespace System.Buffers.Binary
 9{
 10    public static partial class BinaryPrimitives
 11    {
 12        /// <summary>
 13        /// Reads a <see cref="double" /> from the beginning of a read-only span of bytes, as little endian.
 14        /// </summary>
 15        /// <param name="source">The read-only span to read.</param>
 16        /// <returns>The little endian value.</returns>
 17        /// <remarks>Reads exactly 8 bytes from the beginning of the span.</remarks>
 18        /// <exception cref="ArgumentOutOfRangeException">
 19        /// <paramref name="source"/> is too small to contain a <see cref="double" />.
 20        /// </exception>
 21        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 22        public static double ReadDoubleLittleEndian(ReadOnlySpan<byte> source)
 23        {
 24            return !BitConverter.IsLittleEndian ?
 25                BitConverter.Int64BitsToDouble(ReverseEndianness(MemoryMarshal.Read<long>(source))) :
 26                MemoryMarshal.Read<double>(source);
 27        }
 28
 29        /// <summary>
 30        /// Reads a <see cref="Half" /> from the beginning of a read-only span of bytes, as little endian.
 31        /// </summary>
 32        /// <param name="source">The read-only span to read.</param>
 33        /// <returns>The little endian value.</returns>
 34        /// <remarks>Reads exactly 2 bytes from the beginning of the span.</remarks>
 35        /// <exception cref="ArgumentOutOfRangeException">
 36        /// <paramref name="source"/> is too small to contain a <see cref="Half" />.
 37        /// </exception>
 38        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 39        public static Half ReadHalfLittleEndian(ReadOnlySpan<byte> source)
 40        {
 41            return !BitConverter.IsLittleEndian ?
 42                BitConverter.Int16BitsToHalf(ReverseEndianness(MemoryMarshal.Read<short>(source))) :
 43                MemoryMarshal.Read<Half>(source);
 44        }
 45
 46        /// <summary>
 47        /// Reads a <see cref="BFloat16" /> from the beginning of a read-only span of bytes, as little endian.
 48        /// </summary>
 49        /// <param name="source">The read-only span to read.</param>
 50        /// <returns>The little endian value.</returns>
 51        /// <remarks>Reads exactly 2 bytes from the beginning of the span.</remarks>
 52        /// <exception cref="ArgumentOutOfRangeException">
 53        /// <paramref name="source"/> is too small to contain a <see cref="Half" />.
 54        /// </exception>
 55        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 56        public static BFloat16 ReadBFloat16LittleEndian(ReadOnlySpan<byte> source)
 57        {
 58            return !BitConverter.IsLittleEndian ?
 59                BitConverter.Int16BitsToBFloat16(ReverseEndianness(MemoryMarshal.Read<short>(source))) :
 60                MemoryMarshal.Read<BFloat16>(source);
 61        }
 62
 63        /// <summary>
 64        /// Reads a <see cref="short" /> from the beginning of a read-only span of bytes, as little endian.
 65        /// </summary>
 66        /// <param name="source">The read-only span to read.</param>
 67        /// <returns>The little endian value.</returns>
 68        /// <remarks>Reads exactly 2 bytes from the beginning of the span.</remarks>
 69        /// <exception cref="ArgumentOutOfRangeException">
 70        /// <paramref name="source"/> is too small to contain a <see cref="short" />.
 71        /// </exception>
 72        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 73        public static short ReadInt16LittleEndian(ReadOnlySpan<byte> source)
 74        {
 75            return !BitConverter.IsLittleEndian ?
 76                ReverseEndianness(MemoryMarshal.Read<short>(source)) :
 77                MemoryMarshal.Read<short>(source);
 78        }
 79
 80        /// <summary>
 81        /// Reads a <see cref="int" /> from the beginning of a read-only span of bytes, as little endian.
 82        /// </summary>
 83        /// <param name="source">The read-only span to read.</param>
 84        /// <returns>The little endian value.</returns>
 85        /// <remarks>Reads exactly 4 bytes from the beginning of the span.</remarks>
 86        /// <exception cref="ArgumentOutOfRangeException">
 87        /// <paramref name="source"/> is too small to contain a <see cref="int" />.
 88        /// </exception>
 89        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 90        public static int ReadInt32LittleEndian(ReadOnlySpan<byte> source)
 91        {
 92            return !BitConverter.IsLittleEndian ?
 93                ReverseEndianness(MemoryMarshal.Read<int>(source)) :
 94                MemoryMarshal.Read<int>(source);
 95        }
 96
 97        /// <summary>
 98        /// Reads a <see cref="long" /> from the beginning of a read-only span of bytes, as little endian.
 99        /// </summary>
 100        /// <param name="source">The read-only span to read.</param>
 101        /// <returns>The little endian value.</returns>
 102        /// <remarks>Reads exactly 8 bytes from the beginning of the span.</remarks>
 103        /// <exception cref="ArgumentOutOfRangeException">
 104        /// <paramref name="source"/> is too small to contain a <see cref="long" />.
 105        /// </exception>
 106        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 107        public static long ReadInt64LittleEndian(ReadOnlySpan<byte> source)
 108        {
 109            return !BitConverter.IsLittleEndian ?
 110                ReverseEndianness(MemoryMarshal.Read<long>(source)) :
 111                MemoryMarshal.Read<long>(source);
 112        }
 113
 114        /// <summary>
 115        /// Reads a <see cref="Int128" /> from the beginning of a read-only span of bytes, as little endian.
 116        /// </summary>
 117        /// <param name="source">The read-only span to read.</param>
 118        /// <returns>The little endian value.</returns>
 119        /// <remarks>Reads exactly 16 bytes from the beginning of the span.</remarks>
 120        /// <exception cref="ArgumentOutOfRangeException">
 121        /// <paramref name="source"/> is too small to contain a <see cref="Int128" />.
 122        /// </exception>
 123        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 124        public static Int128 ReadInt128LittleEndian(ReadOnlySpan<byte> source)
 125        {
 126            return !BitConverter.IsLittleEndian ?
 127                ReverseEndianness(MemoryMarshal.Read<Int128>(source)) :
 128                MemoryMarshal.Read<Int128>(source);
 129        }
 130
 131        /// <summary>
 132        /// Reads a <see cref="nint" /> from the beginning of a read-only span of bytes, as little endian.
 133        /// </summary>
 134        /// <param name="source">The read-only span to read.</param>
 135        /// <returns>The little endian value.</returns>
 136        /// <remarks>Reads exactly 4 bytes on 32-bit platforms -or- 8 bytes on 64-bit platforms from the beginning of th
 137        /// <exception cref="ArgumentOutOfRangeException">
 138        /// <paramref name="source"/> is too small to contain a <see cref="nint" />.
 139        /// </exception>
 140        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 141        public static nint ReadIntPtrLittleEndian(ReadOnlySpan<byte> source)
 142        {
 143            return !BitConverter.IsLittleEndian ?
 144                ReverseEndianness(MemoryMarshal.Read<nint>(source)) :
 145                MemoryMarshal.Read<nint>(source);
 146        }
 147
 148        /// <summary>
 149        /// Reads a <see cref="float" /> from the beginning of a read-only span of bytes, as little endian.
 150        /// </summary>
 151        /// <param name="source">The read-only span to read.</param>
 152        /// <returns>The little endian value.</returns>
 153        /// <remarks>Reads exactly 4 bytes from the beginning of the span.</remarks>
 154        /// <exception cref="ArgumentOutOfRangeException">
 155        /// <paramref name="source"/> is too small to contain a <see cref="float" />.
 156        /// </exception>
 157        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 158        public static float ReadSingleLittleEndian(ReadOnlySpan<byte> source)
 159        {
 160            return !BitConverter.IsLittleEndian ?
 161                BitConverter.Int32BitsToSingle(ReverseEndianness(MemoryMarshal.Read<int>(source))) :
 162                MemoryMarshal.Read<float>(source);
 163        }
 164
 165        /// <summary>
 166        /// Reads a <see cref="ushort" /> from the beginning of a read-only span of bytes, as little endian.
 167        /// </summary>
 168        /// <param name="source">The read-only span to read.</param>
 169        /// <returns>The little endian value.</returns>
 170        /// <remarks>Reads exactly 2 bytes from the beginning of the span.</remarks>
 171        /// <exception cref="ArgumentOutOfRangeException">
 172        /// <paramref name="source"/> is too small to contain a <see cref="ushort" />.
 173        /// </exception>
 174        [CLSCompliant(false)]
 175        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 176        public static ushort ReadUInt16LittleEndian(ReadOnlySpan<byte> source)
 177        {
 178            return !BitConverter.IsLittleEndian ?
 179                ReverseEndianness(MemoryMarshal.Read<ushort>(source)) :
 180                MemoryMarshal.Read<ushort>(source);
 181        }
 182
 183        /// <summary>
 184        /// Reads a <see cref="uint" /> from the beginning of a read-only span of bytes, as little endian.
 185        /// </summary>
 186        /// <param name="source">The read-only span to read.</param>
 187        /// <returns>The little endian value.</returns>
 188        /// <remarks>Reads exactly 4 bytes from the beginning of the span.</remarks>
 189        /// <exception cref="ArgumentOutOfRangeException">
 190        /// <paramref name="source"/> is too small to contain a <see cref="uint" />.
 191        /// </exception>
 192        [CLSCompliant(false)]
 193        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 194        public static uint ReadUInt32LittleEndian(ReadOnlySpan<byte> source)
 195        {
 196            return !BitConverter.IsLittleEndian ?
 197                ReverseEndianness(MemoryMarshal.Read<uint>(source)) :
 198                MemoryMarshal.Read<uint>(source);
 199        }
 200
 201        /// <summary>
 202        /// Reads a <see cref="ulong" /> from the beginning of a read-only span of bytes, as little endian.
 203        /// </summary>
 204        /// <param name="source">The read-only span to read.</param>
 205        /// <returns>The little endian value.</returns>
 206        /// <remarks>Reads exactly 8 bytes from the beginning of the span.</remarks>
 207        /// <exception cref="ArgumentOutOfRangeException">
 208        /// <paramref name="source"/> is too small to contain a <see cref="ulong" />.
 209        /// </exception>
 210        [CLSCompliant(false)]
 211        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 212        public static ulong ReadUInt64LittleEndian(ReadOnlySpan<byte> source)
 213        {
 214            return !BitConverter.IsLittleEndian ?
 215                ReverseEndianness(MemoryMarshal.Read<ulong>(source)) :
 216                MemoryMarshal.Read<ulong>(source);
 217        }
 218
 219        /// <summary>
 220        /// Reads a <see cref="UInt128" /> from the beginning of a read-only span of bytes, as little endian.
 221        /// </summary>
 222        /// <param name="source">The read-only span to read.</param>
 223        /// <returns>The little endian value.</returns>
 224        /// <remarks>Reads exactly 16 bytes from the beginning of the span.</remarks>
 225        /// <exception cref="ArgumentOutOfRangeException">
 226        /// <paramref name="source"/> is too small to contain a <see cref="UInt128" />.
 227        /// </exception>
 228        [CLSCompliant(false)]
 229        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 230        public static UInt128 ReadUInt128LittleEndian(ReadOnlySpan<byte> source)
 231        {
 232            return !BitConverter.IsLittleEndian ?
 233                ReverseEndianness(MemoryMarshal.Read<UInt128>(source)) :
 234                MemoryMarshal.Read<UInt128>(source);
 235        }
 236
 237        /// <summary>
 238        /// Reads a <see cref="nuint" /> from the beginning of a read-only span of bytes, as little endian.
 239        /// </summary>
 240        /// <param name="source">The read-only span to read.</param>
 241        /// <returns>The little endian value.</returns>
 242        /// <remarks>Reads exactly 4 bytes on 32-bit platforms or 8 bytes on 64-bit platforms from the beginning of the 
 243        /// <exception cref="ArgumentOutOfRangeException">
 244        /// <paramref name="source"/> is too small to contain a <see cref="nuint" />.
 245        /// </exception>
 246        [CLSCompliant(false)]
 247        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 248        public static nuint ReadUIntPtrLittleEndian(ReadOnlySpan<byte> source)
 249        {
 250            return !BitConverter.IsLittleEndian ?
 251                ReverseEndianness(MemoryMarshal.Read<nuint>(source)) :
 252                MemoryMarshal.Read<nuint>(source);
 253        }
 254
 255        /// <summary>
 256        /// Reads a <see cref="double" /> from the beginning of a read-only span of bytes, as little endian.
 257        /// </summary>
 258        /// <param name="source">The read-only span of bytes to read.</param>
 259        /// <param name="value">When this method returns, contains the value read out of the read-only span of bytes, as
 260        /// <returns>
 261        /// <see langword="true" /> if the span is large enough to contain a <see cref="double" />; otherwise, <see lang
 262        /// </returns>
 263        /// <remarks>Reads exactly 8 bytes from the beginning of the span.</remarks>
 264        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 265        public static bool TryReadDoubleLittleEndian(ReadOnlySpan<byte> source, out double value)
 266        {
 267            if (!BitConverter.IsLittleEndian)
 268            {
 269                bool success = MemoryMarshal.TryRead(source, out long tmp);
 270                value = BitConverter.Int64BitsToDouble(ReverseEndianness(tmp));
 271                return success;
 272            }
 273
 0274            return MemoryMarshal.TryRead(source, out value);
 275        }
 276
 277        /// <summary>
 278        /// Reads a <see cref="Half" /> from the beginning of a read-only span of bytes, as little endian.
 279        /// </summary>
 280        /// <param name="source">The read-only span of bytes to read.</param>
 281        /// <param name="value">When this method returns, contains the value read out of the read-only span of bytes, as
 282        /// <returns>
 283        /// <see langword="true" /> if the span is large enough to contain a <see cref="Half" />; otherwise, <see langwo
 284        /// </returns>
 285        /// <remarks>Reads exactly 2 bytes from the beginning of the span.</remarks>
 286        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 287        public static bool TryReadHalfLittleEndian(ReadOnlySpan<byte> source, out Half value)
 288        {
 289            if (!BitConverter.IsLittleEndian)
 290            {
 291                bool success = MemoryMarshal.TryRead(source, out short tmp);
 292                value = BitConverter.Int16BitsToHalf(ReverseEndianness(tmp));
 293                return success;
 294            }
 295
 0296            return MemoryMarshal.TryRead(source, out value);
 297        }
 298
 299        /// <summary>
 300        /// Reads a <see cref="BFloat16" /> from the beginning of a read-only span of bytes, as little endian.
 301        /// </summary>
 302        /// <param name="source">The read-only span of bytes to read.</param>
 303        /// <param name="value">When this method returns, contains the value read out of the read-only span of bytes, as
 304        /// <returns>
 305        /// <see langword="true" /> if the span is large enough to contain a <see cref="BFloat16" />; otherwise, <see la
 306        /// </returns>
 307        /// <remarks>Reads exactly 2 bytes from the beginning of the span.</remarks>
 308        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 309        public static bool TryReadBFloat16LittleEndian(ReadOnlySpan<byte> source, out BFloat16 value)
 310        {
 311            if (!BitConverter.IsLittleEndian)
 312            {
 313                bool success = MemoryMarshal.TryRead(source, out short tmp);
 314                value = BitConverter.Int16BitsToBFloat16(ReverseEndianness(tmp));
 315                return success;
 316            }
 317
 0318            return MemoryMarshal.TryRead(source, out value);
 319        }
 320
 321        /// <summary>
 322        /// Reads a <see cref="short" /> from the beginning of a read-only span of bytes, as little endian.
 323        /// </summary>
 324        /// <param name="source">The read-only span of bytes to read.</param>
 325        /// <param name="value">When this method returns, contains the value read out of the read-only span of bytes, as
 326        /// <returns>
 327        /// <see langword="true" /> if the span is large enough to contain a <see cref="short" />; otherwise, <see langw
 328        /// </returns>
 329        /// <remarks>Reads exactly 2 bytes from the beginning of the span.</remarks>
 330        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 331        public static bool TryReadInt16LittleEndian(ReadOnlySpan<byte> source, out short value)
 332        {
 333            if (BitConverter.IsLittleEndian)
 334            {
 0335                return MemoryMarshal.TryRead(source, out value);
 336            }
 337
 338            bool success = MemoryMarshal.TryRead(source, out short tmp);
 339            value = ReverseEndianness(tmp);
 340            return success;
 341        }
 342
 343        /// <summary>
 344        /// Reads a <see cref="int" /> from the beginning of a read-only span of bytes, as little endian.
 345        /// </summary>
 346        /// <param name="source">The read-only span of bytes to read.</param>
 347        /// <param name="value">When this method returns, contains the value read out of the read-only span of bytes, as
 348        /// <returns>
 349        /// <see langword="true" /> if the span is large enough to contain a <see cref="int" />; otherwise, <see langwor
 350        /// </returns>
 351        /// <remarks>Reads exactly 4 bytes from the beginning of the span.</remarks>
 352        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 353        public static bool TryReadInt32LittleEndian(ReadOnlySpan<byte> source, out int value)
 354        {
 355            if (BitConverter.IsLittleEndian)
 356            {
 0357                return MemoryMarshal.TryRead(source, out value);
 358            }
 359
 360            bool success = MemoryMarshal.TryRead(source, out int tmp);
 361            value = ReverseEndianness(tmp);
 362            return success;
 363        }
 364
 365        /// <summary>
 366        /// Reads a <see cref="long" /> from the beginning of a read-only span of bytes, as little endian.
 367        /// </summary>
 368        /// <param name="source">The read-only span of bytes to read.</param>
 369        /// <param name="value">When this method returns, contains the value read out of the read-only span of bytes, as
 370        /// <returns>
 371        /// <see langword="true" /> if the span is large enough to contain a <see cref="long" />; otherwise, <see langwo
 372        /// </returns>
 373        /// <remarks>Reads exactly 8 bytes from the beginning of the span.</remarks>
 374        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 375        public static bool TryReadInt64LittleEndian(ReadOnlySpan<byte> source, out long value)
 376        {
 377            if (BitConverter.IsLittleEndian)
 378            {
 0379                return MemoryMarshal.TryRead(source, out value);
 380            }
 381
 382            bool success = MemoryMarshal.TryRead(source, out long tmp);
 383            value = ReverseEndianness(tmp);
 384            return success;
 385        }
 386
 387        /// <summary>
 388        /// Reads a <see cref="Int128" /> from the beginning of a read-only span of bytes, as little endian.
 389        /// </summary>
 390        /// <param name="source">The read-only span of bytes to read.</param>
 391        /// <param name="value">When this method returns, contains the value read out of the read-only span of bytes, as
 392        /// <returns>
 393        /// <see langword="true" /> if the span is large enough to contain a <see cref="Int128" />; otherwise, <see lang
 394        /// </returns>
 395        /// <remarks>Reads exactly 16 bytes from the beginning of the span.</remarks>
 396        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 397        public static bool TryReadInt128LittleEndian(ReadOnlySpan<byte> source, out Int128 value)
 398        {
 399            if (BitConverter.IsLittleEndian)
 400            {
 0401                return MemoryMarshal.TryRead(source, out value);
 402            }
 403
 404            bool success = MemoryMarshal.TryRead(source, out Int128 tmp);
 405            value = ReverseEndianness(tmp);
 406            return success;
 407        }
 408
 409        /// <summary>
 410        /// Reads a <see cref="nint" /> from the beginning of a read-only span of bytes, as little endian.
 411        /// </summary>
 412        /// <param name="source">The read-only span of bytes to read.</param>
 413        /// <param name="value">When this method returns, contains the value read out of the read-only span of bytes, as
 414        /// <returns>
 415        /// <see langword="true" /> if the span is large enough to contain a <see cref="nint" />; otherwise, <see langwo
 416        /// </returns>
 417        /// <remarks>Reads exactly 4 bytes on 32-bit platforms or 8 bytes on 64-bit platforms from the beginning of the 
 418        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 419        public static bool TryReadIntPtrLittleEndian(ReadOnlySpan<byte> source, out nint value)
 420        {
 421            if (BitConverter.IsLittleEndian)
 422            {
 0423                return MemoryMarshal.TryRead(source, out value);
 424            }
 425
 426            bool success = MemoryMarshal.TryRead(source, out nint tmp);
 427            value = ReverseEndianness(tmp);
 428            return success;
 429        }
 430
 431        /// <summary>
 432        /// Reads a <see cref="float" /> from the beginning of a read-only span of bytes, as little endian.
 433        /// </summary>
 434        /// <param name="source">The read-only span of bytes to read.</param>
 435        /// <param name="value">When this method returns, contains the value read out of the read-only span of bytes, as
 436        /// <returns>
 437        /// <see langword="true" /> if the span is large enough to contain a <see cref="float" />; otherwise, <see langw
 438        /// </returns>
 439        /// <remarks>Reads exactly 4 bytes from the beginning of the span.</remarks>
 440        public static bool TryReadSingleLittleEndian(ReadOnlySpan<byte> source, out float value)
 441        {
 442            if (!BitConverter.IsLittleEndian)
 443            {
 444                bool success = MemoryMarshal.TryRead(source, out int tmp);
 445                value = BitConverter.Int32BitsToSingle(ReverseEndianness(tmp));
 446                return success;
 447            }
 448
 0449            return MemoryMarshal.TryRead(source, out value);
 450        }
 451
 452        /// <summary>
 453        /// Reads a <see cref="ushort" /> from the beginning of a read-only span of bytes, as little endian.
 454        /// </summary>
 455        /// <param name="source">The read-only span of bytes to read.</param>
 456        /// <param name="value">When this method returns, contains the value read out of the read-only span of bytes, as
 457        /// <returns>
 458        /// <see langword="true" /> if the span is large enough to contain a <see cref="ushort" />; otherwise, <see lang
 459        /// </returns>
 460        /// <remarks>Reads exactly 2 bytes from the beginning of the span.</remarks>
 461        [CLSCompliant(false)]
 462        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 463        public static bool TryReadUInt16LittleEndian(ReadOnlySpan<byte> source, out ushort value)
 464        {
 465            if (BitConverter.IsLittleEndian)
 466            {
 0467                return MemoryMarshal.TryRead(source, out value);
 468            }
 469
 470            bool success = MemoryMarshal.TryRead(source, out ushort tmp);
 471            value = ReverseEndianness(tmp);
 472            return success;
 473        }
 474
 475        /// <summary>
 476        /// Reads a <see cref="uint" /> from the beginning of a read-only span of bytes, as little endian.
 477        /// </summary>
 478        /// <param name="source">The read-only span of bytes to read.</param>
 479        /// <param name="value">When this method returns, contains the value read out of the read-only span of bytes, as
 480        /// <returns>
 481        /// <see langword="true" /> if the span is large enough to contain a <see cref="uint" />; otherwise, <see langwo
 482        /// </returns>
 483        /// <remarks>Reads exactly 4 bytes from the beginning of the span.</remarks>
 484        [CLSCompliant(false)]
 485        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 486        public static bool TryReadUInt32LittleEndian(ReadOnlySpan<byte> source, out uint value)
 487        {
 488            if (BitConverter.IsLittleEndian)
 489            {
 0490                return MemoryMarshal.TryRead(source, out value);
 491            }
 492
 493            bool success = MemoryMarshal.TryRead(source, out uint tmp);
 494            value = ReverseEndianness(tmp);
 495            return success;
 496        }
 497
 498        /// <summary>
 499        /// Reads a <see cref="ulong" /> from the beginning of a read-only span of bytes, as little endian.
 500        /// </summary>
 501        /// <param name="source">The read-only span of bytes to read.</param>
 502        /// <param name="value">When this method returns, contains the value read out of the read-only span of bytes, as
 503        /// <returns>
 504        /// <see langword="true" /> if the span is large enough to contain a <see cref="ulong" />; otherwise, <see langw
 505        /// </returns>
 506        /// <remarks>Reads exactly 8 bytes from the beginning of the span.</remarks>
 507        [CLSCompliant(false)]
 508        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 509        public static bool TryReadUInt64LittleEndian(ReadOnlySpan<byte> source, out ulong value)
 510        {
 511            if (BitConverter.IsLittleEndian)
 512            {
 0513                return MemoryMarshal.TryRead(source, out value);
 514            }
 515
 516            bool success = MemoryMarshal.TryRead(source, out ulong tmp);
 517            value = ReverseEndianness(tmp);
 518            return success;
 519        }
 520
 521        /// <summary>
 522        /// Reads a <see cref="UInt128" /> from the beginning of a read-only span of bytes, as little endian.
 523        /// </summary>
 524        /// <param name="source">The read-only span of bytes to read.</param>
 525        /// <param name="value">When this method returns, contains the value read out of the read-only span of bytes, as
 526        /// <returns>
 527        /// <see langword="true" /> if the span is large enough to contain a <see cref="UInt128" />; otherwise, <see lan
 528        /// </returns>
 529        /// <remarks>Reads exactly 16 bytes from the beginning of the span.</remarks>
 530        [CLSCompliant(false)]
 531        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 532        public static bool TryReadUInt128LittleEndian(ReadOnlySpan<byte> source, out UInt128 value)
 533        {
 534            if (BitConverter.IsLittleEndian)
 535            {
 0536                return MemoryMarshal.TryRead(source, out value);
 537            }
 538
 539            bool success = MemoryMarshal.TryRead(source, out UInt128 tmp);
 540            value = ReverseEndianness(tmp);
 541            return success;
 542        }
 543
 544        /// <summary>
 545        /// Reads a <see cref="nuint" /> from the beginning of a read-only span of bytes, as little endian.
 546        /// </summary>
 547        /// <param name="source">The read-only span of bytes to read.</param>
 548        /// <param name="value">When this method returns, contains the value read out of the read-only span of bytes, as
 549        /// <returns>
 550        /// <see langword="true" /> if the span is large enough to contain a <see cref="nuint" />; otherwise, <see langw
 551        /// </returns>
 552        /// <remarks>Reads exactly 4 bytes on 32-bit platforms -or- 8 bytes on 64-bit platforms from the beginning of th
 553        [CLSCompliant(false)]
 554        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 555        public static bool TryReadUIntPtrLittleEndian(ReadOnlySpan<byte> source, out nuint value)
 556        {
 557            if (BitConverter.IsLittleEndian)
 558            {
 0559                return MemoryMarshal.TryRead(source, out value);
 560            }
 561
 562            bool success = MemoryMarshal.TryRead(source, out nuint tmp);
 563            value = ReverseEndianness(tmp);
 564            return success;
 565        }
 566    }
 567}
 568

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Private.CoreLib/src/System/Buffers/Binary/BinaryPrimitives.ReverseEndianness.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Diagnostics.CodeAnalysis;
 5using System.Numerics;
 6using System.Runtime.CompilerServices;
 7using System.Runtime.InteropServices;
 8using System.Runtime.Intrinsics;
 9
 10#pragma warning disable SA1121 // explicitly using type aliases instead of built-in types
 11
 12#if TARGET_64BIT
 13using nint_t = System.Int64;
 14using nuint_t = System.UInt64;
 15#else
 16using nint_t = System.Int32;
 17using nuint_t = System.UInt32;
 18#endif
 19
 20namespace System.Buffers.Binary
 21{
 22    public static partial class BinaryPrimitives
 23    {
 24        /// <summary>
 25        /// Reverses a primitive value by performing an endianness swap of the specified <see cref="sbyte" /> value, whi
 26        /// </summary>
 27        /// <param name="value">The value to reverse.</param>
 28        /// <returns>The passed-in value, unmodified.</returns>
 29        /// <remarks>This method effectively does nothing and was added only for consistency.</remarks>
 30        [CLSCompliant(false)]
 31        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 032        public static sbyte ReverseEndianness(sbyte value) => value;
 33
 34        /// <summary>
 35        /// Reverses a primitive value by performing an endianness swap of the specified <see cref="short" /> value.
 36        /// </summary>
 37        /// <param name="value">The value to reverse.</param>
 38        /// <returns>The reversed value.</returns>
 39        [Intrinsic]
 40        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 041        public static short ReverseEndianness(short value) => (short)ReverseEndianness((ushort)value);
 42
 43        /// <summary>
 44        /// Reverses a primitive value by performing an endianness swap of the specified <see cref="int" /> value.
 45        /// </summary>
 46        /// <param name="value">The value to reverse.</param>
 47        /// <returns>The reversed value.</returns>
 48        [Intrinsic]
 49        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 050        public static int ReverseEndianness(int value) => (int)ReverseEndianness((uint)value);
 51
 52        /// <summary>
 53        /// Reverses a primitive value by performing an endianness swap of the specified <see cref="long" /> value.
 54        /// </summary>
 55        /// <param name="value">The value to reverse.</param>
 56        /// <returns>The reversed value.</returns>
 57        [Intrinsic]
 58        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 059        public static long ReverseEndianness(long value) => (long)ReverseEndianness((ulong)value);
 60
 61        /// <summary>
 62        /// Reverses a primitive value by performing an endianness swap of the specified <see cref="nint" /> value.
 63        /// </summary>
 64        /// <param name="value">The value to reverse.</param>
 65        /// <returns>The reversed value.</returns>
 66        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 067        public static nint ReverseEndianness(nint value) => (nint)ReverseEndianness((nint_t)value);
 68
 69        /// <summary>
 70        /// Reverses a primitive value by performing an endianness swap of the specified <see cref="Int128" /> value.
 71        /// </summary>
 72        /// <param name="value">The value to reverse.</param>
 73        /// <returns>The reversed value.</returns>
 74        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 75        public static Int128 ReverseEndianness(Int128 value)
 76        {
 077            return new Int128(
 078                ReverseEndianness(value.Lower),
 079                ReverseEndianness(value.Upper)
 080            );
 81        }
 82
 83        /// <summary>
 84        /// Reverses a primitive value by performing an endianness swap of the specified <see cref="byte" /> value, whic
 85        /// </summary>
 86        /// <param name="value">The value to reverse.</param>
 87        /// <returns>The passed-in value, unmodified.</returns>
 88        /// <remarks>This method effectively does nothing and was added only for consistency.</remarks>
 89        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 090        public static byte ReverseEndianness(byte value) => value;
 91
 92        /// <summary>
 93        /// Reverses a primitive value by performing an endianness swap of the specified <see cref="ushort" /> value.
 94        /// </summary>
 95        /// <param name="value">The value to reverse.</param>
 96        /// <returns>The reversed value.</returns>
 97        [CLSCompliant(false)]
 98        [Intrinsic]
 99        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 100        public static ushort ReverseEndianness(ushort value)
 101        {
 102            // Don't need to AND with 0xFF00 or 0x00FF since the final
 103            // cast back to ushort will clear out all bits above [ 15 .. 00 ].
 104            // This is normally implemented via "movzx eax, ax" on the return.
 105            // Alternatively, the compiler could elide the movzx instruction
 106            // entirely if it knows the caller is only going to access "ax"
 107            // instead of "eax" / "rax" when the function returns.
 108
 0109            return (ushort)((value >> 8) + (value << 8));
 110        }
 111
 112        /// <summary>
 113        /// Reverses a primitive value by performing an endianness swap of the specified <see cref="char" /> value.
 114        /// </summary>
 115        /// <param name="value">The value to reverse.</param>
 116        /// <returns>The reversed value.</returns>
 117        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 118        internal static char ReverseEndianness(char value) => (char)ReverseEndianness((ushort)value);
 119
 120        /// <summary>
 121        /// Reverses a primitive value by performing an endianness swap of the specified <see cref="uint" /> value.
 122        /// </summary>
 123        /// <param name="value">The value to reverse.</param>
 124        /// <returns>The reversed value.</returns>
 125        [CLSCompliant(false)]
 126        [Intrinsic]
 127        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 128        public static uint ReverseEndianness(uint value)
 129        {
 130            // This takes advantage of the fact that the JIT can detect
 131            // ROL32 / ROR32 patterns and output the correct intrinsic.
 132            //
 133            // Input: value = [ ww xx yy zz ]
 134            //
 135            // First line generates : [ ww xx yy zz ]
 136            //                      & [ 00 FF 00 FF ]
 137            //                      = [ 00 xx 00 zz ]
 138            //             ROR32(8) = [ zz 00 xx 00 ]
 139            //
 140            // Second line generates: [ ww xx yy zz ]
 141            //                      & [ FF 00 FF 00 ]
 142            //                      = [ ww 00 yy 00 ]
 143            //             ROL32(8) = [ 00 yy 00 ww ]
 144            //
 145            //                (sum) = [ zz yy xx ww ]
 146            //
 147            // Testing shows that throughput increases if the AND
 148            // is performed before the ROL / ROR.
 149
 0150            return BitOperations.RotateRight(value & 0x00FF00FFu, 8) // xx zz
 0151                + BitOperations.RotateLeft(value & 0xFF00FF00u, 8); // ww yy
 152        }
 153
 154        /// <summary>
 155        /// Reverses a primitive value by performing an endianness swap of the specified <see cref="ulong" /> value.
 156        /// </summary>
 157        /// <param name="value">The value to reverse.</param>
 158        /// <returns>The reversed value.</returns>
 159        [CLSCompliant(false)]
 160        [Intrinsic]
 161        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 162        public static ulong ReverseEndianness(ulong value)
 163        {
 164            // Operations on 32-bit values have higher throughput than
 165            // operations on 64-bit values, so decompose.
 166
 0167            return ((ulong)ReverseEndianness((uint)value) << 32)
 0168                + ReverseEndianness((uint)(value >> 32));
 169        }
 170
 171        /// <summary>
 172        /// Reverses a primitive value by performing an endianness swap of the specified <see cref="nuint" /> value.
 173        /// </summary>
 174        /// <param name="value">The value to reverse.</param>
 175        /// <returns>The reversed value.</returns>
 176        [CLSCompliant(false)]
 177        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 0178        public static nuint ReverseEndianness(nuint value) => (nuint)ReverseEndianness((nuint_t)value);
 179
 180        /// <summary>
 181        /// Reverses a primitive value by performing an endianness swap of the specified <see cref="UInt128" /> value.
 182        /// </summary>
 183        /// <param name="value">The value to reverse.</param>
 184        /// <returns>The reversed value.</returns>
 185        [CLSCompliant(false)]
 186        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 187        public static UInt128 ReverseEndianness(UInt128 value)
 188        {
 0189            return new UInt128(
 0190                ReverseEndianness(value.Lower),
 0191                ReverseEndianness(value.Upper)
 0192            );
 193        }
 194
 195        /// <summary>Copies every primitive value from <paramref name="source"/> to <paramref name="destination"/>, reve
 196        /// <param name="source">The source span to copy.</param>
 197        /// <param name="destination">The destination to which the source elements should be copied.</param>
 198        /// <remarks>The source and destination spans may overlap. The same span may be passed as both the source and th
 199        /// <exception cref="ArgumentException">The <paramref name="destination"/>'s length is smaller than that of the 
 200        [CLSCompliant(false)]
 201        public static void ReverseEndianness(ReadOnlySpan<ushort> source, Span<ushort> destination) =>
 0202            ReverseEndianness<short, Int16EndiannessReverser>(MemoryMarshal.Cast<ushort, short>(source), MemoryMarshal.C
 203
 204        /// <inheritdoc cref="ReverseEndianness(ReadOnlySpan{ushort}, Span{ushort})" />
 205        public static void ReverseEndianness(ReadOnlySpan<short> source, Span<short> destination) =>
 0206            ReverseEndianness<short, Int16EndiannessReverser>(source, destination);
 207
 208        /// <inheritdoc cref="ReverseEndianness(ReadOnlySpan{ushort}, Span{ushort})" />
 209        [CLSCompliant(false)]
 210        public static void ReverseEndianness(ReadOnlySpan<uint> source, Span<uint> destination) =>
 0211            ReverseEndianness<int, Int32EndiannessReverser>(MemoryMarshal.Cast<uint, int>(source), MemoryMarshal.Cast<ui
 212
 213        /// <inheritdoc cref="ReverseEndianness(ReadOnlySpan{ushort}, Span{ushort})" />
 214        public static void ReverseEndianness(ReadOnlySpan<int> source, Span<int> destination) =>
 0215            ReverseEndianness<int, Int32EndiannessReverser>(source, destination);
 216
 217        /// <inheritdoc cref="ReverseEndianness(ReadOnlySpan{ushort}, Span{ushort})" />
 218        [CLSCompliant(false)]
 219        public static void ReverseEndianness(ReadOnlySpan<ulong> source, Span<ulong> destination) =>
 0220            ReverseEndianness<long, Int64EndiannessReverser>(MemoryMarshal.Cast<ulong, long>(source), MemoryMarshal.Cast
 221
 222        /// <inheritdoc cref="ReverseEndianness(ReadOnlySpan{ushort}, Span{ushort})" />
 223        public static void ReverseEndianness(ReadOnlySpan<long> source, Span<long> destination) =>
 0224            ReverseEndianness<long, Int64EndiannessReverser>(source, destination);
 225
 226        /// <inheritdoc cref="ReverseEndianness(ReadOnlySpan{ushort}, Span{ushort})" />
 227        [CLSCompliant(false)]
 228        public static void ReverseEndianness(ReadOnlySpan<nuint> source, Span<nuint> destination) =>
 229#if TARGET_64BIT
 0230            ReverseEndianness<long, Int64EndiannessReverser>(MemoryMarshal.Cast<nuint, long>(source), MemoryMarshal.Cast
 231#else
 232            ReverseEndianness<int, Int32EndiannessReverser>(MemoryMarshal.Cast<nuint, int>(source), MemoryMarshal.Cast<n
 233#endif
 234
 235        /// <inheritdoc cref="ReverseEndianness(ReadOnlySpan{ushort}, Span{ushort})" />
 236        public static void ReverseEndianness(ReadOnlySpan<nint> source, Span<nint> destination) =>
 237#if TARGET_64BIT
 0238            ReverseEndianness<long, Int64EndiannessReverser>(MemoryMarshal.Cast<nint, long>(source), MemoryMarshal.Cast<
 239#else
 240            ReverseEndianness<int, Int32EndiannessReverser>(MemoryMarshal.Cast<nint, int>(source), MemoryMarshal.Cast<ni
 241#endif
 242
 243        private readonly struct Int16EndiannessReverser : IEndiannessReverser<short>
 244        {
 245            public static short Reverse(short value) =>
 0246                ReverseEndianness(value);
 247
 248            [MethodImpl(MethodImplOptions.AggressiveInlining)]
 249            public static Vector128<short> Reverse(Vector128<short> vector) =>
 0250                Vector128.ShiftLeft(vector, 8) | Vector128.ShiftRightLogical(vector, 8);
 251
 252            [MethodImpl(MethodImplOptions.AggressiveInlining)]
 253            public static Vector256<short> Reverse(Vector256<short> vector) =>
 0254                Vector256.ShiftLeft(vector, 8) | Vector256.ShiftRightLogical(vector, 8);
 255        }
 256
 257        private readonly struct Int32EndiannessReverser : IEndiannessReverser<int>
 258        {
 259            public static int Reverse(int value) =>
 0260                ReverseEndianness(value);
 261
 262            [MethodImpl(MethodImplOptions.AggressiveInlining)]
 263            public static Vector128<int> Reverse(Vector128<int> vector) =>
 0264                Vector128.Shuffle(vector.AsByte(), Vector128.Create((byte)3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 
 265
 266            [MethodImpl(MethodImplOptions.AggressiveInlining)]
 267            public static Vector256<int> Reverse(Vector256<int> vector) =>
 0268                Vector256.Shuffle(vector.AsByte(), Vector256.Create((byte)3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 
 269        }
 270
 271        private readonly struct Int64EndiannessReverser : IEndiannessReverser<long>
 272        {
 273            public static long Reverse(long value) =>
 0274                ReverseEndianness(value);
 275
 276            [MethodImpl(MethodImplOptions.AggressiveInlining)]
 277            public static Vector128<long> Reverse(Vector128<long> vector) =>
 0278                Vector128.Shuffle(vector.AsByte(), Vector128.Create((byte)7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10
 279
 280            [MethodImpl(MethodImplOptions.AggressiveInlining)]
 281            public static Vector256<long> Reverse(Vector256<long> vector) =>
 0282                Vector256.Shuffle(vector.AsByte(), Vector256.Create((byte)7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10
 283        }
 284
 285        private static void ReverseEndianness<T, TReverser>(ReadOnlySpan<T> source, Span<T> destination)
 286            where T : struct
 287            where TReverser : IEndiannessReverser<T>
 288        {
 0289            if (destination.Length < source.Length)
 290            {
 0291                ThrowDestinationTooSmall();
 292            }
 293
 0294            ref T sourceRef = ref MemoryMarshal.GetReference(source);
 0295            ref T destRef = ref MemoryMarshal.GetReference(destination);
 296
 0297            if (Unsafe.AreSame(ref sourceRef, ref destRef) ||
 0298                !source.Overlaps(destination, out int elementOffset) ||
 0299                elementOffset < 0)
 300            {
 301                // Either there's no overlap between the source and the destination, or there's overlap but the
 302                // destination starts at or before the source.  That means we can safely iterate from beginning
 303                // to end of the source and not have to worry about writing into the destination and clobbering
 304                // source data we haven't yet read.
 305
 0306                int i = 0;
 307
 0308                if (Vector256.IsHardwareAccelerated)
 309                {
 0310                    while (i <= source.Length - Vector256<T>.Count)
 311                    {
 0312                        Vector256.StoreUnsafe(TReverser.Reverse(Vector256.LoadUnsafe(ref sourceRef, (uint)i)), ref destR
 0313                        i += Vector256<T>.Count;
 314                    }
 315                }
 316
 0317                if (Vector128.IsHardwareAccelerated)
 318                {
 0319                    while (i <= source.Length - Vector128<T>.Count)
 320                    {
 0321                        Vector128.StoreUnsafe(TReverser.Reverse(Vector128.LoadUnsafe(ref sourceRef, (uint)i)), ref destR
 0322                        i += Vector128<T>.Count;
 323                    }
 324                }
 325
 0326                while (i < source.Length)
 327                {
 0328                    Unsafe.Add(ref destRef, i) = TReverser.Reverse(Unsafe.Add(ref sourceRef, i));
 0329                    i++;
 330                }
 331            }
 332            else
 333            {
 334                // There's overlap between the source and the destination, and the source starts before the destination.
 335                // That means if we were to iterate from beginning to end, reading from the source and writing to the
 336                // destination, we'd overwrite source elements not yet read.  To avoid that, we iterate from end to begi
 337
 0338                int i = source.Length;
 339
 0340                if (Vector256.IsHardwareAccelerated)
 341                {
 0342                    while (i >= Vector256<T>.Count)
 343                    {
 0344                        i -= Vector256<T>.Count;
 0345                        Vector256.StoreUnsafe(TReverser.Reverse(Vector256.LoadUnsafe(ref sourceRef, (uint)i)), ref destR
 346                    }
 347                }
 348
 0349                if (Vector128.IsHardwareAccelerated)
 350                {
 0351                    while (i >= Vector128<T>.Count)
 352                    {
 0353                        i -= Vector128<T>.Count;
 0354                        Vector128.StoreUnsafe(TReverser.Reverse(Vector128.LoadUnsafe(ref sourceRef, (uint)i)), ref destR
 355                    }
 356                }
 357
 0358                while (i > 0)
 359                {
 0360                    i--;
 0361                    Unsafe.Add(ref destRef, i) = TReverser.Reverse(Unsafe.Add(ref sourceRef, i));
 362                }
 363            }
 0364        }
 365
 366        private interface IEndiannessReverser<T> where T : struct
 367        {
 368            static abstract T Reverse(T value);
 369            static abstract Vector128<T> Reverse(Vector128<T> vector);
 370            static abstract Vector256<T> Reverse(Vector256<T> vector);
 371        }
 372
 373        /// <inheritdoc cref="ReverseEndianness(ReadOnlySpan{ushort}, Span{ushort})" />
 374        [CLSCompliant(false)]
 375        public static void ReverseEndianness(ReadOnlySpan<UInt128> source, Span<UInt128> destination) =>
 0376            ReverseEndianness(MemoryMarshal.Cast<UInt128, Int128>(source), MemoryMarshal.Cast<UInt128, Int128>(destinati
 377
 378        /// <inheritdoc cref="ReverseEndianness(ReadOnlySpan{ushort}, Span{ushort})" />
 379        public static void ReverseEndianness(ReadOnlySpan<Int128> source, Span<Int128> destination)
 380        {
 0381            if (destination.Length < source.Length)
 382            {
 0383                ThrowDestinationTooSmall();
 384            }
 385
 0386            if (!source.Overlaps(destination, out int elementOffset) ||
 0387                elementOffset <= 0)
 388            {
 389                // Iterate from beginning to end
 0390                for (int i = 0; i < source.Length; i++)
 391                {
 0392                    destination[i] = ReverseEndianness(source[i]);
 393                }
 394            }
 395            else
 396            {
 397                // Iterate from end to beginning
 0398                for (int i = source.Length - 1; i >= 0; i--)
 399                {
 0400                    destination[i] = ReverseEndianness(source[i]);
 401                }
 402            }
 0403        }
 404
 405        [DoesNotReturn]
 406        private static void ThrowDestinationTooSmall() =>
 0407            throw new ArgumentException(SR.Arg_BufferTooSmall, "destination");
 408    }
 409}
 410

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Private.CoreLib/src/System/Buffers/Binary/BinaryPrimitives.WriteBigEndian.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Numerics;
 5using System.Runtime.CompilerServices;
 6using System.Runtime.InteropServices;
 7
 8namespace System.Buffers.Binary
 9{
 10    public static partial class BinaryPrimitives
 11    {
 12        /// <summary>
 13        /// Writes a <see cref="double" /> into a span of bytes, as big endian.
 14        /// </summary>
 15        /// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
 16        /// <param name="value">The value to write into the span of bytes.</param>
 17        /// <remarks>Writes exactly 8 bytes to the beginning of the span.</remarks>
 18        /// <exception cref="ArgumentOutOfRangeException">
 19        /// <paramref name="destination" /> is too small to contain a <see cref="double" />.
 20        /// </exception>
 21        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 22        public static void WriteDoubleBigEndian(Span<byte> destination, double value)
 23        {
 24            if (BitConverter.IsLittleEndian)
 25            {
 026                long tmp = ReverseEndianness(BitConverter.DoubleToInt64Bits(value));
 027                MemoryMarshal.Write(destination, in tmp);
 28            }
 29            else
 30            {
 31                MemoryMarshal.Write(destination, in value);
 32            }
 33        }
 34
 35        /// <summary>
 36        /// Writes a <see cref="Half" /> into a span of bytes, as big endian.
 37        /// </summary>
 38        /// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
 39        /// <param name="value">The value to write into the span of bytes.</param>
 40        /// <remarks>Writes exactly 2 bytes to the beginning of the span.</remarks>
 41        /// <exception cref="ArgumentOutOfRangeException">
 42        /// <paramref name="destination" /> is too small to contain a <see cref="Half" />.
 43        /// </exception>
 44        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 45        public static void WriteHalfBigEndian(Span<byte> destination, Half value)
 46        {
 47            if (BitConverter.IsLittleEndian)
 48            {
 049                short tmp = ReverseEndianness(BitConverter.HalfToInt16Bits(value));
 050                MemoryMarshal.Write(destination, in tmp);
 51            }
 52            else
 53            {
 54                MemoryMarshal.Write(destination, in value);
 55            }
 56        }
 57
 58        /// <summary>
 59        /// Writes a <see cref="BFloat16" /> into a span of bytes, as big endian.
 60        /// </summary>
 61        /// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
 62        /// <param name="value">The value to write into the span of bytes.</param>
 63        /// <remarks>Writes exactly 2 bytes to the beginning of the span.</remarks>
 64        /// <exception cref="ArgumentOutOfRangeException">
 65        /// <paramref name="destination" /> is too small to contain a <see cref="BFloat16" />.
 66        /// </exception>
 67        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 68        public static void WriteBFloat16BigEndian(Span<byte> destination, BFloat16 value)
 69        {
 70            if (BitConverter.IsLittleEndian)
 71            {
 072                short tmp = ReverseEndianness(BitConverter.BFloat16ToInt16Bits(value));
 073                MemoryMarshal.Write(destination, in tmp);
 74            }
 75            else
 76            {
 77                MemoryMarshal.Write(destination, in value);
 78            }
 79        }
 80
 81        /// <summary>
 82        /// Writes a <see cref="short" /> into a span of bytes, as big endian.
 83        /// </summary>
 84        /// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
 85        /// <param name="value">The value to write into the span of bytes.</param>
 86        /// <remarks>Writes exactly 2 bytes to the beginning of the span.</remarks>
 87        /// <exception cref="ArgumentOutOfRangeException">
 88        /// <paramref name="destination" /> is too small to contain a <see cref="short" />.
 89        /// </exception>
 90        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 91        public static void WriteInt16BigEndian(Span<byte> destination, short value)
 92        {
 93            if (BitConverter.IsLittleEndian)
 94            {
 095                short tmp = ReverseEndianness(value);
 096                MemoryMarshal.Write(destination, in tmp);
 97            }
 98            else
 99            {
 100                MemoryMarshal.Write(destination, in value);
 101            }
 102        }
 103
 104        /// <summary>
 105        /// Writes a <see cref="int" /> into a span of bytes, as big endian.
 106        /// </summary>
 107        /// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
 108        /// <param name="value">The value to write into the span of bytes.</param>
 109        /// <remarks>Writes exactly 4 bytes to the beginning of the span.</remarks>
 110        /// <exception cref="ArgumentOutOfRangeException">
 111        /// <paramref name="destination" /> is too small to contain a <see cref="int" />.
 112        /// </exception>
 113        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 114        public static void WriteInt32BigEndian(Span<byte> destination, int value)
 115        {
 116            if (BitConverter.IsLittleEndian)
 117            {
 0118                int tmp = ReverseEndianness(value);
 0119                MemoryMarshal.Write(destination, in tmp);
 120            }
 121            else
 122            {
 123                MemoryMarshal.Write(destination, in value);
 124            }
 125        }
 126
 127        /// <summary>
 128        /// Writes a <see cref="long" /> into a span of bytes, as big endian.
 129        /// </summary>
 130        /// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
 131        /// <param name="value">The value to write into the span of bytes.</param>
 132        /// <remarks>Writes exactly 8 bytes to the beginning of the span.</remarks>
 133        /// <exception cref="ArgumentOutOfRangeException">
 134        /// <paramref name="destination" /> is too small to contain a <see cref="long" />.
 135        /// </exception>
 136        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 137        public static void WriteInt64BigEndian(Span<byte> destination, long value)
 138        {
 139            if (BitConverter.IsLittleEndian)
 140            {
 0141                long tmp = ReverseEndianness(value);
 0142                MemoryMarshal.Write(destination, in tmp);
 143            }
 144            else
 145            {
 146                MemoryMarshal.Write(destination, in value);
 147            }
 148        }
 149
 150        /// <summary>
 151        /// Writes a <see cref="Int128" /> into a span of bytes, as big endian.
 152        /// </summary>
 153        /// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
 154        /// <param name="value">The value to write into the span of bytes.</param>
 155        /// <remarks>Writes exactly 16 bytes to the beginning of the span.</remarks>
 156        /// <exception cref="ArgumentOutOfRangeException">
 157        /// <paramref name="destination" /> is too small to contain a <see cref="Int128" />.
 158        /// </exception>
 159        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 160        public static void WriteInt128BigEndian(Span<byte> destination, Int128 value)
 161        {
 162            if (BitConverter.IsLittleEndian)
 163            {
 0164                Int128 tmp = ReverseEndianness(value);
 0165                MemoryMarshal.Write(destination, in tmp);
 166            }
 167            else
 168            {
 169                MemoryMarshal.Write(destination, in value);
 170            }
 171        }
 172
 173        /// <summary>
 174        /// Writes a <see cref="nint" /> into a span of bytes, as big endian.
 175        /// </summary>
 176        /// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
 177        /// <param name="value">The value to write into the span of bytes.</param>
 178        /// <remarks>Writes exactly 4 bytes on 32-bit platforms -or- 8 bytes on 64-bit platforms to the beginning of the
 179        /// <exception cref="ArgumentOutOfRangeException">
 180        /// <paramref name="destination" /> is too small to contain a <see cref="nint" />.
 181        /// </exception>
 182        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 183        public static void WriteIntPtrBigEndian(Span<byte> destination, nint value)
 184        {
 185            if (BitConverter.IsLittleEndian)
 186            {
 0187                nint tmp = ReverseEndianness(value);
 0188                MemoryMarshal.Write(destination, in tmp);
 189            }
 190            else
 191            {
 192                MemoryMarshal.Write(destination, in value);
 193            }
 194        }
 195
 196        /// <summary>
 197        /// Writes a <see cref="float" /> into a span of bytes, as big endian.
 198        /// </summary>
 199        /// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
 200        /// <param name="value">The value to write into the span of bytes.</param>
 201        /// <remarks>Writes exactly 4 bytes to the beginning of the span.</remarks>
 202        /// <exception cref="ArgumentOutOfRangeException">
 203        /// <paramref name="destination" /> is too small to contain a <see cref="float" />.
 204        /// </exception>
 205        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 206        public static void WriteSingleBigEndian(Span<byte> destination, float value)
 207        {
 208            if (BitConverter.IsLittleEndian)
 209            {
 0210                int tmp = ReverseEndianness(BitConverter.SingleToInt32Bits(value));
 0211                MemoryMarshal.Write(destination, in tmp);
 212            }
 213            else
 214            {
 215                MemoryMarshal.Write(destination, in value);
 216            }
 217        }
 218
 219        /// <summary>
 220        /// Write a <see cref="ushort" /> into a span of bytes, as big endian.
 221        /// </summary>
 222        /// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
 223        /// <param name="value">The value to write into the span of bytes.</param>
 224        /// <remarks>Writes exactly 2 bytes to the beginning of the span.</remarks>
 225        /// <exception cref="ArgumentOutOfRangeException">
 226        /// <paramref name="destination" /> is too small to contain a <see cref="ushort" />.
 227        /// </exception>
 228        [CLSCompliant(false)]
 229        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 230        public static void WriteUInt16BigEndian(Span<byte> destination, ushort value)
 231        {
 232            if (BitConverter.IsLittleEndian)
 233            {
 0234                ushort tmp = ReverseEndianness(value);
 0235                MemoryMarshal.Write(destination, in tmp);
 236            }
 237            else
 238            {
 239                MemoryMarshal.Write(destination, in value);
 240            }
 241        }
 242
 243        /// <summary>
 244        /// Write a <see cref="uint" /> into a span of bytes, as big endian.
 245        /// </summary>
 246        /// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
 247        /// <param name="value">The value to write into the span of bytes.</param>
 248        /// <remarks>Writes exactly 4 bytes to the beginning of the span.</remarks>
 249        /// <exception cref="ArgumentOutOfRangeException">
 250        /// <paramref name="destination" /> is too small to contain a <see cref="uint" />.
 251        /// </exception>
 252        [CLSCompliant(false)]
 253        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 254        public static void WriteUInt32BigEndian(Span<byte> destination, uint value)
 255        {
 256            if (BitConverter.IsLittleEndian)
 257            {
 0258                uint tmp = ReverseEndianness(value);
 0259                MemoryMarshal.Write(destination, in tmp);
 260            }
 261            else
 262            {
 263                MemoryMarshal.Write(destination, in value);
 264            }
 265        }
 266
 267        /// <summary>
 268        /// Write a <see cref="ulong" /> into a span of bytes, as big endian.
 269        /// </summary>
 270        /// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
 271        /// <param name="value">The value to write into the span of bytes.</param>
 272        /// <remarks>Writes exactly 8 bytes to the beginning of the span.</remarks>
 273        /// <exception cref="ArgumentOutOfRangeException">
 274        /// <paramref name="destination" /> is too small to contain a <see cref="ulong" />.
 275        /// </exception>
 276        [CLSCompliant(false)]
 277        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 278        public static void WriteUInt64BigEndian(Span<byte> destination, ulong value)
 279        {
 280            if (BitConverter.IsLittleEndian)
 281            {
 0282                ulong tmp = ReverseEndianness(value);
 0283                MemoryMarshal.Write(destination, in tmp);
 284            }
 285            else
 286            {
 287                MemoryMarshal.Write(destination, in value);
 288            }
 289        }
 290
 291        /// <summary>
 292        /// Writes a <see cref="UInt128" /> into a span of bytes, as big endian.
 293        /// </summary>
 294        /// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
 295        /// <param name="value">The value to write into the span of bytes.</param>
 296        /// <remarks>Writes exactly 16 bytes to the beginning of the span.</remarks>
 297        /// <exception cref="ArgumentOutOfRangeException">
 298        /// <paramref name="destination" /> is too small to contain a <see cref="UInt128" />.
 299        /// </exception>
 300        [CLSCompliant(false)]
 301        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 302        public static void WriteUInt128BigEndian(Span<byte> destination, UInt128 value)
 303        {
 304            if (BitConverter.IsLittleEndian)
 305            {
 0306                UInt128 tmp = ReverseEndianness(value);
 0307                MemoryMarshal.Write(destination, in tmp);
 308            }
 309            else
 310            {
 311                MemoryMarshal.Write(destination, in value);
 312            }
 313        }
 314
 315        /// <summary>
 316        /// Writes a <see cref="nuint" /> into a span of bytes, as big endian.
 317        /// </summary>
 318        /// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
 319        /// <param name="value">The value to write into the span of bytes.</param>
 320        /// <remarks>Writes exactly 4 bytes on 32-bit platforms -or- 8 bytes on 64-bit platforms to the beginning of the
 321        /// <exception cref="ArgumentOutOfRangeException">
 322        /// <paramref name="destination" /> is too small to contain a <see cref="nuint" />.
 323        /// </exception>
 324        [CLSCompliant(false)]
 325        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 326        public static void WriteUIntPtrBigEndian(Span<byte> destination, nuint value)
 327        {
 328            if (BitConverter.IsLittleEndian)
 329            {
 0330                nuint tmp = ReverseEndianness(value);
 0331                MemoryMarshal.Write(destination, in tmp);
 332            }
 333            else
 334            {
 335                MemoryMarshal.Write(destination, in value);
 336            }
 337        }
 338
 339        /// <summary>
 340        /// Writes a <see cref="double" /> into a span of bytes, as big endian.
 341        /// </summary>
 342        /// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
 343        /// <param name="value">The value to write into the span of bytes.</param>
 344        /// <returns>
 345        /// <see langword="true" /> if the span is large enough to contain a <see cref="double" />; otherwise, <see lang
 346        /// </returns>
 347        /// <remarks>Writes exactly 8 bytes to the beginning of the span.</remarks>
 348        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 349        public static bool TryWriteDoubleBigEndian(Span<byte> destination, double value)
 350        {
 351            if (BitConverter.IsLittleEndian)
 352            {
 0353                long tmp = ReverseEndianness(BitConverter.DoubleToInt64Bits(value));
 0354                return MemoryMarshal.TryWrite(destination, in tmp);
 355            }
 356
 357            return MemoryMarshal.TryWrite(destination, in value);
 358        }
 359
 360        /// <summary>
 361        /// Writes a <see cref="Half" /> into a span of bytes, as big endian.
 362        /// </summary>
 363        /// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
 364        /// <param name="value">The value to write into the span of bytes.</param>
 365        /// <returns>
 366        /// <see langword="true" /> if the span is large enough to contain a <see cref="Half" />; otherwise, <see langwo
 367        /// </returns>
 368        /// <remarks>Writes exactly 2 bytes to the beginning of the span.</remarks>
 369        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 370        public static bool TryWriteHalfBigEndian(Span<byte> destination, Half value)
 371        {
 372            if (BitConverter.IsLittleEndian)
 373            {
 0374                short tmp = ReverseEndianness(BitConverter.HalfToInt16Bits(value));
 0375                return MemoryMarshal.TryWrite(destination, in tmp);
 376            }
 377
 378            return MemoryMarshal.TryWrite(destination, in value);
 379        }
 380
 381        /// <summary>
 382        /// Writes a <see cref="BFloat16" /> into a span of bytes, as big endian.
 383        /// </summary>
 384        /// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
 385        /// <param name="value">The value to write into the span of bytes.</param>
 386        /// <returns>
 387        /// <see langword="true" /> if the span is large enough to contain a <see cref="BFloat16" />; otherwise, <see la
 388        /// </returns>
 389        /// <remarks>Writes exactly 2 bytes to the beginning of the span.</remarks>
 390        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 391        public static bool TryWriteBFloat16BigEndian(Span<byte> destination, BFloat16 value)
 392        {
 393            if (BitConverter.IsLittleEndian)
 394            {
 0395                short tmp = ReverseEndianness(BitConverter.BFloat16ToInt16Bits(value));
 0396                return MemoryMarshal.TryWrite(destination, in tmp);
 397            }
 398
 399            return MemoryMarshal.TryWrite(destination, in value);
 400        }
 401
 402        /// <summary>
 403        /// Writes a <see cref="short" /> into a span of bytes, as big endian.
 404        /// </summary>
 405        /// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
 406        /// <param name="value">The value to write into the span of bytes.</param>
 407        /// <returns>
 408        /// <see langword="true" /> if the span is large enough to contain a <see cref="short" />; otherwise, <see langw
 409        /// </returns>
 410        /// <remarks>Writes exactly 2 bytes to the beginning of the span.</remarks>
 411        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 412        public static bool TryWriteInt16BigEndian(Span<byte> destination, short value)
 413        {
 414            if (BitConverter.IsLittleEndian)
 415            {
 0416                short tmp = ReverseEndianness(value);
 0417                return MemoryMarshal.TryWrite(destination, in tmp);
 418            }
 419
 420            return MemoryMarshal.TryWrite(destination, in value);
 421        }
 422
 423        /// <summary>
 424        /// Writes a <see cref="int" /> into a span of bytes, as big endian.
 425        /// </summary>
 426        /// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
 427        /// <param name="value">The value to write into the span of bytes.</param>
 428        /// <returns>
 429        /// <see langword="true" /> if the span is large enough to contain a <see cref="int" />; otherwise, <see langwor
 430        /// </returns>
 431        /// <remarks>Writes exactly 4 bytes to the beginning of the span.</remarks>
 432        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 433        public static bool TryWriteInt32BigEndian(Span<byte> destination, int value)
 434        {
 435            if (BitConverter.IsLittleEndian)
 436            {
 0437                int tmp = ReverseEndianness(value);
 0438                return MemoryMarshal.TryWrite(destination, in tmp);
 439            }
 440
 441            return MemoryMarshal.TryWrite(destination, in value);
 442        }
 443
 444        /// <summary>
 445        /// Writes a <see cref="long" /> into a span of bytes, as big endian.
 446        /// </summary>
 447        /// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
 448        /// <param name="value">The value to write into the span of bytes.</param>
 449        /// <returns>
 450        /// <see langword="true" /> if the span is large enough to contain a <see cref="long" />; otherwise, <see langwo
 451        /// </returns>
 452        /// <remarks>Writes exactly 8 bytes to the beginning of the span.</remarks>
 453        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 454        public static bool TryWriteInt64BigEndian(Span<byte> destination, long value)
 455        {
 456            if (BitConverter.IsLittleEndian)
 457            {
 0458                long tmp = ReverseEndianness(value);
 0459                return MemoryMarshal.TryWrite(destination, in tmp);
 460            }
 461
 462            return MemoryMarshal.TryWrite(destination, in value);
 463        }
 464
 465        /// <summary>
 466        /// Writes a <see cref="Int128" /> into a span of bytes, as big endian.
 467        /// </summary>
 468        /// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
 469        /// <param name="value">The value to write into the span of bytes.</param>
 470        /// <returns>
 471        /// <see langword="true" /> if the span is large enough to contain a <see cref="Int128" />; otherwise, <see lang
 472        /// </returns>
 473        /// <remarks>Writes exactly 16 bytes to the beginning of the span.</remarks>
 474        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 475        public static bool TryWriteInt128BigEndian(Span<byte> destination, Int128 value)
 476        {
 477            if (BitConverter.IsLittleEndian)
 478            {
 0479                Int128 tmp = ReverseEndianness(value);
 0480                return MemoryMarshal.TryWrite(destination, in tmp);
 481            }
 482
 483            return MemoryMarshal.TryWrite(destination, in value);
 484        }
 485
 486        /// <summary>
 487        /// Writes a <see cref="nint" /> into a span of bytes, as big endian.
 488        /// </summary>
 489        /// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
 490        /// <param name="value">The value to write into the span of bytes.</param>
 491        /// <returns>
 492        /// <see langword="true" /> if the span is large enough to contain a <see cref="nint" />; otherwise, <see langwo
 493        /// </returns>
 494        /// <remarks>Writes exactly 4 bytes on 32-bit platforms -or- 8 bytes on 64-bit platforms to the beginning of the
 495        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 496        public static bool TryWriteIntPtrBigEndian(Span<byte> destination, nint value)
 497        {
 498            if (BitConverter.IsLittleEndian)
 499            {
 0500                nint tmp = ReverseEndianness(value);
 0501                return MemoryMarshal.TryWrite(destination, in tmp);
 502            }
 503
 504            return MemoryMarshal.TryWrite(destination, in value);
 505        }
 506
 507        /// <summary>
 508        /// Writes a <see cref="float" /> into a span of bytes, as big endian.
 509        /// </summary>
 510        /// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
 511        /// <param name="value">The value to write into the span of bytes.</param>
 512        /// <returns>
 513        /// <see langword="true" /> if the span is large enough to contain a <see cref="float" />; otherwise, <see langw
 514        /// </returns>
 515        /// <remarks>Writes exactly 4 bytes to the beginning of the span.</remarks>
 516        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 517        public static bool TryWriteSingleBigEndian(Span<byte> destination, float value)
 518        {
 519            if (BitConverter.IsLittleEndian)
 520            {
 0521                int tmp = ReverseEndianness(BitConverter.SingleToInt32Bits(value));
 0522                return MemoryMarshal.TryWrite(destination, in tmp);
 523            }
 524
 525            return MemoryMarshal.TryWrite(destination, in value);
 526        }
 527
 528        /// <summary>
 529        /// Write a <see cref="ushort" /> into a span of bytes, as big endian.
 530        /// </summary>
 531        /// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
 532        /// <param name="value">The value to write into the span of bytes.</param>
 533        /// <returns>
 534        /// <see langword="true" /> if the span is large enough to contain a <see cref="ushort" />; otherwise, <see lang
 535        /// </returns>
 536        /// <remarks>Writes exactly 2 bytes to the beginning of the span.</remarks>
 537        [CLSCompliant(false)]
 538        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 539        public static bool TryWriteUInt16BigEndian(Span<byte> destination, ushort value)
 540        {
 541            if (BitConverter.IsLittleEndian)
 542            {
 0543                ushort tmp = ReverseEndianness(value);
 0544                return MemoryMarshal.TryWrite(destination, in tmp);
 545            }
 546
 547            return MemoryMarshal.TryWrite(destination, in value);
 548        }
 549
 550        /// <summary>
 551        /// Write a <see cref="uint" /> into a span of bytes, as big endian.
 552        /// </summary>
 553        /// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
 554        /// <param name="value">The value to write into the span of bytes.</param>
 555        /// <returns>
 556        /// <see langword="true" /> if the span is large enough to contain a <see cref="uint" />; otherwise, <see langwo
 557        /// </returns>
 558        /// <remarks>Writes exactly 4 bytes to the beginning of the span.</remarks>
 559        [CLSCompliant(false)]
 560        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 561        public static bool TryWriteUInt32BigEndian(Span<byte> destination, uint value)
 562        {
 563            if (BitConverter.IsLittleEndian)
 564            {
 0565                uint tmp = ReverseEndianness(value);
 0566                return MemoryMarshal.TryWrite(destination, in tmp);
 567            }
 568
 569            return MemoryMarshal.TryWrite(destination, in value);
 570        }
 571
 572        /// <summary>
 573        /// Write a <see cref="ulong" /> into a span of bytes, as big endian.
 574        /// </summary>
 575        /// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
 576        /// <param name="value">The value to write into the span of bytes.</param>
 577        /// <returns>
 578        /// <see langword="true" /> if the span is large enough to contain a <see cref="ulong" />; otherwise, <see langw
 579        /// </returns>
 580        /// <remarks>Writes exactly 8 bytes to the beginning of the span.</remarks>
 581        [CLSCompliant(false)]
 582        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 583        public static bool TryWriteUInt64BigEndian(Span<byte> destination, ulong value)
 584        {
 585            if (BitConverter.IsLittleEndian)
 586            {
 0587                ulong tmp = ReverseEndianness(value);
 0588                return MemoryMarshal.TryWrite(destination, in tmp);
 589            }
 590
 591            return MemoryMarshal.TryWrite(destination, in value);
 592        }
 593
 594        /// <summary>
 595        /// Writes a <see cref="UInt128" /> into a span of bytes, as big endian.
 596        /// </summary>
 597        /// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
 598        /// <param name="value">The value to write into the span of bytes.</param>
 599        /// <returns>
 600        /// <see langword="true" /> if the span is large enough to contain a <see cref="UInt128" />; otherwise, <see lan
 601        /// </returns>
 602        /// <remarks>Writes exactly 16 bytes to the beginning of the span.</remarks>
 603        [CLSCompliant(false)]
 604        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 605        public static bool TryWriteUInt128BigEndian(Span<byte> destination, UInt128 value)
 606        {
 607            if (BitConverter.IsLittleEndian)
 608            {
 0609                UInt128 tmp = ReverseEndianness(value);
 0610                return MemoryMarshal.TryWrite(destination, in tmp);
 611            }
 612
 613            return MemoryMarshal.TryWrite(destination, in value);
 614        }
 615
 616        /// <summary>
 617        /// Writes a <see cref="nuint" /> into a span of bytes, as big endian.
 618        /// </summary>
 619        /// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
 620        /// <param name="value">The value to write into the span of bytes.</param>
 621        /// <returns>
 622        /// <see langword="true" /> if the span is large enough to contain a <see cref="nuint" />; otherwise, <see langw
 623        /// </returns>
 624        /// <remarks>Writes exactly 4 bytes on 32-bit platforms -or- 8 bytes on 64-bit platforms to the beginning of the
 625        [CLSCompliant(false)]
 626        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 627        public static bool TryWriteUIntPtrBigEndian(Span<byte> destination, nuint value)
 628        {
 629            if (BitConverter.IsLittleEndian)
 630            {
 0631                nuint tmp = ReverseEndianness(value);
 0632                return MemoryMarshal.TryWrite(destination, in tmp);
 633            }
 634
 635            return MemoryMarshal.TryWrite(destination, in value);
 636        }
 637    }
 638}
 639

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Private.CoreLib/src/System/Buffers/Binary/BinaryPrimitives.WriteLittleEndian.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Numerics;
 5using System.Runtime.CompilerServices;
 6using System.Runtime.InteropServices;
 7
 8namespace System.Buffers.Binary
 9{
 10    public static partial class BinaryPrimitives
 11    {
 12        /// <summary>
 13        /// Writes a <see cref="double" /> into a span of bytes, as little endian.
 14        /// </summary>
 15        /// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
 16        /// <param name="value">The value to write into the span of bytes.</param>
 17        /// <remarks>Writes exactly 8 bytes to the beginning of the span.</remarks>
 18        /// <exception cref="ArgumentOutOfRangeException">
 19        /// <paramref name="destination" /> is too small to contain a <see cref="double" />.
 20        /// </exception>
 21        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 22        public static void WriteDoubleLittleEndian(Span<byte> destination, double value)
 23        {
 24            if (!BitConverter.IsLittleEndian)
 25            {
 26                long tmp = ReverseEndianness(BitConverter.DoubleToInt64Bits(value));
 27                MemoryMarshal.Write(destination, in tmp);
 28            }
 29            else
 30            {
 031                MemoryMarshal.Write(destination, in value);
 32            }
 033        }
 34
 35        /// <summary>
 36        /// Writes a <see cref="Half" /> into a span of bytes, as little endian.
 37        /// </summary>
 38        /// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
 39        /// <param name="value">The value to write into the span of bytes.</param>
 40        /// <remarks>Writes exactly 2 bytes to the beginning of the span.</remarks>
 41        /// <exception cref="ArgumentOutOfRangeException">
 42        /// <paramref name="destination" /> is too small to contain a <see cref="Half" />.
 43        /// </exception>
 44        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 45        public static void WriteHalfLittleEndian(Span<byte> destination, Half value)
 46        {
 47            if (!BitConverter.IsLittleEndian)
 48            {
 49                short tmp = ReverseEndianness(BitConverter.HalfToInt16Bits(value));
 50                MemoryMarshal.Write(destination, in tmp);
 51            }
 52            else
 53            {
 054                MemoryMarshal.Write(destination, in value);
 55            }
 056        }
 57
 58        /// <summary>
 59        /// Writes a <see cref="BFloat16" /> into a span of bytes, as little endian.
 60        /// </summary>
 61        /// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
 62        /// <param name="value">The value to write into the span of bytes.</param>
 63        /// <remarks>Writes exactly 2 bytes to the beginning of the span.</remarks>
 64        /// <exception cref="ArgumentOutOfRangeException">
 65        /// <paramref name="destination" /> is too small to contain a <see cref="BFloat16" />.
 66        /// </exception>
 67        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 68        public static void WriteBFloat16LittleEndian(Span<byte> destination, BFloat16 value)
 69        {
 70            if (!BitConverter.IsLittleEndian)
 71            {
 72                short tmp = ReverseEndianness(BitConverter.BFloat16ToInt16Bits(value));
 73                MemoryMarshal.Write(destination, in tmp);
 74            }
 75            else
 76            {
 077                MemoryMarshal.Write(destination, in value);
 78            }
 079        }
 80
 81        /// <summary>
 82        /// Writes a <see cref="short" /> into a span of bytes, as little endian.
 83        /// </summary>
 84        /// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
 85        /// <param name="value">The value to write into the span of bytes.</param>
 86        /// <remarks>Writes exactly 2 bytes to the beginning of the span.</remarks>
 87        /// <exception cref="ArgumentOutOfRangeException">
 88        /// <paramref name="destination" /> is too small to contain a <see cref="short" />.
 89        /// </exception>
 90        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 91        public static void WriteInt16LittleEndian(Span<byte> destination, short value)
 92        {
 93            if (!BitConverter.IsLittleEndian)
 94            {
 95                short tmp = ReverseEndianness(value);
 96                MemoryMarshal.Write(destination, in tmp);
 97            }
 98            else
 99            {
 0100                MemoryMarshal.Write(destination, in value);
 101            }
 0102        }
 103
 104        /// <summary>
 105        /// Writes a <see cref="int" /> into a span of bytes, as little endian.
 106        /// </summary>
 107        /// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
 108        /// <param name="value">The value to write into the span of bytes.</param>
 109        /// <remarks>Writes exactly 4 bytes to the beginning of the span.</remarks>
 110        /// <exception cref="ArgumentOutOfRangeException">
 111        /// <paramref name="destination" /> is too small to contain a <see cref="int" />.
 112        /// </exception>
 113        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 114        public static void WriteInt32LittleEndian(Span<byte> destination, int value)
 115        {
 116            if (!BitConverter.IsLittleEndian)
 117            {
 118                int tmp = ReverseEndianness(value);
 119                MemoryMarshal.Write(destination, in tmp);
 120            }
 121            else
 122            {
 0123                MemoryMarshal.Write(destination, in value);
 124            }
 0125        }
 126
 127        /// <summary>
 128        /// Writes a <see cref="long" /> into a span of bytes, as little endian.
 129        /// </summary>
 130        /// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
 131        /// <param name="value">The value to write into the span of bytes.</param>
 132        /// <remarks>Writes exactly 8 bytes to the beginning of the span.</remarks>
 133        /// <exception cref="ArgumentOutOfRangeException">
 134        /// <paramref name="destination" /> is too small to contain a <see cref="long" />.
 135        /// </exception>
 136        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 137        public static void WriteInt64LittleEndian(Span<byte> destination, long value)
 138        {
 139            if (!BitConverter.IsLittleEndian)
 140            {
 141                long tmp = ReverseEndianness(value);
 142                MemoryMarshal.Write(destination, in tmp);
 143            }
 144            else
 145            {
 0146                MemoryMarshal.Write(destination, in value);
 147            }
 0148        }
 149
 150        /// <summary>
 151        /// Writes a <see cref="Int128" /> into a span of bytes, as little endian.
 152        /// </summary>
 153        /// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
 154        /// <param name="value">The value to write into the span of bytes.</param>
 155        /// <remarks>Writes exactly 16 bytes to the beginning of the span.</remarks>
 156        /// <exception cref="ArgumentOutOfRangeException">
 157        /// <paramref name="destination" /> is too small to contain a <see cref="Int128" />.
 158        /// </exception>
 159        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 160        public static void WriteInt128LittleEndian(Span<byte> destination, Int128 value)
 161        {
 162            if (!BitConverter.IsLittleEndian)
 163            {
 164                Int128 tmp = ReverseEndianness(value);
 165                MemoryMarshal.Write(destination, in tmp);
 166            }
 167            else
 168            {
 0169                MemoryMarshal.Write(destination, in value);
 170            }
 0171        }
 172
 173        /// <summary>
 174        /// Writes a <see cref="nint" /> into a span of bytes, as little endian.
 175        /// </summary>
 176        /// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
 177        /// <param name="value">The value to write into the span of bytes.</param>
 178        /// <remarks>Writes exactly 4 bytes on 32-bit platforms -or- 8 bytes on 64-bit platforms to the beginning of the
 179        /// <exception cref="ArgumentOutOfRangeException">
 180        /// <paramref name="destination" /> is too small to contain a <see cref="nint" />.
 181        /// </exception>
 182        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 183        public static void WriteIntPtrLittleEndian(Span<byte> destination, nint value)
 184        {
 185            if (!BitConverter.IsLittleEndian)
 186            {
 187                nint tmp = ReverseEndianness(value);
 188                MemoryMarshal.Write(destination, in tmp);
 189            }
 190            else
 191            {
 0192                MemoryMarshal.Write(destination, in value);
 193            }
 0194        }
 195
 196        /// <summary>
 197        /// Writes a <see cref="float" /> into a span of bytes, as little endian.
 198        /// </summary>
 199        /// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
 200        /// <param name="value">The value to write into the span of bytes.</param>
 201        /// <remarks>Writes exactly 4 bytes to the beginning of the span.</remarks>
 202        /// <exception cref="ArgumentOutOfRangeException">
 203        /// <paramref name="destination" /> is too small to contain a <see cref="float" />.
 204        /// </exception>
 205        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 206        public static void WriteSingleLittleEndian(Span<byte> destination, float value)
 207        {
 208            if (!BitConverter.IsLittleEndian)
 209            {
 210                int tmp = ReverseEndianness(BitConverter.SingleToInt32Bits(value));
 211                MemoryMarshal.Write(destination, in tmp);
 212            }
 213            else
 214            {
 0215                MemoryMarshal.Write(destination, in value);
 216            }
 0217        }
 218
 219        /// <summary>
 220        /// Write a <see cref="ushort" /> into a span of bytes, as little endian.
 221        /// </summary>
 222        /// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
 223        /// <param name="value">The value to write into the span of bytes.</param>
 224        /// <remarks>Writes exactly 2 bytes to the beginning of the span.</remarks>
 225        /// <exception cref="ArgumentOutOfRangeException">
 226        /// <paramref name="destination" /> is too small to contain a <see cref="ushort" />.
 227        /// </exception>
 228        [CLSCompliant(false)]
 229        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 230        public static void WriteUInt16LittleEndian(Span<byte> destination, ushort value)
 231        {
 232            if (!BitConverter.IsLittleEndian)
 233            {
 234                ushort tmp = ReverseEndianness(value);
 235                MemoryMarshal.Write(destination, in tmp);
 236            }
 237            else
 238            {
 0239                MemoryMarshal.Write(destination, in value);
 240            }
 0241        }
 242
 243        /// <summary>
 244        /// Write a <see cref="uint" /> into a span of bytes, as little endian.
 245        /// </summary>
 246        /// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
 247        /// <param name="value">The value to write into the span of bytes.</param>
 248        /// <remarks>Writes exactly 4 bytes to the beginning of the span.</remarks>
 249        /// <exception cref="ArgumentOutOfRangeException">
 250        /// <paramref name="destination" /> is too small to contain a <see cref="uint" />.
 251        /// </exception>
 252        [CLSCompliant(false)]
 253        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 254        public static void WriteUInt32LittleEndian(Span<byte> destination, uint value)
 255        {
 256            if (!BitConverter.IsLittleEndian)
 257            {
 258                uint tmp = ReverseEndianness(value);
 259                MemoryMarshal.Write(destination, in tmp);
 260            }
 261            else
 262            {
 0263                MemoryMarshal.Write(destination, in value);
 264            }
 0265        }
 266
 267        /// <summary>
 268        /// Write a <see cref="ulong" /> into a span of bytes, as little endian.
 269        /// </summary>
 270        /// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
 271        /// <param name="value">The value to write into the span of bytes.</param>
 272        /// <remarks>Writes exactly 8 bytes to the beginning of the span.</remarks>
 273        /// <exception cref="ArgumentOutOfRangeException">
 274        /// <paramref name="destination" /> is too small to contain a <see cref="ulong" />.
 275        /// </exception>
 276        [CLSCompliant(false)]
 277        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 278        public static void WriteUInt64LittleEndian(Span<byte> destination, ulong value)
 279        {
 280            if (!BitConverter.IsLittleEndian)
 281            {
 282                ulong tmp = ReverseEndianness(value);
 283                MemoryMarshal.Write(destination, in tmp);
 284            }
 285            else
 286            {
 0287                MemoryMarshal.Write(destination, in value);
 288            }
 0289        }
 290
 291        /// <summary>
 292        /// Writes a <see cref="UInt128" /> into a span of bytes, as little endian.
 293        /// </summary>
 294        /// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
 295        /// <param name="value">The value to write into the span of bytes.</param>
 296        /// <remarks>Writes exactly 16 bytes to the beginning of the span.</remarks>
 297        /// <exception cref="ArgumentOutOfRangeException">
 298        /// <paramref name="destination" /> is too small to contain a <see cref="UInt128" />.
 299        /// </exception>
 300        [CLSCompliant(false)]
 301        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 302        public static void WriteUInt128LittleEndian(Span<byte> destination, UInt128 value)
 303        {
 304            if (!BitConverter.IsLittleEndian)
 305            {
 306                UInt128 tmp = ReverseEndianness(value);
 307                MemoryMarshal.Write(destination, in tmp);
 308            }
 309            else
 310            {
 0311                MemoryMarshal.Write(destination, in value);
 312            }
 0313        }
 314
 315        /// <summary>
 316        /// Writes a <see cref="nuint" /> into a span of bytes, as little endian.
 317        /// </summary>
 318        /// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
 319        /// <param name="value">The value to write into the span of bytes.</param>
 320        /// <remarks>Writes exactly 4 bytes on 32-bit platforms -or- 8 bytes on 64-bit platforms to the beginning of the
 321        /// <exception cref="ArgumentOutOfRangeException">
 322        /// <paramref name="destination" /> is too small to contain a <see cref="nuint" />.
 323        /// </exception>
 324        [CLSCompliant(false)]
 325        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 326        public static void WriteUIntPtrLittleEndian(Span<byte> destination, nuint value)
 327        {
 328            if (!BitConverter.IsLittleEndian)
 329            {
 330                nuint tmp = ReverseEndianness(value);
 331                MemoryMarshal.Write(destination, in tmp);
 332            }
 333            else
 334            {
 0335                MemoryMarshal.Write(destination, in value);
 336            }
 0337        }
 338
 339        /// <summary>
 340        /// Writes a <see cref="double" /> into a span of bytes, as little endian.
 341        /// </summary>
 342        /// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
 343        /// <param name="value">The value to write into the span of bytes.</param>
 344        /// <returns>
 345        /// <see langword="true" /> if the span is large enough to contain a <see cref="double" />; otherwise, <see lang
 346        /// </returns>
 347        /// <remarks>Writes exactly 8 bytes to the beginning of the span.</remarks>
 348        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 349        public static bool TryWriteDoubleLittleEndian(Span<byte> destination, double value)
 350        {
 351            if (!BitConverter.IsLittleEndian)
 352            {
 353                long tmp = ReverseEndianness(BitConverter.DoubleToInt64Bits(value));
 354                return MemoryMarshal.TryWrite(destination, in tmp);
 355            }
 356
 0357            return MemoryMarshal.TryWrite(destination, in value);
 358        }
 359
 360        /// <summary>
 361        /// Writes a <see cref="Half" /> into a span of bytes, as little endian.
 362        /// </summary>
 363        /// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
 364        /// <param name="value">The value to write into the span of bytes.</param>
 365        /// <returns>
 366        /// <see langword="true" /> if the span is large enough to contain a <see cref="Half" />; otherwise, <see langwo
 367        /// </returns>
 368        /// <remarks>Writes exactly 2 bytes to the beginning of the span.</remarks>
 369        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 370        public static bool TryWriteHalfLittleEndian(Span<byte> destination, Half value)
 371        {
 372            if (!BitConverter.IsLittleEndian)
 373            {
 374                short tmp = ReverseEndianness(BitConverter.HalfToInt16Bits(value));
 375                return MemoryMarshal.TryWrite(destination, in tmp);
 376            }
 377
 0378            return MemoryMarshal.TryWrite(destination, in value);
 379        }
 380
 381        /// <summary>
 382        /// Writes a <see cref="BFloat16" /> into a span of bytes, as little endian.
 383        /// </summary>
 384        /// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
 385        /// <param name="value">The value to write into the span of bytes.</param>
 386        /// <returns>
 387        /// <see langword="true" /> if the span is large enough to contain a <see cref="BFloat16" />; otherwise, <see la
 388        /// </returns>
 389        /// <remarks>Writes exactly 2 bytes to the beginning of the span.</remarks>
 390        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 391        public static bool TryWriteBFloat16LittleEndian(Span<byte> destination, BFloat16 value)
 392        {
 393            if (!BitConverter.IsLittleEndian)
 394            {
 395                short tmp = ReverseEndianness(BitConverter.BFloat16ToInt16Bits(value));
 396                return MemoryMarshal.TryWrite(destination, in tmp);
 397            }
 398
 0399            return MemoryMarshal.TryWrite(destination, in value);
 400        }
 401
 402        /// <summary>
 403        /// Writes a <see cref="short" /> into a span of bytes, as little endian.
 404        /// </summary>
 405        /// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
 406        /// <param name="value">The value to write into the span of bytes.</param>
 407        /// <returns>
 408        /// <see langword="true" /> if the span is large enough to contain a <see cref="short" />; otherwise, <see langw
 409        /// </returns>
 410        /// <remarks>Writes exactly 2 bytes to the beginning of the span.</remarks>
 411        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 412        public static bool TryWriteInt16LittleEndian(Span<byte> destination, short value)
 413        {
 414            if (!BitConverter.IsLittleEndian)
 415            {
 416                short tmp = ReverseEndianness(value);
 417                return MemoryMarshal.TryWrite(destination, in tmp);
 418            }
 419
 0420            return MemoryMarshal.TryWrite(destination, in value);
 421        }
 422
 423        /// <summary>
 424        /// Writes a <see cref="int" /> into a span of bytes, as little endian.
 425        /// </summary>
 426        /// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
 427        /// <param name="value">The value to write into the span of bytes.</param>
 428        /// <returns>
 429        /// <see langword="true" /> if the span is large enough to contain a <see cref="int" />; otherwise, <see langwor
 430        /// </returns>
 431        /// <remarks>Writes exactly 4 bytes to the beginning of the span.</remarks>
 432        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 433        public static bool TryWriteInt32LittleEndian(Span<byte> destination, int value)
 434        {
 435            if (!BitConverter.IsLittleEndian)
 436            {
 437                int tmp = ReverseEndianness(value);
 438                return MemoryMarshal.TryWrite(destination, in tmp);
 439            }
 440
 0441            return MemoryMarshal.TryWrite(destination, in value);
 442        }
 443
 444        /// <summary>
 445        /// Writes a <see cref="long" /> into a span of bytes, as little endian.
 446        /// </summary>
 447        /// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
 448        /// <param name="value">The value to write into the span of bytes.</param>
 449        /// <returns>
 450        /// <see langword="true" /> if the span is large enough to contain a <see cref="long" />; otherwise, <see langwo
 451        /// </returns>
 452        /// <remarks>Writes exactly 8 bytes to the beginning of the span.</remarks>
 453        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 454        public static bool TryWriteInt64LittleEndian(Span<byte> destination, long value)
 455        {
 456            if (!BitConverter.IsLittleEndian)
 457            {
 458                long tmp = ReverseEndianness(value);
 459                return MemoryMarshal.TryWrite(destination, in tmp);
 460            }
 461
 0462            return MemoryMarshal.TryWrite(destination, in value);
 463        }
 464
 465        /// <summary>
 466        /// Writes a <see cref="Int128" /> into a span of bytes, as little endian.
 467        /// </summary>
 468        /// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
 469        /// <param name="value">The value to write into the span of bytes.</param>
 470        /// <returns>
 471        /// <see langword="true" /> if the span is large enough to contain a <see cref="Int128" />; otherwise, <see lang
 472        /// </returns>
 473        /// <remarks>Writes exactly 16 bytes to the beginning of the span.</remarks>
 474        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 475        public static bool TryWriteInt128LittleEndian(Span<byte> destination, Int128 value)
 476        {
 477            if (!BitConverter.IsLittleEndian)
 478            {
 479                Int128 tmp = ReverseEndianness(value);
 480                return MemoryMarshal.TryWrite(destination, in tmp);
 481            }
 482
 0483            return MemoryMarshal.TryWrite(destination, in value);
 484        }
 485
 486        /// <summary>
 487        /// Writes a <see cref="nint" /> into a span of bytes, as little endian.
 488        /// </summary>
 489        /// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
 490        /// <param name="value">The value to write into the span of bytes.</param>
 491        /// <returns>
 492        /// <see langword="true" /> if the span is large enough to contain a <see cref="nint" />; otherwise, <see langwo
 493        /// </returns>
 494        /// <remarks>Writes exactly 4 bytes on 32-bit platforms -or- 8 bytes on 64-bit platforms to the beginning of the
 495        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 496        public static bool TryWriteIntPtrLittleEndian(Span<byte> destination, nint value)
 497        {
 498            if (!BitConverter.IsLittleEndian)
 499            {
 500                nint tmp = ReverseEndianness(value);
 501                return MemoryMarshal.TryWrite(destination, in tmp);
 502            }
 503
 0504            return MemoryMarshal.TryWrite(destination, in value);
 505        }
 506
 507        /// <summary>
 508        /// Writes a <see cref="float" /> into a span of bytes, as little endian.
 509        /// </summary>
 510        /// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
 511        /// <param name="value">The value to write into the span of bytes.</param>
 512        /// <returns>
 513        /// <see langword="true" /> if the span is large enough to contain a <see cref="float" />; otherwise, <see langw
 514        /// </returns>
 515        /// <remarks>Writes exactly 4 bytes to the beginning of the span.</remarks>
 516        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 517        public static bool TryWriteSingleLittleEndian(Span<byte> destination, float value)
 518        {
 519            if (!BitConverter.IsLittleEndian)
 520            {
 521                int tmp = ReverseEndianness(BitConverter.SingleToInt32Bits(value));
 522                return MemoryMarshal.TryWrite(destination, in tmp);
 523            }
 524
 0525            return MemoryMarshal.TryWrite(destination, in value);
 526        }
 527
 528        /// <summary>
 529        /// Write a <see cref="ushort" /> into a span of bytes, as little endian.
 530        /// </summary>
 531        /// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
 532        /// <param name="value">The value to write into the span of bytes.</param>
 533        /// <returns>
 534        /// <see langword="true" /> if the span is large enough to contain a <see cref="ushort" />; otherwise, <see lang
 535        /// </returns>
 536        /// <remarks>Writes exactly 2 bytes to the beginning of the span.</remarks>
 537        [CLSCompliant(false)]
 538        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 539        public static bool TryWriteUInt16LittleEndian(Span<byte> destination, ushort value)
 540        {
 541            if (!BitConverter.IsLittleEndian)
 542            {
 543                ushort tmp = ReverseEndianness(value);
 544                return MemoryMarshal.TryWrite(destination, in tmp);
 545            }
 546
 0547            return MemoryMarshal.TryWrite(destination, in value);
 548        }
 549
 550        /// <summary>
 551        /// Write a <see cref="uint" /> into a span of bytes, as little endian.
 552        /// </summary>
 553        /// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
 554        /// <param name="value">The value to write into the span of bytes.</param>
 555        /// <returns>
 556        /// <see langword="true" /> if the span is large enough to contain a <see cref="uint" />; otherwise, <see langwo
 557        /// </returns>
 558        /// <remarks>Writes exactly 4 bytes to the beginning of the span.</remarks>
 559        [CLSCompliant(false)]
 560        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 561        public static bool TryWriteUInt32LittleEndian(Span<byte> destination, uint value)
 562        {
 563            if (!BitConverter.IsLittleEndian)
 564            {
 565                uint tmp = ReverseEndianness(value);
 566                return MemoryMarshal.TryWrite(destination, in tmp);
 567            }
 568
 0569            return MemoryMarshal.TryWrite(destination, in value);
 570        }
 571
 572        /// <summary>
 573        /// Write a <see cref="ulong" /> into a span of bytes, as little endian.
 574        /// </summary>
 575        /// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
 576        /// <param name="value">The value to write into the span of bytes.</param>
 577        /// <returns>
 578        /// <see langword="true" /> if the span is large enough to contain a <see cref="ulong" />; otherwise, <see langw
 579        /// </returns>
 580        /// <remarks>Writes exactly 8 bytes to the beginning of the span.</remarks>
 581        [CLSCompliant(false)]
 582        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 583        public static bool TryWriteUInt64LittleEndian(Span<byte> destination, ulong value)
 584        {
 585            if (!BitConverter.IsLittleEndian)
 586            {
 587                ulong tmp = ReverseEndianness(value);
 588                return MemoryMarshal.TryWrite(destination, in tmp);
 589            }
 590
 0591            return MemoryMarshal.TryWrite(destination, in value);
 592        }
 593
 594        /// <summary>
 595        /// Writes a <see cref="UInt128" /> into a span of bytes, as little endian.
 596        /// </summary>
 597        /// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
 598        /// <param name="value">The value to write into the span of bytes.</param>
 599        /// <returns>
 600        /// <see langword="true" /> if the span is large enough to contain a <see cref="UInt128" />; otherwise, <see lan
 601        /// </returns>
 602        /// <remarks>Writes exactly 16 bytes to the beginning of the span.</remarks>
 603        [CLSCompliant(false)]
 604        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 605        public static bool TryWriteUInt128LittleEndian(Span<byte> destination, UInt128 value)
 606        {
 607            if (!BitConverter.IsLittleEndian)
 608            {
 609                UInt128 tmp = ReverseEndianness(value);
 610                return MemoryMarshal.TryWrite(destination, in tmp);
 611            }
 612
 0613            return MemoryMarshal.TryWrite(destination, in value);
 614        }
 615
 616        /// <summary>
 617        /// Writes a <see cref="nuint" /> into a span of bytes, as little endian.
 618        /// </summary>
 619        /// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
 620        /// <param name="value">The value to write into the span of bytes.</param>
 621        /// <returns>
 622        /// <see langword="true" /> if the span is large enough to contain a <see cref="nuint" />; otherwise, <see langw
 623        /// </returns>
 624        /// <remarks>Writes exactly 4 bytes on 32-bit platforms -or- 8 bytes on 64-bit platforms to the beginning of the
 625        [CLSCompliant(false)]
 626        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 627        public static bool TryWriteUIntPtrLittleEndian(Span<byte> destination, nuint value)
 628        {
 629            if (!BitConverter.IsLittleEndian)
 630            {
 631                nuint tmp = ReverseEndianness(value);
 632                return MemoryMarshal.TryWrite(destination, in tmp);
 633            }
 634
 0635            return MemoryMarshal.TryWrite(destination, in value);
 636        }
 637    }
 638}
 639

Methods/Properties

TryReadDoubleBigEndian(System.ReadOnlySpan`1<System.Byte>,System.Double&)
TryReadHalfBigEndian(System.ReadOnlySpan`1<System.Byte>,System.Half&)
TryReadBFloat16BigEndian(System.ReadOnlySpan`1<System.Byte>,System.Numerics.BFloat16&)
TryReadInt16BigEndian(System.ReadOnlySpan`1<System.Byte>,System.Int16&)
TryReadInt32BigEndian(System.ReadOnlySpan`1<System.Byte>,System.Int32&)
TryReadInt64BigEndian(System.ReadOnlySpan`1<System.Byte>,System.Int64&)
TryReadInt128BigEndian(System.ReadOnlySpan`1<System.Byte>,System.Int128&)
TryReadIntPtrBigEndian(System.ReadOnlySpan`1<System.Byte>,System.IntPtr&)
TryReadSingleBigEndian(System.ReadOnlySpan`1<System.Byte>,System.Single&)
TryReadUInt16BigEndian(System.ReadOnlySpan`1<System.Byte>,System.UInt16&)
TryReadUInt32BigEndian(System.ReadOnlySpan`1<System.Byte>,System.UInt32&)
TryReadUInt64BigEndian(System.ReadOnlySpan`1<System.Byte>,System.UInt64&)
TryReadUInt128BigEndian(System.ReadOnlySpan`1<System.Byte>,System.UInt128&)
TryReadUIntPtrBigEndian(System.ReadOnlySpan`1<System.Byte>,System.UIntPtr&)
TryReadDoubleLittleEndian(System.ReadOnlySpan`1<System.Byte>,System.Double&)
TryReadHalfLittleEndian(System.ReadOnlySpan`1<System.Byte>,System.Half&)
TryReadBFloat16LittleEndian(System.ReadOnlySpan`1<System.Byte>,System.Numerics.BFloat16&)
TryReadInt16LittleEndian(System.ReadOnlySpan`1<System.Byte>,System.Int16&)
TryReadInt32LittleEndian(System.ReadOnlySpan`1<System.Byte>,System.Int32&)
TryReadInt64LittleEndian(System.ReadOnlySpan`1<System.Byte>,System.Int64&)
TryReadInt128LittleEndian(System.ReadOnlySpan`1<System.Byte>,System.Int128&)
TryReadIntPtrLittleEndian(System.ReadOnlySpan`1<System.Byte>,System.IntPtr&)
TryReadSingleLittleEndian(System.ReadOnlySpan`1<System.Byte>,System.Single&)
TryReadUInt16LittleEndian(System.ReadOnlySpan`1<System.Byte>,System.UInt16&)
TryReadUInt32LittleEndian(System.ReadOnlySpan`1<System.Byte>,System.UInt32&)
TryReadUInt64LittleEndian(System.ReadOnlySpan`1<System.Byte>,System.UInt64&)
TryReadUInt128LittleEndian(System.ReadOnlySpan`1<System.Byte>,System.UInt128&)
TryReadUIntPtrLittleEndian(System.ReadOnlySpan`1<System.Byte>,System.UIntPtr&)
ReverseEndianness(System.SByte)
ReverseEndianness(System.Int16)
ReverseEndianness(System.Int32)
ReverseEndianness(System.Int64)
ReverseEndianness(System.IntPtr)
ReverseEndianness(System.Int128)
ReverseEndianness(System.Byte)
ReverseEndianness(System.UInt16)
ReverseEndianness(System.UInt32)
ReverseEndianness(System.UInt64)
ReverseEndianness(System.UIntPtr)
ReverseEndianness(System.UInt128)
ReverseEndianness(System.ReadOnlySpan`1<System.UInt16>,System.Span`1<System.UInt16>)
ReverseEndianness(System.ReadOnlySpan`1<System.Int16>,System.Span`1<System.Int16>)
ReverseEndianness(System.ReadOnlySpan`1<System.UInt32>,System.Span`1<System.UInt32>)
ReverseEndianness(System.ReadOnlySpan`1<System.Int32>,System.Span`1<System.Int32>)
ReverseEndianness(System.ReadOnlySpan`1<System.UInt64>,System.Span`1<System.UInt64>)
ReverseEndianness(System.ReadOnlySpan`1<System.Int64>,System.Span`1<System.Int64>)
ReverseEndianness(System.ReadOnlySpan`1<System.UIntPtr>,System.Span`1<System.UIntPtr>)
ReverseEndianness(System.ReadOnlySpan`1<System.IntPtr>,System.Span`1<System.IntPtr>)
Reverse(System.Int16)
Reverse(System.Runtime.Intrinsics.Vector128`1<System.Int16>)
Reverse(System.Runtime.Intrinsics.Vector256`1<System.Int16>)
Reverse(System.Int32)
Reverse(System.Runtime.Intrinsics.Vector128`1<System.Int32>)
Reverse(System.Runtime.Intrinsics.Vector256`1<System.Int32>)
Reverse(System.Int64)
Reverse(System.Runtime.Intrinsics.Vector128`1<System.Int64>)
Reverse(System.Runtime.Intrinsics.Vector256`1<System.Int64>)
ReverseEndianness(System.ReadOnlySpan`1<T>,System.Span`1<T>)
ReverseEndianness(System.ReadOnlySpan`1<System.UInt128>,System.Span`1<System.UInt128>)
ReverseEndianness(System.ReadOnlySpan`1<System.Int128>,System.Span`1<System.Int128>)
ThrowDestinationTooSmall()
WriteDoubleBigEndian(System.Span`1<System.Byte>,System.Double)
WriteHalfBigEndian(System.Span`1<System.Byte>,System.Half)
WriteBFloat16BigEndian(System.Span`1<System.Byte>,System.Numerics.BFloat16)
WriteInt16BigEndian(System.Span`1<System.Byte>,System.Int16)
WriteInt32BigEndian(System.Span`1<System.Byte>,System.Int32)
WriteInt64BigEndian(System.Span`1<System.Byte>,System.Int64)
WriteInt128BigEndian(System.Span`1<System.Byte>,System.Int128)
WriteIntPtrBigEndian(System.Span`1<System.Byte>,System.IntPtr)
WriteSingleBigEndian(System.Span`1<System.Byte>,System.Single)
WriteUInt16BigEndian(System.Span`1<System.Byte>,System.UInt16)
WriteUInt32BigEndian(System.Span`1<System.Byte>,System.UInt32)
WriteUInt64BigEndian(System.Span`1<System.Byte>,System.UInt64)
WriteUInt128BigEndian(System.Span`1<System.Byte>,System.UInt128)
WriteUIntPtrBigEndian(System.Span`1<System.Byte>,System.UIntPtr)
TryWriteDoubleBigEndian(System.Span`1<System.Byte>,System.Double)
TryWriteHalfBigEndian(System.Span`1<System.Byte>,System.Half)
TryWriteBFloat16BigEndian(System.Span`1<System.Byte>,System.Numerics.BFloat16)
TryWriteInt16BigEndian(System.Span`1<System.Byte>,System.Int16)
TryWriteInt32BigEndian(System.Span`1<System.Byte>,System.Int32)
TryWriteInt64BigEndian(System.Span`1<System.Byte>,System.Int64)
TryWriteInt128BigEndian(System.Span`1<System.Byte>,System.Int128)
TryWriteIntPtrBigEndian(System.Span`1<System.Byte>,System.IntPtr)
TryWriteSingleBigEndian(System.Span`1<System.Byte>,System.Single)
TryWriteUInt16BigEndian(System.Span`1<System.Byte>,System.UInt16)
TryWriteUInt32BigEndian(System.Span`1<System.Byte>,System.UInt32)
TryWriteUInt64BigEndian(System.Span`1<System.Byte>,System.UInt64)
TryWriteUInt128BigEndian(System.Span`1<System.Byte>,System.UInt128)
TryWriteUIntPtrBigEndian(System.Span`1<System.Byte>,System.UIntPtr)
WriteDoubleLittleEndian(System.Span`1<System.Byte>,System.Double)
WriteHalfLittleEndian(System.Span`1<System.Byte>,System.Half)
WriteBFloat16LittleEndian(System.Span`1<System.Byte>,System.Numerics.BFloat16)
WriteInt16LittleEndian(System.Span`1<System.Byte>,System.Int16)
WriteInt32LittleEndian(System.Span`1<System.Byte>,System.Int32)
WriteInt64LittleEndian(System.Span`1<System.Byte>,System.Int64)
WriteInt128LittleEndian(System.Span`1<System.Byte>,System.Int128)
WriteIntPtrLittleEndian(System.Span`1<System.Byte>,System.IntPtr)
WriteSingleLittleEndian(System.Span`1<System.Byte>,System.Single)
WriteUInt16LittleEndian(System.Span`1<System.Byte>,System.UInt16)
WriteUInt32LittleEndian(System.Span`1<System.Byte>,System.UInt32)
WriteUInt64LittleEndian(System.Span`1<System.Byte>,System.UInt64)
WriteUInt128LittleEndian(System.Span`1<System.Byte>,System.UInt128)
WriteUIntPtrLittleEndian(System.Span`1<System.Byte>,System.UIntPtr)
TryWriteDoubleLittleEndian(System.Span`1<System.Byte>,System.Double)
TryWriteHalfLittleEndian(System.Span`1<System.Byte>,System.Half)
TryWriteBFloat16LittleEndian(System.Span`1<System.Byte>,System.Numerics.BFloat16)
TryWriteInt16LittleEndian(System.Span`1<System.Byte>,System.Int16)
TryWriteInt32LittleEndian(System.Span`1<System.Byte>,System.Int32)
TryWriteInt64LittleEndian(System.Span`1<System.Byte>,System.Int64)
TryWriteInt128LittleEndian(System.Span`1<System.Byte>,System.Int128)
TryWriteIntPtrLittleEndian(System.Span`1<System.Byte>,System.IntPtr)
TryWriteSingleLittleEndian(System.Span`1<System.Byte>,System.Single)
TryWriteUInt16LittleEndian(System.Span`1<System.Byte>,System.UInt16)
TryWriteUInt32LittleEndian(System.Span`1<System.Byte>,System.UInt32)
TryWriteUInt64LittleEndian(System.Span`1<System.Byte>,System.UInt64)
TryWriteUInt128LittleEndian(System.Span`1<System.Byte>,System.UInt128)
TryWriteUIntPtrLittleEndian(System.Span`1<System.Byte>,System.UIntPtr)