diff --git a/src/coreclr/System.Private.CoreLib/src/System/Buffer.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Buffer.CoreCLR.cs index 59126ce8808f7731c81bbef97835304866ae43f7..f8f7117069ed0017caa8ee42f7f1961bbf298c43 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Buffer.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Buffer.CoreCLR.cs @@ -84,7 +84,7 @@ private static void _BulkMoveWithWriteBarrier(ref byte destination, ref byte sou internal static unsafe void Memcpy(byte* dest, byte* src, int len) { Debug.Assert(len >= 0, "Negative length in memcpy!"); - Memmove(ref *dest, ref *src, (nuint)len); + Memmove(ref *dest, ref *src, (nuint)(uint)len /* force zero-extension */); } // Used by ilmarshalers.cpp @@ -93,7 +93,7 @@ internal static unsafe void Memcpy(byte* pDest, int destIndex, byte[] src, int s Debug.Assert((srcIndex >= 0) && (destIndex >= 0) && (len >= 0), "Index and length must be non-negative!"); Debug.Assert(src.Length - srcIndex >= len, "not enough bytes in src"); - Memmove(ref *(pDest + destIndex), ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(src), srcIndex), (nuint)len); + Memmove(ref *(pDest + (uint)destIndex), ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(src), (nint)(uint)srcIndex /* force zero-extension */), (uint)len); } [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs b/src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs index cb81baf05004066f023c26985ef269569815eb70..2d0fd54f7f2355eee2093f2784ae5635d591d823 100644 --- a/src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs +++ b/src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs @@ -31,7 +31,7 @@ public static Span AsSpan(this T[]? array, int start) if ((uint)start > (uint)array.Length) ThrowHelper.ThrowArgumentOutOfRangeException(); - return new Span(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(array), start), array.Length - start); + return new Span(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(array), (nint)(uint)start /* force zero-extension */), array.Length - start); } /// @@ -55,7 +55,7 @@ public static Span AsSpan(this T[]? array, Index startIndex) if ((uint)actualIndex > (uint)array.Length) ThrowHelper.ThrowArgumentOutOfRangeException(); - return new Span(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(array), actualIndex), array.Length - actualIndex); + return new Span(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(array), (nint)(uint)actualIndex /* force zero-extension */), array.Length - actualIndex); } /// @@ -79,7 +79,7 @@ public static Span AsSpan(this T[]? array, Range range) ThrowHelper.ThrowArrayTypeMismatchException(); (int start, int length) = range.GetOffsetAndLength(array.Length); - return new Span(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(array), start), length); + return new Span(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(array), (nint)(uint)start /* force zero-extension */), length); } /// @@ -118,7 +118,7 @@ public static ReadOnlySpan AsSpan(this string? text, int start) if ((uint)start > (uint)text.Length) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); - return new ReadOnlySpan(ref Unsafe.Add(ref text.GetRawStringData(), start), text.Length - start); + return new ReadOnlySpan(ref Unsafe.Add(ref text.GetRawStringData(), (nint)(uint)start /* force zero-extension */), text.Length - start); } /// @@ -150,7 +150,7 @@ public static ReadOnlySpan AsSpan(this string? text, int start, int length ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); #endif - return new ReadOnlySpan(ref Unsafe.Add(ref text.GetRawStringData(), start), length); + return new ReadOnlySpan(ref Unsafe.Add(ref text.GetRawStringData(), (nint)(uint)start /* force zero-extension */), length); } /// Creates a new over the portion of the target string. @@ -422,7 +422,7 @@ public static ReadOnlyMemory AsMemory(this string? text, Range range) SpanHelpers.SequenceEqual( ref Unsafe.As(ref MemoryMarshal.GetReference(span)), ref Unsafe.As(ref MemoryMarshal.GetReference(other)), - ((nuint)length) * size); // If this multiplication overflows, the Span we got overflows the entire address range. There's no happy outcome for this api in such a case so we choose not to take the overhead of checking. + ((uint)length) * size); // If this multiplication overflows, the Span we got overflows the entire address range. There's no happy outcome for this api in such a case so we choose not to take the overhead of checking. } return length == other.Length && SpanHelpers.SequenceEqual(ref MemoryMarshal.GetReference(span), ref MemoryMarshal.GetReference(other), length); @@ -912,7 +912,7 @@ public static int SequenceCompareTo(this Span span, ReadOnlySpan other) SpanHelpers.SequenceEqual( ref Unsafe.As(ref MemoryMarshal.GetReference(span)), ref Unsafe.As(ref MemoryMarshal.GetReference(other)), - ((nuint)length) * size); // If this multiplication overflows, the Span we got overflows the entire address range. There's no happy outcome for this API in such a case so we choose not to take the overhead of checking. + ((uint)length) * size); // If this multiplication overflows, the Span we got overflows the entire address range. There's no happy outcome for this API in such a case so we choose not to take the overhead of checking. } return length == other.Length && SpanHelpers.SequenceEqual(ref MemoryMarshal.GetReference(span), ref MemoryMarshal.GetReference(other), length); @@ -954,7 +954,7 @@ public static bool SequenceEqual(this ReadOnlySpan span, ReadOnlySpan o return SpanHelpers.SequenceEqual( ref Unsafe.As(ref MemoryMarshal.GetReference(span)), ref Unsafe.As(ref MemoryMarshal.GetReference(other)), - ((nuint)span.Length) * size); // If this multiplication overflows, the Span we got overflows the entire address range. There's no happy outcome for this API in such a case so we choose not to take the overhead of checking. + ((uint)span.Length) * size); // If this multiplication overflows, the Span we got overflows the entire address range. There's no happy outcome for this API in such a case so we choose not to take the overhead of checking. } // Otherwise, compare each element using EqualityComparer.Default.Equals in a way that will enable it to devirtualize. @@ -1024,7 +1024,7 @@ public static int SequenceCompareTo(this ReadOnlySpan span, ReadOnlySpan(ref MemoryMarshal.GetReference(span)), ref Unsafe.As(ref MemoryMarshal.GetReference(value)), - ((nuint)valueLength) * size); // If this multiplication overflows, the Span we got overflows the entire address range. There's no happy outcome for this api in such a case so we choose not to take the overhead of checking. + ((uint)valueLength) * size); // If this multiplication overflows, the Span we got overflows the entire address range. There's no happy outcome for this api in such a case so we choose not to take the overhead of checking. } return valueLength <= span.Length && SpanHelpers.SequenceEqual(ref MemoryMarshal.GetReference(span), ref MemoryMarshal.GetReference(value), valueLength); @@ -1044,7 +1044,7 @@ public static int SequenceCompareTo(this ReadOnlySpan span, ReadOnlySpan(ref MemoryMarshal.GetReference(span)), ref Unsafe.As(ref MemoryMarshal.GetReference(value)), - ((nuint)valueLength) * size); // If this multiplication overflows, the Span we got overflows the entire address range. There's no happy outcome for this api in such a case so we choose not to take the overhead of checking. + ((uint)valueLength) * size); // If this multiplication overflows, the Span we got overflows the entire address range. There's no happy outcome for this api in such a case so we choose not to take the overhead of checking. } return valueLength <= span.Length && SpanHelpers.SequenceEqual(ref MemoryMarshal.GetReference(span), ref MemoryMarshal.GetReference(value), valueLength); @@ -1063,14 +1063,14 @@ public static int SequenceCompareTo(this ReadOnlySpan span, ReadOnlySpan(); return valueLength <= spanLength && SpanHelpers.SequenceEqual( - ref Unsafe.As(ref Unsafe.Add(ref MemoryMarshal.GetReference(span), spanLength - valueLength)), + ref Unsafe.As(ref Unsafe.Add(ref MemoryMarshal.GetReference(span), (nint)(uint)(spanLength - valueLength) /* force zero-extension */)), ref Unsafe.As(ref MemoryMarshal.GetReference(value)), - ((nuint)valueLength) * size); // If this multiplication overflows, the Span we got overflows the entire address range. There's no happy outcome for this api in such a case so we choose not to take the overhead of checking. + ((uint)valueLength) * size); // If this multiplication overflows, the Span we got overflows the entire address range. There's no happy outcome for this api in such a case so we choose not to take the overhead of checking. } return valueLength <= spanLength && SpanHelpers.SequenceEqual( - ref Unsafe.Add(ref MemoryMarshal.GetReference(span), spanLength - valueLength), + ref Unsafe.Add(ref MemoryMarshal.GetReference(span), (nint)(uint)(spanLength - valueLength) /* force zero-extension */), ref MemoryMarshal.GetReference(value), valueLength); } @@ -1088,14 +1088,14 @@ public static int SequenceCompareTo(this ReadOnlySpan span, ReadOnlySpan(); return valueLength <= spanLength && SpanHelpers.SequenceEqual( - ref Unsafe.As(ref Unsafe.Add(ref MemoryMarshal.GetReference(span), spanLength - valueLength)), + ref Unsafe.As(ref Unsafe.Add(ref MemoryMarshal.GetReference(span), (nint)(uint)(spanLength - valueLength) /* force zero-extension */)), ref Unsafe.As(ref MemoryMarshal.GetReference(value)), - ((nuint)valueLength) * size); // If this multiplication overflows, the Span we got overflows the entire address range. There's no happy outcome for this api in such a case so we choose not to take the overhead of checking. + ((uint)valueLength) * size); // If this multiplication overflows, the Span we got overflows the entire address range. There's no happy outcome for this api in such a case so we choose not to take the overhead of checking. } return valueLength <= spanLength && SpanHelpers.SequenceEqual( - ref Unsafe.Add(ref MemoryMarshal.GetReference(span), spanLength - valueLength), + ref Unsafe.Add(ref MemoryMarshal.GetReference(span), (nint)(uint)(spanLength - valueLength) /* force zero-extension */), ref MemoryMarshal.GetReference(value), valueLength); } diff --git a/src/libraries/System.Private.CoreLib/src/System/ReadOnlySpan.cs b/src/libraries/System.Private.CoreLib/src/System/ReadOnlySpan.cs index 0ba9ecfce1d04588f4ecc81c710a3e55d2dc86c6..ee9ce93a43cf95d78571d55436f78fc21d589adf 100644 --- a/src/libraries/System.Private.CoreLib/src/System/ReadOnlySpan.cs +++ b/src/libraries/System.Private.CoreLib/src/System/ReadOnlySpan.cs @@ -76,7 +76,7 @@ public ReadOnlySpan(T[]? array, int start, int length) ThrowHelper.ThrowArgumentOutOfRangeException(); #endif - _pointer = new ByReference(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(array), start)); + _pointer = new ByReference(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(array), (nint)(uint)start /* force zero-extension */)); _length = length; } @@ -134,7 +134,7 @@ internal ReadOnlySpan(ref T ptr, int length) { if ((uint)index >= (uint)_length) ThrowHelper.ThrowIndexOutOfRangeException(); - return ref Unsafe.Add(ref _pointer.Value, index); + return ref Unsafe.Add(ref _pointer.Value, (nint)(uint)index /* force zero-extension */); } } @@ -335,7 +335,7 @@ public ReadOnlySpan Slice(int start) if ((uint)start > (uint)_length) ThrowHelper.ThrowArgumentOutOfRangeException(); - return new ReadOnlySpan(ref Unsafe.Add(ref _pointer.Value, start), _length - start); + return new ReadOnlySpan(ref Unsafe.Add(ref _pointer.Value, (nint)(uint)start /* force zero-extension */), _length - start); } /// @@ -358,7 +358,7 @@ public ReadOnlySpan Slice(int start, int length) ThrowHelper.ThrowArgumentOutOfRangeException(); #endif - return new ReadOnlySpan(ref Unsafe.Add(ref _pointer.Value, start), length); + return new ReadOnlySpan(ref Unsafe.Add(ref _pointer.Value, (nint)(uint)start /* force zero-extension */), length); } /// @@ -372,7 +372,7 @@ public T[] ToArray() return Array.Empty(); var destination = new T[_length]; - Buffer.Memmove(ref MemoryMarshal.GetArrayDataReference(destination), ref _pointer.Value, (nuint)_length); + Buffer.Memmove(ref MemoryMarshal.GetArrayDataReference(destination), ref _pointer.Value, (uint)_length); return destination; } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Span.cs b/src/libraries/System.Private.CoreLib/src/System/Span.cs index 753e9f279f37474f8a3195ae595df9adb9c6212c..d5c957800de4c362c7562bcf294d610aed3c79b3 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Span.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Span.cs @@ -81,7 +81,7 @@ public Span(T[]? array, int start, int length) ThrowHelper.ThrowArgumentOutOfRangeException(); #endif - _pointer = new ByReference(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(array), start)); + _pointer = new ByReference(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(array), (nint)(uint)start /* force zero-extension */)); _length = length; } @@ -139,7 +139,7 @@ internal Span(ref T ptr, int length) { if ((uint)index >= (uint)_length) ThrowHelper.ThrowIndexOutOfRangeException(); - return ref Unsafe.Add(ref _pointer.Value, index); + return ref Unsafe.Add(ref _pointer.Value, (nint)(uint)index /* force zero-extension */); } } @@ -268,11 +268,11 @@ public unsafe void Clear() { if (RuntimeHelpers.IsReferenceOrContainsReferences()) { - SpanHelpers.ClearWithReferences(ref Unsafe.As(ref _pointer.Value), (nuint)_length * (nuint)(Unsafe.SizeOf() / sizeof(nuint))); + SpanHelpers.ClearWithReferences(ref Unsafe.As(ref _pointer.Value), (uint)_length * (nuint)(Unsafe.SizeOf() / sizeof(nuint))); } else { - SpanHelpers.ClearWithoutReferences(ref Unsafe.As(ref _pointer.Value), (nuint)_length * (nuint)Unsafe.SizeOf()); + SpanHelpers.ClearWithoutReferences(ref Unsafe.As(ref _pointer.Value), (uint)_length * (nuint)Unsafe.SizeOf()); } } @@ -389,7 +389,7 @@ public Span Slice(int start) if ((uint)start > (uint)_length) ThrowHelper.ThrowArgumentOutOfRangeException(); - return new Span(ref Unsafe.Add(ref _pointer.Value, start), _length - start); + return new Span(ref Unsafe.Add(ref _pointer.Value, (nint)(uint)start /* force zero-extension */), _length - start); } /// @@ -417,7 +417,7 @@ public Span Slice(int start, int length) ThrowHelper.ThrowArgumentOutOfRangeException(); #endif - return new Span(ref Unsafe.Add(ref _pointer.Value, start), length); + return new Span(ref Unsafe.Add(ref _pointer.Value, (nint)(uint)start /* force zero-extension */), length); } /// @@ -432,7 +432,7 @@ public T[] ToArray() return Array.Empty(); var destination = new T[_length]; - Buffer.Memmove(ref MemoryMarshal.GetArrayDataReference(destination), ref _pointer.Value, (nuint)_length); + Buffer.Memmove(ref MemoryMarshal.GetArrayDataReference(destination), ref _pointer.Value, (uint)_length); return destination; } } diff --git a/src/libraries/System.Private.CoreLib/src/System/String.Comparison.cs b/src/libraries/System.Private.CoreLib/src/System/String.Comparison.cs index c3e9d2fa55c3a23080baa556014ccedfa1fb934a..9d068b4da0bc9200396e6a8e4fe6a88848297711 100644 --- a/src/libraries/System.Private.CoreLib/src/System/String.Comparison.cs +++ b/src/libraries/System.Private.CoreLib/src/System/String.Comparison.cs @@ -30,7 +30,7 @@ private static bool EqualsHelper(string strA, string strB) return SpanHelpers.SequenceEqual( ref Unsafe.As(ref strA.GetRawStringData()), ref Unsafe.As(ref strB.GetRawStringData()), - ((nuint)strA.Length) * 2); + ((uint)strA.Length) * sizeof(char)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -42,7 +42,9 @@ private static int CompareOrdinalHelper(string strA, int indexA, int countA, str Debug.Assert(countA >= 0 && countB >= 0); Debug.Assert(indexA + countA <= strA.Length && indexB + countB <= strB.Length); - return SpanHelpers.SequenceCompareTo(ref Unsafe.Add(ref strA.GetRawStringData(), indexA), countA, ref Unsafe.Add(ref strB.GetRawStringData(), indexB), countB); + return SpanHelpers.SequenceCompareTo( + ref Unsafe.Add(ref strA.GetRawStringData(), (nint)(uint)indexA /* force zero-extension */), countA, + ref Unsafe.Add(ref strB.GetRawStringData(), (nint)(uint)indexB /* force zero-extension */), countB); } internal static bool EqualsOrdinalIgnoreCase(string? strA, string? strB) diff --git a/src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs b/src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs index 558d0a399af12bbcf76f147659d97aaf13f73ac1..0488572e9e3029bb8f7fb4d3f08919bc0e6740e4 100644 --- a/src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs +++ b/src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs @@ -1747,7 +1747,7 @@ private string InternalSubString(int startIndex, int length) Buffer.Memmove( elementCount: (uint)result.Length, // derefing Length now allows JIT to prove 'result' not null below destination: ref result._firstChar, - source: ref Unsafe.Add(ref _firstChar, startIndex)); + source: ref Unsafe.Add(ref _firstChar, (nint)(uint)startIndex /* force zero-extension */)); return result; } diff --git a/src/libraries/System.Private.CoreLib/src/System/String.Searching.cs b/src/libraries/System.Private.CoreLib/src/System/String.Searching.cs index 964a1a2a0c52ef13116d930e2a546a66e745ec86..c7aaa59d72e2529c9ece0aa32978dbccb3b18072 100644 --- a/src/libraries/System.Private.CoreLib/src/System/String.Searching.cs +++ b/src/libraries/System.Private.CoreLib/src/System/String.Searching.cs @@ -212,12 +212,12 @@ private static bool ArrayContains(char searchChar, char[] anyOf) private static unsafe bool IsCharBitSet(uint* charMap, byte value) { - return (charMap[value & PROBABILISTICMAP_BLOCK_INDEX_MASK] & (1u << (value >> PROBABILISTICMAP_BLOCK_INDEX_SHIFT))) != 0; + return (charMap[(uint)value & PROBABILISTICMAP_BLOCK_INDEX_MASK] & (1u << (value >> PROBABILISTICMAP_BLOCK_INDEX_SHIFT))) != 0; } private static unsafe void SetCharBit(uint* charMap, byte value) { - charMap[value & PROBABILISTICMAP_BLOCK_INDEX_MASK] |= 1u << (value >> PROBABILISTICMAP_BLOCK_INDEX_SHIFT); + charMap[(uint)value & PROBABILISTICMAP_BLOCK_INDEX_MASK] |= 1u << (value >> PROBABILISTICMAP_BLOCK_INDEX_SHIFT); } /* diff --git a/src/libraries/System.Private.CoreLib/src/System/String.cs b/src/libraries/System.Private.CoreLib/src/System/String.cs index 858d59fbaae57dc3c8c34e6d86d6d969f873e91f..6ae0c2204b879f4dea814cf948150137587b4962 100644 --- a/src/libraries/System.Private.CoreLib/src/System/String.cs +++ b/src/libraries/System.Private.CoreLib/src/System/String.cs @@ -403,7 +403,7 @@ internal bool TryGetSpan(int startIndex, int count, out ReadOnlySpan slice } #endif - slice = new ReadOnlySpan(ref Unsafe.Add(ref _firstChar, startIndex), count); + slice = new ReadOnlySpan(ref Unsafe.Add(ref _firstChar, (nint)(uint)startIndex /* force zero-extension */), count); return true; }