Skip to content
This repository was archived by the owner on Nov 1, 2020. It is now read-only.
Closed
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
28 changes: 11 additions & 17 deletions src/Common/src/Internal/NativeFormat/TypeHashingAlgorithms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ namespace Internal.NativeFormat
{
static class TypeHashingAlgorithms
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int _rotl(int value, int shift)
{
return (int)(((uint)value << shift) | ((uint)value >> (32 - shift)));
}

//
// Returns the hashcode value of the 'src' string
//
Expand All @@ -32,13 +26,13 @@ public static int ComputeNameHashCode(string src)

for (int i = 0; i < src.Length; i += 2)
{
hash1 = (hash1 + _rotl(hash1, 5)) ^ src[i];
hash1 = (hash1 + BitOps.RotateLeft(hash1, 5)) ^ src[i];
if ((i + 1) < src.Length)
hash2 = (hash2 + _rotl(hash2, 5)) ^ src[i + 1];
hash2 = (hash2 + BitOps.RotateLeft(hash2, 5)) ^ src[i + 1];
}

hash1 += _rotl(hash1, 8);
hash2 += _rotl(hash2, 8);
hash1 += BitOps.RotateLeft(hash1, 8);
hash2 += BitOps.RotateLeft(hash2, 8);

return hash1 ^ hash2;
}
Expand Down Expand Up @@ -87,8 +81,8 @@ public static int ComputeArrayTypeHashCode(int elementTypeHashcode, int rank)
hashCode = ComputeNameHashCode("System.MDArrayRank" + IntToString(rank) + "`1");
}

hashCode = (hashCode + _rotl(hashCode, 13)) ^ elementTypeHashcode;
return (hashCode + _rotl(hashCode, 15));
hashCode = (hashCode + BitOps.RotateLeft(hashCode, 13)) ^ elementTypeHashcode;
return (hashCode + BitOps.RotateLeft(hashCode, 15));
}

public static int ComputeArrayTypeHashCode<T>(T elementType, int rank)
Expand All @@ -99,7 +93,7 @@ public static int ComputeArrayTypeHashCode<T>(T elementType, int rank)

public static int ComputePointerTypeHashCode(int pointeeTypeHashcode)
{
return (pointeeTypeHashcode + _rotl(pointeeTypeHashcode, 5)) ^ 0x12D0;
return (pointeeTypeHashcode + BitOps.RotateLeft(pointeeTypeHashcode, 5)) ^ 0x12D0;
}

public static int ComputePointerTypeHashCode<T>(T pointeeType)
Expand All @@ -110,7 +104,7 @@ public static int ComputePointerTypeHashCode<T>(T pointeeType)

public static int ComputeByrefTypeHashCode(int parameterTypeHashcode)
{
return (parameterTypeHashcode + _rotl(parameterTypeHashcode, 7)) ^ 0x4C85;
return (parameterTypeHashcode + BitOps.RotateLeft(parameterTypeHashcode, 7)) ^ 0x4C85;
}

public static int ComputeByrefTypeHashCode<T>(T parameterType)
Expand All @@ -121,7 +115,7 @@ public static int ComputeByrefTypeHashCode<T>(T parameterType)

public static int ComputeNestedTypeHashCode(int enclosingTypeHashcode, int nestedTypeNameHash)
{
return (enclosingTypeHashcode + _rotl(enclosingTypeHashcode, 11)) ^ nestedTypeNameHash;
return (enclosingTypeHashcode + BitOps.RotateLeft(enclosingTypeHashcode, 11)) ^ nestedTypeNameHash;
}


Expand All @@ -131,9 +125,9 @@ public static int ComputeGenericInstanceHashCode<ARG>(int genericDefinitionHashC
for (int i = 0; i < genericTypeArguments.Length; i++)
{
int argumentHashCode = genericTypeArguments[i].GetHashCode();
hashcode = (hashcode + _rotl(hashcode, 13)) ^ argumentHashCode;
hashcode = (hashcode + BitOps.RotateLeft(hashcode, 13)) ^ argumentHashCode;
}
return (hashcode + _rotl(hashcode, 15));
return (hashcode + BitOps.RotateLeft(hashcode, 15));
}
}
}
17 changes: 5 additions & 12 deletions src/Common/src/Internal/Text/Utf8String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ 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;
Expand All @@ -60,20 +53,20 @@ public unsafe override int GetHashCode()

