Skip to content

Commit

Permalink
The mask of shift amount in IShiftOperators<byte, int, byte> should…
Browse files Browse the repository at this point in the history
… be 7 (#103900)

* The mask of shift amount in IShiftOperators<byte, int, byte> should be 7

* Fix mask of shift amount

---------

Co-authored-by: Tanner Gooding <tagoo@outlook.com>
  • Loading branch information
skyoxZ and tannergooding authored Dec 3, 2024
1 parent 2b679cf commit b3ec8ee
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/libraries/System.Private.CoreLib/src/System/Byte.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1115,13 +1115,13 @@ static bool INumberBase<byte>.TryConvertToTruncating<TOther>(byte value, [MaybeN
//

/// <inheritdoc cref="IShiftOperators{TSelf, TOther, TResult}.op_LeftShift(TSelf, TOther)" />
static byte IShiftOperators<byte, int, byte>.operator <<(byte value, int shiftAmount) => (byte)(value << shiftAmount);
static byte IShiftOperators<byte, int, byte>.operator <<(byte value, int shiftAmount) => (byte)(value << (shiftAmount & 7));

/// <inheritdoc cref="IShiftOperators{TSelf, TOther, TResult}.op_RightShift(TSelf, TOther)" />
static byte IShiftOperators<byte, int, byte>.operator >>(byte value, int shiftAmount) => (byte)(value >> shiftAmount);
static byte IShiftOperators<byte, int, byte>.operator >>(byte value, int shiftAmount) => (byte)(value >> (shiftAmount & 7));

/// <inheritdoc cref="IShiftOperators{TSelf, TOther, TResult}.op_UnsignedRightShift(TSelf, TOther)" />
static byte IShiftOperators<byte, int, byte>.operator >>>(byte value, int shiftAmount) => (byte)(value >>> shiftAmount);
static byte IShiftOperators<byte, int, byte>.operator >>>(byte value, int shiftAmount) => (byte)(value >>> (shiftAmount & 7));

//
// ISpanParsable
Expand Down
6 changes: 3 additions & 3 deletions src/libraries/System.Private.CoreLib/src/System/Char.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1943,13 +1943,13 @@ static bool INumberBase<char>.TryConvertToTruncating<TOther>(char value, [MaybeN
//

/// <inheritdoc cref="IShiftOperators{TSelf, TOther, TResult}.op_LeftShift(TSelf, TOther)" />
static char IShiftOperators<char, int, char>.operator <<(char value, int shiftAmount) => (char)(value << shiftAmount);
static char IShiftOperators<char, int, char>.operator <<(char value, int shiftAmount) => (char)(value << (shiftAmount & 15));

/// <inheritdoc cref="IShiftOperators{TSelf, TOther, TResult}.op_RightShift(TSelf, TOther)" />
static char IShiftOperators<char, int, char>.operator >>(char value, int shiftAmount) => (char)(value >> shiftAmount);
static char IShiftOperators<char, int, char>.operator >>(char value, int shiftAmount) => (char)(value >> (shiftAmount & 15));

/// <inheritdoc cref="IShiftOperators{TSelf, TOther, TResult}.op_UnsignedRightShift(TSelf, TOther)" />
static char IShiftOperators<char, int, char>.operator >>>(char value, int shiftAmount) => (char)(value >>> shiftAmount);
static char IShiftOperators<char, int, char>.operator >>>(char value, int shiftAmount) => (char)(value >>> (shiftAmount & 15));

//
// ISpanParsable
Expand Down
6 changes: 3 additions & 3 deletions src/libraries/System.Private.CoreLib/src/System/Int16.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1322,13 +1322,13 @@ static bool INumberBase<short>.TryConvertToTruncating<TOther>(short value, [Mayb
//

/// <inheritdoc cref="IShiftOperators{TSelf, TOther, TResult}.op_LeftShift(TSelf, TOther)" />
static short IShiftOperators<short, int, short>.operator <<(short value, int shiftAmount) => (short)(value << shiftAmount);
static short IShiftOperators<short, int, short>.operator <<(short value, int shiftAmount) => (short)(value << (shiftAmount & 15));

/// <inheritdoc cref="IShiftOperators{TSelf, TOther, TResult}.op_RightShift(TSelf, TOther)" />
static short IShiftOperators<short, int, short>.operator >>(short value, int shiftAmount) => (short)(value >> shiftAmount);
static short IShiftOperators<short, int, short>.operator >>(short value, int shiftAmount) => (short)(value >> (shiftAmount & 15));

/// <inheritdoc cref="IShiftOperators{TSelf, TOther, TResult}.op_UnsignedRightShift(TSelf, TOther)" />
static short IShiftOperators<short, int, short>.operator >>>(short value, int shiftAmount) => (short)((ushort)value >>> shiftAmount);
static short IShiftOperators<short, int, short>.operator >>>(short value, int shiftAmount) => (short)((ushort)value >>> (shiftAmount & 15));

//
// ISignedNumber
Expand Down
6 changes: 3 additions & 3 deletions src/libraries/System.Private.CoreLib/src/System/SByte.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1283,13 +1283,13 @@ static bool INumberBase<sbyte>.TryConvertToTruncating<TOther>(sbyte value, [Mayb
//

/// <inheritdoc cref="IShiftOperators{TSelf, TOther, TResult}.op_LeftShift(TSelf, TOther)" />
static sbyte IShiftOperators<sbyte, int, sbyte>.operator <<(sbyte value, int shiftAmount) => (sbyte)(value << shiftAmount);
static sbyte IShiftOperators<sbyte, int, sbyte>.operator <<(sbyte value, int shiftAmount) => (sbyte)(value << (shiftAmount & 7));

/// <inheritdoc cref="IShiftOperators{TSelf, TOther, TResult}.op_RightShift(TSelf, TOther)" />
static sbyte IShiftOperators<sbyte, int, sbyte>.operator >>(sbyte value, int shiftAmount) => (sbyte)(value >> shiftAmount);
static sbyte IShiftOperators<sbyte, int, sbyte>.operator >>(sbyte value, int shiftAmount) => (sbyte)(value >> (shiftAmount & 7));

/// <inheritdoc cref="IShiftOperators{TSelf, TOther, TResult}.op_UnsignedRightShift(TSelf, TOther)" />
static sbyte IShiftOperators<sbyte, int, sbyte>.operator >>>(sbyte value, int shiftAmount) => (sbyte)((byte)value >>> shiftAmount);
static sbyte IShiftOperators<sbyte, int, sbyte>.operator >>>(sbyte value, int shiftAmount) => (sbyte)((byte)value >>> (shiftAmount & 7));

//
// ISignedNumber
Expand Down
6 changes: 3 additions & 3 deletions src/libraries/System.Private.CoreLib/src/System/UInt16.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1144,13 +1144,13 @@ static bool INumberBase<ushort>.TryConvertToTruncating<TOther>(ushort value, [Ma
//

/// <inheritdoc cref="IShiftOperators{TSelf, TOther, TResult}.op_LeftShift(TSelf, TOther)" />
static ushort IShiftOperators<ushort, int, ushort>.operator <<(ushort value, int shiftAmount) => (ushort)(value << shiftAmount);
static ushort IShiftOperators<ushort, int, ushort>.operator <<(ushort value, int shiftAmount) => (ushort)(value << (shiftAmount & 15));

/// <inheritdoc cref="IShiftOperators{TSelf, TOther, TResult}.op_RightShift(TSelf, TOther)" />
static ushort IShiftOperators<ushort, int, ushort>.operator >>(ushort value, int shiftAmount) => (ushort)(value >> shiftAmount);
static ushort IShiftOperators<ushort, int, ushort>.operator >>(ushort value, int shiftAmount) => (ushort)(value >> (shiftAmount & 15));

/// <inheritdoc cref="IShiftOperators{TSelf, TOther, TResult}.op_UnsignedRightShift(TSelf, TOther)" />
static ushort IShiftOperators<ushort, int, ushort>.operator >>>(ushort value, int shiftAmount) => (ushort)(value >>> shiftAmount);
static ushort IShiftOperators<ushort, int, ushort>.operator >>>(ushort value, int shiftAmount) => (ushort)(value >>> (shiftAmount & 15));

//
// ISpanParsable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2256,30 +2256,45 @@ public static void MinMagnitudeNumberTest()
public static void op_LeftShiftTest()
{
Assert.Equal((byte)0x00, ShiftOperatorsHelper<byte, int, byte>.op_LeftShift((byte)0x00, 1));
Assert.Equal((byte)0x00, ShiftOperatorsHelper<byte, int, byte>.op_LeftShift((byte)0x00, 9));
Assert.Equal((byte)0x02, ShiftOperatorsHelper<byte, int, byte>.op_LeftShift((byte)0x01, 1));
Assert.Equal((byte)0x02, ShiftOperatorsHelper<byte, int, byte>.op_LeftShift((byte)0x01, 9));
Assert.Equal((byte)0xFE, ShiftOperatorsHelper<byte, int, byte>.op_LeftShift((byte)0x7F, 1));
Assert.Equal((byte)0xFE, ShiftOperatorsHelper<byte, int, byte>.op_LeftShift((byte)0x7F, 9));
Assert.Equal((byte)0x00, ShiftOperatorsHelper<byte, int, byte>.op_LeftShift((byte)0x80, 1));
Assert.Equal((byte)0x00, ShiftOperatorsHelper<byte, int, byte>.op_LeftShift((byte)0x80, 9));
Assert.Equal((byte)0xFE, ShiftOperatorsHelper<byte, int, byte>.op_LeftShift((byte)0xFF, 1));
Assert.Equal((byte)0xFE, ShiftOperatorsHelper<byte, int, byte>.op_LeftShift((byte)0xFF, 9));
}

[Fact]
public static void op_RightShiftTest()
{
Assert.Equal((byte)0x00, ShiftOperatorsHelper<byte, int, byte>.op_RightShift((byte)0x00, 1));
Assert.Equal((byte)0x00, ShiftOperatorsHelper<byte, int, byte>.op_RightShift((byte)0x00, 9));
Assert.Equal((byte)0x00, ShiftOperatorsHelper<byte, int, byte>.op_RightShift((byte)0x01, 1));
Assert.Equal((byte)0x00, ShiftOperatorsHelper<byte, int, byte>.op_RightShift((byte)0x01, 9));
Assert.Equal((byte)0x3F, ShiftOperatorsHelper<byte, int, byte>.op_RightShift((byte)0x7F, 1));
Assert.Equal((byte)0x3F, ShiftOperatorsHelper<byte, int, byte>.op_RightShift((byte)0x7F, 9));
Assert.Equal((byte)0x40, ShiftOperatorsHelper<byte, int, byte>.op_RightShift((byte)0x80, 1));
Assert.Equal((byte)0x40, ShiftOperatorsHelper<byte, int, byte>.op_RightShift((byte)0x80, 9));
Assert.Equal((byte)0x7F, ShiftOperatorsHelper<byte, int, byte>.op_RightShift((byte)0xFF, 1));
Assert.Equal((byte)0x7F, ShiftOperatorsHelper<byte, int, byte>.op_RightShift((byte)0xFF, 9));
}

[Fact]
public static void op_UnsignedRightShiftTest()
{
Assert.Equal((byte)0x00, ShiftOperatorsHelper<byte, int, byte>.op_UnsignedRightShift((byte)0x00, 1));
Assert.Equal((byte)0x00, ShiftOperatorsHelper<byte, int, byte>.op_UnsignedRightShift((byte)0x00, 9));
Assert.Equal((byte)0x00, ShiftOperatorsHelper<byte, int, byte>.op_UnsignedRightShift((byte)0x01, 1));
Assert.Equal((byte)0x00, ShiftOperatorsHelper<byte, int, byte>.op_UnsignedRightShift((byte)0x01, 9));
Assert.Equal((byte)0x3F, ShiftOperatorsHelper<byte, int, byte>.op_UnsignedRightShift((byte)0x7F, 1));
Assert.Equal((byte)0x3F, ShiftOperatorsHelper<byte, int, byte>.op_UnsignedRightShift((byte)0x7F, 9));
Assert.Equal((byte)0x40, ShiftOperatorsHelper<byte, int, byte>.op_UnsignedRightShift((byte)0x80, 1));
Assert.Equal((byte)0x40, ShiftOperatorsHelper<byte, int, byte>.op_UnsignedRightShift((byte)0x80, 9));
Assert.Equal((byte)0x7F, ShiftOperatorsHelper<byte, int, byte>.op_UnsignedRightShift((byte)0xFF, 1));
Assert.Equal((byte)0x7F, ShiftOperatorsHelper<byte, int, byte>.op_UnsignedRightShift((byte)0xFF, 9));
}

//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2249,30 +2249,45 @@ public static void MinMagnitudeNumberTest()
public static void op_LeftShiftTest()
{
Assert.Equal((char)0x0000, ShiftOperatorsHelper<char, int, char>.op_LeftShift((char)0x0000, 1));
Assert.Equal((char)0x0000, ShiftOperatorsHelper<char, int, char>.op_LeftShift((char)0x0000, 17));
Assert.Equal((char)0x0002, ShiftOperatorsHelper<char, int, char>.op_LeftShift((char)0x0001, 1));
Assert.Equal((char)0x0002, ShiftOperatorsHelper<char, int, char>.op_LeftShift((char)0x0001, 17));
Assert.Equal((char)0xFFFE, ShiftOperatorsHelper<char, int, char>.op_LeftShift((char)0x7FFF, 1));
Assert.Equal((char)0xFFFE, ShiftOperatorsHelper<char, int, char>.op_LeftShift((char)0x7FFF, 17));
Assert.Equal((char)0x0000, ShiftOperatorsHelper<char, int, char>.op_LeftShift((char)0x8000, 1));
Assert.Equal((char)0x0000, ShiftOperatorsHelper<char, int, char>.op_LeftShift((char)0x8000, 17));
Assert.Equal((char)0xFFFE, ShiftOperatorsHelper<char, int, char>.op_LeftShift((char)0xFFFF, 1));
Assert.Equal((char)0xFFFE, ShiftOperatorsHelper<char, int, char>.op_LeftShift((char)0xFFFF, 17));
}

[Fact]
public static void op_RightShiftTest()
{
Assert.Equal((char)0x0000, ShiftOperatorsHelper<char, int, char>.op_RightShift((char)0x0000, 1));
Assert.Equal((char)0x0000, ShiftOperatorsHelper<char, int, char>.op_RightShift((char)0x0000, 17));
Assert.Equal((char)0x0000, ShiftOperatorsHelper<char, int, char>.op_RightShift((char)0x0001, 1));
Assert.Equal((char)0x0000, ShiftOperatorsHelper<char, int, char>.op_RightShift((char)0x0001, 17));
Assert.Equal((char)0x3FFF, ShiftOperatorsHelper<char, int, char>.op_RightShift((char)0x7FFF, 1));
Assert.Equal((char)0x3FFF, ShiftOperatorsHelper<char, int, char>.op_RightShift((char)0x7FFF, 17));
Assert.Equal((char)0x4000, ShiftOperatorsHelper<char, int, char>.op_RightShift((char)0x8000, 1));
Assert.Equal((char)0x4000, ShiftOperatorsHelper<char, int, char>.op_RightShift((char)0x8000, 17));
Assert.Equal((char)0x7FFF, ShiftOperatorsHelper<char, int, char>.op_RightShift((char)0xFFFF, 1));
Assert.Equal((char)0x7FFF, ShiftOperatorsHelper<char, int, char>.op_RightShift((char)0xFFFF, 17));
}

[Fact]
public static void op_UnsignedRightShiftTest()
{
Assert.Equal((char)0x0000, ShiftOperatorsHelper<char, int, char>.op_UnsignedRightShift((char)0x0000, 1));
Assert.Equal((char)0x0000, ShiftOperatorsHelper<char, int, char>.op_UnsignedRightShift((char)0x0000, 17));
Assert.Equal((char)0x0000, ShiftOperatorsHelper<char, int, char>.op_UnsignedRightShift((char)0x0001, 1));
Assert.Equal((char)0x0000, ShiftOperatorsHelper<char, int, char>.op_UnsignedRightShift((char)0x0001, 17));
Assert.Equal((char)0x3FFF, ShiftOperatorsHelper<char, int, char>.op_UnsignedRightShift((char)0x7FFF, 1));
Assert.Equal((char)0x3FFF, ShiftOperatorsHelper<char, int, char>.op_UnsignedRightShift((char)0x7FFF, 17));
Assert.Equal((char)0x4000, ShiftOperatorsHelper<char, int, char>.op_UnsignedRightShift((char)0x8000, 1));
Assert.Equal((char)0x4000, ShiftOperatorsHelper<char, int, char>.op_UnsignedRightShift((char)0x8000, 17));
Assert.Equal((char)0x7FFF, ShiftOperatorsHelper<char, int, char>.op_UnsignedRightShift((char)0xFFFF, 1));
Assert.Equal((char)0x7FFF, ShiftOperatorsHelper<char, int, char>.op_UnsignedRightShift((char)0xFFFF, 17));
}

//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2275,30 +2275,45 @@ public static void MinMagnitudeNumberTest()
public static void op_LeftShiftTest()
{
Assert.Equal((short)0x0000, ShiftOperatorsHelper<short, int, short>.op_LeftShift((short)0x0000, 1));
Assert.Equal((short)0x0000, ShiftOperatorsHelper<short, int, short>.op_LeftShift((short)0x0000, 17));
Assert.Equal((short)0x0002, ShiftOperatorsHelper<short, int, short>.op_LeftShift((short)0x0001, 1));
Assert.Equal((short)0x0002, ShiftOperatorsHelper<short, int, short>.op_LeftShift((short)0x0001, 17));
Assert.Equal(unchecked((short)0xFFFE), ShiftOperatorsHelper<short, int, short>.op_LeftShift((short)0x7FFF, 1));
Assert.Equal(unchecked((short)0xFFFE), ShiftOperatorsHelper<short, int, short>.op_LeftShift((short)0x7FFF, 17));
Assert.Equal((short)0x0000, ShiftOperatorsHelper<short, int, short>.op_LeftShift(unchecked((short)0x8000), 1));
Assert.Equal((short)0x0000, ShiftOperatorsHelper<short, int, short>.op_LeftShift(unchecked((short)0x8000), 17));
Assert.Equal(unchecked((short)0xFFFE), ShiftOperatorsHelper<short, int, short>.op_LeftShift(unchecked((short)0xFFFF), 1));
Assert.Equal(unchecked((short)0xFFFE), ShiftOperatorsHelper<short, int, short>.op_LeftShift(unchecked((short)0xFFFF), 17));
}

[Fact]
public static void op_RightShiftTest()
{
Assert.Equal((short)0x0000, ShiftOperatorsHelper<short, int, short>.op_RightShift((short)0x0000, 1));
Assert.Equal((short)0x0000, ShiftOperatorsHelper<short, int, short>.op_RightShift((short)0x0000, 17));
Assert.Equal((short)0x0000, ShiftOperatorsHelper<short, int, short>.op_RightShift((short)0x0001, 1));
Assert.Equal((short)0x0000, ShiftOperatorsHelper<short, int, short>.op_RightShift((short)0x0001, 17));
Assert.Equal((short)0x3FFF, ShiftOperatorsHelper<short, int, short>.op_RightShift((short)0x7FFF, 1));
Assert.Equal((short)0x3FFF, ShiftOperatorsHelper<short, int, short>.op_RightShift((short)0x7FFF, 17));
Assert.Equal(unchecked((short)0xC000), ShiftOperatorsHelper<short, int, short>.op_RightShift(unchecked((short)0x8000), 1));
Assert.Equal(unchecked((short)0xC000), ShiftOperatorsHelper<short, int, short>.op_RightShift(unchecked((short)0x8000), 17));
Assert.Equal(unchecked((short)0xFFFF), ShiftOperatorsHelper<short, int, short>.op_RightShift(unchecked((short)0xFFFF), 1));
Assert.Equal(unchecked((short)0xFFFF), ShiftOperatorsHelper<short, int, short>.op_RightShift(unchecked((short)0xFFFF), 17));
}

[Fact]
public static void op_UnsignedRightShiftTest()
{
Assert.Equal((short)0x0000, ShiftOperatorsHelper<short, int, short>.op_UnsignedRightShift((short)0x0000, 1));
Assert.Equal((short)0x0000, ShiftOperatorsHelper<short, int, short>.op_UnsignedRightShift((short)0x0000, 17));
Assert.Equal((short)0x0000, ShiftOperatorsHelper<short, int, short>.op_UnsignedRightShift((short)0x0001, 1));
Assert.Equal((short)0x0000, ShiftOperatorsHelper<short, int, short>.op_UnsignedRightShift((short)0x0001, 17));
Assert.Equal((short)0x3FFF, ShiftOperatorsHelper<short, int, short>.op_UnsignedRightShift((short)0x7FFF, 1));
Assert.Equal((short)0x3FFF, ShiftOperatorsHelper<short, int, short>.op_UnsignedRightShift((short)0x7FFF, 17));
Assert.Equal((short)0x4000, ShiftOperatorsHelper<short, int, short>.op_UnsignedRightShift(unchecked((short)0x8000), 1));
Assert.Equal((short)0x4000, ShiftOperatorsHelper<short, int, short>.op_UnsignedRightShift(unchecked((short)0x8000), 17));
Assert.Equal((short)0x7FFF, ShiftOperatorsHelper<short, int, short>.op_UnsignedRightShift(unchecked((short)0xFFFF), 1));
Assert.Equal((short)0x7FFF, ShiftOperatorsHelper<short, int, short>.op_UnsignedRightShift(unchecked((short)0xFFFF), 17));
}

//
Expand Down
Loading

0 comments on commit b3ec8ee

Please # to comment.