Skip to content

Commit ce22c0a

Browse files
authored
Optimize Endianness methods with aggressive inlining and with additional methods
Signed-off-by: Xen <lordofxen@deskasoft.com>
1 parent dd0662c commit ce22c0a

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

HashifyNet/Core/Utilities/Endianness.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
using System;
3131
using System.Buffers.Binary;
32+
using System.Runtime.CompilerServices;
3233

3334
namespace HashifyNet.Core.Utilities
3435
{
@@ -50,6 +51,7 @@ internal static class Endianness
5051
/// <param name="value">The 32-bit unsigned integer to convert.</param>
5152
/// <param name="buffer">The byte array to which the little-endian representation will be written.</param>
5253
/// <param name="offset">The zero-based index in the buffer at which to begin writing the bytes.</param>
54+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5355
public static void ToLittleEndianBytes(uint value, byte[] buffer, int offset)
5456
{
5557
buffer[offset] = (byte)value;
@@ -68,6 +70,7 @@ public static void ToLittleEndianBytes(uint value, byte[] buffer, int offset)
6870
/// <param name="value">The 64-bit unsigned integer to convert.</param>
6971
/// <param name="buffer">The byte array to which the little-endian representation of <paramref name="value"/> will be written.</param>
7072
/// <param name="offset">The zero-based index in <paramref name="buffer"/> at which to begin writing the bytes.</param>
73+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7174
public static void ToLittleEndianBytes(ulong value, byte[] buffer, int offset)
7275
{
7376
buffer[offset] = (byte)value;
@@ -80,6 +83,15 @@ public static void ToLittleEndianBytes(ulong value, byte[] buffer, int offset)
8083
buffer[offset + 7] = (byte)(value >> 56);
8184
}
8285

86+
/// <summary>
87+
/// Converts the specified 64-bit unsigned integer to little-endian format.
88+
/// </summary>
89+
/// <remarks>This method ensures that the returned value is in little-endian format, regardless of the
90+
/// system's endianness. It is optimized for performance and uses aggressive inlining.</remarks>
91+
/// <param name="value">The 64-bit unsigned integer to convert.</param>
92+
/// <returns>The value in little-endian format. If the system architecture is already little-endian, the original value is
93+
/// returned unchanged. Otherwise, the byte order is reversed.</returns>
94+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
8395
public static ulong ToLittleEndian(ulong value)
8496
{
8597
// If the system architecture is already little-endian, the value doesn't need to be changed.
@@ -101,6 +113,7 @@ public static ulong ToLittleEndian(ulong value)
101113
/// <param name="offset">The zero-based index in <paramref name="buffer"/> at which to begin reading the 8 bytes.</param>
102114
/// <returns>A 64-bit unsigned integer representing the little-endian interpretation of the 8 bytes starting at <paramref
103115
/// name="offset"/>.</returns>
116+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
104117
public static ulong ToUInt64LittleEndian(byte[] buffer, int offset)
105118
{
106119
return buffer[offset] |
@@ -124,6 +137,7 @@ public static ulong ToUInt64LittleEndian(byte[] buffer, int offset)
124137
/// <param name="offset">The zero-based index in <paramref name="buffer"/> at which to begin reading the bytes.</param>
125138
/// <returns>A 64-bit unsigned integer constructed from the 8 bytes starting at <paramref name="offset"/> in little-endian
126139
/// order.</returns>
140+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
127141
public static ulong ToUInt64LittleEndian(ReadOnlySpan<byte> buffer, int offset)
128142
{
129143
return buffer[offset] |
@@ -144,6 +158,7 @@ public static ulong ToUInt64LittleEndian(ReadOnlySpan<byte> buffer, int offset)
144158
/// name="offset"/>.</param>
145159
/// <param name="offset">The zero-based index in <paramref name="buffer"/> at which to begin reading the four bytes.</param>
146160
/// <returns>A 32-bit unsigned integer representing the value of the four bytes in little-endian order.</returns>
161+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
147162
public static uint ToUInt32LittleEndian(byte[] buffer, int offset)
148163
{
149164
return buffer[offset] |
@@ -161,12 +176,39 @@ public static uint ToUInt32LittleEndian(byte[] buffer, int offset)
161176
/// <param name="offset">The zero-based index in the buffer at which to begin reading the bytes.</param>
162177
/// <returns>A 32-bit unsigned integer representing the value of the four bytes starting at the specified offset, interpreted
163178
/// as little-endian.</returns>
179+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
164180
public static uint ToUInt32LittleEndian(ReadOnlySpan<byte> buffer, int offset)
165181
{
166182
return buffer[offset] |
167183
((uint)buffer[offset + 1] << 8) |
168184
((uint)buffer[offset + 2] << 16) |
169185
((uint)buffer[offset + 3] << 24);
170186
}
187+
188+
/// <summary>
189+
/// Converts the specified 64-bit unsigned integer to an array of bytes in little-endian order.
190+
/// </summary>
191+
/// <param name="value">The 64-bit unsigned integer to convert.</param>
192+
/// <returns>An array of 8 bytes representing the <paramref name="value"/> in little-endian order.</returns>
193+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
194+
public static byte[] GetBytesLittleEndian(ulong value)
195+
{
196+
byte[] bytes = new byte[8];
197+
ToLittleEndianBytes(value, bytes, 0);
198+
return bytes;
199+
}
200+
201+
/// <summary>
202+
/// Converts the specified 32-bit unsigned integer to a byte array in little-endian format.
203+
/// </summary>
204+
/// <param name="value">The 32-bit unsigned integer to convert.</param>
205+
/// <returns>A byte array containing the little-endian representation of the specified value.</returns>
206+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
207+
public static byte[] GetBytesLittleEndian(uint value)
208+
{
209+
byte[] bytes = new byte[4];
210+
ToLittleEndianBytes(value, bytes, 0);
211+
return bytes;
212+
}
171213
}
172214
}

0 commit comments

Comments
 (0)