while (length >= 4)
{
hash = (hash + _rotl(hash, 5)) ^ *(int*)a;
hash = (hash + BitOps.RotateLeft(hash, 5)) ^ *(int*)a;
a += 4; length -= 4;
}
if (length >= 2)
{
hash = (hash + _rotl(hash, 5)) ^ *(short*)a;
hash = (hash + BitOps.RotateLeft(hash, 5)) ^ *(short*)a;
a += 2; length -= 2;
}
if (length > 0)
{
hash = (hash + _rotl(hash, 5)) ^ *a;
hash = (hash + BitOps.RotateLeft(hash, 5)) ^ *a;
}
hash += _rotl(hash, 7);
hash += _rotl(hash, 15);
hash += BitOps.RotateLeft(hash, 7);
hash += BitOps.RotateLeft(hash, 15);
return hash;
}
}
Expand Down
50 changes: 22 additions & 28 deletions src/Common/src/TypeSystem/Common/TypeHashingAlgorithms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,35 +38,29 @@ public void Append(string src)
int startIndex = 0;
if ((_numCharactersHashed & 1) == 1)
{
_hash2 = (_hash2 + _rotl(_hash2, 5)) ^ src[0];
_hash2 = (_hash2 + BitOps.RotateLeft(_hash2, 5)) ^ src[0];
startIndex = 1;
}

for (int i = startIndex; i < src.Length; i += 2)
{
_hash1 = (_hash1 + _rotl(_hash1, 5)) ^ src[i];
_hash1 = (_hash1 + BitOps.RotateLeft(_hash1, 5)) ^ src[i];
if ((i + 1) < src.Length)
_hash2 = (_hash2 + _rotl(_hash2, 5)) ^ src[i + 1];
_hash2 = (_hash2 + BitOps.RotateLeft(_hash2, 5)) ^ src[i + 1];
}

_numCharactersHashed += src.Length;
}

public int ToHashCode()
{
int hash1 = _hash1 + _rotl(_hash1, 8);
int hash2 = _hash2 + _rotl(_hash2, 8);
int hash1 = _hash1 + BitOps.RotateLeft(_hash1, 8);
int hash2 = _hash2 + BitOps.RotateLeft(_hash2, 8);

return hash1 ^ hash2;
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int _rotl(int value, int shift)
{
return (int)(((uint)value << shift) | ((uint)value >> (32 - shift)));
}

//
// Returns the hashcode value of the 'src' string
//
Expand All @@ -77,13 +71,13 @@ public static int ComputeNameHashCode(string src)

for (int i = 0; i < src.Length; i += 2)
{
hash1 = (hash1 + _rotl(hash1, 5)) ^ src[i];
hash1 = (hash1 + BitOps.RotateLeft(hash1, 5)) ^ src[i];
if ((i + 1) < src.Length)
hash2 = (hash2 + _rotl(hash2, 5)) ^ src[i + 1];
hash2 = (hash2 + BitOps.RotateLeft(hash2, 5)) ^ src[i + 1];
}

hash1 += _rotl(hash1, 8);
hash2 += _rotl(hash2, 8);
hash1 += BitOps.RotateLeft(hash1, 8);
hash2 += BitOps.RotateLeft(hash2, 8);

return hash1 ^ hash2;
}
Expand All @@ -98,17 +92,17 @@ public static unsafe int ComputeASCIINameHashCode(byte* data, int length, out bo
{
int b1 = data[i];
asciiMask |= b1;
hash1 = (hash1 + _rotl(hash1, 5)) ^ b1;
hash1 = (hash1 + BitOps.RotateLeft(hash1, 5)) ^ b1;
if ((i + 1) < length)
{
int b2 = data[i];
asciiMask |= b2;
hash2 = (hash2 + _rotl(hash2, 5)) ^ b2;
hash2 = (hash2 + BitOps.RotateLeft(hash2, 5)) ^ b2;
}
}

hash1 += _rotl(hash1, 8);
hash2 += _rotl(hash2, 8);
hash1 += BitOps.RotateLeft(hash1, 8);
hash2 += BitOps.RotateLeft(hash2, 8);

isAscii = (asciiMask & 0x80) == 0;

Expand Down Expand Up @@ -159,8 +153,8 @@ public static int ComputeArrayTypeHashCode(int elementTypeHashCode, int rank)
hashCode = ComputeNameHashCode("System.MDArrayRank" + IntToString(rank) + "`1");
}

