Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 59e8bbc

Browse files
Use BitOperations in couple more places (#101701)
1 parent f272c0e commit 59e8bbc

File tree

7 files changed

+16
-69
lines changed

7 files changed

+16
-69
lines changed

src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/CompilerServices/OpenMethodResolver.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using System.Diagnostics;
76
using System.Runtime;
8-
using System.Runtime.CompilerServices;
97
using System.Runtime.InteropServices;
108

119
using Internal.Runtime.Augments;
@@ -179,26 +177,20 @@ public static IntPtr ResolveMethod(IntPtr resolverPtr, RuntimeTypeHandle thisTyp
179177
return RuntimeImports.RhResolveDispatchOnType(thisType.ToMethodTable(), resolver->_declaringType, (ushort)resolver->_methodHandleOrSlotOrCodePointer);
180178
}
181179

182-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
183-
private static int _rotl(int value, int shift)
184-
{
185-
return (int)(((uint)value << shift) | ((uint)value >> (32 - shift)));
186-
}
187-
188180
private static int CalcHashCode(int hashCode1, int hashCode2, int hashCode3, int hashCode4)
189181
{
190182
int length = 4;
191183

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

195-
hash1 = (hash1 + _rotl(hash1, 5)) ^ hashCode1;
196-
hash2 = (hash2 + _rotl(hash2, 5)) ^ hashCode2;
197-
hash1 = (hash1 + _rotl(hash1, 5)) ^ hashCode3;
198-
hash2 = (hash2 + _rotl(hash2, 5)) ^ hashCode4;
187+
hash1 = (hash1 + int.RotateLeft(hash1, 5)) ^ hashCode1;
188+
hash2 = (hash2 + int.RotateLeft(hash2, 5)) ^ hashCode2;
189+
hash1 = (hash1 + int.RotateLeft(hash1, 5)) ^ hashCode3;
190+
hash2 = (hash2 + int.RotateLeft(hash2, 5)) ^ hashCode4;
199191

200-
hash1 += _rotl(hash1, 8);
201-
hash2 += _rotl(hash2, 8);
192+
hash1 += int.RotateLeft(hash1, 8);
193+
hash2 += int.RotateLeft(hash2, 8);
202194

203195
return hash1 ^ hash2;
204196
}

src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeFieldHandle.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.ComponentModel;
5-
using System.Reflection;
6-
using System.Runtime.CompilerServices;
75
using System.Runtime.InteropServices;
86
using System.Runtime.Serialization;
97

@@ -48,12 +46,6 @@ public bool Equals(RuntimeFieldHandle handle)
4846
return declaringType1.Equals(declaringType2) && fieldName1 == fieldName2;
4947
}
5048

51-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
52-
private static int _rotl(int value, int shift)
53-
{
54-
return (int)(((uint)value << shift) | ((uint)value >> (32 - shift)));
55-
}
56-
5749
public override int GetHashCode()
5850
{
5951
if (_value == IntPtr.Zero)
@@ -64,7 +56,7 @@ public override int GetHashCode()
6456
RuntimeAugments.TypeLoaderCallbacks.GetRuntimeFieldHandleComponents(this, out declaringType, out fieldName);
6557

6658
int hashcode = declaringType.GetHashCode();
67-
return (hashcode + _rotl(hashcode, 13)) ^ fieldName.GetHashCode();
59+
return (hashcode + int.RotateLeft(hashcode, 13)) ^ fieldName.GetHashCode();
6860
}
6961

7062
public static RuntimeFieldHandle FromIntPtr(IntPtr value) => new RuntimeFieldHandle(value);

src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeMethodHandle.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.ComponentModel;
5-
using System.Reflection;
6-
using System.Runtime.CompilerServices;
75
using System.Runtime.InteropServices;
86
using System.Runtime.Serialization;
97

@@ -68,12 +66,6 @@ public bool Equals(RuntimeMethodHandle handle)
6866
return true;
6967
}
7068

