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

Rewriting Scalar<T> to be replaceable by linker stubs when not used as a fallback #71826

Closed
wants to merge 5 commits into from
Closed
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
@@ -0,0 +1,7 @@
<linker>
<assembly fullname="System.Private.CoreLib">
<type fullname="System.Runtime.Intrinsics.Scalar`1">
<method signature="System.Boolean get_IsSupported()" body="stub" value="false" />
</type>
</assembly>
</linker>
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
</ItemGroup>
<PropertyGroup>
<ILLinkDescriptorsLibraryBuildXml>$(ILLinkSharedDirectory)ILLink.Descriptors.LibraryBuild.xml</ILLinkDescriptorsLibraryBuildXml>
<ILLinkSubstitutionsLibraryBuildXml Condition="'$(SupportsArmIntrinsics)' == 'true'">$(ILLinkSharedDirectory)ILLink.Substitutions.NoScalarFallback.xml</ILLinkSubstitutionsLibraryBuildXml>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Internal\AssemblyAttributes.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ namespace System.Runtime.Intrinsics
internal static class Scalar<T>
where T : struct
{
public static bool IsSupported { get => true; }
public static T AllBitsSet
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (!IsSupported)
{
throw new NotSupportedException(SR.Arg_TypeNotSupported);
}

if (typeof(T) == typeof(byte))
{
return (T)(object)byte.MaxValue;
Expand Down Expand Up @@ -73,6 +79,11 @@ public static T One
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (!IsSupported)
{
throw new NotSupportedException(SR.Arg_TypeNotSupported);
}

if (typeof(T) == typeof(byte))
{
return (T)(object)(byte)1;
Expand Down Expand Up @@ -131,6 +142,11 @@ public static T One
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Abs(T value)
{
if (!IsSupported)
{
throw new NotSupportedException(SR.Arg_TypeNotSupported);
}

// byte, ushort, uint, and ulong should have already been handled

if (typeof(T) == typeof(double))
Expand Down Expand Up @@ -170,6 +186,11 @@ public static T Abs(T value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Add(T left, T right)
{
if (!IsSupported)
{
throw new NotSupportedException(SR.Arg_TypeNotSupported);
}

if (typeof(T) == typeof(byte))
{
return (T)(object)(byte)((byte)(object)left + (byte)(object)right);
Expand Down Expand Up @@ -227,6 +248,11 @@ public static T Add(T left, T right)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Ceiling(T value)
{
if (!IsSupported)
{
throw new NotSupportedException(SR.Arg_TypeNotSupported);
}

if (typeof(T) == typeof(double))
{
return (T)(object)Math.Ceiling((double)(object)value);
Expand All @@ -244,6 +270,11 @@ public static T Ceiling(T value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Divide(T left, T right)
{
if (!IsSupported)
{
throw new NotSupportedException(SR.Arg_TypeNotSupported);
}

if (typeof(T) == typeof(byte))
{
return (T)(object)(byte)((byte)(object)left / (byte)(object)right);
Expand Down Expand Up @@ -301,6 +332,11 @@ public static T Divide(T left, T right)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Equals(T left, T right)
{
if (!IsSupported)
{
throw new NotSupportedException(SR.Arg_TypeNotSupported);
}

if (typeof(T) == typeof(byte))
{
return (byte)(object)left == (byte)(object)right;
Expand Down Expand Up @@ -358,6 +394,11 @@ public static bool Equals(T left, T right)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint ExtractMostSignificantBit(T value)
{
if (!IsSupported)
{
throw new NotSupportedException(SR.Arg_TypeNotSupported);
}

if (typeof(T) == typeof(byte))
{
uint bits = (byte)(object)value;
Expand Down Expand Up @@ -437,6 +478,11 @@ public static uint ExtractMostSignificantBit(T value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Floor(T value)
{
if (!IsSupported)
{
throw new NotSupportedException(SR.Arg_TypeNotSupported);
}

if (typeof(T) == typeof(double))
{
return (T)(object)Math.Floor((double)(object)value);
Expand All @@ -454,6 +500,11 @@ public static T Floor(T value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool GreaterThan(T left, T right)
{
if (!IsSupported)
{
throw new NotSupportedException(SR.Arg_TypeNotSupported);
}

if (typeof(T) == typeof(byte))
{
return (byte)(object)left > (byte)(object)right;
Expand Down Expand Up @@ -511,6 +562,11 @@ public static bool GreaterThan(T left, T right)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool GreaterThanOrEqual(T left, T right)
{
if (!IsSupported)
{
throw new NotSupportedException(SR.Arg_TypeNotSupported);
}

if (typeof(T) == typeof(byte))
{
return (byte)(object)left >= (byte)(object)right;
Expand Down Expand Up @@ -568,6 +624,11 @@ public static bool GreaterThanOrEqual(T left, T right)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool LessThan(T left, T right)
{
if (!IsSupported)
{
throw new NotSupportedException(SR.Arg_TypeNotSupported);
}

if (typeof(T) == typeof(byte))
{
return (byte)(object)left < (byte)(object)right;
Expand Down Expand Up @@ -625,6 +686,11 @@ public static bool LessThan(T left, T right)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool LessThanOrEqual(T left, T right)
{
if (!IsSupported)
{
throw new NotSupportedException(SR.Arg_TypeNotSupported);
}

if (typeof(T) == typeof(byte))
{
return (byte)(object)left <= (byte)(object)right;
Expand Down Expand Up @@ -682,6 +748,11 @@ public static bool LessThanOrEqual(T left, T right)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Multiply(T left, T right)
{
if (!IsSupported)
{
throw new NotSupportedException(SR.Arg_TypeNotSupported);
}

if (typeof(T) == typeof(byte))
{
return (T)(object)(byte)((byte)(object)left * (byte)(object)right);
Expand Down Expand Up @@ -738,6 +809,11 @@ public static T Multiply(T left, T right)

public static bool ObjectEquals(T left, T right)
{
if (!IsSupported)
{
throw new NotSupportedException(SR.Arg_TypeNotSupported);
}

if (typeof(T) == typeof(byte))
{
return ((byte)(object)left).Equals((byte)(object)right);
Expand Down Expand Up @@ -795,6 +871,11 @@ public static bool ObjectEquals(T left, T right)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T ShiftLeft(T value, int shiftCount)
{
if (!IsSupported)
{
throw new NotSupportedException(SR.Arg_TypeNotSupported);
}

if (typeof(T) == typeof(byte))
{
return (T)(object)(byte)((byte)(object)value << (shiftCount & 7));
Expand Down Expand Up @@ -844,6 +925,11 @@ public static T ShiftLeft(T value, int shiftCount)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T ShiftRightArithmetic(T value, int shiftCount)
{
if (!IsSupported)
{
throw new NotSupportedException(SR.Arg_TypeNotSupported);
}

if (typeof(T) == typeof(short))
{
return (T)(object)(short)((short)(object)value >> (shiftCount & 15));
Expand Down Expand Up @@ -873,6 +959,11 @@ public static T ShiftRightArithmetic(T value, int shiftCount)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T ShiftRightLogical(T value, int shiftCount)
{
if (!IsSupported)
{
throw new NotSupportedException(SR.Arg_TypeNotSupported);
}

if (typeof(T) == typeof(byte))
{
return (T)(object)(byte)((byte)(object)value >> (shiftCount & 7));
Expand Down Expand Up @@ -922,6 +1013,11 @@ public static T ShiftRightLogical(T value, int shiftCount)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Sqrt(T value)
{
if (!IsSupported)
{
throw new NotSupportedException(SR.Arg_TypeNotSupported);
}

if (typeof(T) == typeof(byte))
{
return (T)(object)(byte)MathF.Sqrt((byte)(object)value);
Expand Down Expand Up @@ -979,6 +1075,11 @@ public static T Sqrt(T value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Subtract(T left, T right)
{
if (!IsSupported)
{
throw new NotSupportedException(SR.Arg_TypeNotSupported);
}

if (typeof(T) == typeof(byte))
{
return (T)(object)(byte)((byte)(object)left - (byte)(object)right);
Expand Down