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

Use BitOperations in couple more places #101701

Merged
merged 26 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
92f155c
Use BitOperations.Log2 in EETypeBuilderHelpers
PaulusParssinen Apr 29, 2024
741fd54
Use BitOperations.LeadingZeroCount in NativeFormatWriter
PaulusParssinen Apr 29, 2024
d10a145
Clarify operator precedence
PaulusParssinen Apr 29, 2024
92b2395
Merge branch 'dotnet:main' into use-bitoperations-more
PaulusParssinen May 6, 2024
1eed107
It's doing LZCNT, not Log2..
PaulusParssinen May 9, 2024
102d4b3
Merge branch 'main' into use-bitoperations-more
PaulusParssinen May 9, 2024
c14d86e
I'm blind
PaulusParssinen May 10, 2024
c423d1f
LZCNT -> TZCNT..
PaulusParssinen May 10, 2024
db3eec9
Use BitOperations.RotateLeft in NodeFactory.NativeLayout.cs
PaulusParssinen May 11, 2024
3e7de82
Add required cast
PaulusParssinen May 11, 2024
ba86069
Replace one more rotate with BitOperations in NativeLayout
PaulusParssinen May 11, 2024
447c5b2
Merge branch 'main' into use-bitoperations-more
PaulusParssinen May 13, 2024
1bcc371
Merge branch 'dotnet:main' into use-bitoperations-more
PaulusParssinen May 17, 2024
a85a69b
Use BitOperations.RotateLeft in the NAOT/CoreLib OpenMethodResolver.cs
PaulusParssinen May 17, 2024
d7b9767
Use BitOperations.RotateLeft in the NAOT/CoreLib RuntimeFieldHandle.cs
PaulusParssinen May 17, 2024
efcae80
Use BitOperations.RotateLeft in the NAOT/CoreLib RuntimeMethodHandle.cs
PaulusParssinen May 17, 2024
27ddd40
Use BitOperations.RotateLeft in the shared TypeHashingAlgorithms.cs
PaulusParssinen May 17, 2024
50ee219
Use nuint overload of BitOperations.RotateLeft in the shared CoreLib …
PaulusParssinen May 17, 2024
6049c07
Make ILVerification target NetCoreAppToolCurrent
PaulusParssinen May 17, 2024
e985569
Apply PR feedback
PaulusParssinen May 17, 2024
8c104e0
Revert TypeHashingAlgorithms.cs
PaulusParssinen May 18, 2024
0d05e17
Revert "Make ILVerification target NetCoreAppToolCurrent"
PaulusParssinen May 18, 2024
a3bb921
Merge branch 'dotnet:main' into use-bitoperations-more
PaulusParssinen May 18, 2024
1f7a788
Merge branch 'dotnet:main' into use-bitoperations-more
PaulusParssinen May 23, 2024
b49a450
Merge branch 'dotnet:main' into use-bitoperations-more
PaulusParssinen May 30, 2024
406b903
Merge branch 'dotnet:main' into use-bitoperations-more
PaulusParssinen Jun 1, 2024
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 @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using System.Runtime;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -179,26 +180,20 @@ public static IntPtr ResolveMethod(IntPtr resolverPtr, RuntimeTypeHandle thisTyp
return RuntimeImports.RhResolveDispatchOnType(thisType.ToMethodTable(), resolver->_declaringType, (ushort)resolver->_methodHandleOrSlotOrCodePointer);
}

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

private static int CalcHashCode(int hashCode1, int hashCode2, int hashCode3, int hashCode4)
{
int length = 4;

int hash1 = 0x449b3ad6;
int hash2 = (length << 3) + 0x55399219;

hash1 = (hash1 + _rotl(hash1, 5)) ^ hashCode1;
hash2 = (hash2 + _rotl(hash2, 5)) ^ hashCode2;
hash1 = (hash1 + _rotl(hash1, 5)) ^ hashCode3;
hash2 = (hash2 + _rotl(hash2, 5)) ^ hashCode4;
hash1 = (hash1 + (int)BitOperations.RotateLeft((uint)hash1, 5)) ^ hashCode1;
hash2 = (hash2 + (int)BitOperations.RotateLeft((uint)hash2, 5)) ^ hashCode2;
hash1 = (hash1 + (int)BitOperations.RotateLeft((uint)hash1, 5)) ^ hashCode3;
hash2 = (hash2 + (int)BitOperations.RotateLeft((uint)hash2, 5)) ^ hashCode4;

hash1 += _rotl(hash1, 8);
hash2 += _rotl(hash2, 8);
hash1 += (int)BitOperations.RotateLeft((uint)hash1, 8);
hash2 += (int)BitOperations.RotateLeft((uint)hash2, 8);
PaulusParssinen marked this conversation as resolved.
Show resolved Hide resolved

return hash1 ^ hash2;
}
Expand Down
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.ComponentModel;
using System.Numerics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -48,12 +49,6 @@ public bool Equals(RuntimeFieldHandle handle)
return declaringType1.Equals(declaringType2) && fieldName1 == fieldName2;
}

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