71-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
72-
private static int _rotl(int value, int shift)
73-
{
74-
return (int)(((uint)value << shift) | ((uint)value >> (32 - shift)));
75-
}
76-
7769
public override int GetHashCode()
7870
{
7971
if (_value == IntPtr.Zero)
@@ -85,13 +77,13 @@ public override int GetHashCode()
8577
RuntimeAugments.TypeLoaderCallbacks.GetRuntimeMethodHandleComponents(this, out declaringType, out nameAndSignature, out genericArgs);
8678

8779
int hashcode = declaringType.GetHashCode();
88-
hashcode = (hashcode + _rotl(hashcode, 13)) ^ nameAndSignature.Name.GetHashCode();
80+
hashcode = (hashcode + int.RotateLeft(hashcode, 13)) ^ nameAndSignature.Name.GetHashCode();
8981
if (genericArgs != null)
9082
{
9183
for (int i = 0; i < genericArgs.Length; i++)
9284
{
9385
int argumentHashCode = genericArgs[i].GetHashCode();
94-
hashcode = (hashcode + _rotl(hashcode, 13)) ^ argumentHashCode;
86+
hashcode = (hashcode + int.RotateLeft(hashcode, 13)) ^ argumentHashCode;
9587
}
9688
}
9789

src/coreclr/tools/Common/Internal/NativeFormat/NativeFormatWriter.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Collections.Generic;
88
using System.Linq;
99
using System.Text;
10+
using System.Numerics;
1011

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

2016-
// Returns 1 + log2(x) rounded up, 0 iff x == 0
2017+
// Calculates the highest bit set in a given unsigned integer.
20172018
private static int HighestBit(uint x)
20182019
{
2019-
int ret = 0;
2020-
while (x != 0)
2021-
{
2022-
x >>= 1;
2023-
ret++;
2024-
}
2025-
return ret;
2020+
return (sizeof(uint) * 8) - BitOperations.LeadingZeroCount(x);
20262021
}
20272022

20282023
// Helper method to back patch entry index in the bucket table

src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,7 @@ internal static uint ComputeValueTypeFieldPaddingFieldValue(uint padding, uint a
193193
if ((padding == 0) && (alignment == targetPointerSize))
194194
return 0;
195195

196-
uint alignmentLog2 = 0;
197-
Debug.Assert(alignment != 0);
198-
199-
while ((alignment & 1) == 0)
200-
{
201-
alignmentLog2++;
202-
alignment >>= 1;
203-
}
204-
Debug.Assert(alignment == 1);
196+
uint alignmentLog2 = uint.TrailingZeroCount(alignment);
205197

206198
Debug.Assert(ValueTypePaddingMax >= padding);
207199

src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/NodeFactory.NativeLayout.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using System.Runtime.CompilerServices;
76
using Internal.Text;
87
using Internal.TypeSystem;
98
using ILCompiler.DependencyAnalysisFramework;
@@ -305,20 +304,13 @@ public bool Equals(VertexSequenceKey other)
305304
return true;
306305
}
307306

308-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
309-
private static int _rotl(int value, int shift)
310-
{
311-
// This is expected to be optimized into a single rotl instruction
312-
return (int)(((uint)value << shift) | ((uint)value >> (32 - shift)));
313-
}
314-
315307
public override int GetHashCode()
316308
{
317309
int hashcode = 0;
318310
foreach (NativeLayoutVertexNode node in Vertices)
319311
{
320312
hashcode ^= node.GetHashCode();
321-
hashcode = _rotl(hashcode, 5);
313+
hashcode = int.RotateLeft(hashcode, 5);
322314
}
323315
return hashcode;
324316
}
@@ -345,20 +337,13 @@ bool IEqualityComparer<List<uint>>.Equals(List<uint> x, List<uint> y)
345337
return true;
346338
}
347339

348-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
349-
private static int _rotl(int value, int shift)
350-
{
351-
// This is expected to be optimized into a single rotl instruction
352-
return (int)(((uint)value << shift) | ((uint)value >> (32 - shift)));
353-
}
354-
355340
int IEqualityComparer<List<uint>>.GetHashCode(List<uint> obj)
356341
{
357342
int hashcode = 0x42284781;
358343
foreach (uint u in obj)
359344
{
360345
hashcode ^= (int)u;
361-
hashcode = _rotl(hashcode, 5);
346+
hashcode = int.RotateLeft(hashcode, 5);
362347
}
363348

364349
return hashcode;

src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/CastCache.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,10 @@ private static int KeyToBucket(ref int tableData, nuint source, nuint target)
9191
// then we use fibonacci hashing to reduce the value to desired size.
9292

9393
int hashShift = HashShift(ref tableData);
94+
nuint hash = BitOperations.RotateLeft(source, (nuint.Size * 8) / 2) ^ target;
9495
#if TARGET_64BIT
95-
ulong hash = BitOperations.RotateLeft((ulong)source, 32) ^ (ulong)target;
9696
return (int)((hash * 11400714819323198485ul) >> hashShift);
9797
#else
98-
uint hash = BitOperations.RotateLeft((uint)source, 16) ^ (uint)target;
9998
return (int)((hash * 2654435769u) >> hashShift);
10099
#endif
101100
}

0 commit comments

Comments
 (0)