From 59e8bbcf83b664c3de6cfa553d9bbfad76578765 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paulus=20P=C3=A4rssinen?= Date: Mon, 3 Jun 2024 11:18:00 +0300 Subject: [PATCH] Use ``BitOperations`` in couple more places (#101701) --- .../CompilerServices/OpenMethodResolver.cs | 20 ++++++------------- .../src/System/RuntimeFieldHandle.cs | 10 +--------- .../src/System/RuntimeMethodHandle.cs | 12 ++--------- .../NativeFormat/NativeFormatWriter.cs | 11 +++------- .../Internal/Runtime/EETypeBuilderHelpers.cs | 10 +--------- .../NodeFactory.NativeLayout.cs | 19 ++---------------- .../Runtime/CompilerServices/CastCache.cs | 3 +-- 7 files changed, 16 insertions(+), 69 deletions(-) diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/CompilerServices/OpenMethodResolver.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/CompilerServices/OpenMethodResolver.cs index 0fef8d453dac3..e3ee1f3eedb79 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/CompilerServices/OpenMethodResolver.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/CompilerServices/OpenMethodResolver.cs @@ -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; @@ -179,12 +177,6 @@ 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; @@ -192,13 +184,13 @@ private static int CalcHashCode(int hashCode1, int hashCode2, int hashCode3, int 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; } diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeFieldHandle.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeFieldHandle.cs index db507507496ce..01624ba5469dd 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeFieldHandle.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeFieldHandle.cs @@ -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; @@ -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) @@ -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); diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeMethodHandle.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeMethodHandle.cs index 139b61b3bbcdb..b79874529f651 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeMethodHandle.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeMethodHandle.cs @@ -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; @@ -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) @@ -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; } } diff --git a/src/coreclr/tools/Common/Internal/NativeFormat/NativeFormatWriter.cs b/src/coreclr/tools/Common/Internal/NativeFormat/NativeFormatWriter.cs index 67b62fc84c508..0a347d7ab3c56 100644 --- a/src/coreclr/tools/Common/Internal/NativeFormat/NativeFormatWriter.cs +++ b/src/coreclr/tools/Common/Internal/NativeFormat/NativeFormatWriter.cs @@ -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 @@ -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 diff --git a/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs b/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs index 35d3c877e0506..2f6d2c7f04d6f 100644 --- a/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs +++ b/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs @@ -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); diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/NodeFactory.NativeLayout.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/NodeFactory.NativeLayout.cs index e98c243b2b726..e3eb154d8d0c3 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/NodeFactory.NativeLayout.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/NodeFactory.NativeLayout.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; -using System.Runtime.CompilerServices; using Internal.Text; using Internal.TypeSystem; using ILCompiler.DependencyAnalysisFramework; @@ -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; } @@ -345,20 +337,13 @@ bool IEqualityComparer>.Equals(List x, List 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>.GetHashCode(List obj) { int hashcode = 0x42284781; foreach (uint u in obj) { hashcode ^= (int)u; - hashcode = _rotl(hashcode, 5); + hashcode = int.RotateLeft(hashcode, 5); } return hashcode; diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/CastCache.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/CastCache.cs index 22f9e762d473b..cc7a2faa23426 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/CastCache.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/CastCache.cs @@ -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 }