Skip to content

Commit

Permalink
Changed: Switch to BinaryPrimitives to reduce IL Size
Browse files Browse the repository at this point in the history
  • Loading branch information
Sewer56 committed Nov 5, 2023
1 parent fcd6044 commit c048e70
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/Reloaded.Memory/Extensions/EndianExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Buffers.Binary;
using System.Diagnostics.CodeAnalysis;
using Reloaded.Memory.Utilities;

Expand Down Expand Up @@ -32,7 +33,7 @@ public static class EndianExtensions
/// </summary>
/// <param name="value">The value whose endian to convert.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static short AsLittleEndian(this short value) => BitConverter.IsLittleEndian ? value : Endian.Reverse(value);
public static short AsLittleEndian(this short value) => BitConverter.IsLittleEndian ? value : BinaryPrimitives.ReverseEndianness(value);

/// <summary>
/// Converts <paramref name="value"/> to little endian.
Expand All @@ -41,39 +42,39 @@ public static class EndianExtensions
/// <param name="value">The value whose endian to convert.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ushort AsLittleEndian(this ushort value) =>
BitConverter.IsLittleEndian ? value : Endian.Reverse(value);
BitConverter.IsLittleEndian ? value : BinaryPrimitives.ReverseEndianness(value);

/// <summary>
/// Converts <paramref name="value"/> to little endian.
/// On little endian this is a no-op. On big endian the bytes are swapped.
/// </summary>
/// <param name="value">The value whose endian to convert.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int AsLittleEndian(this int value) => BitConverter.IsLittleEndian ? value : Endian.Reverse(value);
public static int AsLittleEndian(this int value) => BitConverter.IsLittleEndian ? value : BinaryPrimitives.ReverseEndianness(value);

/// <summary>
/// Converts <paramref name="value"/> to little endian.
/// On little endian this is a no-op. On big endian the bytes are swapped.
/// </summary>
/// <param name="value">The value whose endian to convert.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint AsLittleEndian(this uint value) => BitConverter.IsLittleEndian ? value : Endian.Reverse(value);
public static uint AsLittleEndian(this uint value) => BitConverter.IsLittleEndian ? value : BinaryPrimitives.ReverseEndianness(value);

/// <summary>
/// Converts <paramref name="value"/> to little endian.
/// On little endian this is a no-op. On big endian the bytes are swapped.
/// </summary>
/// <param name="value">The value whose endian to convert.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long AsLittleEndian(this long value) => BitConverter.IsLittleEndian ? value : Endian.Reverse(value);
public static long AsLittleEndian(this long value) => BitConverter.IsLittleEndian ? value : BinaryPrimitives.ReverseEndianness(value);

/// <summary>
/// Converts <paramref name="value"/> to little endian.
/// On little endian this is a no-op. On big endian the bytes are swapped.
/// </summary>
/// <param name="value">The value whose endian to convert.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong AsLittleEndian(this ulong value) => BitConverter.IsLittleEndian ? value : Endian.Reverse(value);
public static ulong AsLittleEndian(this ulong value) => BitConverter.IsLittleEndian ? value : BinaryPrimitives.ReverseEndianness(value);

/// <summary>
/// Converts <paramref name="value"/> to little endian.
Expand Down Expand Up @@ -113,47 +114,47 @@ public static ushort AsLittleEndian(this ushort value) =>
/// </summary>
/// <param name="value">The value whose endian to convert.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static short AsBigEndian(this short value) => BitConverter.IsLittleEndian ? Endian.Reverse(value) : value;
public static short AsBigEndian(this short value) => BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(value) : value;

/// <summary>
/// Converts <paramref name="value"/> to big endian.
/// On big endian this is a no-op. On little endian the bytes are swapped.
/// </summary>
/// <param name="value">The value whose endian to convert.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ushort AsBigEndian(this ushort value) => BitConverter.IsLittleEndian ? Endian.Reverse(value) : value;
public static ushort AsBigEndian(this ushort value) => BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(value) : value;

/// <summary>
/// Converts <paramref name="value"/> to big endian.
/// On big endian this is a no-op. On little endian the bytes are swapped.
/// </summary>
/// <param name="value">The value whose endian to convert.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int AsBigEndian(this int value) => BitConverter.IsLittleEndian ? Endian.Reverse(value) : value;
public static int AsBigEndian(this int value) => BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(value) : value;

/// <summary>
/// Converts <paramref name="value"/> to big endian.
/// On big endian this is a no-op. On little endian the bytes are swapped.
/// </summary>
/// <param name="value">The value whose endian to convert.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint AsBigEndian(this uint value) => BitConverter.IsLittleEndian ? Endian.Reverse(value) : value;
public static uint AsBigEndian(this uint value) => BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(value) : value;

/// <summary>
/// Converts <paramref name="value"/> to big endian.
/// On big endian this is a no-op. On little endian the bytes are swapped.
/// </summary>
/// <param name="value">The value whose endian to convert.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long AsBigEndian(this long value) => BitConverter.IsLittleEndian ? Endian.Reverse(value) : value;
public static long AsBigEndian(this long value) => BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(value) : value;

/// <summary>
/// Converts <paramref name="value"/> to big endian.
/// On big endian this is a no-op. On little endian the bytes are swapped.
/// </summary>
/// <param name="value">The value whose endian to convert.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong AsBigEndian(this ulong value) => BitConverter.IsLittleEndian ? Endian.Reverse(value) : value;
public static ulong AsBigEndian(this ulong value) => BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(value) : value;

/// <summary>
/// Converts <paramref name="value"/> to big endian.
Expand Down

0 comments on commit c048e70

Please sign in to comment.