Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup rotate patterns #54099

Merged
merged 5 commits into from
Jul 25, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Threading;

Expand Down Expand Up @@ -45,10 +46,10 @@ private static int KeyToBucket(ref int tableData, nuint source, nuint target)

int hashShift = HashShift(ref tableData);
#if TARGET_64BIT
ulong hash = (((ulong)source << 32) | ((ulong)source >> 32)) ^ (ulong)target;
ulong hash = BitOperations.RotateLeft((ulong)source, 32) ^ (ulong)target;
return (int)((hash * 11400714819323198485ul) >> hashShift);
#else
uint hash = (((uint)source >> 16) | ((uint)source << 16)) ^ (uint)target;
uint hash = BitOperations.RotateLeft((uint)source, 16) ^ (uint)target;
return (int)((hash * 2654435769u) >> hashShift);
#endif
}
Expand Down
22 changes: 8 additions & 14 deletions src/coreclr/tools/Common/Internal/Text/Utf8String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;

Expand Down Expand Up @@ -42,38 +43,31 @@ public override bool Equals(object obj)
return (obj is Utf8String) && Equals((Utf8String)obj);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int _rotl(int value, int shift)
{
// This is expected to be optimized into a single rotl instruction
return (int)(((uint)value << shift) | ((uint)value >> (32 - shift)));
}

public unsafe override int GetHashCode()
{
int length = _value.Length;
int hash = length;
uint hash = (uint)length;
fixed (byte* ap = _value)
{
byte* a = ap;

while (length >= 4)
{
hash = (hash + _rotl(hash, 5)) ^ *(int*)a;
hash = (hash + BitOperations.RotateLeft(hash, 5)) ^ *(uint*)a;
a += 4; length -= 4;
}
if (length >= 2)
{
hash = (hash + _rotl(hash, 5)) ^ *(short*)a;
hash = (hash + BitOperations.RotateLeft(hash, 5)) ^ *(ushort*)a;
a += 2; length -= 2;
}
if (length > 0)
{
hash = (hash + _rotl(hash, 5)) ^ *a;
hash = (hash + BitOperations.RotateLeft(hash, 5)) ^ *a;
}
hash += _rotl(hash, 7);
hash += _rotl(hash, 15);
return hash;
hash += BitOperations.RotateLeft(hash, 7);
hash += BitOperations.RotateLeft(hash, 15);
return (int)hash;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Diagnostics;
using System.Numerics;
using System.Text;

using Internal.TypeSystem;
Expand Down Expand Up @@ -219,7 +220,7 @@ public static int ModuleNameHashCode(ModuleDesc module)
/// <param name="bitCount">Number of bits</param>
private static int RotateLeft(int value, int bitCount)
{
return unchecked((int)(((uint)value << bitCount) | ((uint)value >> (32 - bitCount))));
return (int)BitOperations.RotateLeft((uint)value, bitCount);
}

private static uint XXHash32_MixEmptyState()
Expand All @@ -231,7 +232,7 @@ private static uint XXHash32_MixEmptyState()

private static uint XXHash32_QueueRound(uint hash, uint queuedValue)
{
return ((uint)RotateLeft((int)(hash + queuedValue * 3266489917U/*Prime3*/), 17)) * 668265263U/*Prime4*/;
return (BitOperations.RotateLeft((hash + queuedValue * 3266489917U/*Prime3*/), 17)) * 668265263U/*Prime4*/;
}

private static uint XXHash32_MixFinal(uint hash)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
using System;
using System.IO;
using System.Diagnostics;
using System.Numerics;
using System.Security.Cryptography;
using static System.Numerics.BitOperations;

namespace Internal.Cryptography
{
Expand Down Expand Up @@ -342,11 +342,6 @@ private static unsafe void SHATransform(uint* expandedBuffer, uint* state, byte*
state[7] += h;
}

private static uint RotateRight(uint x, int n)
{
return (((x) >> (n)) | ((x) << (32 - (n))));
}

private static uint Ch(uint x, uint y, uint z)
{
return ((x & y) ^ ((x ^ 0xffffffff) & z));
Expand Down Expand Up @@ -917,11 +912,6 @@ private static unsafe void SHATransform(ulong* expandedBuffer, ulong* state, byt
state[7] += h;
}

private static ulong RotateRight(ulong x, int n)
{
return (((x) >> (n)) | ((x) << (64 - (n))));
}

private static ulong Ch(ulong x, ulong y, ulong z)
{
return ((x & y) ^ ((x ^ 0xffffffffffffffff) & z));
Expand Down