public override int GetHashCode()
{
if (_value == IntPtr.Zero)
Expand All @@ -64,7 +59,7 @@ public override int GetHashCode()
RuntimeAugments.TypeLoaderCallbacks.GetRuntimeFieldHandleComponents(this, out declaringType, out fieldName);

int hashcode = declaringType.GetHashCode();
return (hashcode + _rotl(hashcode, 13)) ^ fieldName.GetHashCode();
return (hashcode + (int)BitOperations.RotateLeft((uint)hashcode, 13)) ^ fieldName.GetHashCode();
PaulusParssinen marked this conversation as resolved.
Show resolved Hide resolved
}

public static RuntimeFieldHandle FromIntPtr(IntPtr value) => new RuntimeFieldHandle(value);
Expand Down
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.ComponentModel;
using System.Numerics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -68,12 +69,6 @@ public bool Equals(RuntimeMethodHandle handle)
return true;
}

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

public override int GetHashCode()
{
if (_value == IntPtr.Zero)
Expand All @@ -85,13 +80,13 @@ public override int GetHashCode()
RuntimeAugments.TypeLoaderCallbacks.GetRuntimeMethodHandleComponents(this, out declaringType, out nameAndSignature, out genericArgs);

int hashcode = declaringType.GetHashCode();
hashcode = (hashcode + _rotl(hashcode, 13)) ^ nameAndSignature.Name.GetHashCode();
hashcode = (hashcode + (int)BitOperations.RotateLeft((uint)hashcode, 13)) ^ nameAndSignature.Name.GetHashCode();
if (genericArgs != null)
{
for (int i = 0; i < genericArgs.Length; i++)
{
int argumentHashCode = genericArgs[i].GetHashCode();
hashcode = (hashcode + _rotl(hashcode, 13)) ^ argumentHashCode;
hashcode = (hashcode + (int)BitOperations.RotateLeft((uint)hashcode, 13)) ^ argumentHashCode;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;

// Managed mirror of NativeFormatWriter.h/.cpp
namespace Internal.NativeFormat
Expand Down Expand Up @@ -2013,16 +2014,10 @@ public void Append(uint hashcode, Vertex element)
_Entries.Add(new Entry(hashcode, element));
}

// Returns 1 + log2(x) rounded up, 0 iff x == 0
// Calculates the highest bit set in a given unsigned integer.
private static int HighestBit(uint x)
{
int ret = 0;
while (x != 0)
{
x >>= 1;
ret++;
}
return ret;
return (sizeof(uint) * 8) - BitOperations.LeadingZeroCount(x);
}

// Helper method to back patch entry index in the bucket table
Expand Down
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 Internal.TypeSystem;

Expand Down Expand Up @@ -193,15 +194,7 @@ internal static uint ComputeValueTypeFieldPaddingFieldValue(uint padding, uint a
if ((padding == 0) && (alignment == targetPointerSize))
return 0;

uint alignmentLog2 = 0;
Debug.Assert(alignment != 0);

while ((alignment & 1) == 0)
{
alignmentLog2++;
alignment >>= 1;
}
Debug.Assert(alignment == 1);
uint alignmentLog2 = (uint)BitOperations.TrailingZeroCount(alignment);
PaulusParssinen marked this conversation as resolved.
Show resolved Hide resolved

Debug.Assert(ValueTypePaddingMax >= padding);

Expand Down
51 changes: 23 additions & 28 deletions src/coreclr/tools/Common/TypeSystem/Common/TypeHashingAlgorithms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// ---------------------------------------------------------------------------

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

Expand Down Expand Up @@ -36,35 +37,29 @@ public void Append(string src)
int startIndex = 0;
if ((_numCharactersHashed & 1) == 1)
{
_hash2 = (_hash2 + _rotl(_hash2, 5)) ^ src[0];
_hash2 = (_hash2 + (int)BitOperations.RotateLeft((uint)_hash2, 5)) ^ src[0];
PaulusParssinen marked this conversation as resolved.
Show resolved Hide resolved
startIndex = 1;
}

for (int i = startIndex; i < src.Length; i += 2)
{
_hash1 = (_hash1 + _rotl(_hash1, 5)) ^ src[i];
_hash1 = (_hash1 + (int)BitOperations.RotateLeft((uint)_hash1, 5)) ^ src[i];
if ((i + 1) < src.Length)
_hash2 = (_hash2 + _rotl(_hash2, 5)) ^ src[i + 1];
_hash2 = (_hash2 + (int)BitOperations.RotateLeft((uint)_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 + (int)BitOperations.RotateLeft((uint)_hash1, 8);
int hash2 = _hash2 + (int)BitOperations.RotateLeft((uint)_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 @@ -75,13 +70,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 + (int)BitOperations.RotateLeft((uint)hash1, 5)) ^ src[i];
if ((i + 1) < src.Length)
hash2 = (hash2 + _rotl(hash2, 5)) ^ src[i + 1];
hash2 = (hash2 + (int)BitOperations.RotateLeft((uint)hash2, 5)) ^ src[i + 1];
}

hash1 += _rotl(hash1, 8);
hash2 += _rotl(hash2, 8);
hash1 += (int)BitOperations.RotateLeft((uint)hash1, 8);
hash2 += (int)BitOperations.RotateLeft((uint)hash2, 8);

return hash1 ^ hash2;
}
Expand All @@ -96,17 +91,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 + (int)BitOperations.RotateLeft((uint)hash1, 5)) ^ b1;
if ((i + 1) < length)
{
int b2 = data[i];
asciiMask |= b2;
hash2 = (hash2 + _rotl(hash2, 5)) ^ b2;
hash2 = (hash2 + (int)BitOperations.RotateLeft((uint)hash2, 5)) ^ b2;
}
}

hash1 += _rotl(hash1, 8);
hash2 += _rotl(hash2, 8);
hash1 += (int)BitOperations.RotateLeft((uint)hash1, 8);
hash2 += (int)BitOperations.RotateLeft((uint)hash2, 8);

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

Expand Down Expand Up @@ -157,8 +152,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 + (int)BitOperations.RotateLeft((uint)hashCode, 13)) ^ elementTypeHashCode;
return (hashCode + (int)BitOperations.RotateLeft((uint)hashCode, 15));
}

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

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

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

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

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

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