hashCode = (hashCode + _rotl(hashCode, 13)) ^ elementTypeHashCode;
return (hashCode + _rotl(hashCode, 15));
hashCode = (hashCode + BitOps.RotateLeft(hashCode, 13)) ^ elementTypeHashCode;
return (hashCode + BitOps.RotateLeft(hashCode, 15));
}

public static int ComputeArrayTypeHashCode<T>(T elementType, int rank)
Expand All @@ -171,7 +165,7 @@ public static int ComputeArrayTypeHashCode<T>(T elementType, int rank)

public static int ComputePointerTypeHashCode(int pointeeTypeHashCode)
{
return (pointeeTypeHashCode + _rotl(pointeeTypeHashCode, 5)) ^ 0x12D0;
return (pointeeTypeHashCode + BitOps.RotateLeft(pointeeTypeHashCode, 5)) ^ 0x12D0;
}

public static int ComputePointerTypeHashCode<T>(T pointeeType)
Expand All @@ -182,7 +176,7 @@ public static int ComputePointerTypeHashCode<T>(T pointeeType)

public static int ComputeByrefTypeHashCode(int parameterTypeHashCode)
{
return (parameterTypeHashCode + _rotl(parameterTypeHashCode, 7)) ^ 0x4C85;
return (parameterTypeHashCode + BitOps.RotateLeft(parameterTypeHashCode, 7)) ^ 0x4C85;
}

public static int ComputeByrefTypeHashCode<T>(T parameterType)
Expand All @@ -193,7 +187,7 @@ public static int ComputeByrefTypeHashCode<T>(T parameterType)

public static int ComputeNestedTypeHashCode(int enclosingTypeHashCode, int nestedTypeNameHash)
{
return (enclosingTypeHashCode + _rotl(enclosingTypeHashCode, 11)) ^ nestedTypeNameHash;
return (enclosingTypeHashCode + BitOps.RotateLeft(enclosingTypeHashCode, 11)) ^ nestedTypeNameHash;
}


Expand All @@ -203,9 +197,9 @@ public static int ComputeGenericInstanceHashCode<ARG>(int genericDefinitionHashC
for (int i = 0; i < genericTypeArguments.Length; i++)
{
int argumentHashCode = genericTypeArguments[i].GetHashCode();
hashcode = (hashcode + _rotl(hashcode, 13)) ^ argumentHashCode;
hashcode = (hashcode + BitOps.RotateLeft(hashcode, 13)) ^ argumentHashCode;
}
return (hashcode + _rotl(hashcode, 15));
return (hashcode + BitOps.RotateLeft(hashcode, 15));
}

public static int ComputeMethodSignatureHashCode<ARG>(int returnTypeHashCode, ARG[] parameters)
Expand All @@ -217,9 +211,9 @@ public static int ComputeMethodSignatureHashCode<ARG>(int returnTypeHashCode, AR
for (int i = 0; i < parameters.Length; i++)
{
int parameterHashCode = parameters[i].GetHashCode();
hashcode = (hashcode + _rotl(hashcode, 13)) ^ parameterHashCode;
hashcode = (hashcode + BitOps.RotateLeft(hashcode, 13)) ^ parameterHashCode;
}
return (hashcode + _rotl(hashcode, 15));
return (hashcode + BitOps.RotateLeft(hashcode, 15));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,20 +341,13 @@ public bool Equals(VertexSequenceKey other)
return true;
}

[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 override int GetHashCode()
{
int hashcode = 0;
foreach (NativeLayoutVertexNode node in Vertices)
{
hashcode ^= node.GetHashCode();
hashcode = _rotl(hashcode, 5);
hashcode = BitOps.RotateLeft(hashcode, 5);
}
return hashcode;
}
Expand All @@ -381,20 +374,13 @@ bool IEqualityComparer<List<uint>>.Equals(List<uint> x, List<uint> y)
return true;
}

[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)));
}

