Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/coreclr/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12371,7 +12371,7 @@ GenTree* Compiler::fgRecognizeAndMorphBitwiseRotation(GenTree* tree)
// N == bitsize(x)
// M is const
// M & (N - 1) == N - 1
// op is either | or ^
// op is | for variable counts, and either | or ^ for constant counts

if (((tree->gtFlags & GTF_PERSISTENT_SIDE_EFFECTS) != 0) || ((tree->gtFlags & GTF_ORDER_SIDEEFF) != 0))
{
Expand Down Expand Up @@ -12479,6 +12479,13 @@ GenTree* Compiler::fgRecognizeAndMorphBitwiseRotation(GenTree* tree)

if (shiftIndexWithAdd != nullptr)
{
if (oper == GT_XOR)
{
// When the effective shift count is zero, both shifts yield the original value,
// so XOR yields zero rather than the value produced by a rotation.
return nullptr;
}

if (shiftIndexWithAdd->gtGetOp2()->IsCnsIntOrI())
{
if (shiftIndexWithAdd->gtGetOp2()->AsIntCon()->IconValue() == rotatedValueBitSize)
Expand Down
77 changes: 76 additions & 1 deletion src/tests/JIT/CodeGenBringUpTests/Rotate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,19 @@ static uint rol32const()
[MethodImpl(MethodImplOptions.NoInlining)]
static uint rol32xor(uint value, int amount)
{
return (value << amount) ^ (value >> (32 - amount));
return (value << (amount & 31)) ^ (value >> ((32 - amount) & 31));
}

[MethodImpl(MethodImplOptions.NoInlining)]
static uint ror32xor(uint value, int amount)
{
return (value >> (amount & 31)) ^ (value << ((32 - amount) & 31));
}

[MethodImpl(MethodImplOptions.NoInlining)]
static uint rol32xor_1(uint value)
{
return (value << 1) ^ (value >> 31);
}

[MethodImpl(MethodImplOptions.NoInlining)]
Expand Down Expand Up @@ -93,6 +105,24 @@ static ulong rol64(ulong value, int amount)
return (value << amount) | (value >> (64 - amount));
}

[MethodImpl(MethodImplOptions.NoInlining)]
static ulong rol64xor(ulong value, int amount)
{
return (value << (amount & 63)) ^ (value >> ((64 - amount) & 63));
}

[MethodImpl(MethodImplOptions.NoInlining)]
static ulong ror64xor(ulong value, int amount)
{
return (value >> (amount & 63)) ^ (value << ((64 - amount) & 63));
}

[MethodImpl(MethodImplOptions.NoInlining)]
static ulong ror64xor_1(ulong value)
{
return (value >> 1) ^ (value << 63);
}

[MethodImpl(MethodImplOptions.NoInlining)]
static ulong rol64comm(ulong value, int amount)
{
Expand Down Expand Up @@ -379,6 +409,16 @@ public static int TestEntryPoint()
return Fail;
}

if (rol32xor_1(0x89abcdef) != 0x13579bdf)
{
return Fail;
}

if (ror64xor_1(0x123456789abcdef) != 0x8091a2b3c4d5e6f7)
{
return Fail;
}

if (ror64sfield(7) != 0xde02468acf13579b)
{
return Fail;
Expand All @@ -403,4 +443,39 @@ public static int TestEntryPoint()

return Pass;
}

[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(31)]
[InlineData(32)]
[InlineData(33)]
[InlineData(63)]
[InlineData(64)]
[InlineData(65)]
[InlineData(-1)]
[InlineData(-32)]
[InlineData(-64)]
[InlineData(int.MinValue)]
[InlineData(int.MaxValue)]
public static void TestXor(int amount)
{
const uint Value32 = 0x89abcdef;
const ulong Value64 = 0xfedcba9876543210;

uint left32 = uint.RotateLeft(Value32, amount);
uint right32 = uint.RotateRight(Value32, amount);
ulong left64 = ulong.RotateLeft(Value64, amount);
ulong right64 = ulong.RotateRight(Value64, amount);

Assert.Equal(left32, rol32(Value32, amount));
Assert.Equal(right32, ror32(Value32, amount));
Assert.Equal(left64, rol64(Value64, amount));
Assert.Equal(right64, ror64(Value64, amount));

Assert.Equal((amount & 31) == 0 ? 0 : left32, rol32xor(Value32, amount));
Assert.Equal((amount & 31) == 0 ? 0 : right32, ror32xor(Value32, amount));
Assert.Equal((amount & 63) == 0 ? 0 : left64, rol64xor(Value64, amount));
Assert.Equal((amount & 63) == 0 ? 0 : right64, ror64xor(Value64, amount));
}
}
Loading