Expand All @@ -201,9 +196,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 + (int)BitOperations.RotateLeft((uint)hashcode, 13)) ^ argumentHashCode;
}
return (hashcode + _rotl(hashcode, 15));
return (hashcode + (int)BitOperations.RotateLeft((uint)hashcode, 15));
}

public static int ComputeMethodSignatureHashCode<ARG>(int returnTypeHashCode, ARG[] parameters)
Expand All @@ -215,9 +210,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 + (int)BitOperations.RotateLeft((uint)hashcode, 13)) ^ parameterHashCode;
}
return (hashcode + _rotl(hashcode, 15));
return (hashcode + (int)BitOperations.RotateLeft((uint)hashcode, 15));
}

/// <summary>
Expand Down
3 changes: 1 addition & 2 deletions src/coreclr/tools/ILVerification/ILVerification.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
<EnableDefaultEmbeddedResourceItems>false</EnableDefaultEmbeddedResourceItems>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>$(NetCoreAppToolCurrent)</TargetFramework>
<SignAssembly>true</SignAssembly>
<StrongNameKeyId>Open</StrongNameKeyId>
<RunAnalyzers>false</RunAnalyzers>
<DefineConstants>NETSTANDARD2_0</DefineConstants>
</PropertyGroup>

<Import Project="ILVerification.projitems" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Internal.Text;
using Internal.TypeSystem;
using ILCompiler.DependencyAnalysisFramework;
using System.Numerics;

namespace ILCompiler.DependencyAnalysis
{
Expand Down Expand Up @@ -305,20 +306,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 = (int)BitOperations.RotateLeft((uint)hashcode, 5);
}
return hashcode;
}
Expand All @@ -345,20 +339,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 = (int)BitOperations.RotateLeft((uint)hashcode, 5);
}

return hashcode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,10 @@ private static int KeyToBucket(ref int tableData, nuint source, nuint target)
// then we use fibonacci hashing to reduce the value to desired size.

int hashShift = HashShift(ref tableData);
nuint hash = BitOperations.RotateLeft(source, (nuint.Size * 8) / 2) ^ target;
#if TARGET_64BIT
ulong hash = BitOperations.RotateLeft((ulong)source, 32) ^ (ulong)target;
return (int)((hash * 11400714819323198485ul) >> hashShift);
#else
uint hash = BitOperations.RotateLeft((uint)source, 16) ^ (uint)target;
return (int)((hash * 2654435769u) >> hashShift);
#endif
}
Expand Down