Skip to content

Commit

Permalink
Use BitOperations in couple more places (#101701)
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulusParssinen committed Jun 3, 2024
1 parent f272c0e commit 59e8bbc
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

using Internal.Runtime.Augments;
Expand Down Expand Up @@ -179,26 +177,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.RotateLeft(hash1, 5)) ^ hashCode1;
hash2 = (hash2 + int.RotateLeft(hash2, 5)) ^ hashCode2;
hash1 = (hash1 + int.RotateLeft(hash1, 5)) ^ hashCode3;
hash2 = (hash2 + int.RotateLeft(hash2, 5)) ^ hashCode4;

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

return hash1 ^ hash2;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.ComponentModel;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;

Expand Down Expand Up @@ -48,12 +46,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 +56,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.RotateLeft(hashcode, 13)) ^ fieldName.GetHashCode();
}

public static RuntimeFieldHandle FromIntPtr(IntPtr value) => new RuntimeFieldHandle(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.ComponentModel;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;

Expand Down Expand Up @@ -68,12 +66,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 +77,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.RotateLeft(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.RotateLeft(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 @@ -193,15 +193,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.TrailingZeroCount(alignment);

Debug.Assert(ValueTypePaddingMax >= padding);

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

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Internal.Text;
using Internal.TypeSystem;
using ILCompiler.DependencyAnalysisFramework;
Expand Down Expand Up @@ -305,20 +304,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.RotateLeft(hashcode, 5);
}
return hashcode;
}
Expand All @@ -345,20 +337,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.RotateLeft(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

0 comments on commit 59e8bbc

Please sign in to comment.