From 92f155cdc7eb714f2c961877d50e9c9001524fa4 Mon Sep 17 00:00:00 2001 From: PaulusParssinen Date: Tue, 30 Apr 2024 02:04:44 +0300 Subject: [PATCH 01/18] Use BitOperations.Log2 in EETypeBuilderHelpers --- .../Common/Internal/Runtime/EETypeBuilderHelpers.cs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs b/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs index 35d3c877e0506e..acae3dedce49cc 100644 --- a/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs +++ b/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs @@ -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; @@ -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.Log2(alignment); Debug.Assert(ValueTypePaddingMax >= padding); From 741fd54fc199aeda69d03b0da3042a6b7c978f1e Mon Sep 17 00:00:00 2001 From: PaulusParssinen Date: Tue, 30 Apr 2024 02:07:20 +0300 Subject: [PATCH 02/18] Use BitOperations.LeadingZeroCount in NativeFormatWriter Big thanks to @MichalPetryka for showing me simpler alternative to this --- .../Internal/NativeFormat/NativeFormatWriter.cs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/coreclr/tools/Common/Internal/NativeFormat/NativeFormatWriter.cs b/src/coreclr/tools/Common/Internal/NativeFormat/NativeFormatWriter.cs index 67b62fc84c5089..f366fd81e02f3d 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 1 << (sizeof(uint) * 8) - BitOperations.LeadingZeroCount(x); } // Helper method to back patch entry index in the bucket table @@ -2051,7 +2046,7 @@ private void ComputeLayout() uint bucketsEstimate = (uint)(_Entries.Count / _nFillFactor); // Round number of buckets up to the power of two - _nBuckets = (uint)(1 << HighestBit(bucketsEstimate)); + _nBuckets = (uint)HighestBit(bucketsEstimate); // Lowest byte of the hashcode is used for lookup within the bucket. Keep it sorted too so that // we can use the ordering to terminate the lookup prematurely. From d10a14537cc684a942de0387f3b6942f3d8d0a80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paulus=20P=C3=A4rssinen?= Date: Mon, 29 Apr 2024 23:36:22 +0000 Subject: [PATCH 03/18] Clarify operator precedence Co-authored-by: Adeel Mujahid <3840695+am11@users.noreply.github.com> --- .../tools/Common/Internal/NativeFormat/NativeFormatWriter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/tools/Common/Internal/NativeFormat/NativeFormatWriter.cs b/src/coreclr/tools/Common/Internal/NativeFormat/NativeFormatWriter.cs index f366fd81e02f3d..f36c513f4482dc 100644 --- a/src/coreclr/tools/Common/Internal/NativeFormat/NativeFormatWriter.cs +++ b/src/coreclr/tools/Common/Internal/NativeFormat/NativeFormatWriter.cs @@ -2017,7 +2017,7 @@ public void Append(uint hashcode, Vertex element) // Calculates the highest bit set in a given unsigned integer. private static int HighestBit(uint x) { - return 1 << (sizeof(uint) * 8) - BitOperations.LeadingZeroCount(x); + return 1 << ((sizeof(uint) * 8) - BitOperations.LeadingZeroCount(x)); } // Helper method to back patch entry index in the bucket table From 1eed1076dafc0f3bab6ed1e1ca5a80a962629bac Mon Sep 17 00:00:00 2001 From: PaulusParssinen Date: Fri, 10 May 2024 02:29:22 +0300 Subject: [PATCH 04/18] It's doing LZCNT, not Log2.. --- .../tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs b/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs index acae3dedce49cc..caee285a182e88 100644 --- a/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs +++ b/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs @@ -194,7 +194,7 @@ internal static uint ComputeValueTypeFieldPaddingFieldValue(uint padding, uint a if ((padding == 0) && (alignment == targetPointerSize)) return 0; - uint alignmentLog2 = (uint)BitOperations.Log2(alignment); + uint alignmentLog2 = (uint)BitOperations.LeadingZeroCount(alignment); Debug.Assert(ValueTypePaddingMax >= padding); From c14d86ea6d6895ffda4dbad62916d457361aee45 Mon Sep 17 00:00:00 2001 From: PaulusParssinen Date: Fri, 10 May 2024 03:24:51 +0300 Subject: [PATCH 05/18] I'm blind --- .../tools/Common/Internal/NativeFormat/NativeFormatWriter.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/coreclr/tools/Common/Internal/NativeFormat/NativeFormatWriter.cs b/src/coreclr/tools/Common/Internal/NativeFormat/NativeFormatWriter.cs index f36c513f4482dc..0a347d7ab3c567 100644 --- a/src/coreclr/tools/Common/Internal/NativeFormat/NativeFormatWriter.cs +++ b/src/coreclr/tools/Common/Internal/NativeFormat/NativeFormatWriter.cs @@ -2017,7 +2017,7 @@ public void Append(uint hashcode, Vertex element) // Calculates the highest bit set in a given unsigned integer. private static int HighestBit(uint x) { - return 1 << ((sizeof(uint) * 8) - BitOperations.LeadingZeroCount(x)); + return (sizeof(uint) * 8) - BitOperations.LeadingZeroCount(x); } // Helper method to back patch entry index in the bucket table @@ -2046,7 +2046,7 @@ private void ComputeLayout() uint bucketsEstimate = (uint)(_Entries.Count / _nFillFactor); // Round number of buckets up to the power of two - _nBuckets = (uint)HighestBit(bucketsEstimate); + _nBuckets = (uint)(1 << HighestBit(bucketsEstimate)); // Lowest byte of the hashcode is used for lookup within the bucket. Keep it sorted too so that // we can use the ordering to terminate the lookup prematurely. From c423d1f73c7284a920aa9464eca0b61a40db754b Mon Sep 17 00:00:00 2001 From: PaulusParssinen Date: Fri, 10 May 2024 19:55:05 +0300 Subject: [PATCH 06/18] LZCNT -> TZCNT.. - Yep.. --- .../tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs b/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs index caee285a182e88..04ce1b5a2d97f9 100644 --- a/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs +++ b/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs @@ -194,7 +194,7 @@ internal static uint ComputeValueTypeFieldPaddingFieldValue(uint padding, uint a if ((padding == 0) && (alignment == targetPointerSize)) return 0; - uint alignmentLog2 = (uint)BitOperations.LeadingZeroCount(alignment); + uint alignmentLog2 = (uint)BitOperations.TrailingZeroCount(alignment); Debug.Assert(ValueTypePaddingMax >= padding); From db3eec9544d4f02ca62d0239c737c0ed1854012f Mon Sep 17 00:00:00 2001 From: PaulusParssinen Date: Sat, 11 May 2024 19:54:51 +0300 Subject: [PATCH 07/18] Use BitOperations.RotateLeft in NodeFactory.NativeLayout.cs --- .../DependencyAnalysis/NodeFactory.NativeLayout.cs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) 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 e98c243b2b7261..0839256dfc3aa0 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 @@ -7,6 +7,7 @@ using Internal.Text; using Internal.TypeSystem; using ILCompiler.DependencyAnalysisFramework; +using System.Numerics; namespace ILCompiler.DependencyAnalysis { @@ -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 = BitOperations.RotateLeft((uint)hashcode, 5); } return hashcode; } From 3e7de82993a30f71378dd34cf938ccf63cb243c4 Mon Sep 17 00:00:00 2001 From: PaulusParssinen Date: Sat, 11 May 2024 19:55:31 +0300 Subject: [PATCH 08/18] Add required cast --- .../Compiler/DependencyAnalysis/NodeFactory.NativeLayout.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 0839256dfc3aa0..24b37081a18aa1 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 @@ -312,7 +312,7 @@ public override int GetHashCode() foreach (NativeLayoutVertexNode node in Vertices) { hashcode ^= node.GetHashCode(); - hashcode = BitOperations.RotateLeft((uint)hashcode, 5); + hashcode = (int)BitOperations.RotateLeft((uint)hashcode, 5); } return hashcode; } From ba8606955a051766c81b12f533fd33802823187a Mon Sep 17 00:00:00 2001 From: PaulusParssinen Date: Sat, 11 May 2024 19:57:49 +0300 Subject: [PATCH 09/18] Replace one more rotate with BitOperations in NativeLayout --- .../DependencyAnalysis/NodeFactory.NativeLayout.cs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) 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 24b37081a18aa1..44ae40f783884b 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 @@ -339,20 +339,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)BitOperations.RotateLeft((uint)hashcode, 5); } return hashcode; From a85a69bff1ad5ef20233524f5ed53b4333b927b7 Mon Sep 17 00:00:00 2001 From: PaulusParssinen Date: Fri, 17 May 2024 05:45:36 +0300 Subject: [PATCH 10/18] Use BitOperations.RotateLeft in the NAOT/CoreLib OpenMethodResolver.cs --- .../CompilerServices/OpenMethodResolver.cs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 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 0fef8d453dac3a..eb0d20588abe9a 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 @@ -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; @@ -179,12 +180,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 +187,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)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); return hash1 ^ hash2; } From d7b976786aad8df0853eb5de29d74ffadbce79f4 Mon Sep 17 00:00:00 2001 From: PaulusParssinen Date: Fri, 17 May 2024 05:45:38 +0300 Subject: [PATCH 11/18] Use BitOperations.RotateLeft in the NAOT/CoreLib RuntimeFieldHandle.cs --- .../src/System/RuntimeFieldHandle.cs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) 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 db507507496ced..15a72be2ba682f 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeFieldHandle.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeFieldHandle.cs @@ -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; @@ -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) @@ -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(); } public static RuntimeFieldHandle FromIntPtr(IntPtr value) => new RuntimeFieldHandle(value); From efcae8099c8bd1f98d7b5bb681edc96c3ec7793d Mon Sep 17 00:00:00 2001 From: PaulusParssinen Date: Fri, 17 May 2024 05:45:41 +0300 Subject: [PATCH 12/18] Use BitOperations.RotateLeft in the NAOT/CoreLib RuntimeMethodHandle.cs --- .../src/System/RuntimeMethodHandle.cs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) 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 139b61b3bbcdb4..15f06991a857d7 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeMethodHandle.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeMethodHandle.cs @@ -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; @@ -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) @@ -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; } } From 27ddd40dc8042e09afa1996f7580acc854eca13c Mon Sep 17 00:00:00 2001 From: PaulusParssinen Date: Fri, 17 May 2024 05:45:43 +0300 Subject: [PATCH 13/18] Use BitOperations.RotateLeft in the shared TypeHashingAlgorithms.cs --- .../Common/TypeHashingAlgorithms.cs | 51 +++++++++---------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/src/coreclr/tools/Common/TypeSystem/Common/TypeHashingAlgorithms.cs b/src/coreclr/tools/Common/TypeSystem/Common/TypeHashingAlgorithms.cs index 1b9692037fe94e..e4a8ca0b836ea6 100644 --- a/src/coreclr/tools/Common/TypeSystem/Common/TypeHashingAlgorithms.cs +++ b/src/coreclr/tools/Common/TypeSystem/Common/TypeHashingAlgorithms.cs @@ -6,6 +6,7 @@ // --------------------------------------------------------------------------- using System.Diagnostics; +using System.Numerics; using System.Runtime.CompilerServices; using System.Text; @@ -36,15 +37,15 @@ 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]; 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; @@ -52,19 +53,13 @@ public void Append(string src) 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 // @@ -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; } @@ -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; @@ -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 elementType, int rank) @@ -169,7 +164,7 @@ public static int ComputeArrayTypeHashCode(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 pointeeType) @@ -180,7 +175,7 @@ public static int ComputePointerTypeHashCode(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 parameterType) @@ -191,7 +186,7 @@ public static int ComputeByrefTypeHashCode(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; } @@ -201,9 +196,9 @@ public static int ComputeGenericInstanceHashCode(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(int returnTypeHashCode, ARG[] parameters) @@ -215,9 +210,9 @@ public static int ComputeMethodSignatureHashCode(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)); } /// From 50ee2199a32bb904adcc1b2c932459e4e8f00d08 Mon Sep 17 00:00:00 2001 From: PaulusParssinen Date: Fri, 17 May 2024 05:45:46 +0300 Subject: [PATCH 14/18] Use nuint overload of BitOperations.RotateLeft in the shared CoreLib CastCache.cs --- .../src/System/Runtime/CompilerServices/CastCache.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 22f9e762d473b5..cc7a2faa234268 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 } From 6049c0771d2834c451e3eb59d9ba7cf12bb22569 Mon Sep 17 00:00:00 2001 From: PaulusParssinen Date: Fri, 17 May 2024 10:08:07 +0300 Subject: [PATCH 15/18] Make ILVerification target NetCoreAppToolCurrent The only consumer of this library is ILVerify, which itself is NetCoreAppToolCurrent. --- src/coreclr/tools/ILVerification/ILVerification.csproj | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/coreclr/tools/ILVerification/ILVerification.csproj b/src/coreclr/tools/ILVerification/ILVerification.csproj index 4ac252c3e0c446..89687645741e3f 100644 --- a/src/coreclr/tools/ILVerification/ILVerification.csproj +++ b/src/coreclr/tools/ILVerification/ILVerification.csproj @@ -6,11 +6,10 @@ true false false - netstandard2.0 + $(NetCoreAppToolCurrent) true Open false - NETSTANDARD2_0 From e9855693f3af5a7f608d8183c545c1a111df75d3 Mon Sep 17 00:00:00 2001 From: PaulusParssinen Date: Fri, 17 May 2024 17:14:08 +0300 Subject: [PATCH 16/18] Apply PR feedback --- .../CompilerServices/OpenMethodResolver.cs | 15 +++--- .../src/System/RuntimeFieldHandle.cs | 5 +- .../src/System/RuntimeMethodHandle.cs | 7 +-- .../Internal/Runtime/EETypeBuilderHelpers.cs | 3 +- .../Common/TypeHashingAlgorithms.cs | 46 +++++++++---------- .../NodeFactory.NativeLayout.cs | 6 +-- 6 files changed, 34 insertions(+), 48 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 eb0d20588abe9a..e3ee1f3eedb79c 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,10 +3,7 @@ using System; using System.Collections.Generic; -using System.Diagnostics; -using System.Numerics; using System.Runtime; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using Internal.Runtime.Augments; @@ -187,13 +184,13 @@ private static int CalcHashCode(int hashCode1, int hashCode2, int hashCode3, int int hash1 = 0x449b3ad6; int hash2 = (length << 3) + 0x55399219; - 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 = (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 += (int)BitOperations.RotateLeft((uint)hash1, 8); - hash2 += (int)BitOperations.RotateLeft((uint)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 15a72be2ba682f..01624ba5469dde 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeFieldHandle.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeFieldHandle.cs @@ -2,9 +2,6 @@ // 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; using System.Runtime.Serialization; @@ -59,7 +56,7 @@ public override int GetHashCode() RuntimeAugments.TypeLoaderCallbacks.GetRuntimeFieldHandleComponents(this, out declaringType, out fieldName); int hashcode = declaringType.GetHashCode(); - return (hashcode + (int)BitOperations.RotateLeft((uint)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 15f06991a857d7..b79874529f6512 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeMethodHandle.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeMethodHandle.cs @@ -2,9 +2,6 @@ // 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; using System.Runtime.Serialization; @@ -80,13 +77,13 @@ public override int GetHashCode() RuntimeAugments.TypeLoaderCallbacks.GetRuntimeMethodHandleComponents(this, out declaringType, out nameAndSignature, out genericArgs); int hashcode = declaringType.GetHashCode(); - hashcode = (hashcode + (int)BitOperations.RotateLeft((uint)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 + (int)BitOperations.RotateLeft((uint)hashcode, 13)) ^ argumentHashCode; + hashcode = (hashcode + int.RotateLeft(hashcode, 13)) ^ argumentHashCode; } } diff --git a/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs b/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs index 04ce1b5a2d97f9..2f6d2c7f04d6fe 100644 --- a/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs +++ b/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics; -using System.Numerics; using Internal.TypeSystem; @@ -194,7 +193,7 @@ internal static uint ComputeValueTypeFieldPaddingFieldValue(uint padding, uint a if ((padding == 0) && (alignment == targetPointerSize)) return 0; - uint alignmentLog2 = (uint)BitOperations.TrailingZeroCount(alignment); + uint alignmentLog2 = uint.TrailingZeroCount(alignment); Debug.Assert(ValueTypePaddingMax >= padding); diff --git a/src/coreclr/tools/Common/TypeSystem/Common/TypeHashingAlgorithms.cs b/src/coreclr/tools/Common/TypeSystem/Common/TypeHashingAlgorithms.cs index e4a8ca0b836ea6..e64b32c70ca201 100644 --- a/src/coreclr/tools/Common/TypeSystem/Common/TypeHashingAlgorithms.cs +++ b/src/coreclr/tools/Common/TypeSystem/Common/TypeHashingAlgorithms.cs @@ -6,8 +6,6 @@ // --------------------------------------------------------------------------- using System.Diagnostics; -using System.Numerics; -using System.Runtime.CompilerServices; using System.Text; namespace Internal.NativeFormat @@ -37,15 +35,15 @@ public void Append(string src) int startIndex = 0; if ((_numCharactersHashed & 1) == 1) { - _hash2 = (_hash2 + (int)BitOperations.RotateLeft((uint)_hash2, 5)) ^ src[0]; + _hash2 = (_hash2 + int.RotateLeft(_hash2, 5)) ^ src[0]; startIndex = 1; } for (int i = startIndex; i < src.Length; i += 2) { - _hash1 = (_hash1 + (int)BitOperations.RotateLeft((uint)_hash1, 5)) ^ src[i]; + _hash1 = (_hash1 + int.RotateLeft(_hash1, 5)) ^ src[i]; if ((i + 1) < src.Length) - _hash2 = (_hash2 + (int)BitOperations.RotateLeft((uint)_hash2, 5)) ^ src[i + 1]; + _hash2 = (_hash2 + int.RotateLeft(_hash2, 5)) ^ src[i + 1]; } _numCharactersHashed += src.Length; @@ -53,8 +51,8 @@ public void Append(string src) public int ToHashCode() { - int hash1 = _hash1 + (int)BitOperations.RotateLeft((uint)_hash1, 8); - int hash2 = _hash2 + (int)BitOperations.RotateLeft((uint)_hash2, 8); + int hash1 = _hash1 + int.RotateLeft(_hash1, 8); + int hash2 = _hash2 + int.RotateLeft(_hash2, 8); return hash1 ^ hash2; } @@ -70,13 +68,13 @@ public static int ComputeNameHashCode(string src) for (int i = 0; i < src.Length; i += 2) { - hash1 = (hash1 + (int)BitOperations.RotateLeft((uint)hash1, 5)) ^ src[i]; + hash1 = (hash1 + int.RotateLeft(hash1, 5)) ^ src[i]; if ((i + 1) < src.Length) - hash2 = (hash2 + (int)BitOperations.RotateLeft((uint)hash2, 5)) ^ src[i + 1]; + hash2 = (hash2 + int.RotateLeft(hash2, 5)) ^ src[i + 1]; } - hash1 += (int)BitOperations.RotateLeft((uint)hash1, 8); - hash2 += (int)BitOperations.RotateLeft((uint)hash2, 8); + hash1 += int.RotateLeft(hash1, 8); + hash2 += int.RotateLeft(hash2, 8); return hash1 ^ hash2; } @@ -91,17 +89,17 @@ public static unsafe int ComputeASCIINameHashCode(byte* data, int length, out bo { int b1 = data[i]; asciiMask |= b1; - hash1 = (hash1 + (int)BitOperations.RotateLeft((uint)hash1, 5)) ^ b1; + hash1 = (hash1 + int.RotateLeft(hash1, 5)) ^ b1; if ((i + 1) < length) { int b2 = data[i]; asciiMask |= b2; - hash2 = (hash2 + (int)BitOperations.RotateLeft((uint)hash2, 5)) ^ b2; + hash2 = (hash2 + int.RotateLeft(hash2, 5)) ^ b2; } } - hash1 += (int)BitOperations.RotateLeft((uint)hash1, 8); - hash2 += (int)BitOperations.RotateLeft((uint)hash2, 8); + hash1 += int.RotateLeft(hash1, 8); + hash2 += int.RotateLeft(hash2, 8); isAscii = (asciiMask & 0x80) == 0; @@ -152,8 +150,8 @@ public static int ComputeArrayTypeHashCode(int elementTypeHashCode, int rank) hashCode = ComputeNameHashCode("System.MDArrayRank" + IntToString(rank) + "`1"); } - hashCode = (hashCode + (int)BitOperations.RotateLeft((uint)hashCode, 13)) ^ elementTypeHashCode; - return (hashCode + (int)BitOperations.RotateLeft((uint)hashCode, 15)); + hashCode = (hashCode + int.RotateLeft(hashCode, 13)) ^ elementTypeHashCode; + return (hashCode + int.RotateLeft(hashCode, 15)); } public static int ComputeArrayTypeHashCode(T elementType, int rank) @@ -164,7 +162,7 @@ public static int ComputeArrayTypeHashCode(T elementType, int rank) public static int ComputePointerTypeHashCode(int pointeeTypeHashCode) { - return (pointeeTypeHashCode + (int)BitOperations.RotateLeft((uint)pointeeTypeHashCode, 5)) ^ 0x12D0; + return (pointeeTypeHashCode + int.RotateLeft(pointeeTypeHashCode, 5)) ^ 0x12D0; } public static int ComputePointerTypeHashCode(T pointeeType) @@ -175,7 +173,7 @@ public static int ComputePointerTypeHashCode(T pointeeType) public static int ComputeByrefTypeHashCode(int parameterTypeHashCode) { - return (parameterTypeHashCode + (int)BitOperations.RotateLeft((uint)parameterTypeHashCode, 7)) ^ 0x4C85; + return (parameterTypeHashCode + int.RotateLeft(parameterTypeHashCode, 7)) ^ 0x4C85; } public static int ComputeByrefTypeHashCode(T parameterType) @@ -186,7 +184,7 @@ public static int ComputeByrefTypeHashCode(T parameterType) public static int ComputeNestedTypeHashCode(int enclosingTypeHashCode, int nestedTypeNameHash) { - return (enclosingTypeHashCode + (int)BitOperations.RotateLeft((uint)enclosingTypeHashCode, 11)) ^ nestedTypeNameHash; + return (enclosingTypeHashCode + int.RotateLeft(enclosingTypeHashCode, 11)) ^ nestedTypeNameHash; } @@ -196,9 +194,9 @@ public static int ComputeGenericInstanceHashCode(int genericDefinitionHashC for (int i = 0; i < genericTypeArguments.Length; i++) { int argumentHashCode = genericTypeArguments[i].GetHashCode(); - hashcode = (hashcode + (int)BitOperations.RotateLeft((uint)hashcode, 13)) ^ argumentHashCode; + hashcode = (hashcode + int.RotateLeft(hashcode, 13)) ^ argumentHashCode; } - return (hashcode + (int)BitOperations.RotateLeft((uint)hashcode, 15)); + return (hashcode + int.RotateLeft(hashcode, 15)); } public static int ComputeMethodSignatureHashCode(int returnTypeHashCode, ARG[] parameters) @@ -210,9 +208,9 @@ public static int ComputeMethodSignatureHashCode(int returnTypeHashCode, AR for (int i = 0; i < parameters.Length; i++) { int parameterHashCode = parameters[i].GetHashCode(); - hashcode = (hashcode + (int)BitOperations.RotateLeft((uint)hashcode, 13)) ^ parameterHashCode; + hashcode = (hashcode + int.RotateLeft(hashcode, 13)) ^ parameterHashCode; } - return (hashcode + (int)BitOperations.RotateLeft((uint)hashcode, 15)); + return (hashcode + int.RotateLeft(hashcode, 15)); } /// 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 44ae40f783884b..e3eb154d8d0c3b 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,11 +3,9 @@ using System; using System.Collections.Generic; -using System.Runtime.CompilerServices; using Internal.Text; using Internal.TypeSystem; using ILCompiler.DependencyAnalysisFramework; -using System.Numerics; namespace ILCompiler.DependencyAnalysis { @@ -312,7 +310,7 @@ public override int GetHashCode() foreach (NativeLayoutVertexNode node in Vertices) { hashcode ^= node.GetHashCode(); - hashcode = (int)BitOperations.RotateLeft((uint)hashcode, 5); + hashcode = int.RotateLeft(hashcode, 5); } return hashcode; } @@ -345,7 +343,7 @@ int IEqualityComparer>.GetHashCode(List obj) foreach (uint u in obj) { hashcode ^= (int)u; - hashcode = (int)BitOperations.RotateLeft((uint)hashcode, 5); + hashcode = int.RotateLeft(hashcode, 5); } return hashcode; From 8c104e00701f9646004e732304f4c7e01ec6e158 Mon Sep 17 00:00:00 2001 From: PaulusParssinen Date: Sat, 18 May 2024 14:36:45 +0300 Subject: [PATCH 17/18] Revert TypeHashingAlgorithms.cs * ILVerification was shipped OOB --- .../Common/TypeHashingAlgorithms.cs | 51 +++++++++++-------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/src/coreclr/tools/Common/TypeSystem/Common/TypeHashingAlgorithms.cs b/src/coreclr/tools/Common/TypeSystem/Common/TypeHashingAlgorithms.cs index e64b32c70ca201..1b9692037fe94e 100644 --- a/src/coreclr/tools/Common/TypeSystem/Common/TypeHashingAlgorithms.cs +++ b/src/coreclr/tools/Common/TypeSystem/Common/TypeHashingAlgorithms.cs @@ -6,6 +6,7 @@ // --------------------------------------------------------------------------- using System.Diagnostics; +using System.Runtime.CompilerServices; using System.Text; namespace Internal.NativeFormat @@ -35,15 +36,15 @@ public void Append(string src) int startIndex = 0; if ((_numCharactersHashed & 1) == 1) { - _hash2 = (_hash2 + int.RotateLeft(_hash2, 5)) ^ src[0]; + _hash2 = (_hash2 + _rotl(_hash2, 5)) ^ src[0]; startIndex = 1; } for (int i = startIndex; i < src.Length; i += 2) { - _hash1 = (_hash1 + int.RotateLeft(_hash1, 5)) ^ src[i]; + _hash1 = (_hash1 + _rotl(_hash1, 5)) ^ src[i]; if ((i + 1) < src.Length) - _hash2 = (_hash2 + int.RotateLeft(_hash2, 5)) ^ src[i + 1]; + _hash2 = (_hash2 + _rotl(_hash2, 5)) ^ src[i + 1]; } _numCharactersHashed += src.Length; @@ -51,13 +52,19 @@ public void Append(string src) public int ToHashCode() { - int hash1 = _hash1 + int.RotateLeft(_hash1, 8); - int hash2 = _hash2 + int.RotateLeft(_hash2, 8); + int hash1 = _hash1 + _rotl(_hash1, 8); + int hash2 = _hash2 + _rotl(_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 // @@ -68,13 +75,13 @@ public static int ComputeNameHashCode(string src) for (int i = 0; i < src.Length; i += 2) { - hash1 = (hash1 + int.RotateLeft(hash1, 5)) ^ src[i]; + hash1 = (hash1 + _rotl(hash1, 5)) ^ src[i]; if ((i + 1) < src.Length) - hash2 = (hash2 + int.RotateLeft(hash2, 5)) ^ src[i + 1]; + hash2 = (hash2 + _rotl(hash2, 5)) ^ src[i + 1]; } - hash1 += int.RotateLeft(hash1, 8); - hash2 += int.RotateLeft(hash2, 8); + hash1 += _rotl(hash1, 8); + hash2 += _rotl(hash2, 8); return hash1 ^ hash2; } @@ -89,17 +96,17 @@ public static unsafe int ComputeASCIINameHashCode(byte* data, int length, out bo { int b1 = data[i]; asciiMask |= b1; - hash1 = (hash1 + int.RotateLeft(hash1, 5)) ^ b1; + hash1 = (hash1 + _rotl(hash1, 5)) ^ b1; if ((i + 1) < length) { int b2 = data[i]; asciiMask |= b2; - hash2 = (hash2 + int.RotateLeft(hash2, 5)) ^ b2; + hash2 = (hash2 + _rotl(hash2, 5)) ^ b2; } } - hash1 += int.RotateLeft(hash1, 8); - hash2 += int.RotateLeft(hash2, 8); + hash1 += _rotl(hash1, 8); + hash2 += _rotl(hash2, 8); isAscii = (asciiMask & 0x80) == 0; @@ -150,8 +157,8 @@ public static int ComputeArrayTypeHashCode(int elementTypeHashCode, int rank) hashCode = ComputeNameHashCode("System.MDArrayRank" + IntToString(rank) + "`1"); } - hashCode = (hashCode + int.RotateLeft(hashCode, 13)) ^ elementTypeHashCode; - return (hashCode + int.RotateLeft(hashCode, 15)); + hashCode = (hashCode + _rotl(hashCode, 13)) ^ elementTypeHashCode; + return (hashCode + _rotl(hashCode, 15)); } public static int ComputeArrayTypeHashCode(T elementType, int rank) @@ -162,7 +169,7 @@ public static int ComputeArrayTypeHashCode(T elementType, int rank) public static int ComputePointerTypeHashCode(int pointeeTypeHashCode) { - return (pointeeTypeHashCode + int.RotateLeft(pointeeTypeHashCode, 5)) ^ 0x12D0; + return (pointeeTypeHashCode + _rotl(pointeeTypeHashCode, 5)) ^ 0x12D0; } public static int ComputePointerTypeHashCode(T pointeeType) @@ -173,7 +180,7 @@ public static int ComputePointerTypeHashCode(T pointeeType) public static int ComputeByrefTypeHashCode(int parameterTypeHashCode) { - return (parameterTypeHashCode + int.RotateLeft(parameterTypeHashCode, 7)) ^ 0x4C85; + return (parameterTypeHashCode + _rotl(parameterTypeHashCode, 7)) ^ 0x4C85; } public static int ComputeByrefTypeHashCode(T parameterType) @@ -184,7 +191,7 @@ public static int ComputeByrefTypeHashCode(T parameterType) public static int ComputeNestedTypeHashCode(int enclosingTypeHashCode, int nestedTypeNameHash) { - return (enclosingTypeHashCode + int.RotateLeft(enclosingTypeHashCode, 11)) ^ nestedTypeNameHash; + return (enclosingTypeHashCode + _rotl(enclosingTypeHashCode, 11)) ^ nestedTypeNameHash; } @@ -194,9 +201,9 @@ public static int ComputeGenericInstanceHashCode(int genericDefinitionHashC for (int i = 0; i < genericTypeArguments.Length; i++) { int argumentHashCode = genericTypeArguments[i].GetHashCode(); - hashcode = (hashcode + int.RotateLeft(hashcode, 13)) ^ argumentHashCode; + hashcode = (hashcode + _rotl(hashcode, 13)) ^ argumentHashCode; } - return (hashcode + int.RotateLeft(hashcode, 15)); + return (hashcode + _rotl(hashcode, 15)); } public static int ComputeMethodSignatureHashCode(int returnTypeHashCode, ARG[] parameters) @@ -208,9 +215,9 @@ public static int ComputeMethodSignatureHashCode(int returnTypeHashCode, AR for (int i = 0; i < parameters.Length; i++) { int parameterHashCode = parameters[i].GetHashCode(); - hashcode = (hashcode + int.RotateLeft(hashcode, 13)) ^ parameterHashCode; + hashcode = (hashcode + _rotl(hashcode, 13)) ^ parameterHashCode; } - return (hashcode + int.RotateLeft(hashcode, 15)); + return (hashcode + _rotl(hashcode, 15)); } /// From 0d05e17aaf599813a9747e77dde6d46cc80c1dad Mon Sep 17 00:00:00 2001 From: PaulusParssinen Date: Sat, 18 May 2024 14:36:58 +0300 Subject: [PATCH 18/18] Revert "Make ILVerification target NetCoreAppToolCurrent" This reverts commit 6049c0771d2834c451e3eb59d9ba7cf12bb22569. --- src/coreclr/tools/ILVerification/ILVerification.csproj | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/coreclr/tools/ILVerification/ILVerification.csproj b/src/coreclr/tools/ILVerification/ILVerification.csproj index 89687645741e3f..4ac252c3e0c446 100644 --- a/src/coreclr/tools/ILVerification/ILVerification.csproj +++ b/src/coreclr/tools/ILVerification/ILVerification.csproj @@ -6,10 +6,11 @@ true false false - $(NetCoreAppToolCurrent) + netstandard2.0 true Open false + NETSTANDARD2_0