Cleanup rotate patterns (#54099)
authorHuo Yaoyuan <huoyaoyuan@hotmail.com>
Sun, 25 Jul 2021 05:33:52 +0000 (13:33 +0800)
committerGitHub <noreply@github.com>
Sun, 25 Jul 2021 05:33:52 +0000 (22:33 -0700)
* Clean RotateLeft

* Cleanup RotateRight

src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/CastHelpers.cs
src/coreclr/tools/Common/Internal/Text/Utf8String.cs
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/ReadyToRunHashCode.cs
src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/SHAHashProvider.Browser.cs

index cb88009..a31f67b 100644 (file)
@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 
 using System.Diagnostics;
+using System.Numerics;
 using System.Runtime.InteropServices;
 using System.Threading;
 
@@ -45,10 +46,10 @@ namespace System.Runtime.CompilerServices
 
             int hashShift = HashShift(ref tableData);
 #if TARGET_64BIT
-            ulong hash = (((ulong)source << 32) | ((ulong)source >> 32)) ^ (ulong)target;
+            ulong hash = BitOperations.RotateLeft((ulong)source, 32) ^ (ulong)target;
             return (int)((hash * 11400714819323198485ul) >> hashShift);
 #else
-            uint hash = (((uint)source >> 16) | ((uint)source << 16)) ^ (uint)target;
+            uint hash = BitOperations.RotateLeft((uint)source, 16) ^ (uint)target;
             return (int)((hash * 2654435769u) >> hashShift);
 #endif
         }
index cc1b045..9b44102 100644 (file)
@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 
 using System;
+using System.Numerics;
 using System.Runtime.CompilerServices;
 using System.Text;
 
@@ -42,38 +43,31 @@ namespace Internal.Text
             return (obj is Utf8String) && Equals((Utf8String)obj);
         }
 
-        [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 unsafe override int GetHashCode()
         {
             int length = _value.Length;
-            int hash = length;
+            uint hash = (uint)length;
             fixed (byte* ap = _value)
             {
                 byte* a = ap;
 
                 while (length >= 4)
                 {
-                    hash = (hash + _rotl(hash, 5)) ^ *(int*)a;
+                    hash = (hash + BitOperations.RotateLeft(hash, 5)) ^ *(uint*)a;
                     a += 4; length -= 4;
                 }
                 if (length >= 2)
                 {
-                    hash = (hash + _rotl(hash, 5)) ^ *(short*)a;
+                    hash = (hash + BitOperations.RotateLeft(hash, 5)) ^ *(ushort*)a;
                     a += 2; length -= 2;
                 }
                 if (length > 0)
                 {
-                    hash = (hash + _rotl(hash, 5)) ^ *a;
+                    hash = (hash + BitOperations.RotateLeft(hash, 5)) ^ *a;
                 }
-                hash += _rotl(hash, 7);
-                hash += _rotl(hash, 15);
-                return hash;
+                hash += BitOperations.RotateLeft(hash, 7);
+                hash += BitOperations.RotateLeft(hash, 15);
+                return (int)hash;
             }
         }
 
index 19a6434..e506bb8 100644 (file)
@@ -3,6 +3,7 @@
 
 using System;
 using System.Diagnostics;
+using System.Numerics;
 using System.Text;
 
 using Internal.TypeSystem;
@@ -219,7 +220,7 @@ namespace ILCompiler
         /// <param name="bitCount">Number of bits</param>
         private static int RotateLeft(int value, int bitCount)
         {
-            return unchecked((int)(((uint)value << bitCount) | ((uint)value >> (32 - bitCount))));
+            return (int)BitOperations.RotateLeft((uint)value, bitCount);
         }
 
         private static uint XXHash32_MixEmptyState()
@@ -231,7 +232,7 @@ namespace ILCompiler
 
         private static uint XXHash32_QueueRound(uint hash, uint queuedValue)
         {
-            return ((uint)RotateLeft((int)(hash + queuedValue * 3266489917U/*Prime3*/), 17)) * 668265263U/*Prime4*/;
+            return (BitOperations.RotateLeft((hash + queuedValue * 3266489917U/*Prime3*/), 17)) * 668265263U/*Prime4*/;
         }
 
         private static uint XXHash32_MixFinal(uint hash)
index 7a83e35..fccdff3 100644 (file)
@@ -4,8 +4,8 @@
 using System;
 using System.IO;
 using System.Diagnostics;
-using System.Numerics;
 using System.Security.Cryptography;
+using static System.Numerics.BitOperations;
 
 namespace Internal.Cryptography
 {
@@ -342,11 +342,6 @@ namespace Internal.Cryptography
                 state[7] += h;
             }
 
-            private static uint RotateRight(uint x, int n)
-            {
-                return (((x) >> (n)) | ((x) << (32 - (n))));
-            }
-
             private static uint Ch(uint x, uint y, uint z)
             {
                 return ((x & y) ^ ((x ^ 0xffffffff) & z));
@@ -917,11 +912,6 @@ namespace Internal.Cryptography
                 state[7] += h;
             }
 
-            private static ulong RotateRight(ulong x, int n)
-            {
-                return (((x) >> (n)) | ((x) << (64 - (n))));
-            }
-
             private static ulong Ch(ulong x, ulong y, ulong z)
             {
                 return ((x & y) ^ ((x ^ 0xffffffffffffffff) & z));