diff --git a/src/coreclr/src/vm/mscorlib.h b/src/coreclr/src/vm/mscorlib.h index 575d92c4b7d05b..ce78a3f3b593ce 100644 --- a/src/coreclr/src/vm/mscorlib.h +++ b/src/coreclr/src/vm/mscorlib.h @@ -731,7 +731,7 @@ DEFINE_METHOD(UNSAFE, BYREF_ADD, Add, GM_RefT_Int_Ret DEFINE_METHOD(UNSAFE, BYREF_INTPTR_ADD, Add, GM_RefT_IntPtr_RetRefT) DEFINE_METHOD(UNSAFE, PTR_ADD, Add, GM_PtrVoid_Int_RetPtrVoid) DEFINE_METHOD(UNSAFE, BYREF_BYTE_OFFSET, ByteOffset, NoSig) -DEFINE_METHOD(UNSAFE, BYREF_ADD_BYTE_OFFSET, AddByteOffset, NoSig) +DEFINE_METHOD(UNSAFE, BYREF_ADD_BYTE_OFFSET, AddByteOffset, GM_RefT_IntPtr_RetRefT) DEFINE_METHOD(UNSAFE, BYREF_ARE_SAME, AreSame, NoSig) DEFINE_METHOD(UNSAFE, BYREF_IS_ADDRESS_GREATER_THAN, IsAddressGreaterThan, NoSig) DEFINE_METHOD(UNSAFE, BYREF_IS_ADDRESS_LESS_THAN, IsAddressLessThan, NoSig) diff --git a/src/libraries/System.Private.CoreLib/src/Internal/Runtime/CompilerServices/Unsafe.cs b/src/libraries/System.Private.CoreLib/src/Internal/Runtime/CompilerServices/Unsafe.cs index 7c5f9f70b9249e..32ceaf8b62b338 100644 --- a/src/libraries/System.Private.CoreLib/src/Internal/Runtime/CompilerServices/Unsafe.cs +++ b/src/libraries/System.Private.CoreLib/src/Internal/Runtime/CompilerServices/Unsafe.cs @@ -11,16 +11,11 @@ #pragma warning disable SA1121 // explicitly using type aliases instead of built-in types #if BIT64 using nuint = System.UInt64; -#else -using nuint = System.UInt32; -#endif -#if !CORECLR -#if BIT64 using nint = System.Int64; #else +using nuint = System.UInt32; using nint = System.Int32; #endif -#endif // // The implementations of most the methods in this file are provided as intrinsics. @@ -150,12 +145,25 @@ public static ref T Add(ref T source, IntPtr elementOffset) #endif } +#if BIT64 /// /// Adds an element offset to the given reference. /// [Intrinsic] [NonVersionable] [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static ref T Add(ref T source, nint elementOffset) + { + return ref Unsafe.Add(ref source, (IntPtr)(void*)elementOffset); + } +#endif + + /// + /// Adds an byte offset to the given reference. + /// + [Intrinsic] + [NonVersionable] + [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static ref T AddByteOffset(ref T source, nuint byteOffset) { return ref AddByteOffset(ref source, (IntPtr)(void*)byteOffset); @@ -295,7 +303,7 @@ public static void WriteUnaligned(ref byte destination, T value) } /// - /// Adds an element offset to the given reference. + /// Adds an byte offset to the given reference. /// [Intrinsic] [NonVersionable] diff --git a/src/libraries/System.Private.CoreLib/src/System/SpanHelpers.Char.cs b/src/libraries/System.Private.CoreLib/src/System/SpanHelpers.Char.cs index 7d4d27aded2cf6..25dc8f6fae4c6f 100644 --- a/src/libraries/System.Private.CoreLib/src/System/SpanHelpers.Char.cs +++ b/src/libraries/System.Private.CoreLib/src/System/SpanHelpers.Char.cs @@ -107,7 +107,8 @@ public static unsafe int SequenceCompareTo(ref char first, int firstLength, ref } } - if (sizeof(UIntPtr) > sizeof(int) && (byte*)minLength >= (byte*)(i + sizeof(int) / sizeof(char))) +#if BIT64 + if ((byte*)minLength >= (byte*)(i + sizeof(int) / sizeof(char))) { if (Unsafe.ReadUnaligned(ref Unsafe.As(ref Unsafe.Add(ref first, i))) == Unsafe.ReadUnaligned(ref Unsafe.As(ref Unsafe.Add(ref second, i)))) @@ -115,6 +116,7 @@ public static unsafe int SequenceCompareTo(ref char first, int firstLength, ref i += sizeof(int) / sizeof(char); } } +#endif while ((byte*)i < (byte*)minLength) { @@ -254,15 +256,15 @@ public static unsafe int IndexOf(ref char searchSpace, char value, int length) // remaining data that is shorter than a Vector length. while (lengthToExamine >= 4) { - ref char current = ref Add(ref searchSpace, offset); + ref char current = ref Unsafe.Add(ref searchSpace, offset); if (value == current) goto Found; - if (value == Add(ref current, 1)) + if (value == Unsafe.Add(ref current, 1)) goto Found1; - if (value == Add(ref current, 2)) + if (value == Unsafe.Add(ref current, 2)) goto Found2; - if (value == Add(ref current, 3)) + if (value == Unsafe.Add(ref current, 3)) goto Found3; offset += 4; @@ -271,7 +273,7 @@ public static unsafe int IndexOf(ref char searchSpace, char value, int length) while (lengthToExamine > 0) { - if (value == Add(ref searchSpace, offset)) + if (value == Unsafe.Add(ref searchSpace, offset)) goto Found; offset++; @@ -1031,10 +1033,6 @@ private static int LocateLastFoundChar(ulong match) return 3 - (BitOperations.LeadingZeroCount(match) >> 4); } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ref char Add(ref char source, nint elementOffset) - => ref Unsafe.Add(ref source, (IntPtr)elementOffset); - [MethodImpl(MethodImplOptions.AggressiveInlining)] private static unsafe Vector LoadVector(ref char start, nint offset) => Unsafe.ReadUnaligned>(ref Unsafe.As(ref Unsafe.Add(ref start, (IntPtr)offset)));