int IEqualityComparer<List<uint>>.GetHashCode(List<uint> obj)
{
int hashcode = 0x42284781;
foreach (uint u in obj)
{
hashcode ^= (int)u;
hashcode = _rotl(hashcode, 5);
hashcode = BitOps.RotateLeft(hashcode, 5);
}

return hashcode;
Expand Down
32 changes: 11 additions & 21 deletions src/ILCompiler.ReadyToRun/src/Compiler/ReadyToRunHashCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ public static int NameHashCode(string name)
byte[] src = Encoding.UTF8.GetBytes(name);
for (int i = 0; i < src.Length; i += 2)
{
hash1 = unchecked(hash1 + RotateLeft(hash1, 5)) ^ src[i];
hash1 = unchecked(hash1 + BitOps.RotateLeft(hash1, 5)) ^ src[i];
if (i + 1 < src.Length)
{
hash2 = unchecked(hash2 + RotateLeft(hash2, 5)) ^ src[i + 1];
hash2 = unchecked(hash2 + BitOps.RotateLeft(hash2, 5)) ^ src[i + 1];
}
else
{
break;
}
}

hash1 = unchecked(hash1 + RotateLeft(hash1, 8));
hash2 = unchecked(hash2 + RotateLeft(hash2, 8));
hash1 = unchecked(hash1 + BitOps.RotateLeft(hash1, 8));
hash2 = unchecked(hash2 + BitOps.RotateLeft(hash2, 8));

return unchecked((int)(hash1 ^ hash2));
}
Expand Down Expand Up @@ -130,7 +130,7 @@ public static int TypeHashCode(TypeDesc type)
/// <param name="nestedTypeNameHash">Hash code of the nested type name</param>
private static int NestedTypeHashCode(int enclosingTypeHashcode, int nestedTypeNameHash)
{
return unchecked(enclosingTypeHashcode + RotateLeft(enclosingTypeHashcode, 11)) ^ nestedTypeNameHash;
return unchecked(enclosingTypeHashcode + BitOps.RotateLeft(enclosingTypeHashcode, 11)) ^ nestedTypeNameHash;
}

/// <summary>
Expand All @@ -147,8 +147,8 @@ private static int ArrayTypeHashCode(int elementTypeHashcode, int rank)
{
Debug.Assert(hashCode == NameHashCode("System.Array`1"));
}
hashCode = unchecked(hashCode + RotateLeft(hashCode, 13)) ^ elementTypeHashcode;
return unchecked(hashCode + RotateLeft(hashCode, 15));
hashCode = unchecked(hashCode + BitOps.RotateLeft(hashCode, 13)) ^ elementTypeHashcode;
return unchecked(hashCode + BitOps.RotateLeft(hashCode, 15));
}

/// <summary>
Expand All @@ -157,7 +157,7 @@ private static int ArrayTypeHashCode(int elementTypeHashcode, int rank)
/// <param name="pointeeTypeHashcode">Hash code of the pointee type</param>
private static int PointerTypeHashCode(int pointeeTypeHashcode)
{
return unchecked(pointeeTypeHashcode + RotateLeft(pointeeTypeHashcode, 5)) ^ 0x12D0;
return unchecked(pointeeTypeHashcode + BitOps.RotateLeft(pointeeTypeHashcode, 5)) ^ 0x12D0;
}

/// <summary>
Expand All @@ -166,7 +166,7 @@ private static int PointerTypeHashCode(int pointeeTypeHashcode)
/// <param name="parameterTypeHashCode">Hash code representing the parameter type</param>
private static int ByrefTypeHashCode(int parameterTypeHashcode)
{
return unchecked(parameterTypeHashcode + RotateLeft(parameterTypeHashcode, 7)) ^ 0x4C85;
return unchecked(parameterTypeHashcode + BitOps.RotateLeft(parameterTypeHashcode, 7)) ^ 0x4C85;
}

/// <summary>
Expand All @@ -179,9 +179,9 @@ private static int GenericInstanceHashCode(int hashcode, Instantiation instantia
for (int i = 0; i < instantiation.Length; i++)
{
int argumentHashCode = TypeHashCode(instantiation[i]);
hashcode = unchecked(hashcode + RotateLeft(hashcode, 13)) ^ argumentHashCode;
hashcode = unchecked(hashcode + BitOps.RotateLeft(hashcode, 13)) ^ argumentHashCode;
}
return unchecked(hashcode + RotateLeft(hashcode, 15));
return unchecked(hashcode + BitOps.RotateLeft(hashcode, 15));
}

/// <summary>
Expand All @@ -205,15 +205,5 @@ public static int MethodHashCode(MethodDesc method)

return hashCode;
}

/// <summary>
/// Bitwise left 32-bit rotation with wraparound.
/// </summary>
/// <param name="value">Value to rotate</param>
/// <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))));
}
}
}
Loading