Skip to content

Commit

Permalink
Use C# compiler provided nint/nuint (#36159)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkotas authored May 9, 2020
1 parent 8b02df8 commit 6b5850f
Show file tree
Hide file tree
Showing 40 changed files with 102 additions and 448 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@
using System.Reflection;
using Internal.Runtime.CompilerServices;

#pragma warning disable SA1121 // explicitly using type aliases instead of built-in types
#if TARGET_64BIT
using nuint = System.UInt64;
#else
using nuint = System.UInt32;
#endif

namespace System
{
// Note that we make a T[] (single-dimensional w/ zero as the lower bound) implement both
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,6 @@
using System.Runtime.InteropServices;
using Internal.Runtime.CompilerServices;

#pragma warning disable SA1121 // explicitly using type aliases instead of built-in types
#if TARGET_64BIT
using nuint = System.UInt64;
using nint = System.UInt64;
#else
using nuint = System.UInt32;
using nint = System.UInt32;
#endif

namespace System
{
public partial class Buffer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@

using System.Runtime.CompilerServices;

#pragma warning disable SA1121 // explicitly using type aliases instead of built-in types
#if TARGET_64BIT
using nuint = System.UInt64;
#else
using nuint = System.UInt32;
#endif

namespace System
{
public partial class Object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;

#pragma warning disable SA1121 // explicitly using type aliases instead of built-in types
#if TARGET_64BIT
using nuint = System.UInt64;
#else
using nuint = System.UInt32;
#endif

namespace System.Runtime.CompilerServices
{
internal static unsafe class CastHelpers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@
using System.Runtime.InteropServices;
using Internal.Runtime.CompilerServices;

#pragma warning disable SA1121 // explicitly using type aliases instead of built-in types
#if TARGET_64BIT
using nuint = System.UInt64;
#else
using nuint = System.UInt32;
#endif

namespace System.Runtime.CompilerServices
{
public static partial class RuntimeHelpers
Expand Down Expand Up @@ -348,31 +341,31 @@ internal unsafe struct MethodTable
| 0x40000000 // enum_flag_ComObject
| 0x00400000;// enum_flag_ICastable;

private const int ParentMethodTableOffset = 0x10
private const int DebugClassNamePtr = // adjust for debug_m_szClassName
#if DEBUG
+ sizeof(nuint) // adjust for debug_m_szClassName
#endif
;

#if TARGET_64BIT
private const int ElementTypeOffset = 0x30
8
#else
private const int ElementTypeOffset = 0x20
4
#endif
#if DEBUG
+ sizeof(nuint) // adjust for debug_m_szClassName
#else
0
#endif
;
;

private const int ParentMethodTableOffset = 0x10 + DebugClassNamePtr;

#if TARGET_64BIT
private const int InterfaceMapOffset = 0x38
private const int ElementTypeOffset = 0x30 + DebugClassNamePtr;
#else
private const int InterfaceMapOffset = 0x24
private const int ElementTypeOffset = 0x20 + DebugClassNamePtr;
#endif
#if DEBUG
+ sizeof(nuint) // adjust for debug_m_szClassName

#if TARGET_64BIT
private const int InterfaceMapOffset = 0x38 + DebugClassNamePtr;
#else
private const int InterfaceMapOffset = 0x24 + DebugClassNamePtr;
#endif
;

public bool HasComponentSize
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;

#pragma warning disable SA1121 // explicitly using type aliases instead of built-in types
#if TARGET_64BIT
using nuint = System.UInt64;
using nint = System.Int64;
#else
using nuint = System.UInt32;
using nint = System.Int32;
#endif

//
// The implementations of most the methods in this file are provided as intrinsics.
// In CoreCLR, the body of the functions are replaced by the EE with unsafe code. See see getILIntrinsicImplementationForUnsafe for details.
Expand Down Expand Up @@ -145,19 +136,6 @@ public static ref T Add<T>(ref T source, IntPtr elementOffset)
#endif
}

#if TARGET_64BIT
/// <summary>
/// Adds an element offset to the given reference.
/// </summary>
[Intrinsic]
[NonVersionable]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static ref T Add<T>(ref T source, nint elementOffset)
{
return ref Unsafe.Add(ref source, (IntPtr)(void*)elementOffset);
}
#endif

/// <summary>
/// Adds an byte offset to the given reference.
/// </summary>
Expand Down
19 changes: 6 additions & 13 deletions src/libraries/System.Private.CoreLib/src/System/Buffer.Unix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,19 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#pragma warning disable SA1121 // explicitly using type aliases instead of built-in types
#if TARGET_64BIT
using nuint = System.UInt64;
#else
using nuint = System.UInt32;
#endif

namespace System
{
public static partial class Buffer
{
#if TARGET_ARM64
// Managed code is currently faster than glibc unoptimized memmove
// TODO-ARM64-UNIX-OPT revisit when glibc optimized memmove is in Linux distros
// https://github.com/dotnet/runtime/issues/8897
private const nuint MemmoveNativeThreshold = ulong.MaxValue;
// Managed code is currently faster than glibc unoptimized memmove
// TODO-ARM64-UNIX-OPT revisit when glibc optimized memmove is in Linux distros
// https://github.com/dotnet/runtime/issues/8897
private static nuint MemmoveNativeThreshold => nuint.MaxValue;
#elif TARGET_ARM
private const nuint MemmoveNativeThreshold = 512;
private const nuint MemmoveNativeThreshold = 512;
#else
private const nuint MemmoveNativeThreshold = 2048;
private const nuint MemmoveNativeThreshold = 2048;
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,14 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#pragma warning disable SA1121 // explicitly using type aliases instead of built-in types
#if TARGET_64BIT
using nuint = System.UInt64;
#else
using nuint = System.UInt32;
#endif

namespace System
{
public static partial class Buffer
{
#if TARGET_ARM64
// Determine optimal value for Windows.
// https://github.com/dotnet/runtime/issues/8896
private const nuint MemmoveNativeThreshold = ulong.MaxValue;
private static nuint MemmoveNativeThreshold => nuint.MaxValue;
#else
private const nuint MemmoveNativeThreshold = 2048;
#endif
Expand Down
11 changes: 1 addition & 10 deletions src/libraries/System.Private.CoreLib/src/System/Buffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,6 @@

using Internal.Runtime.CompilerServices;

#pragma warning disable SA1121 // explicitly using type aliases instead of built-in types
#if TARGET_64BIT
using nint = System.Int64;
using nuint = System.UInt64;
#else
using nint = System.Int32;
using nuint = System.UInt32;
#endif

namespace System
{
public static partial class Buffer
Expand Down Expand Up @@ -341,7 +332,7 @@ ref Unsafe.As<T, byte>(ref source),
private static void Memmove(ref byte dest, ref byte src, nuint len)
{
// P/Invoke into the native version when the buffers are overlapping.
if (((nuint)Unsafe.ByteOffset(ref src, ref dest) < len) || ((nuint)Unsafe.ByteOffset(ref dest, ref src) < len))
if (((nuint)(nint)Unsafe.ByteOffset(ref src, ref dest) < len) || ((nuint)(nint)Unsafe.ByteOffset(ref dest, ref src) < len))
{
goto BuffersOverlap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@
using System.Runtime.InteropServices;
using Internal.Runtime.CompilerServices;

#pragma warning disable SA1121 // explicitly using type aliases instead of built-in types
#if TARGET_64BIT
using nint = System.Int64;
#else
using nint = System.Int32;
#endif

namespace System.Collections.Generic
{
#region ArraySortHelper for single arrays
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@
using System.Text.Unicode;
using Internal.Runtime.CompilerServices;

#pragma warning disable SA1121 // explicitly using type aliases instead of built-in types
#if TARGET_64BIT
using nuint = System.UInt64;
#else
using nuint = System.UInt32;
#endif

namespace System.Globalization
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@
using System.Text.Unicode;
using Internal.Runtime.CompilerServices;

#pragma warning disable SA1121 // explicitly using type aliases instead of built-in types
#if TARGET_64BIT
using nuint = System.UInt64;
#else // TARGET_64BIT
using nuint = System.UInt32;
#endif // TARGET_64BIT

namespace System.Globalization
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@
using System.Threading;
using System.Threading.Tasks;

#pragma warning disable SA1121 // explicitly using type aliases instead of built-in types
#if TARGET_64BIT
using nuint = System.UInt64;
#else
using nuint = System.UInt32;
#endif

namespace System.IO
{
/*
Expand Down
39 changes: 19 additions & 20 deletions src/libraries/System.Private.CoreLib/src/System/IntPtr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

#pragma warning disable SA1121 // explicitly using type aliases instead of built-in types
#if TARGET_64BIT
using nint = System.Int64;
using nint_t = System.Int64;
#else
using nint = System.Int32;
using nint_t = System.Int32;
#endif

namespace System
Expand Down Expand Up @@ -162,7 +162,7 @@ public static IntPtr Subtract(IntPtr pointer, int offset) =>
public static int Size
{
[NonVersionable]
get => sizeof(nint);
get => sizeof(nint_t);
}

[CLSCompliant(false)]
Expand All @@ -172,25 +172,24 @@ public static int Size
public static IntPtr MaxValue
{
[NonVersionable]
get => (IntPtr)nint.MaxValue;
get => (IntPtr)nint_t.MaxValue;
}

public static IntPtr MinValue
{
[NonVersionable]
get => (IntPtr)nint.MinValue;
get => (IntPtr)nint_t.MinValue;
}

// Don't just delegate to nint.CompareTo as it needs to throw when not IntPtr
// Don't just delegate to nint_t.CompareTo as it needs to throw when not IntPtr
public unsafe int CompareTo(object? value)
{
if (value is null)
{
return 1;
}
if (value is IntPtr o)
if (value is nint i)
{
nint i = (nint)o;
if ((nint)_value < i) return -1;
if ((nint)_value > i) return 1;
return 0;
Expand All @@ -199,31 +198,31 @@ public unsafe int CompareTo(object? value)
throw new ArgumentException(SR.Arg_MustBeIntPtr);
}

public unsafe int CompareTo(IntPtr value) => ((nint)_value).CompareTo((nint)value);
public unsafe int CompareTo(IntPtr value) => ((nint_t)_value).CompareTo((nint_t)value);

[NonVersionable]
public unsafe bool Equals(IntPtr other) => (nint)_value == (nint)other;
public unsafe bool Equals(IntPtr other) => (nint_t)_value == (nint_t)other;

public unsafe override string ToString() => ((nint)_value).ToString();
public unsafe string ToString(string? format) => ((nint)_value).ToString(format);
public unsafe string ToString(IFormatProvider? provider) => ((nint)_value).ToString(provider);
public unsafe string ToString(string? format, IFormatProvider? provider) => ((nint)_value).ToString(format, provider);
public unsafe override string ToString() => ((nint_t)_value).ToString();
public unsafe string ToString(string? format) => ((nint_t)_value).ToString(format);
public unsafe string ToString(IFormatProvider? provider) => ((nint_t)_value).ToString(provider);
public unsafe string ToString(string? format, IFormatProvider? provider) => ((nint_t)_value).ToString(format, provider);

public static IntPtr Parse(string s) => (IntPtr)nint.Parse(s);
public static IntPtr Parse(string s, NumberStyles style) => (IntPtr)nint.Parse(s, style);
public static IntPtr Parse(string s, IFormatProvider? provider) => (IntPtr)nint.Parse(s, provider);
public static IntPtr Parse(string s, NumberStyles style, IFormatProvider? provider) => (IntPtr)nint.Parse(s, style, provider);
public static IntPtr Parse(string s) => (IntPtr)nint_t.Parse(s);
public static IntPtr Parse(string s, NumberStyles style) => (IntPtr)nint_t.Parse(s, style);
public static IntPtr Parse(string s, IFormatProvider? provider) => (IntPtr)nint_t.Parse(s, provider);
public static IntPtr Parse(string s, NumberStyles style, IFormatProvider? provider) => (IntPtr)nint_t.Parse(s, style, provider);

public static bool TryParse(string? s, out IntPtr result)
{
Unsafe.SkipInit(out result);
return nint.TryParse(s, out Unsafe.As<IntPtr, nint>(ref result));
return nint_t.TryParse(s, out Unsafe.As<IntPtr, nint_t>(ref result));
}

public static bool TryParse(string? s, NumberStyles style, IFormatProvider? provider, out IntPtr result)
{
Unsafe.SkipInit(out result);
return nint.TryParse(s, style, provider, out Unsafe.As<IntPtr, nint>(ref result));
return nint_t.TryParse(s, style, provider, out Unsafe.As<IntPtr, nint_t>(ref result));
}
}
}
Loading

0 comments on commit 6b5850f

Please sign in to comment.