From 8ea879af03852e3d7d916badcf836af61fabb33c Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Mon, 18 Apr 2022 10:19:34 -0700 Subject: [PATCH] Exposing generic math support for unsigned right shift (#68096) * Updating Microsoft.Net.Compilers.Toolset to v4.3.0-1.22215.4 for unsigned-right-shift * Exposing operator >>> (Unsigned Right Shift) * Ensure that SByte and Int16 have unsigned-right-shift operate on themselves * Adding tests covering Unsigned Right Shift --- eng/Versions.props | 2 +- .../System.Private.CoreLib/src/System/Byte.cs | 4 ++-- .../System.Private.CoreLib/src/System/Char.cs | 4 ++-- .../src/System/Int16.cs | 4 ++-- .../src/System/Int32.cs | 4 ++-- .../src/System/Int64.cs | 8 +++---- .../src/System/IntPtr.cs | 8 +++---- .../src/System/Numerics/IShiftOperators.cs | 12 +++++------ .../src/System/SByte.cs | 4 ++-- .../src/System/UInt16.cs | 4 ++-- .../src/System/UInt32.cs | 8 +++---- .../src/System/UInt64.cs | 8 +++---- .../src/System/UIntPtr.cs | 8 +++---- .../System.Runtime/ref/System.Runtime.cs | 12 +++++++++++ .../tests/System/ByteTests.GenericMath.cs | 10 +++++++++ .../tests/System/CharTests.GenericMath.cs | 10 +++++++++ .../tests/System/GenericMathHelpers.cs | 2 ++ .../tests/System/Int16Tests.GenericMath.cs | 10 +++++++++ .../tests/System/Int32Tests.GenericMath.cs | 10 +++++++++ .../tests/System/Int64Tests.GenericMath.cs | 10 +++++++++ .../tests/System/IntPtrTests.GenericMath.cs | 21 +++++++++++++++++++ .../tests/System/SByteTests.GenericMath.cs | 10 +++++++++ .../tests/System/UInt16Tests.GenericMath.cs | 10 +++++++++ .../tests/System/UInt32Tests.GenericMath.cs | 10 +++++++++ .../tests/System/UInt64Tests.GenericMath.cs | 10 +++++++++ .../tests/System/UIntPtrTests.GenericMath.cs | 21 +++++++++++++++++++ 26 files changed, 185 insertions(+), 39 deletions(-) diff --git a/eng/Versions.props b/eng/Versions.props index b7291e449e747..2c60a8dcb5a75 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -49,7 +49,7 @@ - 4.3.0-1.22208.1 + 4.3.0-1.22215.4 2.0.0-alpha.1.21525.11 diff --git a/src/libraries/System.Private.CoreLib/src/System/Byte.cs b/src/libraries/System.Private.CoreLib/src/System/Byte.cs index cd4cc3d0bccf7..c5a6d8f5c0113 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Byte.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Byte.cs @@ -941,8 +941,8 @@ public static bool TryCreate(TOther value, out byte result) /// static byte IShiftOperators.operator >>(byte value, int shiftAmount) => (byte)(value >> shiftAmount); - // /// - // static byte IShiftOperators.operator >>>(byte value, int shiftAmount) => (byte)(value >> shiftAmount); + /// + static byte IShiftOperators.operator >>>(byte value, int shiftAmount) => (byte)(value >>> shiftAmount); // // ISpanParsable diff --git a/src/libraries/System.Private.CoreLib/src/System/Char.cs b/src/libraries/System.Private.CoreLib/src/System/Char.cs index 457093f7267df..aa3a244fbc486 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Char.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Char.cs @@ -1724,8 +1724,8 @@ static bool INumber.TryParse(ReadOnlySpan s, NumberStyles style, IFo /// static char IShiftOperators.operator >>(char value, int shiftAmount) => (char)(value >> shiftAmount); - // /// - // static char IShiftOperators.operator >>>(char value, int shiftAmount) => (char)(value >> shiftAmount); + /// + static char IShiftOperators.operator >>>(char value, int shiftAmount) => (char)(value >>> shiftAmount); // // ISpanParsable diff --git a/src/libraries/System.Private.CoreLib/src/System/Int16.cs b/src/libraries/System.Private.CoreLib/src/System/Int16.cs index da9899368e4ae..9003f15af0cee 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Int16.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Int16.cs @@ -1021,8 +1021,8 @@ public static bool TryCreate(TOther value, out short result) /// static short IShiftOperators.operator >>(short value, int shiftAmount) => (short)(value >> shiftAmount); - // /// - // static short IShiftOperators.operator >>>(short value, int shiftAmount) => (short)((ushort)value >> shiftAmount); + /// + static short IShiftOperators.operator >>>(short value, int shiftAmount) => (short)((ushort)value >>> shiftAmount); // // ISignedNumber diff --git a/src/libraries/System.Private.CoreLib/src/System/Int32.cs b/src/libraries/System.Private.CoreLib/src/System/Int32.cs index 623c3ae861846..6445473568caf 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Int32.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Int32.cs @@ -985,8 +985,8 @@ public static bool TryCreate(TOther value, out int result) /// static int IShiftOperators.operator >>(int value, int shiftAmount) => value >> shiftAmount; - // /// - // static int IShiftOperators.operator >>>(int value, int shiftAmount) => (int)((uint)value >> shiftAmount); + /// + static int IShiftOperators.operator >>>(int value, int shiftAmount) => value >>> shiftAmount; // // ISignedNumber diff --git a/src/libraries/System.Private.CoreLib/src/System/Int64.cs b/src/libraries/System.Private.CoreLib/src/System/Int64.cs index abfdafc2a5125..85f94d6420048 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Int64.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Int64.cs @@ -938,13 +938,13 @@ public static bool TryCreate(TOther value, out long result) // /// - static long IShiftOperators.operator <<(long value, int shiftAmount) => value << (int)shiftAmount; + static long IShiftOperators.operator <<(long value, int shiftAmount) => value << shiftAmount; /// - static long IShiftOperators.operator >>(long value, int shiftAmount) => value >> (int)shiftAmount; + static long IShiftOperators.operator >>(long value, int shiftAmount) => value >> shiftAmount; - // /// - // static long IShiftOperators.operator >>>(long value, int shiftAmount) => (long)((ulong)value >> (int)shiftAmount); + /// + static long IShiftOperators.operator >>>(long value, int shiftAmount) => value >>> shiftAmount; // // ISignedNumber diff --git a/src/libraries/System.Private.CoreLib/src/System/IntPtr.cs b/src/libraries/System.Private.CoreLib/src/System/IntPtr.cs index a2a093c69263b..bc97638d3568f 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IntPtr.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IntPtr.cs @@ -980,13 +980,13 @@ static bool INumber.TryCreate(TOther value, out nint result) // /// - static nint IShiftOperators.operator <<(nint value, int shiftAmount) => value << (int)shiftAmount; + static nint IShiftOperators.operator <<(nint value, int shiftAmount) => value << shiftAmount; /// - static nint IShiftOperators.operator >>(nint value, int shiftAmount) => value >> (int)shiftAmount; + static nint IShiftOperators.operator >>(nint value, int shiftAmount) => value >> shiftAmount; - // /// - // static nint IShiftOperators.operator >>>(nint value, int shiftAmount) => (nint)((nuint)value >> (int)shiftAmount); + /// + static nint IShiftOperators.operator >>>(nint value, int shiftAmount) => value >>> shiftAmount; // // ISignedNumber diff --git a/src/libraries/System.Private.CoreLib/src/System/Numerics/IShiftOperators.cs b/src/libraries/System.Private.CoreLib/src/System/Numerics/IShiftOperators.cs index f7994afa139e6..9b572bb799242 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Numerics/IShiftOperators.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Numerics/IShiftOperators.cs @@ -22,11 +22,11 @@ public interface IShiftOperators /// This operation is meant to perform a signed (otherwise known as an arithmetic) right shift on signed types. static abstract TResult operator >>(TSelf value, int shiftAmount); // TODO_GENERIC_MATH: shiftAmount should be TOther - // /// Shifts a value right by a given amount. - // /// The value which is shifted right by . - // /// The amount by which is shifted right. - // /// The result of shifting right by . - // /// This operation is meant to perform n unsigned (otherwise known as a logical) right shift on all types. - // static abstract TResult operator >>>(TSelf value, int shiftAmount); // TODO_GENERIC_MATH: shiftAmount should be TOther + /// Shifts a value right by a given amount. + /// The value which is shifted right by . + /// The amount by which is shifted right. + /// The result of shifting right by . + /// This operation is meant to perform n unsigned (otherwise known as a logical) right shift on all types. + static abstract TResult operator >>>(TSelf value, int shiftAmount); // TODO_GENERIC_MATH: shiftAmount should be TOther } } diff --git a/src/libraries/System.Private.CoreLib/src/System/SByte.cs b/src/libraries/System.Private.CoreLib/src/System/SByte.cs index 99e3054d86e04..7fd04196ed9cb 100644 --- a/src/libraries/System.Private.CoreLib/src/System/SByte.cs +++ b/src/libraries/System.Private.CoreLib/src/System/SByte.cs @@ -1047,8 +1047,8 @@ public static bool TryCreate(TOther value, out sbyte result) /// static sbyte IShiftOperators.operator >>(sbyte value, int shiftAmount) => (sbyte)(value >> shiftAmount); - // /// - // static sbyte IShiftOperators.operator >>>(sbyte value, int shiftAmount) => (sbyte)((byte)value >> shiftAmount); + /// + static sbyte IShiftOperators.operator >>>(sbyte value, int shiftAmount) => (sbyte)((byte)value >>> shiftAmount); // // ISignedNumber diff --git a/src/libraries/System.Private.CoreLib/src/System/UInt16.cs b/src/libraries/System.Private.CoreLib/src/System/UInt16.cs index dda9162c89d94..e3c8c23b758ce 100644 --- a/src/libraries/System.Private.CoreLib/src/System/UInt16.cs +++ b/src/libraries/System.Private.CoreLib/src/System/UInt16.cs @@ -916,8 +916,8 @@ public static bool TryCreate(TOther value, out ushort result) /// static ushort IShiftOperators.operator >>(ushort value, int shiftAmount) => (ushort)(value >> shiftAmount); - // /// - // static ushort IShiftOperators.operator >>>(ushort value, int shiftAmount) => (ushort)(value >> shiftAmount); + /// + static ushort IShiftOperators.operator >>>(ushort value, int shiftAmount) => (ushort)(value >>> shiftAmount); // // ISpanParsable diff --git a/src/libraries/System.Private.CoreLib/src/System/UInt32.cs b/src/libraries/System.Private.CoreLib/src/System/UInt32.cs index a38bd89c0ab50..3c8a024187216 100644 --- a/src/libraries/System.Private.CoreLib/src/System/UInt32.cs +++ b/src/libraries/System.Private.CoreLib/src/System/UInt32.cs @@ -887,13 +887,13 @@ public static bool TryCreate(TOther value, out uint result) // /// - static uint IShiftOperators.operator <<(uint value, int shiftAmount) => value << (int)shiftAmount; + static uint IShiftOperators.operator <<(uint value, int shiftAmount) => value << shiftAmount; /// - static uint IShiftOperators.operator >>(uint value, int shiftAmount) => value >> (int)shiftAmount; + static uint IShiftOperators.operator >>(uint value, int shiftAmount) => value >> shiftAmount; - // /// - // static uint IShiftOperators.operator >>>(uint value, int shiftAmount) => value >> (int)shiftAmount; + /// + static uint IShiftOperators.operator >>>(uint value, int shiftAmount) => value >>> shiftAmount; // // ISpanParsable diff --git a/src/libraries/System.Private.CoreLib/src/System/UInt64.cs b/src/libraries/System.Private.CoreLib/src/System/UInt64.cs index 50f9b42a8eab3..d19d88c51114b 100644 --- a/src/libraries/System.Private.CoreLib/src/System/UInt64.cs +++ b/src/libraries/System.Private.CoreLib/src/System/UInt64.cs @@ -866,13 +866,13 @@ public static bool TryCreate(TOther value, out ulong result) // /// - static ulong IShiftOperators.operator <<(ulong value, int shiftAmount) => value << (int)shiftAmount; + static ulong IShiftOperators.operator <<(ulong value, int shiftAmount) => value << shiftAmount; /// - static ulong IShiftOperators.operator >>(ulong value, int shiftAmount) => value >> (int)shiftAmount; + static ulong IShiftOperators.operator >>(ulong value, int shiftAmount) => value >> shiftAmount; - // /// - // static ulong IShiftOperators.operator >>>(ulong value, int shiftAmount) => value >> (int)shiftAmount; + /// + static ulong IShiftOperators.operator >>>(ulong value, int shiftAmount) => value >>> shiftAmount; // // ISpanParsable diff --git a/src/libraries/System.Private.CoreLib/src/System/UIntPtr.cs b/src/libraries/System.Private.CoreLib/src/System/UIntPtr.cs index 51faf46f9d85c..715e00dfb1d23 100644 --- a/src/libraries/System.Private.CoreLib/src/System/UIntPtr.cs +++ b/src/libraries/System.Private.CoreLib/src/System/UIntPtr.cs @@ -910,13 +910,13 @@ static bool INumber.TryCreate(TOther value, out nuint result) // /// - static nuint IShiftOperators.operator <<(nuint value, int shiftAmount) => value << (int)shiftAmount; + static nuint IShiftOperators.operator <<(nuint value, int shiftAmount) => value << shiftAmount; /// - static nuint IShiftOperators.operator >>(nuint value, int shiftAmount) => value >> (int)shiftAmount; + static nuint IShiftOperators.operator >>(nuint value, int shiftAmount) => value >> shiftAmount; - // /// - // static nuint IShiftOperators.operator >>>(nuint value, int shiftAmount) => value >> (int)shiftAmount; + /// + static nuint IShiftOperators.operator >>>(nuint value, int shiftAmount) => value >>> shiftAmount; // // ISubtractionOperators diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index f25a89a45c77f..f14d8631892ab 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -796,6 +796,7 @@ public static void SetByte(System.Array array, int index, byte value) { } static byte System.Numerics.INumber.MinMagnitude(byte x, byte y) { throw null; } static byte System.Numerics.IShiftOperators.operator <<(byte value, int shiftAmount) { throw null; } static byte System.Numerics.IShiftOperators.operator >>(byte value, int shiftAmount) { throw null; } + static byte System.Numerics.IShiftOperators.operator >>>(byte value, int shiftAmount) { throw null; } static byte System.Numerics.ISubtractionOperators.operator checked -(byte left, byte right) { throw null; } static byte System.Numerics.ISubtractionOperators.operator -(byte left, byte right) { throw null; } static byte System.Numerics.IUnaryNegationOperators.operator checked -(byte value) { throw null; } @@ -948,6 +949,7 @@ public CannotUnloadAppDomainException(string? message, System.Exception? innerEx static bool System.Numerics.INumber.TryParse([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] string? s, System.Globalization.NumberStyles style, System.IFormatProvider? provider, out char result) { throw null; } static char System.Numerics.IShiftOperators.operator <<(char value, int shiftAmount) { throw null; } static char System.Numerics.IShiftOperators.operator >>(char value, int shiftAmount) { throw null; } + static char System.Numerics.IShiftOperators.operator >>>(char value, int shiftAmount) { throw null; } static char System.Numerics.ISubtractionOperators.operator checked -(char left, char right) { throw null; } static char System.Numerics.ISubtractionOperators.operator -(char left, char right) { throw null; } static char System.Numerics.IUnaryNegationOperators.operator checked -(char value) { throw null; } @@ -2949,6 +2951,7 @@ public InsufficientMemoryException(string? message, System.Exception? innerExcep static short System.Numerics.IMultiplyOperators.operator *(short left, short right) { throw null; } static short System.Numerics.IShiftOperators.operator <<(short value, int shiftAmount) { throw null; } static short System.Numerics.IShiftOperators.operator >>(short value, int shiftAmount) { throw null; } + static short System.Numerics.IShiftOperators.operator >>>(short value, int shiftAmount) { throw null; } static short System.Numerics.ISubtractionOperators.operator checked -(short left, short right) { throw null; } static short System.Numerics.ISubtractionOperators.operator -(short left, short right) { throw null; } static short System.Numerics.IUnaryNegationOperators.operator checked -(short value) { throw null; } @@ -3052,6 +3055,7 @@ public InsufficientMemoryException(string? message, System.Exception? innerExcep static int System.Numerics.IMultiplyOperators.operator *(int left, int right) { throw null; } static int System.Numerics.IShiftOperators.operator <<(int value, int shiftAmount) { throw null; } static int System.Numerics.IShiftOperators.operator >>(int value, int shiftAmount) { throw null; } + static int System.Numerics.IShiftOperators.operator >>>(int value, int shiftAmount) { throw null; } static int System.Numerics.ISubtractionOperators.operator checked -(int left, int right) { throw null; } static int System.Numerics.ISubtractionOperators.operator -(int left, int right) { throw null; } static int System.Numerics.IUnaryNegationOperators.operator checked -(int value) { throw null; } @@ -3155,6 +3159,7 @@ public InsufficientMemoryException(string? message, System.Exception? innerExcep static long System.Numerics.IMultiplyOperators.operator *(long left, long right) { throw null; } static long System.Numerics.IShiftOperators.operator <<(long value, int shiftAmount) { throw null; } static long System.Numerics.IShiftOperators.operator >>(long value, int shiftAmount) { throw null; } + static long System.Numerics.IShiftOperators.operator >>>(long value, int shiftAmount) { throw null; } static long System.Numerics.ISubtractionOperators.operator checked -(long left, long right) { throw null; } static long System.Numerics.ISubtractionOperators.operator -(long left, long right) { throw null; } static long System.Numerics.IUnaryNegationOperators.operator checked -(long value) { throw null; } @@ -3262,6 +3267,7 @@ public InsufficientMemoryException(string? message, System.Exception? innerExcep static bool System.Numerics.INumber.TryCreate(TOther value, out nint result) { throw null; } static nint System.Numerics.IShiftOperators.operator <<(nint value, int shiftAmount) { throw null; } static nint System.Numerics.IShiftOperators.operator >>(nint value, int shiftAmount) { throw null; } + static nint System.Numerics.IShiftOperators.operator >>>(nint value, int shiftAmount) { throw null; } static nint System.Numerics.ISubtractionOperators.operator checked -(nint left, nint right) { throw null; } static nint System.Numerics.ISubtractionOperators.operator -(nint left, nint right) { throw null; } static nint System.Numerics.IUnaryNegationOperators.operator checked -(nint value) { throw null; } @@ -4167,6 +4173,7 @@ public void GetObjectData(System.Runtime.Serialization.SerializationInfo info, S static sbyte System.Numerics.IMultiplyOperators.operator *(sbyte left, sbyte right) { throw null; } static sbyte System.Numerics.IShiftOperators.operator <<(sbyte value, int shiftAmount) { throw null; } static sbyte System.Numerics.IShiftOperators.operator >>(sbyte value, int shiftAmount) { throw null; } + static sbyte System.Numerics.IShiftOperators.operator >>>(sbyte value, int shiftAmount) { throw null; } static sbyte System.Numerics.ISubtractionOperators.operator checked -(sbyte left, sbyte right) { throw null; } static sbyte System.Numerics.ISubtractionOperators.operator -(sbyte left, sbyte right) { throw null; } static sbyte System.Numerics.IUnaryNegationOperators.operator checked -(sbyte value) { throw null; } @@ -5604,6 +5611,7 @@ public TypeUnloadedException(string? message, System.Exception? innerException) static ushort System.Numerics.INumber.MinMagnitude(ushort x, ushort y) { throw null; } static ushort System.Numerics.IShiftOperators.operator <<(ushort value, int shiftAmount) { throw null; } static ushort System.Numerics.IShiftOperators.operator >>(ushort value, int shiftAmount) { throw null; } + static ushort System.Numerics.IShiftOperators.operator >>>(ushort value, int shiftAmount) { throw null; } static ushort System.Numerics.ISubtractionOperators.operator checked -(ushort left, ushort right) { throw null; } static ushort System.Numerics.ISubtractionOperators.operator -(ushort left, ushort right) { throw null; } static ushort System.Numerics.IUnaryNegationOperators.operator checked -(ushort value) { throw null; } @@ -5707,6 +5715,7 @@ public TypeUnloadedException(string? message, System.Exception? innerException) static uint System.Numerics.INumber.MinMagnitude(uint x, uint y) { throw null; } static uint System.Numerics.IShiftOperators.operator <<(uint value, int shiftAmount) { throw null; } static uint System.Numerics.IShiftOperators.operator >>(uint value, int shiftAmount) { throw null; } + static uint System.Numerics.IShiftOperators.operator >>>(uint value, int shiftAmount) { throw null; } static uint System.Numerics.ISubtractionOperators.operator checked -(uint left, uint right) { throw null; } static uint System.Numerics.ISubtractionOperators.operator -(uint left, uint right) { throw null; } static uint System.Numerics.IUnaryNegationOperators.operator checked -(uint value) { throw null; } @@ -5810,6 +5819,7 @@ public TypeUnloadedException(string? message, System.Exception? innerException) static ulong System.Numerics.INumber.MinMagnitude(ulong x, ulong y) { throw null; } static ulong System.Numerics.IShiftOperators.operator <<(ulong value, int shiftAmount) { throw null; } static ulong System.Numerics.IShiftOperators.operator >>(ulong value, int shiftAmount) { throw null; } + static ulong System.Numerics.IShiftOperators.operator >>>(ulong value, int shiftAmount) { throw null; } static ulong System.Numerics.ISubtractionOperators.operator checked -(ulong left, ulong right) { throw null; } static ulong System.Numerics.ISubtractionOperators.operator -(ulong left, ulong right) { throw null; } static ulong System.Numerics.IUnaryNegationOperators.operator checked -(ulong value) { throw null; } @@ -5914,6 +5924,7 @@ public TypeUnloadedException(string? message, System.Exception? innerException) static bool System.Numerics.INumber.TryCreate(TOther value, out nuint result) { throw null; } static nuint System.Numerics.IShiftOperators.operator <<(nuint value, int shiftAmount) { throw null; } static nuint System.Numerics.IShiftOperators.operator >>(nuint value, int shiftAmount) { throw null; } + static nuint System.Numerics.IShiftOperators.operator >>>(nuint value, int shiftAmount) { throw null; } static nuint System.Numerics.ISubtractionOperators.operator checked -(nuint left, nuint right) { throw null; } static nuint System.Numerics.ISubtractionOperators.operator -(nuint left, nuint right) { throw null; } static nuint System.Numerics.IUnaryNegationOperators.operator checked -(nuint value) { throw null; } @@ -9689,6 +9700,7 @@ public partial interface IShiftOperators where TSelf : System.Nu { static abstract TResult operator <<(TSelf value, int shiftAmount); static abstract TResult operator >>(TSelf value, int shiftAmount); + static abstract TResult operator >>>(TSelf value, int shiftAmount); } public partial interface ISignedNumber where TSelf : System.Numerics.INumberBase, System.Numerics.ISignedNumber { diff --git a/src/libraries/System.Runtime/tests/System/ByteTests.GenericMath.cs b/src/libraries/System.Runtime/tests/System/ByteTests.GenericMath.cs index 1a4cf8c6bf679..f824be8e34a15 100644 --- a/src/libraries/System.Runtime/tests/System/ByteTests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System/ByteTests.GenericMath.cs @@ -1132,6 +1132,16 @@ public static void op_RightShiftTest() Assert.Equal((byte)0x7F, ShiftOperatorsHelper.op_RightShift((byte)0xFF, 1)); } + [Fact] + public static void op_UnsignedRightShiftTest() + { + Assert.Equal((byte)0x00, ShiftOperatorsHelper.op_UnsignedRightShift((byte)0x00, 1)); + Assert.Equal((byte)0x00, ShiftOperatorsHelper.op_UnsignedRightShift((byte)0x01, 1)); + Assert.Equal((byte)0x3F, ShiftOperatorsHelper.op_UnsignedRightShift((byte)0x7F, 1)); + Assert.Equal((byte)0x40, ShiftOperatorsHelper.op_UnsignedRightShift((byte)0x80, 1)); + Assert.Equal((byte)0x7F, ShiftOperatorsHelper.op_UnsignedRightShift((byte)0xFF, 1)); + } + [Fact] public static void op_SubtractionTest() { diff --git a/src/libraries/System.Runtime/tests/System/CharTests.GenericMath.cs b/src/libraries/System.Runtime/tests/System/CharTests.GenericMath.cs index bf2af8406ff39..6a19c753a715a 100644 --- a/src/libraries/System.Runtime/tests/System/CharTests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System/CharTests.GenericMath.cs @@ -1131,6 +1131,16 @@ public static void op_RightShiftTest() Assert.Equal((char)0x7FFF, ShiftOperatorsHelper.op_RightShift((char)0xFFFF, 1)); } + [Fact] + public static void op_UnsignedRightShiftTest() + { + Assert.Equal((char)0x0000, ShiftOperatorsHelper.op_UnsignedRightShift((char)0x0000, 1)); + Assert.Equal((char)0x0000, ShiftOperatorsHelper.op_UnsignedRightShift((char)0x0001, 1)); + Assert.Equal((char)0x3FFF, ShiftOperatorsHelper.op_UnsignedRightShift((char)0x7FFF, 1)); + Assert.Equal((char)0x4000, ShiftOperatorsHelper.op_UnsignedRightShift((char)0x8000, 1)); + Assert.Equal((char)0x7FFF, ShiftOperatorsHelper.op_UnsignedRightShift((char)0xFFFF, 1)); + } + [Fact] public static void op_SubtractionTest() { diff --git a/src/libraries/System.Runtime/tests/System/GenericMathHelpers.cs b/src/libraries/System.Runtime/tests/System/GenericMathHelpers.cs index ca5a62d861f16..1db350ef46357 100644 --- a/src/libraries/System.Runtime/tests/System/GenericMathHelpers.cs +++ b/src/libraries/System.Runtime/tests/System/GenericMathHelpers.cs @@ -206,6 +206,8 @@ public static class ShiftOperatorsHelper public static TResult op_LeftShift(TSelf value, int shiftAmount) => value << shiftAmount; public static TResult op_RightShift(TSelf value, int shiftAmount) => value >> shiftAmount; + + public static TResult op_UnsignedRightShift(TSelf value, int shiftAmount) => value >>> shiftAmount; } public static class SignedNumberHelper diff --git a/src/libraries/System.Runtime/tests/System/Int16Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System/Int16Tests.GenericMath.cs index 44b19d8e08e8b..38a8be51f3b61 100644 --- a/src/libraries/System.Runtime/tests/System/Int16Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System/Int16Tests.GenericMath.cs @@ -1138,6 +1138,16 @@ public static void op_RightShiftTest() Assert.Equal(unchecked((short)0xFFFF), ShiftOperatorsHelper.op_RightShift(unchecked((short)0xFFFF), 1)); } + [Fact] + public static void op_UnsignedRightShiftTest() + { + Assert.Equal((short)0x0000, ShiftOperatorsHelper.op_UnsignedRightShift((short)0x0000, 1)); + Assert.Equal((short)0x0000, ShiftOperatorsHelper.op_UnsignedRightShift((short)0x0001, 1)); + Assert.Equal((short)0x3FFF, ShiftOperatorsHelper.op_UnsignedRightShift((short)0x7FFF, 1)); + Assert.Equal((short)0x4000, ShiftOperatorsHelper.op_UnsignedRightShift(unchecked((short)0x8000), 1)); + Assert.Equal((short)0x7FFF, ShiftOperatorsHelper.op_UnsignedRightShift(unchecked((short)0xFFFF), 1)); + } + [Fact] public static void op_SubtractionTest() { diff --git a/src/libraries/System.Runtime/tests/System/Int32Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System/Int32Tests.GenericMath.cs index 8fd0a7da4e129..164fb6d9e609f 100644 --- a/src/libraries/System.Runtime/tests/System/Int32Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System/Int32Tests.GenericMath.cs @@ -1138,6 +1138,16 @@ public static void op_RightShiftTest() Assert.Equal(unchecked((int)0xFFFFFFFF), ShiftOperatorsHelper.op_RightShift(unchecked((int)0xFFFFFFFF), 1)); } + [Fact] + public static void op_UnsignedRightShiftTest() + { + Assert.Equal((int)0x00000000, ShiftOperatorsHelper.op_UnsignedRightShift((int)0x00000000, 1)); + Assert.Equal((int)0x00000000, ShiftOperatorsHelper.op_UnsignedRightShift((int)0x00000001, 1)); + Assert.Equal((int)0x3FFFFFFF, ShiftOperatorsHelper.op_UnsignedRightShift((int)0x7FFFFFFF, 1)); + Assert.Equal((int)0x40000000, ShiftOperatorsHelper.op_UnsignedRightShift(unchecked((int)0x80000000), 1)); + Assert.Equal((int)0x7FFFFFFF, ShiftOperatorsHelper.op_UnsignedRightShift(unchecked((int)0xFFFFFFFF), 1)); + } + [Fact] public static void op_SubtractionTest() { diff --git a/src/libraries/System.Runtime/tests/System/Int64Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System/Int64Tests.GenericMath.cs index 58717eb3b380a..805964b0d3487 100644 --- a/src/libraries/System.Runtime/tests/System/Int64Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System/Int64Tests.GenericMath.cs @@ -1138,6 +1138,16 @@ public static void op_RightShiftTest() Assert.Equal(unchecked((long)0xFFFFFFFFFFFFFFFF), ShiftOperatorsHelper.op_RightShift(unchecked((long)0xFFFFFFFFFFFFFFFF), 1)); } + [Fact] + public static void op_UnsignedRightShiftTest() + { + Assert.Equal((long)0x0000000000000000, ShiftOperatorsHelper.op_UnsignedRightShift((long)0x0000000000000000, 1)); + Assert.Equal((long)0x0000000000000000, ShiftOperatorsHelper.op_UnsignedRightShift((long)0x0000000000000001, 1)); + Assert.Equal((long)0x3FFFFFFFFFFFFFFF, ShiftOperatorsHelper.op_UnsignedRightShift((long)0x7FFFFFFFFFFFFFFF, 1)); + Assert.Equal((long)0x4000000000000000, ShiftOperatorsHelper.op_UnsignedRightShift(unchecked((long)0x8000000000000000), 1)); + Assert.Equal((long)0x7FFFFFFFFFFFFFFF, ShiftOperatorsHelper.op_UnsignedRightShift(unchecked((long)0xFFFFFFFFFFFFFFFF), 1)); + } + [Fact] public static void op_SubtractionTest() { diff --git a/src/libraries/System.Runtime/tests/System/IntPtrTests.GenericMath.cs b/src/libraries/System.Runtime/tests/System/IntPtrTests.GenericMath.cs index c763518ffef9f..61f3828963587 100644 --- a/src/libraries/System.Runtime/tests/System/IntPtrTests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System/IntPtrTests.GenericMath.cs @@ -1790,6 +1790,27 @@ public static void op_RightShiftTest() } } + [Fact] + public static void op_UnsignedRightShiftTest() + { + if (Environment.Is64BitProcess) + { + Assert.Equal(unchecked((nint)0x0000000000000000), ShiftOperatorsHelper.op_UnsignedRightShift(unchecked((nint)0x0000000000000000), 1)); + Assert.Equal(unchecked((nint)0x0000000000000000), ShiftOperatorsHelper.op_UnsignedRightShift(unchecked((nint)0x0000000000000001), 1)); + Assert.Equal(unchecked((nint)0x3FFFFFFFFFFFFFFF), ShiftOperatorsHelper.op_UnsignedRightShift(unchecked((nint)0x7FFFFFFFFFFFFFFF), 1)); + Assert.Equal(unchecked((nint)0x4000000000000000), ShiftOperatorsHelper.op_UnsignedRightShift(unchecked((nint)0x8000000000000000), 1)); + Assert.Equal(unchecked((nint)0x7FFFFFFFFFFFFFFF), ShiftOperatorsHelper.op_UnsignedRightShift(unchecked((nint)0xFFFFFFFFFFFFFFFF), 1)); + } + else + { + Assert.Equal((nint)0x00000000, ShiftOperatorsHelper.op_UnsignedRightShift((nint)0x00000000, 1)); + Assert.Equal((nint)0x00000000, ShiftOperatorsHelper.op_UnsignedRightShift((nint)0x00000001, 1)); + Assert.Equal((nint)0x3FFFFFFF, ShiftOperatorsHelper.op_UnsignedRightShift((nint)0x7FFFFFFF, 1)); + Assert.Equal((nint)0x40000000, ShiftOperatorsHelper.op_UnsignedRightShift(unchecked((nint)0x80000000), 1)); + Assert.Equal((nint)0x7FFFFFFF, ShiftOperatorsHelper.op_UnsignedRightShift(unchecked((nint)0xFFFFFFFF), 1)); + } + } + [Fact] public static void op_SubtractionTest() { diff --git a/src/libraries/System.Runtime/tests/System/SByteTests.GenericMath.cs b/src/libraries/System.Runtime/tests/System/SByteTests.GenericMath.cs index 1a6fe286e03f8..9e75484b4005b 100644 --- a/src/libraries/System.Runtime/tests/System/SByteTests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System/SByteTests.GenericMath.cs @@ -1138,6 +1138,16 @@ public static void op_RightShiftTest() Assert.Equal(unchecked((sbyte)0xFF), ShiftOperatorsHelper.op_RightShift(unchecked((sbyte)0xFF), 1)); } + [Fact] + public static void op_UnsignedRightShiftTest() + { + Assert.Equal((sbyte)0x00, ShiftOperatorsHelper.op_UnsignedRightShift((sbyte)0x00, 1)); + Assert.Equal((sbyte)0x00, ShiftOperatorsHelper.op_UnsignedRightShift((sbyte)0x01, 1)); + Assert.Equal((sbyte)0x3F, ShiftOperatorsHelper.op_UnsignedRightShift((sbyte)0x7F, 1)); + Assert.Equal((sbyte)0x40, ShiftOperatorsHelper.op_UnsignedRightShift(unchecked((sbyte)0x80), 1)); + Assert.Equal((sbyte)0x7F, ShiftOperatorsHelper.op_UnsignedRightShift(unchecked((sbyte)0xFF), 1)); + } + [Fact] public static void op_SubtractionTest() { diff --git a/src/libraries/System.Runtime/tests/System/UInt16Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System/UInt16Tests.GenericMath.cs index f1d5adf11cae7..e456553c3df74 100644 --- a/src/libraries/System.Runtime/tests/System/UInt16Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System/UInt16Tests.GenericMath.cs @@ -1132,6 +1132,16 @@ public static void op_RightShiftTest() Assert.Equal((ushort)0x7FFF, ShiftOperatorsHelper.op_RightShift((ushort)0xFFFF, 1)); } + [Fact] + public static void op_UnsignedRightShiftTest() + { + Assert.Equal((ushort)0x0000, ShiftOperatorsHelper.op_UnsignedRightShift((ushort)0x0000, 1)); + Assert.Equal((ushort)0x0000, ShiftOperatorsHelper.op_UnsignedRightShift((ushort)0x0001, 1)); + Assert.Equal((ushort)0x3FFF, ShiftOperatorsHelper.op_UnsignedRightShift((ushort)0x7FFF, 1)); + Assert.Equal((ushort)0x4000, ShiftOperatorsHelper.op_UnsignedRightShift((ushort)0x8000, 1)); + Assert.Equal((ushort)0x7FFF, ShiftOperatorsHelper.op_UnsignedRightShift((ushort)0xFFFF, 1)); + } + [Fact] public static void op_SubtractionTest() { diff --git a/src/libraries/System.Runtime/tests/System/UInt32Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System/UInt32Tests.GenericMath.cs index b4e343ec9f5e5..5815c07b9c8c3 100644 --- a/src/libraries/System.Runtime/tests/System/UInt32Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System/UInt32Tests.GenericMath.cs @@ -1133,6 +1133,16 @@ public static void op_RightShiftTest() Assert.Equal((uint)0x7FFFFFFF, ShiftOperatorsHelper.op_RightShift((uint)0xFFFFFFFF, 1)); } + [Fact] + public static void op_UnsignedRightShiftTest() + { + Assert.Equal((uint)0x00000000, ShiftOperatorsHelper.op_UnsignedRightShift((uint)0x00000000, 1)); + Assert.Equal((uint)0x00000000, ShiftOperatorsHelper.op_UnsignedRightShift((uint)0x00000001, 1)); + Assert.Equal((uint)0x3FFFFFFF, ShiftOperatorsHelper.op_UnsignedRightShift((uint)0x7FFFFFFF, 1)); + Assert.Equal((uint)0x40000000, ShiftOperatorsHelper.op_UnsignedRightShift((uint)0x80000000, 1)); + Assert.Equal((uint)0x7FFFFFFF, ShiftOperatorsHelper.op_UnsignedRightShift((uint)0xFFFFFFFF, 1)); + } + [Fact] public static void op_SubtractionTest() { diff --git a/src/libraries/System.Runtime/tests/System/UInt64Tests.GenericMath.cs b/src/libraries/System.Runtime/tests/System/UInt64Tests.GenericMath.cs index 02b6280c2e522..6765bee80e33a 100644 --- a/src/libraries/System.Runtime/tests/System/UInt64Tests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System/UInt64Tests.GenericMath.cs @@ -1132,6 +1132,16 @@ public static void op_RightShiftTest() Assert.Equal((ulong)0x7FFFFFFFFFFFFFFF, ShiftOperatorsHelper.op_RightShift((ulong)0xFFFFFFFFFFFFFFFF, 1)); } + [Fact] + public static void op_UnsignedRightShiftTest() + { + Assert.Equal((ulong)0x0000000000000000, ShiftOperatorsHelper.op_UnsignedRightShift((ulong)0x0000000000000000, 1)); + Assert.Equal((ulong)0x0000000000000000, ShiftOperatorsHelper.op_UnsignedRightShift((ulong)0x0000000000000001, 1)); + Assert.Equal((ulong)0x3FFFFFFFFFFFFFFF, ShiftOperatorsHelper.op_UnsignedRightShift((ulong)0x7FFFFFFFFFFFFFFF, 1)); + Assert.Equal((ulong)0x4000000000000000, ShiftOperatorsHelper.op_UnsignedRightShift((ulong)0x8000000000000000, 1)); + Assert.Equal((ulong)0x7FFFFFFFFFFFFFFF, ShiftOperatorsHelper.op_UnsignedRightShift((ulong)0xFFFFFFFFFFFFFFFF, 1)); + } + [Fact] public static void op_SubtractionTest() { diff --git a/src/libraries/System.Runtime/tests/System/UIntPtrTests.GenericMath.cs b/src/libraries/System.Runtime/tests/System/UIntPtrTests.GenericMath.cs index 0fadcb2c1ebf5..6bc58d33b8515 100644 --- a/src/libraries/System.Runtime/tests/System/UIntPtrTests.GenericMath.cs +++ b/src/libraries/System.Runtime/tests/System/UIntPtrTests.GenericMath.cs @@ -1738,6 +1738,27 @@ public static void op_RightShiftTest() } } + [Fact] + public static void op_UnsignedRightShiftTest() + { + if (Environment.Is64BitProcess) + { + Assert.Equal(unchecked((nuint)0x0000000000000000), ShiftOperatorsHelper.op_UnsignedRightShift(unchecked((nuint)0x0000000000000000), 1)); + Assert.Equal(unchecked((nuint)0x0000000000000000), ShiftOperatorsHelper.op_UnsignedRightShift(unchecked((nuint)0x0000000000000001), 1)); + Assert.Equal(unchecked((nuint)0x3FFFFFFFFFFFFFFF), ShiftOperatorsHelper.op_UnsignedRightShift(unchecked((nuint)0x7FFFFFFFFFFFFFFF), 1)); + Assert.Equal(unchecked((nuint)0x4000000000000000), ShiftOperatorsHelper.op_UnsignedRightShift(unchecked((nuint)0x8000000000000000), 1)); + Assert.Equal(unchecked((nuint)0x7FFFFFFFFFFFFFFF), ShiftOperatorsHelper.op_UnsignedRightShift(unchecked((nuint)0xFFFFFFFFFFFFFFFF), 1)); + } + else + { + Assert.Equal((nuint)0x00000000, ShiftOperatorsHelper.op_UnsignedRightShift((nuint)0x00000000, 1)); + Assert.Equal((nuint)0x00000000, ShiftOperatorsHelper.op_UnsignedRightShift((nuint)0x00000001, 1)); + Assert.Equal((nuint)0x3FFFFFFF, ShiftOperatorsHelper.op_UnsignedRightShift((nuint)0x7FFFFFFF, 1)); + Assert.Equal((nuint)0x40000000, ShiftOperatorsHelper.op_UnsignedRightShift((nuint)0x80000000, 1)); + Assert.Equal((nuint)0x7FFFFFFF, ShiftOperatorsHelper.op_UnsignedRightShift((nuint)0xFFFFFFFF, 1)); + } + } + [Fact] public static void op_SubtractionTest() {