From 6a02d88430f1d85324dc5bfef650fe908a5e0717 Mon Sep 17 00:00:00 2001 From: Ivan Povazan Date: Fri, 8 Jul 2022 13:07:05 +0200 Subject: [PATCH 1/5] Rewriting Scalar to be replacable by linker stubs --- .../src/System/Runtime/Intrinsics/Scalar.cs | 1708 +++++++++-------- 1 file changed, 925 insertions(+), 783 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Scalar.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Scalar.cs index 4046064c92ae0..46bc09ac91ccf 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Scalar.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Scalar.cs @@ -8,158 +8,397 @@ namespace System.Runtime.Intrinsics internal static class Scalar where T : struct { + // FIXME: getter wired to Vector256.IsHardwareAccelerated + public static bool IsSupported { get => Vector256.IsHardwareAccelerated; } + public static T AllBitsSet { [MethodImpl(MethodImplOptions.AggressiveInlining)] get + { + if (IsSupported) + { + if (typeof(T) == typeof(byte)) + { + return (T)(object)byte.MaxValue; + } + else if (typeof(T) == typeof(double)) + { + return (T)(object)BitConverter.Int64BitsToDouble(-1); + } + else if (typeof(T) == typeof(short)) + { + return (T)(object)(short)-1; + } + else if (typeof(T) == typeof(int)) + { + return (T)(object)-1; + } + else if (typeof(T) == typeof(long)) + { + return (T)(object)(long)-1; + } + else if (typeof(T) == typeof(nint)) + { + return (T)(object)(nint)(-1); + } + else if (typeof(T) == typeof(nuint)) + { + return (T)(object)nuint.MaxValue; + } + else if (typeof(T) == typeof(sbyte)) + { + return (T)(object)(sbyte)-1; + } + else if (typeof(T) == typeof(float)) + { + return (T)(object)BitConverter.Int32BitsToSingle(-1); + } + else if (typeof(T) == typeof(ushort)) + { + return (T)(object)ushort.MaxValue; + } + else if (typeof(T) == typeof(uint)) + { + return (T)(object)uint.MaxValue; + } + else if (typeof(T) == typeof(ulong)) + { + return (T)(object)ulong.MaxValue; + } + else + { + throw new NotSupportedException(SR.Arg_TypeNotSupported); + } + } + else + { + throw new NotSupportedException(SR.Arg_TypeNotSupported); + } + } + } + + public static T One + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + if (IsSupported) + { + if (typeof(T) == typeof(byte)) + { + return (T)(object)(byte)1; + } + else if (typeof(T) == typeof(double)) + { + return (T)(object)(double)1; + } + else if (typeof(T) == typeof(short)) + { + return (T)(object)(short)1; + } + else if (typeof(T) == typeof(int)) + { + return (T)(object)1; + } + else if (typeof(T) == typeof(long)) + { + return (T)(object)(long)1; + } + else if (typeof(T) == typeof(nint)) + { + return (T)(object)(nint)1; + } + else if (typeof(T) == typeof(nuint)) + { + return (T)(object)(nuint)1; + } + else if (typeof(T) == typeof(sbyte)) + { + return (T)(object)(sbyte)1; + } + else if (typeof(T) == typeof(float)) + { + return (T)(object)(float)1; + } + else if (typeof(T) == typeof(ushort)) + { + return (T)(object)(ushort)1; + } + else if (typeof(T) == typeof(uint)) + { + return (T)(object)(uint)1; + } + else if (typeof(T) == typeof(ulong)) + { + return (T)(object)(ulong)1; + } + else + { + throw new NotSupportedException(SR.Arg_TypeNotSupported); + } + } + else + { + throw new NotSupportedException(SR.Arg_TypeNotSupported); + } + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T Abs(T value) + { + // byte, ushort, uint, and ulong should have already been handled + if (IsSupported) + { + if (typeof(T) == typeof(double)) + { + return (T)(object)Math.Abs((double)(object)value); + } + else if (typeof(T) == typeof(short)) + { + return (T)(object)Math.Abs((short)(object)value); + } + else if (typeof(T) == typeof(int)) + { + return (T)(object)Math.Abs((int)(object)value); + } + else if (typeof(T) == typeof(long)) + { + return (T)(object)Math.Abs((long)(object)value); + } + else if (typeof(T) == typeof(nint)) + { + return (T)(object)Math.Abs((nint)(object)value); + } + else if (typeof(T) == typeof(sbyte)) + { + return (T)(object)Math.Abs((sbyte)(object)value); + } + else if (typeof(T) == typeof(float)) + { + return (T)(object)Math.Abs((float)(object)value); + } + else + { + throw new NotSupportedException(SR.Arg_TypeNotSupported); + } + } + else + { + throw new NotSupportedException(SR.Arg_TypeNotSupported); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T Add(T left, T right) + { + if (IsSupported) { if (typeof(T) == typeof(byte)) { - return (T)(object)byte.MaxValue; + return (T)(object)(byte)((byte)(object)left + (byte)(object)right); } else if (typeof(T) == typeof(double)) { - return (T)(object)BitConverter.Int64BitsToDouble(-1); + return (T)(object)(double)((double)(object)left + (double)(object)right); } else if (typeof(T) == typeof(short)) { - return (T)(object)(short)-1; + return (T)(object)(short)((short)(object)left + (short)(object)right); } else if (typeof(T) == typeof(int)) { - return (T)(object)-1; + return (T)(object)((int)(object)left + (int)(object)right); } else if (typeof(T) == typeof(long)) { - return (T)(object)(long)-1; + return (T)(object)((long)(object)left + (long)(object)right); } else if (typeof(T) == typeof(nint)) { - return (T)(object)(nint)(-1); + return (T)(object)((nint)(object)left + (nint)(object)right); } else if (typeof(T) == typeof(nuint)) { - return (T)(object)nuint.MaxValue; + return (T)(object)((nuint)(object)left + (nuint)(object)right); } else if (typeof(T) == typeof(sbyte)) { - return (T)(object)(sbyte)-1; + return (T)(object)(sbyte)((sbyte)(object)left + (sbyte)(object)right); } else if (typeof(T) == typeof(float)) { - return (T)(object)BitConverter.Int32BitsToSingle(-1); + return (T)(object)(float)((float)(object)left + (float)(object)right); } else if (typeof(T) == typeof(ushort)) { - return (T)(object)ushort.MaxValue; + return (T)(object)(ushort)((ushort)(object)left + (ushort)(object)right); } else if (typeof(T) == typeof(uint)) { - return (T)(object)uint.MaxValue; + return (T)(object)((uint)(object)left + (uint)(object)right); } else if (typeof(T) == typeof(ulong)) { - return (T)(object)ulong.MaxValue; + return (T)(object)((ulong)(object)left + (ulong)(object)right); } else { throw new NotSupportedException(SR.Arg_TypeNotSupported); } } + else + { + throw new NotSupportedException(SR.Arg_TypeNotSupported); + } } - public static T One + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T Ceiling(T value) { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get + if (IsSupported) + { + if (typeof(T) == typeof(double)) + { + return (T)(object)Math.Ceiling((double)(object)value); + } + else if (typeof(T) == typeof(float)) + { + return (T)(object)MathF.Ceiling((float)(object)value); + } + else + { + throw new NotSupportedException(SR.Arg_TypeNotSupported); + } + } + else + { + throw new NotSupportedException(SR.Arg_TypeNotSupported); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T Divide(T left, T right) + { + if (IsSupported) { if (typeof(T) == typeof(byte)) { - return (T)(object)(byte)1; + return (T)(object)(byte)((byte)(object)left / (byte)(object)right); } else if (typeof(T) == typeof(double)) { - return (T)(object)(double)1; + return (T)(object)(double)((double)(object)left / (double)(object)right); } else if (typeof(T) == typeof(short)) { - return (T)(object)(short)1; + return (T)(object)(short)((short)(object)left / (short)(object)right); } else if (typeof(T) == typeof(int)) { - return (T)(object)1; + return (T)(object)((int)(object)left / (int)(object)right); } else if (typeof(T) == typeof(long)) { - return (T)(object)(long)1; + return (T)(object)((long)(object)left / (long)(object)right); } else if (typeof(T) == typeof(nint)) { - return (T)(object)(nint)1; + return (T)(object)((nint)(object)left / (nint)(object)right); } else if (typeof(T) == typeof(nuint)) { - return (T)(object)(nuint)1; + return (T)(object)((nuint)(object)left / (nuint)(object)right); } else if (typeof(T) == typeof(sbyte)) { - return (T)(object)(sbyte)1; + return (T)(object)(sbyte)((sbyte)(object)left / (sbyte)(object)right); } else if (typeof(T) == typeof(float)) { - return (T)(object)(float)1; + return (T)(object)(float)((float)(object)left / (float)(object)right); } else if (typeof(T) == typeof(ushort)) { - return (T)(object)(ushort)1; + return (T)(object)(ushort)((ushort)(object)left / (ushort)(object)right); } else if (typeof(T) == typeof(uint)) { - return (T)(object)(uint)1; + return (T)(object)((uint)(object)left / (uint)(object)right); } else if (typeof(T) == typeof(ulong)) { - return (T)(object)(ulong)1; + return (T)(object)((ulong)(object)left / (ulong)(object)right); } else { throw new NotSupportedException(SR.Arg_TypeNotSupported); } } + else + { + throw new NotSupportedException(SR.Arg_TypeNotSupported); + } } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static T Abs(T value) + public static bool Equals(T left, T right) { - // byte, ushort, uint, and ulong should have already been handled - - if (typeof(T) == typeof(double)) - { - return (T)(object)Math.Abs((double)(object)value); - } - else if (typeof(T) == typeof(short)) - { - return (T)(object)Math.Abs((short)(object)value); - } - else if (typeof(T) == typeof(int)) - { - return (T)(object)Math.Abs((int)(object)value); - } - else if (typeof(T) == typeof(long)) - { - return (T)(object)Math.Abs((long)(object)value); - } - else if (typeof(T) == typeof(nint)) - { - return (T)(object)Math.Abs((nint)(object)value); - } - else if (typeof(T) == typeof(sbyte)) - { - return (T)(object)Math.Abs((sbyte)(object)value); - } - else if (typeof(T) == typeof(float)) + if (IsSupported) { - return (T)(object)Math.Abs((float)(object)value); + if (typeof(T) == typeof(byte)) + { + return (byte)(object)left == (byte)(object)right; + } + else if (typeof(T) == typeof(double)) + { + return (double)(object)left == (double)(object)right; + } + else if (typeof(T) == typeof(short)) + { + return (short)(object)left == (short)(object)right; + } + else if (typeof(T) == typeof(int)) + { + return (int)(object)left == (int)(object)right; + } + else if (typeof(T) == typeof(long)) + { + return (long)(object)left == (long)(object)right; + } + else if (typeof(T) == typeof(nint)) + { + return (nint)(object)left == (nint)(object)right; + } + else if (typeof(T) == typeof(nuint)) + { + return (nuint)(object)left == (nuint)(object)right; + } + else if (typeof(T) == typeof(sbyte)) + { + return (sbyte)(object)left == (sbyte)(object)right; + } + else if (typeof(T) == typeof(float)) + { + return (float)(object)left == (float)(object)right; + } + else if (typeof(T) == typeof(ushort)) + { + return (ushort)(object)left == (ushort)(object)right; + } + else if (typeof(T) == typeof(uint)) + { + return (uint)(object)left == (uint)(object)right; + } + else if (typeof(T) == typeof(ulong)) + { + return (ulong)(object)left == (ulong)(object)right; + } + else + { + throw new NotSupportedException(SR.Arg_TypeNotSupported); + } } else { @@ -168,55 +407,84 @@ public static T Abs(T value) } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static T Add(T left, T right) + public static uint ExtractMostSignificantBit(T value) { - if (typeof(T) == typeof(byte)) - { - return (T)(object)(byte)((byte)(object)left + (byte)(object)right); - } - else if (typeof(T) == typeof(double)) - { - return (T)(object)(double)((double)(object)left + (double)(object)right); - } - else if (typeof(T) == typeof(short)) - { - return (T)(object)(short)((short)(object)left + (short)(object)right); - } - else if (typeof(T) == typeof(int)) - { - return (T)(object)((int)(object)left + (int)(object)right); - } - else if (typeof(T) == typeof(long)) - { - return (T)(object)((long)(object)left + (long)(object)right); - } - else if (typeof(T) == typeof(nint)) - { - return (T)(object)((nint)(object)left + (nint)(object)right); - } - else if (typeof(T) == typeof(nuint)) - { - return (T)(object)((nuint)(object)left + (nuint)(object)right); - } - else if (typeof(T) == typeof(sbyte)) - { - return (T)(object)(sbyte)((sbyte)(object)left + (sbyte)(object)right); - } - else if (typeof(T) == typeof(float)) - { - return (T)(object)(float)((float)(object)left + (float)(object)right); - } - else if (typeof(T) == typeof(ushort)) - { - return (T)(object)(ushort)((ushort)(object)left + (ushort)(object)right); - } - else if (typeof(T) == typeof(uint)) - { - return (T)(object)((uint)(object)left + (uint)(object)right); - } - else if (typeof(T) == typeof(ulong)) + if (IsSupported) { - return (T)(object)((ulong)(object)left + (ulong)(object)right); + if (typeof(T) == typeof(byte)) + { + uint bits = (byte)(object)value; + return bits >> 7; + } + else if (typeof(T) == typeof(double)) + { + ulong bits = BitConverter.DoubleToUInt64Bits((double)(object)value); + return (uint)(bits >> 63); + } + else if (typeof(T) == typeof(short)) + { + uint bits = (ushort)(short)(object)value; + return bits >> 15; + } + else if (typeof(T) == typeof(int)) + { + uint bits = (uint)(int)(object)value; + return bits >> 31; + } + else if (typeof(T) == typeof(long)) + { + ulong bits = (ulong)(long)(object)value; + return (uint)(bits >> 63); + } + else if (typeof(T) == typeof(nint)) + { +#if TARGET_64BIT + ulong bits = (ulong)(nint)(object)value; + return (uint)(bits >> 63); +#else + uint bits = (uint)(nint)(object)value; + return bits >> 31; +#endif + } + else if (typeof(T) == typeof(nuint)) + { +#if TARGET_64BIT + ulong bits = (ulong)(nuint)(object)value; + return (uint)(bits >> 63); +#else + uint bits = (uint)(nuint)(object)value; + return bits >> 31; +#endif + } + else if (typeof(T) == typeof(sbyte)) + { + uint bits = (byte)(sbyte)(object)value; + return bits >> 7; + } + else if (typeof(T) == typeof(float)) + { + uint bits = BitConverter.SingleToUInt32Bits((float)(object)value); + return bits >> 31; + } + else if (typeof(T) == typeof(ushort)) + { + uint bits = (ushort)(object)value; + return bits >> 15; + } + else if (typeof(T) == typeof(uint)) + { + uint bits = (uint)(object)value; + return bits >> 31; + } + else if (typeof(T) == typeof(ulong)) + { + ulong bits = (ulong)(object)value; + return (uint)(bits >> 63); + } + else + { + throw new NotSupportedException(SR.Arg_TypeNotSupported); + } } else { @@ -225,15 +493,22 @@ public static T Add(T left, T right) } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static T Ceiling(T value) + public static T Floor(T value) { - if (typeof(T) == typeof(double)) - { - return (T)(object)Math.Ceiling((double)(object)value); - } - else if (typeof(T) == typeof(float)) + if (IsSupported) { - return (T)(object)MathF.Ceiling((float)(object)value); + if (typeof(T) == typeof(double)) + { + return (T)(object)Math.Floor((double)(object)value); + } + else if (typeof(T) == typeof(float)) + { + return (T)(object)MathF.Floor((float)(object)value); + } + else + { + throw new NotSupportedException(SR.Arg_TypeNotSupported); + } } else { @@ -242,265 +517,62 @@ public static T Ceiling(T value) } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static T Divide(T left, T right) + public static bool GreaterThan(T left, T right) { - if (typeof(T) == typeof(byte)) - { - return (T)(object)(byte)((byte)(object)left / (byte)(object)right); - } - else if (typeof(T) == typeof(double)) - { - return (T)(object)(double)((double)(object)left / (double)(object)right); - } - else if (typeof(T) == typeof(short)) - { - return (T)(object)(short)((short)(object)left / (short)(object)right); - } - else if (typeof(T) == typeof(int)) - { - return (T)(object)((int)(object)left / (int)(object)right); - } - else if (typeof(T) == typeof(long)) - { - return (T)(object)((long)(object)left / (long)(object)right); - } - else if (typeof(T) == typeof(nint)) - { - return (T)(object)((nint)(object)left / (nint)(object)right); - } - else if (typeof(T) == typeof(nuint)) - { - return (T)(object)((nuint)(object)left / (nuint)(object)right); - } - else if (typeof(T) == typeof(sbyte)) - { - return (T)(object)(sbyte)((sbyte)(object)left / (sbyte)(object)right); - } - else if (typeof(T) == typeof(float)) - { - return (T)(object)(float)((float)(object)left / (float)(object)right); - } - else if (typeof(T) == typeof(ushort)) + if (IsSupported) { - return (T)(object)(ushort)((ushort)(object)left / (ushort)(object)right); - } - else if (typeof(T) == typeof(uint)) - { - return (T)(object)((uint)(object)left / (uint)(object)right); - } - else if (typeof(T) == typeof(ulong)) - { - return (T)(object)((ulong)(object)left / (ulong)(object)right); - } - else - { - throw new NotSupportedException(SR.Arg_TypeNotSupported); - } - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool Equals(T left, T right) - { - if (typeof(T) == typeof(byte)) - { - return (byte)(object)left == (byte)(object)right; - } - else if (typeof(T) == typeof(double)) - { - return (double)(object)left == (double)(object)right; - } - else if (typeof(T) == typeof(short)) - { - return (short)(object)left == (short)(object)right; - } - else if (typeof(T) == typeof(int)) - { - return (int)(object)left == (int)(object)right; - } - else if (typeof(T) == typeof(long)) - { - return (long)(object)left == (long)(object)right; - } - else if (typeof(T) == typeof(nint)) - { - return (nint)(object)left == (nint)(object)right; - } - else if (typeof(T) == typeof(nuint)) - { - return (nuint)(object)left == (nuint)(object)right; - } - else if (typeof(T) == typeof(sbyte)) - { - return (sbyte)(object)left == (sbyte)(object)right; - } - else if (typeof(T) == typeof(float)) - { - return (float)(object)left == (float)(object)right; - } - else if (typeof(T) == typeof(ushort)) - { - return (ushort)(object)left == (ushort)(object)right; - } - else if (typeof(T) == typeof(uint)) - { - return (uint)(object)left == (uint)(object)right; - } - else if (typeof(T) == typeof(ulong)) - { - return (ulong)(object)left == (ulong)(object)right; - } - else - { - throw new NotSupportedException(SR.Arg_TypeNotSupported); - } - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static uint ExtractMostSignificantBit(T value) - { - if (typeof(T) == typeof(byte)) - { - uint bits = (byte)(object)value; - return bits >> 7; - } - else if (typeof(T) == typeof(double)) - { - ulong bits = BitConverter.DoubleToUInt64Bits((double)(object)value); - return (uint)(bits >> 63); - } - else if (typeof(T) == typeof(short)) - { - uint bits = (ushort)(short)(object)value; - return bits >> 15; - } - else if (typeof(T) == typeof(int)) - { - uint bits = (uint)(int)(object)value; - return bits >> 31; - } - else if (typeof(T) == typeof(long)) - { - ulong bits = (ulong)(long)(object)value; - return (uint)(bits >> 63); - } - else if (typeof(T) == typeof(nint)) - { -#if TARGET_64BIT - ulong bits = (ulong)(nint)(object)value; - return (uint)(bits >> 63); -#else - uint bits = (uint)(nint)(object)value; - return bits >> 31; -#endif - } - else if (typeof(T) == typeof(nuint)) - { -#if TARGET_64BIT - ulong bits = (ulong)(nuint)(object)value; - return (uint)(bits >> 63); -#else - uint bits = (uint)(nuint)(object)value; - return bits >> 31; -#endif - } - else if (typeof(T) == typeof(sbyte)) - { - uint bits = (byte)(sbyte)(object)value; - return bits >> 7; - } - else if (typeof(T) == typeof(float)) - { - uint bits = BitConverter.SingleToUInt32Bits((float)(object)value); - return bits >> 31; - } - else if (typeof(T) == typeof(ushort)) - { - uint bits = (ushort)(object)value; - return bits >> 15; - } - else if (typeof(T) == typeof(uint)) - { - uint bits = (uint)(object)value; - return bits >> 31; - } - else if (typeof(T) == typeof(ulong)) - { - ulong bits = (ulong)(object)value; - return (uint)(bits >> 63); - } - else - { - throw new NotSupportedException(SR.Arg_TypeNotSupported); - } - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static T Floor(T value) - { - if (typeof(T) == typeof(double)) - { - return (T)(object)Math.Floor((double)(object)value); - } - else if (typeof(T) == typeof(float)) - { - return (T)(object)MathF.Floor((float)(object)value); - } - else - { - throw new NotSupportedException(SR.Arg_TypeNotSupported); - } - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool GreaterThan(T left, T right) - { - if (typeof(T) == typeof(byte)) - { - return (byte)(object)left > (byte)(object)right; - } - else if (typeof(T) == typeof(double)) - { - return (double)(object)left > (double)(object)right; - } - else if (typeof(T) == typeof(short)) - { - return (short)(object)left > (short)(object)right; - } - else if (typeof(T) == typeof(int)) - { - return (int)(object)left > (int)(object)right; - } - else if (typeof(T) == typeof(long)) - { - return (long)(object)left > (long)(object)right; - } - else if (typeof(T) == typeof(nint)) - { - return (nint)(object)left > (nint)(object)right; - } - else if (typeof(T) == typeof(nuint)) - { - return (nuint)(object)left > (nuint)(object)right; - } - else if (typeof(T) == typeof(sbyte)) - { - return (sbyte)(object)left > (sbyte)(object)right; - } - else if (typeof(T) == typeof(float)) - { - return (float)(object)left > (float)(object)right; - } - else if (typeof(T) == typeof(ushort)) - { - return (ushort)(object)left > (ushort)(object)right; - } - else if (typeof(T) == typeof(uint)) - { - return (uint)(object)left > (uint)(object)right; - } - else if (typeof(T) == typeof(ulong)) - { - return (ulong)(object)left > (ulong)(object)right; + if (typeof(T) == typeof(byte)) + { + return (byte)(object)left > (byte)(object)right; + } + else if (typeof(T) == typeof(double)) + { + return (double)(object)left > (double)(object)right; + } + else if (typeof(T) == typeof(short)) + { + return (short)(object)left > (short)(object)right; + } + else if (typeof(T) == typeof(int)) + { + return (int)(object)left > (int)(object)right; + } + else if (typeof(T) == typeof(long)) + { + return (long)(object)left > (long)(object)right; + } + else if (typeof(T) == typeof(nint)) + { + return (nint)(object)left > (nint)(object)right; + } + else if (typeof(T) == typeof(nuint)) + { + return (nuint)(object)left > (nuint)(object)right; + } + else if (typeof(T) == typeof(sbyte)) + { + return (sbyte)(object)left > (sbyte)(object)right; + } + else if (typeof(T) == typeof(float)) + { + return (float)(object)left > (float)(object)right; + } + else if (typeof(T) == typeof(ushort)) + { + return (ushort)(object)left > (ushort)(object)right; + } + else if (typeof(T) == typeof(uint)) + { + return (uint)(object)left > (uint)(object)right; + } + else if (typeof(T) == typeof(ulong)) + { + return (ulong)(object)left > (ulong)(object)right; + } + else + { + throw new NotSupportedException(SR.Arg_TypeNotSupported); + } } else { @@ -511,53 +583,60 @@ public static bool GreaterThan(T left, T right) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool GreaterThanOrEqual(T left, T right) { - if (typeof(T) == typeof(byte)) + if (IsSupported) { - return (byte)(object)left >= (byte)(object)right; - } - else if (typeof(T) == typeof(double)) - { - return (double)(object)left >= (double)(object)right; - } - else if (typeof(T) == typeof(short)) - { - return (short)(object)left >= (short)(object)right; - } - else if (typeof(T) == typeof(int)) - { - return (int)(object)left >= (int)(object)right; - } - else if (typeof(T) == typeof(long)) - { - return (long)(object)left >= (long)(object)right; - } - else if (typeof(T) == typeof(nint)) - { - return (nint)(object)left >= (nint)(object)right; - } - else if (typeof(T) == typeof(nuint)) - { - return (nuint)(object)left >= (nuint)(object)right; - } - else if (typeof(T) == typeof(sbyte)) - { - return (sbyte)(object)left >= (sbyte)(object)right; - } - else if (typeof(T) == typeof(float)) - { - return (float)(object)left >= (float)(object)right; - } - else if (typeof(T) == typeof(ushort)) - { - return (ushort)(object)left >= (ushort)(object)right; - } - else if (typeof(T) == typeof(uint)) - { - return (uint)(object)left >= (uint)(object)right; - } - else if (typeof(T) == typeof(ulong)) - { - return (ulong)(object)left >= (ulong)(object)right; + if (typeof(T) == typeof(byte)) + { + return (byte)(object)left >= (byte)(object)right; + } + else if (typeof(T) == typeof(double)) + { + return (double)(object)left >= (double)(object)right; + } + else if (typeof(T) == typeof(short)) + { + return (short)(object)left >= (short)(object)right; + } + else if (typeof(T) == typeof(int)) + { + return (int)(object)left >= (int)(object)right; + } + else if (typeof(T) == typeof(long)) + { + return (long)(object)left >= (long)(object)right; + } + else if (typeof(T) == typeof(nint)) + { + return (nint)(object)left >= (nint)(object)right; + } + else if (typeof(T) == typeof(nuint)) + { + return (nuint)(object)left >= (nuint)(object)right; + } + else if (typeof(T) == typeof(sbyte)) + { + return (sbyte)(object)left >= (sbyte)(object)right; + } + else if (typeof(T) == typeof(float)) + { + return (float)(object)left >= (float)(object)right; + } + else if (typeof(T) == typeof(ushort)) + { + return (ushort)(object)left >= (ushort)(object)right; + } + else if (typeof(T) == typeof(uint)) + { + return (uint)(object)left >= (uint)(object)right; + } + else if (typeof(T) == typeof(ulong)) + { + return (ulong)(object)left >= (ulong)(object)right; + } + else + { + throw new NotSupportedException(SR.Arg_TypeNotSupported); + } } else { @@ -568,53 +647,60 @@ public static bool GreaterThanOrEqual(T left, T right) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool LessThan(T left, T right) { - if (typeof(T) == typeof(byte)) - { - return (byte)(object)left < (byte)(object)right; - } - else if (typeof(T) == typeof(double)) - { - return (double)(object)left < (double)(object)right; - } - else if (typeof(T) == typeof(short)) - { - return (short)(object)left < (short)(object)right; - } - else if (typeof(T) == typeof(int)) - { - return (int)(object)left < (int)(object)right; - } - else if (typeof(T) == typeof(long)) - { - return (long)(object)left < (long)(object)right; - } - else if (typeof(T) == typeof(nint)) - { - return (nint)(object)left < (nint)(object)right; - } - else if (typeof(T) == typeof(nuint)) - { - return (nuint)(object)left < (nuint)(object)right; - } - else if (typeof(T) == typeof(sbyte)) - { - return (sbyte)(object)left < (sbyte)(object)right; - } - else if (typeof(T) == typeof(float)) - { - return (float)(object)left < (float)(object)right; - } - else if (typeof(T) == typeof(ushort)) + if (IsSupported) { - return (ushort)(object)left < (ushort)(object)right; - } - else if (typeof(T) == typeof(uint)) - { - return (uint)(object)left < (uint)(object)right; - } - else if (typeof(T) == typeof(ulong)) - { - return (ulong)(object)left < (ulong)(object)right; + if (typeof(T) == typeof(byte)) + { + return (byte)(object)left < (byte)(object)right; + } + else if (typeof(T) == typeof(double)) + { + return (double)(object)left < (double)(object)right; + } + else if (typeof(T) == typeof(short)) + { + return (short)(object)left < (short)(object)right; + } + else if (typeof(T) == typeof(int)) + { + return (int)(object)left < (int)(object)right; + } + else if (typeof(T) == typeof(long)) + { + return (long)(object)left < (long)(object)right; + } + else if (typeof(T) == typeof(nint)) + { + return (nint)(object)left < (nint)(object)right; + } + else if (typeof(T) == typeof(nuint)) + { + return (nuint)(object)left < (nuint)(object)right; + } + else if (typeof(T) == typeof(sbyte)) + { + return (sbyte)(object)left < (sbyte)(object)right; + } + else if (typeof(T) == typeof(float)) + { + return (float)(object)left < (float)(object)right; + } + else if (typeof(T) == typeof(ushort)) + { + return (ushort)(object)left < (ushort)(object)right; + } + else if (typeof(T) == typeof(uint)) + { + return (uint)(object)left < (uint)(object)right; + } + else if (typeof(T) == typeof(ulong)) + { + return (ulong)(object)left < (ulong)(object)right; + } + else + { + throw new NotSupportedException(SR.Arg_TypeNotSupported); + } } else { @@ -625,53 +711,60 @@ public static bool LessThan(T left, T right) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool LessThanOrEqual(T left, T right) { - if (typeof(T) == typeof(byte)) - { - return (byte)(object)left <= (byte)(object)right; - } - else if (typeof(T) == typeof(double)) - { - return (double)(object)left <= (double)(object)right; - } - else if (typeof(T) == typeof(short)) - { - return (short)(object)left <= (short)(object)right; - } - else if (typeof(T) == typeof(int)) - { - return (int)(object)left <= (int)(object)right; - } - else if (typeof(T) == typeof(long)) + if (IsSupported) { - return (long)(object)left <= (long)(object)right; - } - else if (typeof(T) == typeof(nint)) - { - return (nint)(object)left <= (nint)(object)right; - } - else if (typeof(T) == typeof(nuint)) - { - return (nuint)(object)left <= (nuint)(object)right; - } - else if (typeof(T) == typeof(sbyte)) - { - return (sbyte)(object)left <= (sbyte)(object)right; - } - else if (typeof(T) == typeof(float)) - { - return (float)(object)left <= (float)(object)right; - } - else if (typeof(T) == typeof(ushort)) - { - return (ushort)(object)left <= (ushort)(object)right; - } - else if (typeof(T) == typeof(uint)) - { - return (uint)(object)left <= (uint)(object)right; - } - else if (typeof(T) == typeof(ulong)) - { - return (ulong)(object)left <= (ulong)(object)right; + if (typeof(T) == typeof(byte)) + { + return (byte)(object)left <= (byte)(object)right; + } + else if (typeof(T) == typeof(double)) + { + return (double)(object)left <= (double)(object)right; + } + else if (typeof(T) == typeof(short)) + { + return (short)(object)left <= (short)(object)right; + } + else if (typeof(T) == typeof(int)) + { + return (int)(object)left <= (int)(object)right; + } + else if (typeof(T) == typeof(long)) + { + return (long)(object)left <= (long)(object)right; + } + else if (typeof(T) == typeof(nint)) + { + return (nint)(object)left <= (nint)(object)right; + } + else if (typeof(T) == typeof(nuint)) + { + return (nuint)(object)left <= (nuint)(object)right; + } + else if (typeof(T) == typeof(sbyte)) + { + return (sbyte)(object)left <= (sbyte)(object)right; + } + else if (typeof(T) == typeof(float)) + { + return (float)(object)left <= (float)(object)right; + } + else if (typeof(T) == typeof(ushort)) + { + return (ushort)(object)left <= (ushort)(object)right; + } + else if (typeof(T) == typeof(uint)) + { + return (uint)(object)left <= (uint)(object)right; + } + else if (typeof(T) == typeof(ulong)) + { + return (ulong)(object)left <= (ulong)(object)right; + } + else + { + throw new NotSupportedException(SR.Arg_TypeNotSupported); + } } else { @@ -682,53 +775,60 @@ public static bool LessThanOrEqual(T left, T right) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T Multiply(T left, T right) { - if (typeof(T) == typeof(byte)) - { - return (T)(object)(byte)((byte)(object)left * (byte)(object)right); - } - else if (typeof(T) == typeof(double)) + if (IsSupported) { - return (T)(object)(double)((double)(object)left * (double)(object)right); - } - else if (typeof(T) == typeof(short)) - { - return (T)(object)(short)((short)(object)left * (short)(object)right); - } - else if (typeof(T) == typeof(int)) - { - return (T)(object)((int)(object)left * (int)(object)right); - } - else if (typeof(T) == typeof(long)) - { - return (T)(object)((long)(object)left * (long)(object)right); - } - else if (typeof(T) == typeof(nint)) - { - return (T)(object)((nint)(object)left * (nint)(object)right); - } - else if (typeof(T) == typeof(nuint)) - { - return (T)(object)((nuint)(object)left * (nuint)(object)right); - } - else if (typeof(T) == typeof(sbyte)) - { - return (T)(object)(sbyte)((sbyte)(object)left * (sbyte)(object)right); - } - else if (typeof(T) == typeof(float)) - { - return (T)(object)(float)((float)(object)left * (float)(object)right); - } - else if (typeof(T) == typeof(ushort)) - { - return (T)(object)(ushort)((ushort)(object)left * (ushort)(object)right); - } - else if (typeof(T) == typeof(uint)) - { - return (T)(object)((uint)(object)left * (uint)(object)right); - } - else if (typeof(T) == typeof(ulong)) - { - return (T)(object)((ulong)(object)left * (ulong)(object)right); + if (typeof(T) == typeof(byte)) + { + return (T)(object)(byte)((byte)(object)left * (byte)(object)right); + } + else if (typeof(T) == typeof(double)) + { + return (T)(object)(double)((double)(object)left * (double)(object)right); + } + else if (typeof(T) == typeof(short)) + { + return (T)(object)(short)((short)(object)left * (short)(object)right); + } + else if (typeof(T) == typeof(int)) + { + return (T)(object)((int)(object)left * (int)(object)right); + } + else if (typeof(T) == typeof(long)) + { + return (T)(object)((long)(object)left * (long)(object)right); + } + else if (typeof(T) == typeof(nint)) + { + return (T)(object)((nint)(object)left * (nint)(object)right); + } + else if (typeof(T) == typeof(nuint)) + { + return (T)(object)((nuint)(object)left * (nuint)(object)right); + } + else if (typeof(T) == typeof(sbyte)) + { + return (T)(object)(sbyte)((sbyte)(object)left * (sbyte)(object)right); + } + else if (typeof(T) == typeof(float)) + { + return (T)(object)(float)((float)(object)left * (float)(object)right); + } + else if (typeof(T) == typeof(ushort)) + { + return (T)(object)(ushort)((ushort)(object)left * (ushort)(object)right); + } + else if (typeof(T) == typeof(uint)) + { + return (T)(object)((uint)(object)left * (uint)(object)right); + } + else if (typeof(T) == typeof(ulong)) + { + return (T)(object)((ulong)(object)left * (ulong)(object)right); + } + else + { + throw new NotSupportedException(SR.Arg_TypeNotSupported); + } } else { @@ -738,53 +838,60 @@ public static T Multiply(T left, T right) public static bool ObjectEquals(T left, T right) { - if (typeof(T) == typeof(byte)) - { - return ((byte)(object)left).Equals((byte)(object)right); - } - else if (typeof(T) == typeof(double)) + if (IsSupported) { - return ((double)(object)left).Equals((double)(object)right); - } - else if (typeof(T) == typeof(short)) - { - return ((short)(object)left).Equals((short)(object)right); - } - else if (typeof(T) == typeof(int)) - { - return ((int)(object)left).Equals((int)(object)right); - } - else if (typeof(T) == typeof(long)) - { - return ((long)(object)left).Equals((long)(object)right); - } - else if (typeof(T) == typeof(nint)) - { - return ((nint)(object)left).Equals((nint)(object)right); - } - else if (typeof(T) == typeof(nuint)) - { - return ((nuint)(object)left).Equals((nuint)(object)right); - } - else if (typeof(T) == typeof(sbyte)) - { - return ((sbyte)(object)left).Equals((sbyte)(object)right); - } - else if (typeof(T) == typeof(float)) - { - return ((float)(object)left).Equals((float)(object)right); - } - else if (typeof(T) == typeof(ushort)) - { - return ((ushort)(object)left).Equals((ushort)(object)right); - } - else if (typeof(T) == typeof(uint)) - { - return ((uint)(object)left).Equals((uint)(object)right); - } - else if (typeof(T) == typeof(ulong)) - { - return ((ulong)(object)left).Equals((ulong)(object)right); + if (typeof(T) == typeof(byte)) + { + return ((byte)(object)left).Equals((byte)(object)right); + } + else if (typeof(T) == typeof(double)) + { + return ((double)(object)left).Equals((double)(object)right); + } + else if (typeof(T) == typeof(short)) + { + return ((short)(object)left).Equals((short)(object)right); + } + else if (typeof(T) == typeof(int)) + { + return ((int)(object)left).Equals((int)(object)right); + } + else if (typeof(T) == typeof(long)) + { + return ((long)(object)left).Equals((long)(object)right); + } + else if (typeof(T) == typeof(nint)) + { + return ((nint)(object)left).Equals((nint)(object)right); + } + else if (typeof(T) == typeof(nuint)) + { + return ((nuint)(object)left).Equals((nuint)(object)right); + } + else if (typeof(T) == typeof(sbyte)) + { + return ((sbyte)(object)left).Equals((sbyte)(object)right); + } + else if (typeof(T) == typeof(float)) + { + return ((float)(object)left).Equals((float)(object)right); + } + else if (typeof(T) == typeof(ushort)) + { + return ((ushort)(object)left).Equals((ushort)(object)right); + } + else if (typeof(T) == typeof(uint)) + { + return ((uint)(object)left).Equals((uint)(object)right); + } + else if (typeof(T) == typeof(ulong)) + { + return ((ulong)(object)left).Equals((ulong)(object)right); + } + else + { + throw new NotSupportedException(SR.Arg_TypeNotSupported); + } } else { @@ -795,45 +902,52 @@ public static bool ObjectEquals(T left, T right) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T ShiftLeft(T value, int shiftCount) { - if (typeof(T) == typeof(byte)) - { - return (T)(object)(byte)((byte)(object)value << (shiftCount & 7)); - } - else if (typeof(T) == typeof(short)) - { - return (T)(object)(short)((short)(object)value << (shiftCount & 15)); - } - else if (typeof(T) == typeof(int)) - { - return (T)(object)(int)((int)(object)value << shiftCount); - } - else if (typeof(T) == typeof(long)) - { - return (T)(object)(long)((long)(object)value << shiftCount); - } - else if (typeof(T) == typeof(nint)) - { - return (T)(object)(nint)((nint)(object)value << shiftCount); - } - else if (typeof(T) == typeof(nuint)) - { - return (T)(object)(nuint)((nuint)(object)value << shiftCount); - } - else if (typeof(T) == typeof(sbyte)) - { - return (T)(object)(sbyte)((sbyte)(object)value << (shiftCount & 7)); - } - else if (typeof(T) == typeof(ushort)) + if (IsSupported) { - return (T)(object)(ushort)((ushort)(object)value << (shiftCount & 15)); - } - else if (typeof(T) == typeof(uint)) - { - return (T)(object)(uint)((uint)(object)value << shiftCount); - } - else if (typeof(T) == typeof(ulong)) - { - return (T)(object)(ulong)((ulong)(object)value << shiftCount); + if (typeof(T) == typeof(byte)) + { + return (T)(object)(byte)((byte)(object)value << (shiftCount & 7)); + } + else if (typeof(T) == typeof(short)) + { + return (T)(object)(short)((short)(object)value << (shiftCount & 15)); + } + else if (typeof(T) == typeof(int)) + { + return (T)(object)(int)((int)(object)value << shiftCount); + } + else if (typeof(T) == typeof(long)) + { + return (T)(object)(long)((long)(object)value << shiftCount); + } + else if (typeof(T) == typeof(nint)) + { + return (T)(object)(nint)((nint)(object)value << shiftCount); + } + else if (typeof(T) == typeof(nuint)) + { + return (T)(object)(nuint)((nuint)(object)value << shiftCount); + } + else if (typeof(T) == typeof(sbyte)) + { + return (T)(object)(sbyte)((sbyte)(object)value << (shiftCount & 7)); + } + else if (typeof(T) == typeof(ushort)) + { + return (T)(object)(ushort)((ushort)(object)value << (shiftCount & 15)); + } + else if (typeof(T) == typeof(uint)) + { + return (T)(object)(uint)((uint)(object)value << shiftCount); + } + else if (typeof(T) == typeof(ulong)) + { + return (T)(object)(ulong)((ulong)(object)value << shiftCount); + } + else + { + throw new NotSupportedException(SR.Arg_TypeNotSupported); + } } else { @@ -844,25 +958,32 @@ public static T ShiftLeft(T value, int shiftCount) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T ShiftRightArithmetic(T value, int shiftCount) { - if (typeof(T) == typeof(short)) - { - return (T)(object)(short)((short)(object)value >> (shiftCount & 15)); - } - else if (typeof(T) == typeof(int)) + if (IsSupported) { - return (T)(object)(int)((int)(object)value >> shiftCount); - } - else if (typeof(T) == typeof(long)) - { - return (T)(object)(long)((long)(object)value >> shiftCount); - } - else if (typeof(T) == typeof(nint)) - { - return (T)(object)(nint)((nint)(object)value >> shiftCount); - } - else if (typeof(T) == typeof(sbyte)) - { - return (T)(object)(sbyte)((sbyte)(object)value >> (shiftCount & 7)); + if (typeof(T) == typeof(short)) + { + return (T)(object)(short)((short)(object)value >> (shiftCount & 15)); + } + else if (typeof(T) == typeof(int)) + { + return (T)(object)(int)((int)(object)value >> shiftCount); + } + else if (typeof(T) == typeof(long)) + { + return (T)(object)(long)((long)(object)value >> shiftCount); + } + else if (typeof(T) == typeof(nint)) + { + return (T)(object)(nint)((nint)(object)value >> shiftCount); + } + else if (typeof(T) == typeof(sbyte)) + { + return (T)(object)(sbyte)((sbyte)(object)value >> (shiftCount & 7)); + } + else + { + throw new NotSupportedException(SR.Arg_TypeNotSupported); + } } else { @@ -873,45 +994,52 @@ public static T ShiftRightArithmetic(T value, int shiftCount) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T ShiftRightLogical(T value, int shiftCount) { - if (typeof(T) == typeof(byte)) - { - return (T)(object)(byte)((byte)(object)value >> (shiftCount & 7)); - } - else if (typeof(T) == typeof(short)) - { - return (T)(object)(short)((ushort)(short)(object)value >> (shiftCount & 15)); - } - else if (typeof(T) == typeof(int)) - { - return (T)(object)(int)((uint)(int)(object)value >> shiftCount); - } - else if (typeof(T) == typeof(long)) - { - return (T)(object)(long)((ulong)(long)(object)value >> shiftCount); - } - else if (typeof(T) == typeof(nint)) - { - return (T)(object)(nint)((nuint)(nint)(object)value >> shiftCount); - } - else if (typeof(T) == typeof(nuint)) - { - return (T)(object)(nuint)((nuint)(object)value >> shiftCount); - } - else if (typeof(T) == typeof(sbyte)) - { - return (T)(object)(sbyte)((byte)(sbyte)(object)value >> (shiftCount & 7)); - } - else if (typeof(T) == typeof(ushort)) - { - return (T)(object)(ushort)((ushort)(object)value >> (shiftCount & 15)); - } - else if (typeof(T) == typeof(uint)) + if (IsSupported) { - return (T)(object)(uint)((uint)(object)value >> shiftCount); - } - else if (typeof(T) == typeof(ulong)) - { - return (T)(object)(ulong)((ulong)(object)value >> shiftCount); + if (typeof(T) == typeof(byte)) + { + return (T)(object)(byte)((byte)(object)value >> (shiftCount & 7)); + } + else if (typeof(T) == typeof(short)) + { + return (T)(object)(short)((ushort)(short)(object)value >> (shiftCount & 15)); + } + else if (typeof(T) == typeof(int)) + { + return (T)(object)(int)((uint)(int)(object)value >> shiftCount); + } + else if (typeof(T) == typeof(long)) + { + return (T)(object)(long)((ulong)(long)(object)value >> shiftCount); + } + else if (typeof(T) == typeof(nint)) + { + return (T)(object)(nint)((nuint)(nint)(object)value >> shiftCount); + } + else if (typeof(T) == typeof(nuint)) + { + return (T)(object)(nuint)((nuint)(object)value >> shiftCount); + } + else if (typeof(T) == typeof(sbyte)) + { + return (T)(object)(sbyte)((byte)(sbyte)(object)value >> (shiftCount & 7)); + } + else if (typeof(T) == typeof(ushort)) + { + return (T)(object)(ushort)((ushort)(object)value >> (shiftCount & 15)); + } + else if (typeof(T) == typeof(uint)) + { + return (T)(object)(uint)((uint)(object)value >> shiftCount); + } + else if (typeof(T) == typeof(ulong)) + { + return (T)(object)(ulong)((ulong)(object)value >> shiftCount); + } + else + { + throw new NotSupportedException(SR.Arg_TypeNotSupported); + } } else { @@ -922,53 +1050,60 @@ public static T ShiftRightLogical(T value, int shiftCount) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T Sqrt(T value) { - if (typeof(T) == typeof(byte)) - { - return (T)(object)(byte)MathF.Sqrt((byte)(object)value); - } - else if (typeof(T) == typeof(double)) - { - return (T)(object)(double)Math.Sqrt((double)(object)value); - } - else if (typeof(T) == typeof(short)) - { - return (T)(object)(short)MathF.Sqrt((short)(object)value); - } - else if (typeof(T) == typeof(int)) - { - return (T)(object)(int)Math.Sqrt((int)(object)value); - } - else if (typeof(T) == typeof(long)) - { - return (T)(object)(long)Math.Sqrt((long)(object)value); - } - else if (typeof(T) == typeof(nint)) - { - return (T)(object)(nint)Math.Sqrt((nint)(object)value); - } - else if (typeof(T) == typeof(nuint)) + if (IsSupported) { - return (T)(object)(nuint)Math.Sqrt((nuint)(object)value); - } - else if (typeof(T) == typeof(sbyte)) - { - return (T)(object)(sbyte)MathF.Sqrt((sbyte)(object)value); - } - else if (typeof(T) == typeof(float)) - { - return (T)(object)(float)MathF.Sqrt((float)(object)value); - } - else if (typeof(T) == typeof(ushort)) - { - return (T)(object)(ushort)MathF.Sqrt((ushort)(object)value); - } - else if (typeof(T) == typeof(uint)) - { - return (T)(object)(uint)Math.Sqrt((uint)(object)value); - } - else if (typeof(T) == typeof(ulong)) - { - return (T)(object)(ulong)Math.Sqrt((ulong)(object)value); + if (typeof(T) == typeof(byte)) + { + return (T)(object)(byte)MathF.Sqrt((byte)(object)value); + } + else if (typeof(T) == typeof(double)) + { + return (T)(object)(double)Math.Sqrt((double)(object)value); + } + else if (typeof(T) == typeof(short)) + { + return (T)(object)(short)MathF.Sqrt((short)(object)value); + } + else if (typeof(T) == typeof(int)) + { + return (T)(object)(int)Math.Sqrt((int)(object)value); + } + else if (typeof(T) == typeof(long)) + { + return (T)(object)(long)Math.Sqrt((long)(object)value); + } + else if (typeof(T) == typeof(nint)) + { + return (T)(object)(nint)Math.Sqrt((nint)(object)value); + } + else if (typeof(T) == typeof(nuint)) + { + return (T)(object)(nuint)Math.Sqrt((nuint)(object)value); + } + else if (typeof(T) == typeof(sbyte)) + { + return (T)(object)(sbyte)MathF.Sqrt((sbyte)(object)value); + } + else if (typeof(T) == typeof(float)) + { + return (T)(object)(float)MathF.Sqrt((float)(object)value); + } + else if (typeof(T) == typeof(ushort)) + { + return (T)(object)(ushort)MathF.Sqrt((ushort)(object)value); + } + else if (typeof(T) == typeof(uint)) + { + return (T)(object)(uint)Math.Sqrt((uint)(object)value); + } + else if (typeof(T) == typeof(ulong)) + { + return (T)(object)(ulong)Math.Sqrt((ulong)(object)value); + } + else + { + throw new NotSupportedException(SR.Arg_TypeNotSupported); + } } else { @@ -979,53 +1114,60 @@ public static T Sqrt(T value) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T Subtract(T left, T right) { - if (typeof(T) == typeof(byte)) - { - return (T)(object)(byte)((byte)(object)left - (byte)(object)right); - } - else if (typeof(T) == typeof(double)) - { - return (T)(object)(double)((double)(object)left - (double)(object)right); - } - else if (typeof(T) == typeof(short)) - { - return (T)(object)(short)((short)(object)left - (short)(object)right); - } - else if (typeof(T) == typeof(int)) - { - return (T)(object)((int)(object)left - (int)(object)right); - } - else if (typeof(T) == typeof(long)) - { - return (T)(object)((long)(object)left - (long)(object)right); - } - else if (typeof(T) == typeof(nint)) - { - return (T)(object)((nint)(object)left - (nint)(object)right); - } - else if (typeof(T) == typeof(nuint)) - { - return (T)(object)((nuint)(object)left - (nuint)(object)right); - } - else if (typeof(T) == typeof(sbyte)) + if (IsSupported) { - return (T)(object)(sbyte)((sbyte)(object)left - (sbyte)(object)right); - } - else if (typeof(T) == typeof(float)) - { - return (T)(object)(float)((float)(object)left - (float)(object)right); - } - else if (typeof(T) == typeof(ushort)) - { - return (T)(object)(ushort)((ushort)(object)left - (ushort)(object)right); - } - else if (typeof(T) == typeof(uint)) - { - return (T)(object)((uint)(object)left - (uint)(object)right); - } - else if (typeof(T) == typeof(ulong)) - { - return (T)(object)((ulong)(object)left - (ulong)(object)right); + if (typeof(T) == typeof(byte)) + { + return (T)(object)(byte)((byte)(object)left - (byte)(object)right); + } + else if (typeof(T) == typeof(double)) + { + return (T)(object)(double)((double)(object)left - (double)(object)right); + } + else if (typeof(T) == typeof(short)) + { + return (T)(object)(short)((short)(object)left - (short)(object)right); + } + else if (typeof(T) == typeof(int)) + { + return (T)(object)((int)(object)left - (int)(object)right); + } + else if (typeof(T) == typeof(long)) + { + return (T)(object)((long)(object)left - (long)(object)right); + } + else if (typeof(T) == typeof(nint)) + { + return (T)(object)((nint)(object)left - (nint)(object)right); + } + else if (typeof(T) == typeof(nuint)) + { + return (T)(object)((nuint)(object)left - (nuint)(object)right); + } + else if (typeof(T) == typeof(sbyte)) + { + return (T)(object)(sbyte)((sbyte)(object)left - (sbyte)(object)right); + } + else if (typeof(T) == typeof(float)) + { + return (T)(object)(float)((float)(object)left - (float)(object)right); + } + else if (typeof(T) == typeof(ushort)) + { + return (T)(object)(ushort)((ushort)(object)left - (ushort)(object)right); + } + else if (typeof(T) == typeof(uint)) + { + return (T)(object)((uint)(object)left - (uint)(object)right); + } + else if (typeof(T) == typeof(ulong)) + { + return (T)(object)((ulong)(object)left - (ulong)(object)right); + } + else + { + throw new NotSupportedException(SR.Arg_TypeNotSupported); + } } else { From ef7727dc7b51f2b504f61d1783e2530c885344bf Mon Sep 17 00:00:00 2001 From: Ivan Povazan Date: Fri, 8 Jul 2022 14:03:31 +0200 Subject: [PATCH 2/5] Simplyfing previous change to eliminate whitespace differences --- .../src/System/Runtime/Intrinsics/Scalar.cs | 1818 ++++++++--------- 1 file changed, 889 insertions(+), 929 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Scalar.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Scalar.cs index 46bc09ac91ccf..d9ba6b4e22650 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Scalar.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Scalar.cs @@ -8,397 +8,175 @@ namespace System.Runtime.Intrinsics internal static class Scalar where T : struct { - // FIXME: getter wired to Vector256.IsHardwareAccelerated + // FIXME: getter wired to Vector256.IsHardwareAccelerated, should be in separate linker substitution XML public static bool IsSupported { get => Vector256.IsHardwareAccelerated; } - public static T AllBitsSet { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (IsSupported) - { - if (typeof(T) == typeof(byte)) - { - return (T)(object)byte.MaxValue; - } - else if (typeof(T) == typeof(double)) - { - return (T)(object)BitConverter.Int64BitsToDouble(-1); - } - else if (typeof(T) == typeof(short)) - { - return (T)(object)(short)-1; - } - else if (typeof(T) == typeof(int)) - { - return (T)(object)-1; - } - else if (typeof(T) == typeof(long)) - { - return (T)(object)(long)-1; - } - else if (typeof(T) == typeof(nint)) - { - return (T)(object)(nint)(-1); - } - else if (typeof(T) == typeof(nuint)) - { - return (T)(object)nuint.MaxValue; - } - else if (typeof(T) == typeof(sbyte)) - { - return (T)(object)(sbyte)-1; - } - else if (typeof(T) == typeof(float)) - { - return (T)(object)BitConverter.Int32BitsToSingle(-1); - } - else if (typeof(T) == typeof(ushort)) - { - return (T)(object)ushort.MaxValue; - } - else if (typeof(T) == typeof(uint)) - { - return (T)(object)uint.MaxValue; - } - else if (typeof(T) == typeof(ulong)) - { - return (T)(object)ulong.MaxValue; - } - else - { - throw new NotSupportedException(SR.Arg_TypeNotSupported); - } - } - else - { - throw new NotSupportedException(SR.Arg_TypeNotSupported); - } - } - } - - public static T One - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get - { - if (IsSupported) - { - if (typeof(T) == typeof(byte)) - { - return (T)(object)(byte)1; - } - else if (typeof(T) == typeof(double)) - { - return (T)(object)(double)1; - } - else if (typeof(T) == typeof(short)) - { - return (T)(object)(short)1; - } - else if (typeof(T) == typeof(int)) - { - return (T)(object)1; - } - else if (typeof(T) == typeof(long)) - { - return (T)(object)(long)1; - } - else if (typeof(T) == typeof(nint)) - { - return (T)(object)(nint)1; - } - else if (typeof(T) == typeof(nuint)) - { - return (T)(object)(nuint)1; - } - else if (typeof(T) == typeof(sbyte)) - { - return (T)(object)(sbyte)1; - } - else if (typeof(T) == typeof(float)) - { - return (T)(object)(float)1; - } - else if (typeof(T) == typeof(ushort)) - { - return (T)(object)(ushort)1; - } - else if (typeof(T) == typeof(uint)) - { - return (T)(object)(uint)1; - } - else if (typeof(T) == typeof(ulong)) - { - return (T)(object)(ulong)1; - } - else - { - throw new NotSupportedException(SR.Arg_TypeNotSupported); - } - } - else - { - throw new NotSupportedException(SR.Arg_TypeNotSupported); - } - } - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static T Abs(T value) - { - // byte, ushort, uint, and ulong should have already been handled - if (IsSupported) - { - if (typeof(T) == typeof(double)) - { - return (T)(object)Math.Abs((double)(object)value); - } - else if (typeof(T) == typeof(short)) + if (!IsSupported) { - return (T)(object)Math.Abs((short)(object)value); + throw new PlatformNotSupportedException(); } - else if (typeof(T) == typeof(int)) - { - return (T)(object)Math.Abs((int)(object)value); - } - else if (typeof(T) == typeof(long)) - { - return (T)(object)Math.Abs((long)(object)value); - } - else if (typeof(T) == typeof(nint)) - { - return (T)(object)Math.Abs((nint)(object)value); - } - else if (typeof(T) == typeof(sbyte)) - { - return (T)(object)Math.Abs((sbyte)(object)value); - } - else if (typeof(T) == typeof(float)) - { - return (T)(object)Math.Abs((float)(object)value); - } - else - { - throw new NotSupportedException(SR.Arg_TypeNotSupported); - } - } - else - { - throw new NotSupportedException(SR.Arg_TypeNotSupported); - } - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static T Add(T left, T right) - { - if (IsSupported) - { if (typeof(T) == typeof(byte)) { - return (T)(object)(byte)((byte)(object)left + (byte)(object)right); + return (T)(object)byte.MaxValue; } else if (typeof(T) == typeof(double)) { - return (T)(object)(double)((double)(object)left + (double)(object)right); + return (T)(object)BitConverter.Int64BitsToDouble(-1); } else if (typeof(T) == typeof(short)) { - return (T)(object)(short)((short)(object)left + (short)(object)right); + return (T)(object)(short)-1; } else if (typeof(T) == typeof(int)) { - return (T)(object)((int)(object)left + (int)(object)right); + return (T)(object)-1; } else if (typeof(T) == typeof(long)) { - return (T)(object)((long)(object)left + (long)(object)right); + return (T)(object)(long)-1; } else if (typeof(T) == typeof(nint)) { - return (T)(object)((nint)(object)left + (nint)(object)right); + return (T)(object)(nint)(-1); } else if (typeof(T) == typeof(nuint)) { - return (T)(object)((nuint)(object)left + (nuint)(object)right); + return (T)(object)nuint.MaxValue; } else if (typeof(T) == typeof(sbyte)) { - return (T)(object)(sbyte)((sbyte)(object)left + (sbyte)(object)right); + return (T)(object)(sbyte)-1; } else if (typeof(T) == typeof(float)) { - return (T)(object)(float)((float)(object)left + (float)(object)right); + return (T)(object)BitConverter.Int32BitsToSingle(-1); } else if (typeof(T) == typeof(ushort)) { - return (T)(object)(ushort)((ushort)(object)left + (ushort)(object)right); + return (T)(object)ushort.MaxValue; } else if (typeof(T) == typeof(uint)) { - return (T)(object)((uint)(object)left + (uint)(object)right); + return (T)(object)uint.MaxValue; } else if (typeof(T) == typeof(ulong)) { - return (T)(object)((ulong)(object)left + (ulong)(object)right); + return (T)(object)ulong.MaxValue; } else { throw new NotSupportedException(SR.Arg_TypeNotSupported); } } - else - { - throw new NotSupportedException(SR.Arg_TypeNotSupported); - } } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static T Ceiling(T value) + public static T One { - if (IsSupported) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get { - if (typeof(T) == typeof(double)) - { - return (T)(object)Math.Ceiling((double)(object)value); - } - else if (typeof(T) == typeof(float)) - { - return (T)(object)MathF.Ceiling((float)(object)value); - } - else + if (!IsSupported) { - throw new NotSupportedException(SR.Arg_TypeNotSupported); + throw new PlatformNotSupportedException(); } - } - else - { - throw new NotSupportedException(SR.Arg_TypeNotSupported); - } - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static T Divide(T left, T right) - { - if (IsSupported) - { if (typeof(T) == typeof(byte)) { - return (T)(object)(byte)((byte)(object)left / (byte)(object)right); + return (T)(object)(byte)1; } else if (typeof(T) == typeof(double)) { - return (T)(object)(double)((double)(object)left / (double)(object)right); + return (T)(object)(double)1; } else if (typeof(T) == typeof(short)) { - return (T)(object)(short)((short)(object)left / (short)(object)right); + return (T)(object)(short)1; } else if (typeof(T) == typeof(int)) { - return (T)(object)((int)(object)left / (int)(object)right); + return (T)(object)1; } else if (typeof(T) == typeof(long)) { - return (T)(object)((long)(object)left / (long)(object)right); + return (T)(object)(long)1; } else if (typeof(T) == typeof(nint)) { - return (T)(object)((nint)(object)left / (nint)(object)right); + return (T)(object)(nint)1; } else if (typeof(T) == typeof(nuint)) { - return (T)(object)((nuint)(object)left / (nuint)(object)right); + return (T)(object)(nuint)1; } else if (typeof(T) == typeof(sbyte)) { - return (T)(object)(sbyte)((sbyte)(object)left / (sbyte)(object)right); + return (T)(object)(sbyte)1; } else if (typeof(T) == typeof(float)) { - return (T)(object)(float)((float)(object)left / (float)(object)right); + return (T)(object)(float)1; } else if (typeof(T) == typeof(ushort)) { - return (T)(object)(ushort)((ushort)(object)left / (ushort)(object)right); + return (T)(object)(ushort)1; } else if (typeof(T) == typeof(uint)) { - return (T)(object)((uint)(object)left / (uint)(object)right); + return (T)(object)(uint)1; } else if (typeof(T) == typeof(ulong)) { - return (T)(object)((ulong)(object)left / (ulong)(object)right); + return (T)(object)(ulong)1; } else { throw new NotSupportedException(SR.Arg_TypeNotSupported); } } - else - { - throw new NotSupportedException(SR.Arg_TypeNotSupported); - } } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool Equals(T left, T right) + public static T Abs(T value) { - if (IsSupported) + if (!IsSupported) { - if (typeof(T) == typeof(byte)) - { - return (byte)(object)left == (byte)(object)right; - } - else if (typeof(T) == typeof(double)) - { - return (double)(object)left == (double)(object)right; - } - else if (typeof(T) == typeof(short)) - { - return (short)(object)left == (short)(object)right; - } - else if (typeof(T) == typeof(int)) - { - return (int)(object)left == (int)(object)right; - } - else if (typeof(T) == typeof(long)) - { - return (long)(object)left == (long)(object)right; - } - else if (typeof(T) == typeof(nint)) - { - return (nint)(object)left == (nint)(object)right; - } - else if (typeof(T) == typeof(nuint)) - { - return (nuint)(object)left == (nuint)(object)right; - } - else if (typeof(T) == typeof(sbyte)) - { - return (sbyte)(object)left == (sbyte)(object)right; - } - else if (typeof(T) == typeof(float)) - { - return (float)(object)left == (float)(object)right; - } - else if (typeof(T) == typeof(ushort)) - { - return (ushort)(object)left == (ushort)(object)right; - } - else if (typeof(T) == typeof(uint)) - { - return (uint)(object)left == (uint)(object)right; - } - else if (typeof(T) == typeof(ulong)) - { - return (ulong)(object)left == (ulong)(object)right; - } - else - { - throw new NotSupportedException(SR.Arg_TypeNotSupported); - } + throw new PlatformNotSupportedException(); + } + + // byte, ushort, uint, and ulong should have already been handled + + if (typeof(T) == typeof(double)) + { + return (T)(object)Math.Abs((double)(object)value); + } + else if (typeof(T) == typeof(short)) + { + return (T)(object)Math.Abs((short)(object)value); + } + else if (typeof(T) == typeof(int)) + { + return (T)(object)Math.Abs((int)(object)value); + } + else if (typeof(T) == typeof(long)) + { + return (T)(object)Math.Abs((long)(object)value); + } + else if (typeof(T) == typeof(nint)) + { + return (T)(object)Math.Abs((nint)(object)value); + } + else if (typeof(T) == typeof(sbyte)) + { + return (T)(object)Math.Abs((sbyte)(object)value); + } + else if (typeof(T) == typeof(float)) + { + return (T)(object)Math.Abs((float)(object)value); } else { @@ -407,84 +185,60 @@ public static bool Equals(T left, T right) } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static uint ExtractMostSignificantBit(T value) + public static T Add(T left, T right) { - if (IsSupported) + if (!IsSupported) { - if (typeof(T) == typeof(byte)) - { - uint bits = (byte)(object)value; - return bits >> 7; - } - else if (typeof(T) == typeof(double)) - { - ulong bits = BitConverter.DoubleToUInt64Bits((double)(object)value); - return (uint)(bits >> 63); - } - else if (typeof(T) == typeof(short)) - { - uint bits = (ushort)(short)(object)value; - return bits >> 15; - } - else if (typeof(T) == typeof(int)) - { - uint bits = (uint)(int)(object)value; - return bits >> 31; - } - else if (typeof(T) == typeof(long)) - { - ulong bits = (ulong)(long)(object)value; - return (uint)(bits >> 63); - } - else if (typeof(T) == typeof(nint)) - { -#if TARGET_64BIT - ulong bits = (ulong)(nint)(object)value; - return (uint)(bits >> 63); -#else - uint bits = (uint)(nint)(object)value; - return bits >> 31; -#endif - } - else if (typeof(T) == typeof(nuint)) - { -#if TARGET_64BIT - ulong bits = (ulong)(nuint)(object)value; - return (uint)(bits >> 63); -#else - uint bits = (uint)(nuint)(object)value; - return bits >> 31; -#endif - } - else if (typeof(T) == typeof(sbyte)) - { - uint bits = (byte)(sbyte)(object)value; - return bits >> 7; - } - else if (typeof(T) == typeof(float)) - { - uint bits = BitConverter.SingleToUInt32Bits((float)(object)value); - return bits >> 31; - } - else if (typeof(T) == typeof(ushort)) - { - uint bits = (ushort)(object)value; - return bits >> 15; - } - else if (typeof(T) == typeof(uint)) - { - uint bits = (uint)(object)value; - return bits >> 31; - } - else if (typeof(T) == typeof(ulong)) - { - ulong bits = (ulong)(object)value; - return (uint)(bits >> 63); - } - else - { - throw new NotSupportedException(SR.Arg_TypeNotSupported); - } + throw new PlatformNotSupportedException(); + } + + if (typeof(T) == typeof(byte)) + { + return (T)(object)(byte)((byte)(object)left + (byte)(object)right); + } + else if (typeof(T) == typeof(double)) + { + return (T)(object)(double)((double)(object)left + (double)(object)right); + } + else if (typeof(T) == typeof(short)) + { + return (T)(object)(short)((short)(object)left + (short)(object)right); + } + else if (typeof(T) == typeof(int)) + { + return (T)(object)((int)(object)left + (int)(object)right); + } + else if (typeof(T) == typeof(long)) + { + return (T)(object)((long)(object)left + (long)(object)right); + } + else if (typeof(T) == typeof(nint)) + { + return (T)(object)((nint)(object)left + (nint)(object)right); + } + else if (typeof(T) == typeof(nuint)) + { + return (T)(object)((nuint)(object)left + (nuint)(object)right); + } + else if (typeof(T) == typeof(sbyte)) + { + return (T)(object)(sbyte)((sbyte)(object)left + (sbyte)(object)right); + } + else if (typeof(T) == typeof(float)) + { + return (T)(object)(float)((float)(object)left + (float)(object)right); + } + else if (typeof(T) == typeof(ushort)) + { + return (T)(object)(ushort)((ushort)(object)left + (ushort)(object)right); + } + else if (typeof(T) == typeof(uint)) + { + return (T)(object)((uint)(object)left + (uint)(object)right); + } + else if (typeof(T) == typeof(ulong)) + { + return (T)(object)((ulong)(object)left + (ulong)(object)right); } else { @@ -493,22 +247,20 @@ public static uint ExtractMostSignificantBit(T value) } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static T Floor(T value) + public static T Ceiling(T value) { - if (IsSupported) + if (!IsSupported) { - if (typeof(T) == typeof(double)) - { - return (T)(object)Math.Floor((double)(object)value); - } - else if (typeof(T) == typeof(float)) - { - return (T)(object)MathF.Floor((float)(object)value); - } - else - { - throw new NotSupportedException(SR.Arg_TypeNotSupported); - } + throw new PlatformNotSupportedException(); + } + + if (typeof(T) == typeof(double)) + { + return (T)(object)Math.Ceiling((double)(object)value); + } + else if (typeof(T) == typeof(float)) + { + return (T)(object)MathF.Ceiling((float)(object)value); } else { @@ -517,62 +269,60 @@ public static T Floor(T value) } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool GreaterThan(T left, T right) + public static T Divide(T left, T right) { - if (IsSupported) + if (!IsSupported) { - if (typeof(T) == typeof(byte)) - { - return (byte)(object)left > (byte)(object)right; - } - else if (typeof(T) == typeof(double)) - { - return (double)(object)left > (double)(object)right; - } - else if (typeof(T) == typeof(short)) - { - return (short)(object)left > (short)(object)right; - } - else if (typeof(T) == typeof(int)) - { - return (int)(object)left > (int)(object)right; - } - else if (typeof(T) == typeof(long)) - { - return (long)(object)left > (long)(object)right; - } - else if (typeof(T) == typeof(nint)) - { - return (nint)(object)left > (nint)(object)right; - } - else if (typeof(T) == typeof(nuint)) - { - return (nuint)(object)left > (nuint)(object)right; - } - else if (typeof(T) == typeof(sbyte)) - { - return (sbyte)(object)left > (sbyte)(object)right; - } - else if (typeof(T) == typeof(float)) - { - return (float)(object)left > (float)(object)right; - } - else if (typeof(T) == typeof(ushort)) - { - return (ushort)(object)left > (ushort)(object)right; - } - else if (typeof(T) == typeof(uint)) - { - return (uint)(object)left > (uint)(object)right; - } - else if (typeof(T) == typeof(ulong)) - { - return (ulong)(object)left > (ulong)(object)right; - } - else - { - throw new NotSupportedException(SR.Arg_TypeNotSupported); - } + throw new PlatformNotSupportedException(); + } + + if (typeof(T) == typeof(byte)) + { + return (T)(object)(byte)((byte)(object)left / (byte)(object)right); + } + else if (typeof(T) == typeof(double)) + { + return (T)(object)(double)((double)(object)left / (double)(object)right); + } + else if (typeof(T) == typeof(short)) + { + return (T)(object)(short)((short)(object)left / (short)(object)right); + } + else if (typeof(T) == typeof(int)) + { + return (T)(object)((int)(object)left / (int)(object)right); + } + else if (typeof(T) == typeof(long)) + { + return (T)(object)((long)(object)left / (long)(object)right); + } + else if (typeof(T) == typeof(nint)) + { + return (T)(object)((nint)(object)left / (nint)(object)right); + } + else if (typeof(T) == typeof(nuint)) + { + return (T)(object)((nuint)(object)left / (nuint)(object)right); + } + else if (typeof(T) == typeof(sbyte)) + { + return (T)(object)(sbyte)((sbyte)(object)left / (sbyte)(object)right); + } + else if (typeof(T) == typeof(float)) + { + return (T)(object)(float)((float)(object)left / (float)(object)right); + } + else if (typeof(T) == typeof(ushort)) + { + return (T)(object)(ushort)((ushort)(object)left / (ushort)(object)right); + } + else if (typeof(T) == typeof(uint)) + { + return (T)(object)((uint)(object)left / (uint)(object)right); + } + else if (typeof(T) == typeof(ulong)) + { + return (T)(object)((ulong)(object)left / (ulong)(object)right); } else { @@ -581,62 +331,60 @@ public static bool GreaterThan(T left, T right) } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool GreaterThanOrEqual(T left, T right) + public static bool Equals(T left, T right) { - if (IsSupported) + if (!IsSupported) { - if (typeof(T) == typeof(byte)) - { - return (byte)(object)left >= (byte)(object)right; - } - else if (typeof(T) == typeof(double)) - { - return (double)(object)left >= (double)(object)right; - } - else if (typeof(T) == typeof(short)) - { - return (short)(object)left >= (short)(object)right; - } - else if (typeof(T) == typeof(int)) - { - return (int)(object)left >= (int)(object)right; - } - else if (typeof(T) == typeof(long)) - { - return (long)(object)left >= (long)(object)right; - } - else if (typeof(T) == typeof(nint)) - { - return (nint)(object)left >= (nint)(object)right; - } - else if (typeof(T) == typeof(nuint)) - { - return (nuint)(object)left >= (nuint)(object)right; - } - else if (typeof(T) == typeof(sbyte)) - { - return (sbyte)(object)left >= (sbyte)(object)right; - } - else if (typeof(T) == typeof(float)) - { - return (float)(object)left >= (float)(object)right; - } - else if (typeof(T) == typeof(ushort)) - { - return (ushort)(object)left >= (ushort)(object)right; - } - else if (typeof(T) == typeof(uint)) - { - return (uint)(object)left >= (uint)(object)right; - } - else if (typeof(T) == typeof(ulong)) - { - return (ulong)(object)left >= (ulong)(object)right; - } - else - { - throw new NotSupportedException(SR.Arg_TypeNotSupported); - } + throw new PlatformNotSupportedException(); + } + + if (typeof(T) == typeof(byte)) + { + return (byte)(object)left == (byte)(object)right; + } + else if (typeof(T) == typeof(double)) + { + return (double)(object)left == (double)(object)right; + } + else if (typeof(T) == typeof(short)) + { + return (short)(object)left == (short)(object)right; + } + else if (typeof(T) == typeof(int)) + { + return (int)(object)left == (int)(object)right; + } + else if (typeof(T) == typeof(long)) + { + return (long)(object)left == (long)(object)right; + } + else if (typeof(T) == typeof(nint)) + { + return (nint)(object)left == (nint)(object)right; + } + else if (typeof(T) == typeof(nuint)) + { + return (nuint)(object)left == (nuint)(object)right; + } + else if (typeof(T) == typeof(sbyte)) + { + return (sbyte)(object)left == (sbyte)(object)right; + } + else if (typeof(T) == typeof(float)) + { + return (float)(object)left == (float)(object)right; + } + else if (typeof(T) == typeof(ushort)) + { + return (ushort)(object)left == (ushort)(object)right; + } + else if (typeof(T) == typeof(uint)) + { + return (uint)(object)left == (uint)(object)right; + } + else if (typeof(T) == typeof(ulong)) + { + return (ulong)(object)left == (ulong)(object)right; } else { @@ -645,62 +393,82 @@ public static bool GreaterThanOrEqual(T left, T right) } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool LessThan(T left, T right) + public static uint ExtractMostSignificantBit(T value) { - if (IsSupported) + if (!IsSupported) { - if (typeof(T) == typeof(byte)) - { - return (byte)(object)left < (byte)(object)right; - } - else if (typeof(T) == typeof(double)) - { - return (double)(object)left < (double)(object)right; - } - else if (typeof(T) == typeof(short)) - { - return (short)(object)left < (short)(object)right; - } - else if (typeof(T) == typeof(int)) - { - return (int)(object)left < (int)(object)right; - } - else if (typeof(T) == typeof(long)) - { - return (long)(object)left < (long)(object)right; - } - else if (typeof(T) == typeof(nint)) - { - return (nint)(object)left < (nint)(object)right; - } - else if (typeof(T) == typeof(nuint)) - { - return (nuint)(object)left < (nuint)(object)right; - } - else if (typeof(T) == typeof(sbyte)) - { - return (sbyte)(object)left < (sbyte)(object)right; - } - else if (typeof(T) == typeof(float)) - { - return (float)(object)left < (float)(object)right; - } - else if (typeof(T) == typeof(ushort)) - { - return (ushort)(object)left < (ushort)(object)right; - } - else if (typeof(T) == typeof(uint)) - { - return (uint)(object)left < (uint)(object)right; - } - else if (typeof(T) == typeof(ulong)) - { - return (ulong)(object)left < (ulong)(object)right; - } - else - { - throw new NotSupportedException(SR.Arg_TypeNotSupported); - } + throw new PlatformNotSupportedException(); + } + + if (typeof(T) == typeof(byte)) + { + uint bits = (byte)(object)value; + return bits >> 7; + } + else if (typeof(T) == typeof(double)) + { + ulong bits = BitConverter.DoubleToUInt64Bits((double)(object)value); + return (uint)(bits >> 63); + } + else if (typeof(T) == typeof(short)) + { + uint bits = (ushort)(short)(object)value; + return bits >> 15; + } + else if (typeof(T) == typeof(int)) + { + uint bits = (uint)(int)(object)value; + return bits >> 31; + } + else if (typeof(T) == typeof(long)) + { + ulong bits = (ulong)(long)(object)value; + return (uint)(bits >> 63); + } + else if (typeof(T) == typeof(nint)) + { +#if TARGET_64BIT + ulong bits = (ulong)(nint)(object)value; + return (uint)(bits >> 63); +#else + uint bits = (uint)(nint)(object)value; + return bits >> 31; +#endif + } + else if (typeof(T) == typeof(nuint)) + { +#if TARGET_64BIT + ulong bits = (ulong)(nuint)(object)value; + return (uint)(bits >> 63); +#else + uint bits = (uint)(nuint)(object)value; + return bits >> 31; +#endif + } + else if (typeof(T) == typeof(sbyte)) + { + uint bits = (byte)(sbyte)(object)value; + return bits >> 7; + } + else if (typeof(T) == typeof(float)) + { + uint bits = BitConverter.SingleToUInt32Bits((float)(object)value); + return bits >> 31; + } + else if (typeof(T) == typeof(ushort)) + { + uint bits = (ushort)(object)value; + return bits >> 15; + } + else if (typeof(T) == typeof(uint)) + { + uint bits = (uint)(object)value; + return bits >> 31; + } + else if (typeof(T) == typeof(ulong)) + { + ulong bits = (ulong)(object)value; + return (uint)(bits >> 63); } else { @@ -709,62 +477,20 @@ public static bool LessThan(T left, T right) } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool LessThanOrEqual(T left, T right) + public static T Floor(T value) { - if (IsSupported) + if (!IsSupported) { - if (typeof(T) == typeof(byte)) - { - return (byte)(object)left <= (byte)(object)right; - } - else if (typeof(T) == typeof(double)) - { - return (double)(object)left <= (double)(object)right; - } - else if (typeof(T) == typeof(short)) - { - return (short)(object)left <= (short)(object)right; - } - else if (typeof(T) == typeof(int)) - { - return (int)(object)left <= (int)(object)right; - } - else if (typeof(T) == typeof(long)) - { - return (long)(object)left <= (long)(object)right; - } - else if (typeof(T) == typeof(nint)) - { - return (nint)(object)left <= (nint)(object)right; - } - else if (typeof(T) == typeof(nuint)) - { - return (nuint)(object)left <= (nuint)(object)right; - } - else if (typeof(T) == typeof(sbyte)) - { - return (sbyte)(object)left <= (sbyte)(object)right; - } - else if (typeof(T) == typeof(float)) - { - return (float)(object)left <= (float)(object)right; - } - else if (typeof(T) == typeof(ushort)) - { - return (ushort)(object)left <= (ushort)(object)right; - } - else if (typeof(T) == typeof(uint)) - { - return (uint)(object)left <= (uint)(object)right; - } - else if (typeof(T) == typeof(ulong)) - { - return (ulong)(object)left <= (ulong)(object)right; - } - else - { - throw new NotSupportedException(SR.Arg_TypeNotSupported); - } + throw new PlatformNotSupportedException(); + } + + if (typeof(T) == typeof(double)) + { + return (T)(object)Math.Floor((double)(object)value); + } + else if (typeof(T) == typeof(float)) + { + return (T)(object)MathF.Floor((float)(object)value); } else { @@ -773,62 +499,308 @@ public static bool LessThanOrEqual(T left, T right) } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static T Multiply(T left, T right) + public static bool GreaterThan(T left, T right) { - if (IsSupported) + if (!IsSupported) { - if (typeof(T) == typeof(byte)) - { - return (T)(object)(byte)((byte)(object)left * (byte)(object)right); - } - else if (typeof(T) == typeof(double)) - { - return (T)(object)(double)((double)(object)left * (double)(object)right); - } - else if (typeof(T) == typeof(short)) - { - return (T)(object)(short)((short)(object)left * (short)(object)right); - } - else if (typeof(T) == typeof(int)) - { - return (T)(object)((int)(object)left * (int)(object)right); - } - else if (typeof(T) == typeof(long)) - { - return (T)(object)((long)(object)left * (long)(object)right); - } - else if (typeof(T) == typeof(nint)) - { - return (T)(object)((nint)(object)left * (nint)(object)right); - } - else if (typeof(T) == typeof(nuint)) - { - return (T)(object)((nuint)(object)left * (nuint)(object)right); - } - else if (typeof(T) == typeof(sbyte)) - { - return (T)(object)(sbyte)((sbyte)(object)left * (sbyte)(object)right); - } - else if (typeof(T) == typeof(float)) - { - return (T)(object)(float)((float)(object)left * (float)(object)right); - } - else if (typeof(T) == typeof(ushort)) - { - return (T)(object)(ushort)((ushort)(object)left * (ushort)(object)right); - } - else if (typeof(T) == typeof(uint)) - { - return (T)(object)((uint)(object)left * (uint)(object)right); - } - else if (typeof(T) == typeof(ulong)) - { - return (T)(object)((ulong)(object)left * (ulong)(object)right); - } - else - { - throw new NotSupportedException(SR.Arg_TypeNotSupported); - } + throw new PlatformNotSupportedException(); + } + + if (typeof(T) == typeof(byte)) + { + return (byte)(object)left > (byte)(object)right; + } + else if (typeof(T) == typeof(double)) + { + return (double)(object)left > (double)(object)right; + } + else if (typeof(T) == typeof(short)) + { + return (short)(object)left > (short)(object)right; + } + else if (typeof(T) == typeof(int)) + { + return (int)(object)left > (int)(object)right; + } + else if (typeof(T) == typeof(long)) + { + return (long)(object)left > (long)(object)right; + } + else if (typeof(T) == typeof(nint)) + { + return (nint)(object)left > (nint)(object)right; + } + else if (typeof(T) == typeof(nuint)) + { + return (nuint)(object)left > (nuint)(object)right; + } + else if (typeof(T) == typeof(sbyte)) + { + return (sbyte)(object)left > (sbyte)(object)right; + } + else if (typeof(T) == typeof(float)) + { + return (float)(object)left > (float)(object)right; + } + else if (typeof(T) == typeof(ushort)) + { + return (ushort)(object)left > (ushort)(object)right; + } + else if (typeof(T) == typeof(uint)) + { + return (uint)(object)left > (uint)(object)right; + } + else if (typeof(T) == typeof(ulong)) + { + return (ulong)(object)left > (ulong)(object)right; + } + else + { + throw new NotSupportedException(SR.Arg_TypeNotSupported); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool GreaterThanOrEqual(T left, T right) + { + if (!IsSupported) + { + throw new PlatformNotSupportedException(); + } + + if (typeof(T) == typeof(byte)) + { + return (byte)(object)left >= (byte)(object)right; + } + else if (typeof(T) == typeof(double)) + { + return (double)(object)left >= (double)(object)right; + } + else if (typeof(T) == typeof(short)) + { + return (short)(object)left >= (short)(object)right; + } + else if (typeof(T) == typeof(int)) + { + return (int)(object)left >= (int)(object)right; + } + else if (typeof(T) == typeof(long)) + { + return (long)(object)left >= (long)(object)right; + } + else if (typeof(T) == typeof(nint)) + { + return (nint)(object)left >= (nint)(object)right; + } + else if (typeof(T) == typeof(nuint)) + { + return (nuint)(object)left >= (nuint)(object)right; + } + else if (typeof(T) == typeof(sbyte)) + { + return (sbyte)(object)left >= (sbyte)(object)right; + } + else if (typeof(T) == typeof(float)) + { + return (float)(object)left >= (float)(object)right; + } + else if (typeof(T) == typeof(ushort)) + { + return (ushort)(object)left >= (ushort)(object)right; + } + else if (typeof(T) == typeof(uint)) + { + return (uint)(object)left >= (uint)(object)right; + } + else if (typeof(T) == typeof(ulong)) + { + return (ulong)(object)left >= (ulong)(object)right; + } + else + { + throw new NotSupportedException(SR.Arg_TypeNotSupported); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool LessThan(T left, T right) + { + if (!IsSupported) + { + throw new PlatformNotSupportedException(); + } + + if (typeof(T) == typeof(byte)) + { + return (byte)(object)left < (byte)(object)right; + } + else if (typeof(T) == typeof(double)) + { + return (double)(object)left < (double)(object)right; + } + else if (typeof(T) == typeof(short)) + { + return (short)(object)left < (short)(object)right; + } + else if (typeof(T) == typeof(int)) + { + return (int)(object)left < (int)(object)right; + } + else if (typeof(T) == typeof(long)) + { + return (long)(object)left < (long)(object)right; + } + else if (typeof(T) == typeof(nint)) + { + return (nint)(object)left < (nint)(object)right; + } + else if (typeof(T) == typeof(nuint)) + { + return (nuint)(object)left < (nuint)(object)right; + } + else if (typeof(T) == typeof(sbyte)) + { + return (sbyte)(object)left < (sbyte)(object)right; + } + else if (typeof(T) == typeof(float)) + { + return (float)(object)left < (float)(object)right; + } + else if (typeof(T) == typeof(ushort)) + { + return (ushort)(object)left < (ushort)(object)right; + } + else if (typeof(T) == typeof(uint)) + { + return (uint)(object)left < (uint)(object)right; + } + else if (typeof(T) == typeof(ulong)) + { + return (ulong)(object)left < (ulong)(object)right; + } + else + { + throw new NotSupportedException(SR.Arg_TypeNotSupported); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool LessThanOrEqual(T left, T right) + { + if (!IsSupported) + { + throw new PlatformNotSupportedException(); + } + + if (typeof(T) == typeof(byte)) + { + return (byte)(object)left <= (byte)(object)right; + } + else if (typeof(T) == typeof(double)) + { + return (double)(object)left <= (double)(object)right; + } + else if (typeof(T) == typeof(short)) + { + return (short)(object)left <= (short)(object)right; + } + else if (typeof(T) == typeof(int)) + { + return (int)(object)left <= (int)(object)right; + } + else if (typeof(T) == typeof(long)) + { + return (long)(object)left <= (long)(object)right; + } + else if (typeof(T) == typeof(nint)) + { + return (nint)(object)left <= (nint)(object)right; + } + else if (typeof(T) == typeof(nuint)) + { + return (nuint)(object)left <= (nuint)(object)right; + } + else if (typeof(T) == typeof(sbyte)) + { + return (sbyte)(object)left <= (sbyte)(object)right; + } + else if (typeof(T) == typeof(float)) + { + return (float)(object)left <= (float)(object)right; + } + else if (typeof(T) == typeof(ushort)) + { + return (ushort)(object)left <= (ushort)(object)right; + } + else if (typeof(T) == typeof(uint)) + { + return (uint)(object)left <= (uint)(object)right; + } + else if (typeof(T) == typeof(ulong)) + { + return (ulong)(object)left <= (ulong)(object)right; + } + else + { + throw new NotSupportedException(SR.Arg_TypeNotSupported); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T Multiply(T left, T right) + { + if (!IsSupported) + { + throw new PlatformNotSupportedException(); + } + + if (typeof(T) == typeof(byte)) + { + return (T)(object)(byte)((byte)(object)left * (byte)(object)right); + } + else if (typeof(T) == typeof(double)) + { + return (T)(object)(double)((double)(object)left * (double)(object)right); + } + else if (typeof(T) == typeof(short)) + { + return (T)(object)(short)((short)(object)left * (short)(object)right); + } + else if (typeof(T) == typeof(int)) + { + return (T)(object)((int)(object)left * (int)(object)right); + } + else if (typeof(T) == typeof(long)) + { + return (T)(object)((long)(object)left * (long)(object)right); + } + else if (typeof(T) == typeof(nint)) + { + return (T)(object)((nint)(object)left * (nint)(object)right); + } + else if (typeof(T) == typeof(nuint)) + { + return (T)(object)((nuint)(object)left * (nuint)(object)right); + } + else if (typeof(T) == typeof(sbyte)) + { + return (T)(object)(sbyte)((sbyte)(object)left * (sbyte)(object)right); + } + else if (typeof(T) == typeof(float)) + { + return (T)(object)(float)((float)(object)left * (float)(object)right); + } + else if (typeof(T) == typeof(ushort)) + { + return (T)(object)(ushort)((ushort)(object)left * (ushort)(object)right); + } + else if (typeof(T) == typeof(uint)) + { + return (T)(object)((uint)(object)left * (uint)(object)right); + } + else if (typeof(T) == typeof(ulong)) + { + return (T)(object)((ulong)(object)left * (ulong)(object)right); } else { @@ -838,116 +810,112 @@ public static T Multiply(T left, T right) public static bool ObjectEquals(T left, T right) { - if (IsSupported) + if (!IsSupported) { - if (typeof(T) == typeof(byte)) - { - return ((byte)(object)left).Equals((byte)(object)right); - } - else if (typeof(T) == typeof(double)) - { - return ((double)(object)left).Equals((double)(object)right); - } - else if (typeof(T) == typeof(short)) - { - return ((short)(object)left).Equals((short)(object)right); - } - else if (typeof(T) == typeof(int)) - { - return ((int)(object)left).Equals((int)(object)right); - } - else if (typeof(T) == typeof(long)) - { - return ((long)(object)left).Equals((long)(object)right); - } - else if (typeof(T) == typeof(nint)) - { - return ((nint)(object)left).Equals((nint)(object)right); - } - else if (typeof(T) == typeof(nuint)) - { - return ((nuint)(object)left).Equals((nuint)(object)right); - } - else if (typeof(T) == typeof(sbyte)) - { - return ((sbyte)(object)left).Equals((sbyte)(object)right); - } - else if (typeof(T) == typeof(float)) - { - return ((float)(object)left).Equals((float)(object)right); - } - else if (typeof(T) == typeof(ushort)) - { - return ((ushort)(object)left).Equals((ushort)(object)right); - } - else if (typeof(T) == typeof(uint)) - { - return ((uint)(object)left).Equals((uint)(object)right); - } - else if (typeof(T) == typeof(ulong)) - { - return ((ulong)(object)left).Equals((ulong)(object)right); - } - else - { - throw new NotSupportedException(SR.Arg_TypeNotSupported); - } + throw new PlatformNotSupportedException(); + } + + if (typeof(T) == typeof(byte)) + { + return ((byte)(object)left).Equals((byte)(object)right); + } + else if (typeof(T) == typeof(double)) + { + return ((double)(object)left).Equals((double)(object)right); + } + else if (typeof(T) == typeof(short)) + { + return ((short)(object)left).Equals((short)(object)right); + } + else if (typeof(T) == typeof(int)) + { + return ((int)(object)left).Equals((int)(object)right); + } + else if (typeof(T) == typeof(long)) + { + return ((long)(object)left).Equals((long)(object)right); + } + else if (typeof(T) == typeof(nint)) + { + return ((nint)(object)left).Equals((nint)(object)right); + } + else if (typeof(T) == typeof(nuint)) + { + return ((nuint)(object)left).Equals((nuint)(object)right); + } + else if (typeof(T) == typeof(sbyte)) + { + return ((sbyte)(object)left).Equals((sbyte)(object)right); + } + else if (typeof(T) == typeof(float)) + { + return ((float)(object)left).Equals((float)(object)right); + } + else if (typeof(T) == typeof(ushort)) + { + return ((ushort)(object)left).Equals((ushort)(object)right); + } + else if (typeof(T) == typeof(uint)) + { + return ((uint)(object)left).Equals((uint)(object)right); + } + else if (typeof(T) == typeof(ulong)) + { + return ((ulong)(object)left).Equals((ulong)(object)right); + } + else + { + throw new NotSupportedException(SR.Arg_TypeNotSupported); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T ShiftLeft(T value, int shiftCount) + { + if (!IsSupported) + { + throw new PlatformNotSupportedException(); + } + + if (typeof(T) == typeof(byte)) + { + return (T)(object)(byte)((byte)(object)value << (shiftCount & 7)); + } + else if (typeof(T) == typeof(short)) + { + return (T)(object)(short)((short)(object)value << (shiftCount & 15)); + } + else if (typeof(T) == typeof(int)) + { + return (T)(object)(int)((int)(object)value << shiftCount); + } + else if (typeof(T) == typeof(long)) + { + return (T)(object)(long)((long)(object)value << shiftCount); + } + else if (typeof(T) == typeof(nint)) + { + return (T)(object)(nint)((nint)(object)value << shiftCount); + } + else if (typeof(T) == typeof(nuint)) + { + return (T)(object)(nuint)((nuint)(object)value << shiftCount); + } + else if (typeof(T) == typeof(sbyte)) + { + return (T)(object)(sbyte)((sbyte)(object)value << (shiftCount & 7)); + } + else if (typeof(T) == typeof(ushort)) + { + return (T)(object)(ushort)((ushort)(object)value << (shiftCount & 15)); } - else + else if (typeof(T) == typeof(uint)) { - throw new NotSupportedException(SR.Arg_TypeNotSupported); + return (T)(object)(uint)((uint)(object)value << shiftCount); } - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static T ShiftLeft(T value, int shiftCount) - { - if (IsSupported) + else if (typeof(T) == typeof(ulong)) { - if (typeof(T) == typeof(byte)) - { - return (T)(object)(byte)((byte)(object)value << (shiftCount & 7)); - } - else if (typeof(T) == typeof(short)) - { - return (T)(object)(short)((short)(object)value << (shiftCount & 15)); - } - else if (typeof(T) == typeof(int)) - { - return (T)(object)(int)((int)(object)value << shiftCount); - } - else if (typeof(T) == typeof(long)) - { - return (T)(object)(long)((long)(object)value << shiftCount); - } - else if (typeof(T) == typeof(nint)) - { - return (T)(object)(nint)((nint)(object)value << shiftCount); - } - else if (typeof(T) == typeof(nuint)) - { - return (T)(object)(nuint)((nuint)(object)value << shiftCount); - } - else if (typeof(T) == typeof(sbyte)) - { - return (T)(object)(sbyte)((sbyte)(object)value << (shiftCount & 7)); - } - else if (typeof(T) == typeof(ushort)) - { - return (T)(object)(ushort)((ushort)(object)value << (shiftCount & 15)); - } - else if (typeof(T) == typeof(uint)) - { - return (T)(object)(uint)((uint)(object)value << shiftCount); - } - else if (typeof(T) == typeof(ulong)) - { - return (T)(object)(ulong)((ulong)(object)value << shiftCount); - } - else - { - throw new NotSupportedException(SR.Arg_TypeNotSupported); - } + return (T)(object)(ulong)((ulong)(object)value << shiftCount); } else { @@ -958,32 +926,30 @@ public static T ShiftLeft(T value, int shiftCount) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T ShiftRightArithmetic(T value, int shiftCount) { - if (IsSupported) + if (!IsSupported) { - if (typeof(T) == typeof(short)) - { - return (T)(object)(short)((short)(object)value >> (shiftCount & 15)); - } - else if (typeof(T) == typeof(int)) - { - return (T)(object)(int)((int)(object)value >> shiftCount); - } - else if (typeof(T) == typeof(long)) - { - return (T)(object)(long)((long)(object)value >> shiftCount); - } - else if (typeof(T) == typeof(nint)) - { - return (T)(object)(nint)((nint)(object)value >> shiftCount); - } - else if (typeof(T) == typeof(sbyte)) - { - return (T)(object)(sbyte)((sbyte)(object)value >> (shiftCount & 7)); - } - else - { - throw new NotSupportedException(SR.Arg_TypeNotSupported); - } + throw new PlatformNotSupportedException(); + } + + if (typeof(T) == typeof(short)) + { + return (T)(object)(short)((short)(object)value >> (shiftCount & 15)); + } + else if (typeof(T) == typeof(int)) + { + return (T)(object)(int)((int)(object)value >> shiftCount); + } + else if (typeof(T) == typeof(long)) + { + return (T)(object)(long)((long)(object)value >> shiftCount); + } + else if (typeof(T) == typeof(nint)) + { + return (T)(object)(nint)((nint)(object)value >> shiftCount); + } + else if (typeof(T) == typeof(sbyte)) + { + return (T)(object)(sbyte)((sbyte)(object)value >> (shiftCount & 7)); } else { @@ -994,52 +960,50 @@ public static T ShiftRightArithmetic(T value, int shiftCount) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T ShiftRightLogical(T value, int shiftCount) { - if (IsSupported) + if (!IsSupported) { - if (typeof(T) == typeof(byte)) - { - return (T)(object)(byte)((byte)(object)value >> (shiftCount & 7)); - } - else if (typeof(T) == typeof(short)) - { - return (T)(object)(short)((ushort)(short)(object)value >> (shiftCount & 15)); - } - else if (typeof(T) == typeof(int)) - { - return (T)(object)(int)((uint)(int)(object)value >> shiftCount); - } - else if (typeof(T) == typeof(long)) - { - return (T)(object)(long)((ulong)(long)(object)value >> shiftCount); - } - else if (typeof(T) == typeof(nint)) - { - return (T)(object)(nint)((nuint)(nint)(object)value >> shiftCount); - } - else if (typeof(T) == typeof(nuint)) - { - return (T)(object)(nuint)((nuint)(object)value >> shiftCount); - } - else if (typeof(T) == typeof(sbyte)) - { - return (T)(object)(sbyte)((byte)(sbyte)(object)value >> (shiftCount & 7)); - } - else if (typeof(T) == typeof(ushort)) - { - return (T)(object)(ushort)((ushort)(object)value >> (shiftCount & 15)); - } - else if (typeof(T) == typeof(uint)) - { - return (T)(object)(uint)((uint)(object)value >> shiftCount); - } - else if (typeof(T) == typeof(ulong)) - { - return (T)(object)(ulong)((ulong)(object)value >> shiftCount); - } - else - { - throw new NotSupportedException(SR.Arg_TypeNotSupported); - } + throw new PlatformNotSupportedException(); + } + + if (typeof(T) == typeof(byte)) + { + return (T)(object)(byte)((byte)(object)value >> (shiftCount & 7)); + } + else if (typeof(T) == typeof(short)) + { + return (T)(object)(short)((ushort)(short)(object)value >> (shiftCount & 15)); + } + else if (typeof(T) == typeof(int)) + { + return (T)(object)(int)((uint)(int)(object)value >> shiftCount); + } + else if (typeof(T) == typeof(long)) + { + return (T)(object)(long)((ulong)(long)(object)value >> shiftCount); + } + else if (typeof(T) == typeof(nint)) + { + return (T)(object)(nint)((nuint)(nint)(object)value >> shiftCount); + } + else if (typeof(T) == typeof(nuint)) + { + return (T)(object)(nuint)((nuint)(object)value >> shiftCount); + } + else if (typeof(T) == typeof(sbyte)) + { + return (T)(object)(sbyte)((byte)(sbyte)(object)value >> (shiftCount & 7)); + } + else if (typeof(T) == typeof(ushort)) + { + return (T)(object)(ushort)((ushort)(object)value >> (shiftCount & 15)); + } + else if (typeof(T) == typeof(uint)) + { + return (T)(object)(uint)((uint)(object)value >> shiftCount); + } + else if (typeof(T) == typeof(ulong)) + { + return (T)(object)(ulong)((ulong)(object)value >> shiftCount); } else { @@ -1050,60 +1014,58 @@ public static T ShiftRightLogical(T value, int shiftCount) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T Sqrt(T value) { - if (IsSupported) + if (!IsSupported) { - if (typeof(T) == typeof(byte)) - { - return (T)(object)(byte)MathF.Sqrt((byte)(object)value); - } - else if (typeof(T) == typeof(double)) - { - return (T)(object)(double)Math.Sqrt((double)(object)value); - } - else if (typeof(T) == typeof(short)) - { - return (T)(object)(short)MathF.Sqrt((short)(object)value); - } - else if (typeof(T) == typeof(int)) - { - return (T)(object)(int)Math.Sqrt((int)(object)value); - } - else if (typeof(T) == typeof(long)) - { - return (T)(object)(long)Math.Sqrt((long)(object)value); - } - else if (typeof(T) == typeof(nint)) - { - return (T)(object)(nint)Math.Sqrt((nint)(object)value); - } - else if (typeof(T) == typeof(nuint)) - { - return (T)(object)(nuint)Math.Sqrt((nuint)(object)value); - } - else if (typeof(T) == typeof(sbyte)) - { - return (T)(object)(sbyte)MathF.Sqrt((sbyte)(object)value); - } - else if (typeof(T) == typeof(float)) - { - return (T)(object)(float)MathF.Sqrt((float)(object)value); - } - else if (typeof(T) == typeof(ushort)) - { - return (T)(object)(ushort)MathF.Sqrt((ushort)(object)value); - } - else if (typeof(T) == typeof(uint)) - { - return (T)(object)(uint)Math.Sqrt((uint)(object)value); - } - else if (typeof(T) == typeof(ulong)) - { - return (T)(object)(ulong)Math.Sqrt((ulong)(object)value); - } - else - { - throw new NotSupportedException(SR.Arg_TypeNotSupported); - } + throw new PlatformNotSupportedException(); + } + + if (typeof(T) == typeof(byte)) + { + return (T)(object)(byte)MathF.Sqrt((byte)(object)value); + } + else if (typeof(T) == typeof(double)) + { + return (T)(object)(double)Math.Sqrt((double)(object)value); + } + else if (typeof(T) == typeof(short)) + { + return (T)(object)(short)MathF.Sqrt((short)(object)value); + } + else if (typeof(T) == typeof(int)) + { + return (T)(object)(int)Math.Sqrt((int)(object)value); + } + else if (typeof(T) == typeof(long)) + { + return (T)(object)(long)Math.Sqrt((long)(object)value); + } + else if (typeof(T) == typeof(nint)) + { + return (T)(object)(nint)Math.Sqrt((nint)(object)value); + } + else if (typeof(T) == typeof(nuint)) + { + return (T)(object)(nuint)Math.Sqrt((nuint)(object)value); + } + else if (typeof(T) == typeof(sbyte)) + { + return (T)(object)(sbyte)MathF.Sqrt((sbyte)(object)value); + } + else if (typeof(T) == typeof(float)) + { + return (T)(object)(float)MathF.Sqrt((float)(object)value); + } + else if (typeof(T) == typeof(ushort)) + { + return (T)(object)(ushort)MathF.Sqrt((ushort)(object)value); + } + else if (typeof(T) == typeof(uint)) + { + return (T)(object)(uint)Math.Sqrt((uint)(object)value); + } + else if (typeof(T) == typeof(ulong)) + { + return (T)(object)(ulong)Math.Sqrt((ulong)(object)value); } else { @@ -1114,60 +1076,58 @@ public static T Sqrt(T value) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T Subtract(T left, T right) { - if (IsSupported) + if (!IsSupported) { - if (typeof(T) == typeof(byte)) - { - return (T)(object)(byte)((byte)(object)left - (byte)(object)right); - } - else if (typeof(T) == typeof(double)) - { - return (T)(object)(double)((double)(object)left - (double)(object)right); - } - else if (typeof(T) == typeof(short)) - { - return (T)(object)(short)((short)(object)left - (short)(object)right); - } - else if (typeof(T) == typeof(int)) - { - return (T)(object)((int)(object)left - (int)(object)right); - } - else if (typeof(T) == typeof(long)) - { - return (T)(object)((long)(object)left - (long)(object)right); - } - else if (typeof(T) == typeof(nint)) - { - return (T)(object)((nint)(object)left - (nint)(object)right); - } - else if (typeof(T) == typeof(nuint)) - { - return (T)(object)((nuint)(object)left - (nuint)(object)right); - } - else if (typeof(T) == typeof(sbyte)) - { - return (T)(object)(sbyte)((sbyte)(object)left - (sbyte)(object)right); - } - else if (typeof(T) == typeof(float)) - { - return (T)(object)(float)((float)(object)left - (float)(object)right); - } - else if (typeof(T) == typeof(ushort)) - { - return (T)(object)(ushort)((ushort)(object)left - (ushort)(object)right); - } - else if (typeof(T) == typeof(uint)) - { - return (T)(object)((uint)(object)left - (uint)(object)right); - } - else if (typeof(T) == typeof(ulong)) - { - return (T)(object)((ulong)(object)left - (ulong)(object)right); - } - else - { - throw new NotSupportedException(SR.Arg_TypeNotSupported); - } + throw new PlatformNotSupportedException(); + } + + if (typeof(T) == typeof(byte)) + { + return (T)(object)(byte)((byte)(object)left - (byte)(object)right); + } + else if (typeof(T) == typeof(double)) + { + return (T)(object)(double)((double)(object)left - (double)(object)right); + } + else if (typeof(T) == typeof(short)) + { + return (T)(object)(short)((short)(object)left - (short)(object)right); + } + else if (typeof(T) == typeof(int)) + { + return (T)(object)((int)(object)left - (int)(object)right); + } + else if (typeof(T) == typeof(long)) + { + return (T)(object)((long)(object)left - (long)(object)right); + } + else if (typeof(T) == typeof(nint)) + { + return (T)(object)((nint)(object)left - (nint)(object)right); + } + else if (typeof(T) == typeof(nuint)) + { + return (T)(object)((nuint)(object)left - (nuint)(object)right); + } + else if (typeof(T) == typeof(sbyte)) + { + return (T)(object)(sbyte)((sbyte)(object)left - (sbyte)(object)right); + } + else if (typeof(T) == typeof(float)) + { + return (T)(object)(float)((float)(object)left - (float)(object)right); + } + else if (typeof(T) == typeof(ushort)) + { + return (T)(object)(ushort)((ushort)(object)left - (ushort)(object)right); + } + else if (typeof(T) == typeof(uint)) + { + return (T)(object)((uint)(object)left - (uint)(object)right); + } + else if (typeof(T) == typeof(ulong)) + { + return (T)(object)((ulong)(object)left - (ulong)(object)right); } else { From a010dddcf50d53b163218f2a68f80fdd4bb7bab4 Mon Sep 17 00:00:00 2001 From: Ivan Povazan Date: Fri, 8 Jul 2022 18:15:01 +0200 Subject: [PATCH 3/5] Adding linker subs file --- .../src/ILLink/ILLink.Substitutions.NoScalarFallback.xml | 7 +++++++ .../src/System.Private.CoreLib.Shared.projitems | 1 + .../src/System/Runtime/Intrinsics/Scalar.cs | 3 +-- 3 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.NoScalarFallback.xml diff --git a/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.NoScalarFallback.xml b/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.NoScalarFallback.xml new file mode 100644 index 0000000000000..079c551f991e4 --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.NoScalarFallback.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems index 93eab8cdde985..cd9bb5806d9e6 100644 --- a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems +++ b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems @@ -48,6 +48,7 @@ + diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Scalar.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Scalar.cs index d9ba6b4e22650..3d27f37360e04 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Scalar.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Scalar.cs @@ -8,8 +8,7 @@ namespace System.Runtime.Intrinsics internal static class Scalar where T : struct { - // FIXME: getter wired to Vector256.IsHardwareAccelerated, should be in separate linker substitution XML - public static bool IsSupported { get => Vector256.IsHardwareAccelerated; } + public static bool IsSupported { get => true; } public static T AllBitsSet { [MethodImpl(MethodImplOptions.AggressiveInlining)] From de22d2da9da8f24b5dfe79968d16278421c886b8 Mon Sep 17 00:00:00 2001 From: Ivan Povazan Date: Wed, 13 Jul 2022 01:05:24 +0200 Subject: [PATCH 4/5] Fixing the linker substitutions for internal class --- .../src/System.Private.CoreLib.Shared.projitems | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems index cd9bb5806d9e6..97b5229a7a668 100644 --- a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems +++ b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems @@ -48,7 +48,6 @@ - @@ -56,6 +55,7 @@ $(ILLinkSharedDirectory)ILLink.Descriptors.LibraryBuild.xml + $(ILLinkSharedDirectory)ILLink.Substitutions.NoScalarFallback.xml From 2e2643b89834204b5bbe77ff2d5efc46477f7376 Mon Sep 17 00:00:00 2001 From: Ivan Povazan Date: Wed, 13 Jul 2022 19:08:06 +0200 Subject: [PATCH 5/5] Change the exception type to match the unsupported branch --- .../src/System/Runtime/Intrinsics/Scalar.cs | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Scalar.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Scalar.cs index 3d27f37360e04..9994d2d93dbf6 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Scalar.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Scalar.cs @@ -16,7 +16,7 @@ public static T AllBitsSet { if (!IsSupported) { - throw new PlatformNotSupportedException(); + throw new NotSupportedException(SR.Arg_TypeNotSupported); } if (typeof(T) == typeof(byte)) @@ -81,7 +81,7 @@ public static T One { if (!IsSupported) { - throw new PlatformNotSupportedException(); + throw new NotSupportedException(SR.Arg_TypeNotSupported); } if (typeof(T) == typeof(byte)) @@ -144,7 +144,7 @@ public static T Abs(T value) { if (!IsSupported) { - throw new PlatformNotSupportedException(); + throw new NotSupportedException(SR.Arg_TypeNotSupported); } // byte, ushort, uint, and ulong should have already been handled @@ -188,7 +188,7 @@ public static T Add(T left, T right) { if (!IsSupported) { - throw new PlatformNotSupportedException(); + throw new NotSupportedException(SR.Arg_TypeNotSupported); } if (typeof(T) == typeof(byte)) @@ -250,7 +250,7 @@ public static T Ceiling(T value) { if (!IsSupported) { - throw new PlatformNotSupportedException(); + throw new NotSupportedException(SR.Arg_TypeNotSupported); } if (typeof(T) == typeof(double)) @@ -272,7 +272,7 @@ public static T Divide(T left, T right) { if (!IsSupported) { - throw new PlatformNotSupportedException(); + throw new NotSupportedException(SR.Arg_TypeNotSupported); } if (typeof(T) == typeof(byte)) @@ -334,7 +334,7 @@ public static bool Equals(T left, T right) { if (!IsSupported) { - throw new PlatformNotSupportedException(); + throw new NotSupportedException(SR.Arg_TypeNotSupported); } if (typeof(T) == typeof(byte)) @@ -396,7 +396,7 @@ public static uint ExtractMostSignificantBit(T value) { if (!IsSupported) { - throw new PlatformNotSupportedException(); + throw new NotSupportedException(SR.Arg_TypeNotSupported); } if (typeof(T) == typeof(byte)) @@ -480,7 +480,7 @@ public static T Floor(T value) { if (!IsSupported) { - throw new PlatformNotSupportedException(); + throw new NotSupportedException(SR.Arg_TypeNotSupported); } if (typeof(T) == typeof(double)) @@ -502,7 +502,7 @@ public static bool GreaterThan(T left, T right) { if (!IsSupported) { - throw new PlatformNotSupportedException(); + throw new NotSupportedException(SR.Arg_TypeNotSupported); } if (typeof(T) == typeof(byte)) @@ -564,7 +564,7 @@ public static bool GreaterThanOrEqual(T left, T right) { if (!IsSupported) { - throw new PlatformNotSupportedException(); + throw new NotSupportedException(SR.Arg_TypeNotSupported); } if (typeof(T) == typeof(byte)) @@ -626,7 +626,7 @@ public static bool LessThan(T left, T right) { if (!IsSupported) { - throw new PlatformNotSupportedException(); + throw new NotSupportedException(SR.Arg_TypeNotSupported); } if (typeof(T) == typeof(byte)) @@ -688,7 +688,7 @@ public static bool LessThanOrEqual(T left, T right) { if (!IsSupported) { - throw new PlatformNotSupportedException(); + throw new NotSupportedException(SR.Arg_TypeNotSupported); } if (typeof(T) == typeof(byte)) @@ -750,7 +750,7 @@ public static T Multiply(T left, T right) { if (!IsSupported) { - throw new PlatformNotSupportedException(); + throw new NotSupportedException(SR.Arg_TypeNotSupported); } if (typeof(T) == typeof(byte)) @@ -811,7 +811,7 @@ public static bool ObjectEquals(T left, T right) { if (!IsSupported) { - throw new PlatformNotSupportedException(); + throw new NotSupportedException(SR.Arg_TypeNotSupported); } if (typeof(T) == typeof(byte)) @@ -873,7 +873,7 @@ public static T ShiftLeft(T value, int shiftCount) { if (!IsSupported) { - throw new PlatformNotSupportedException(); + throw new NotSupportedException(SR.Arg_TypeNotSupported); } if (typeof(T) == typeof(byte)) @@ -927,7 +927,7 @@ public static T ShiftRightArithmetic(T value, int shiftCount) { if (!IsSupported) { - throw new PlatformNotSupportedException(); + throw new NotSupportedException(SR.Arg_TypeNotSupported); } if (typeof(T) == typeof(short)) @@ -961,7 +961,7 @@ public static T ShiftRightLogical(T value, int shiftCount) { if (!IsSupported) { - throw new PlatformNotSupportedException(); + throw new NotSupportedException(SR.Arg_TypeNotSupported); } if (typeof(T) == typeof(byte)) @@ -1015,7 +1015,7 @@ public static T Sqrt(T value) { if (!IsSupported) { - throw new PlatformNotSupportedException(); + throw new NotSupportedException(SR.Arg_TypeNotSupported); } if (typeof(T) == typeof(byte)) @@ -1077,7 +1077,7 @@ public static T Subtract(T left, T right) { if (!IsSupported) { - throw new PlatformNotSupportedException(); + throw new NotSupportedException(SR.Arg_TypeNotSupported); } if (typeof(T) == typeof(byte))