// 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;
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
}
// The .NET Foundation licenses this file to you under the MIT license.
using System;
+using System.Numerics;
using System.Runtime.CompilerServices;
using System.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;
}
}
using System;
using System.Diagnostics;
+using System.Numerics;
using System.Text;
using Internal.TypeSystem;
/// <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()
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)
using System;
using System.IO;
using System.Diagnostics;
-using System.Numerics;
using System.Security.Cryptography;
+using static System.Numerics.BitOperations;
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));
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));