Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark unaligned reads in NativeFormatReader as unaligned for the JIT #97917

Merged
merged 2 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,15 @@ public static ref T AsRef<T>(in T source)
{
throw new PlatformNotSupportedException();
}

/// <summary>
/// Reads a value of type <typeparamref name="T"/> from the given location.
/// </summary>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T ReadUnaligned<T>(void* source)
{
throw new PlatformNotSupportedException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// ---------------------------------------------------------------------------

using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace Internal.NativeFormat
{
Expand All @@ -23,21 +24,21 @@ public static byte ReadUInt8(ref byte* stream)

public static ushort ReadUInt16(ref byte* stream)
{
ushort result = *(ushort*)(stream); // Assumes little endian and unaligned access
ushort result = Unsafe.ReadUnaligned<ushort>(stream); // Assumes little endian and unaligned access
stream += 2;
return result;
}

public static uint ReadUInt32(ref byte* stream)
{
uint result = *(uint*)(stream); // Assumes little endian and unaligned access
uint result = Unsafe.ReadUnaligned<uint>(stream); // Assumes little endian and unaligned access
stream += 4;
return result;
}

public static ulong ReadUInt64(ref byte* stream)
{
ulong result = *(ulong*)(stream); // Assumes little endian and unaligned access
ulong result = Unsafe.ReadUnaligned<ulong>(stream); // Assumes little endian and unaligned access
stream += 8;
return result;
}
Expand Down
Loading