Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
285 changes: 285 additions & 0 deletions src/libraries/Common/src/System/IO/Hashing/XxHash3.Common.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,285 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// Based on the XXH3 implementation from https://github.com/Cyan4973/xxHash.

using System.Buffers.Binary;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using static System.IO.Hashing.XxHashShared;

namespace System.IO.Hashing
{
/// <summary>Provides an implementation of the XXH3 hash algorithm for generating a 64-bit hash.</summary>
/// <remarks>
/// For methods that persist the computed numerical hash value as bytes,
/// the value is written in the Big Endian byte order.
/// </remarks>
#if NET
[SkipLocalsInit]
#endif
#if SYSTEM_PRIVATE_CORELIB
internal
#else
public
#endif
sealed unsafe partial class XxHash3
{
/// <summary>Computes the XXH3 hash of the provided data.</summary>
/// <param name="source">The data to hash.</param>
/// <param name="seed">The seed value for this hash computation.</param>
/// <returns>The computed XXH3 hash.</returns>
#if !SYSTEM_PRIVATE_CORELIB
[CLSCompliant(false)]
#endif
public static ulong HashToUInt64(ReadOnlySpan<byte> source, long seed = 0)
{
uint length = (uint)source.Length;
fixed (byte* sourcePtr = &MemoryMarshal.GetReference(source))
{
if (length <= 16)
{
return HashLength0To16(sourcePtr, length, (ulong)seed);
}

if (length <= 128)
{
return HashLength17To128(sourcePtr, length, (ulong)seed);
}

if (length <= MidSizeMaxBytes)
{
return HashLength129To240(sourcePtr, length, (ulong)seed);
}

return HashLengthOver240(sourcePtr, length, (ulong)seed);
}
}


#if SYSTEM_PRIVATE_CORELIB
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int NonRandomizedHashToInt32(byte* sourcePtr, uint length)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we still need this method?

{
switch (length)
{
case 0:
return (int)Avalanche(DefaultSecretUInt64_7 ^ DefaultSecretUInt64_8);

case 1:
case 2:
case 3:
return (int)HashLength1To3(sourcePtr, length, 0UL);

case 4:
case 5:
case 6:
case 7:
case 8:
return (int)HashLength4To8(sourcePtr, length, 0UL);

case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
return (int)HashLength9To16(sourcePtr, length, 0UL);

default:
{
if (length <= 128)
{
return (int)HashLength17To128(sourcePtr, length, 0UL);
}
if (length <= MidSizeMaxBytes)
{
return (int)HashLength129To240(sourcePtr, length, 0UL);
}
return (int)HashLengthOver240(sourcePtr, length, 0UL);
}
}
}
#endif

private static ulong HashLength0To16(byte* source, uint length, ulong seed)
{
if (length > 8)
{
return HashLength9To16(source, length, seed);
}

if (length >= 4)
{
return HashLength4To8(source, length, seed);
}

if (length != 0)
{
return HashLength1To3(source, length, seed);
}

const ulong SecretXor = DefaultSecretUInt64_7 ^ DefaultSecretUInt64_8;
return Avalanche(seed ^ SecretXor);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ulong HashLength1To3(byte* source, uint length, ulong seed)
{
Debug.Assert(length >= 1 && length <= 3);

// When source.Length == 1, c1 == source[0], c2 == source[0], c3 == source[0]
// When source.Length == 2, c1 == source[0], c2 == source[1], c3 == source[1]
// When source.Length == 3, c1 == source[0], c2 == source[1], c3 == source[2]
byte c1 = *source;
byte c2 = source[length >> 1];
byte c3 = source[length - 1];

uint combined = ((uint)c1 << 16) | ((uint)c2 << 24) | c3 | (length << 8);

const uint SecretXor = unchecked((uint)DefaultSecretUInt64_0) ^ (uint)(DefaultSecretUInt64_0 >> 32);
return Avalanche(combined ^ (SecretXor + seed));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ulong HashLength4To8(byte* source, uint length, ulong seed)
{
Debug.Assert(length >= 4 && length <= 8);

seed ^= (ulong)BinaryPrimitives.ReverseEndianness((uint)seed) << 32;

uint inputLow = ReadUInt32LE(source);
uint inputHigh = ReadUInt32LE(source + length - sizeof(uint));

const ulong SecretXor = DefaultSecretUInt64_1 ^ DefaultSecretUInt64_2;
ulong bitflip = SecretXor - seed;
ulong input64 = inputHigh + (((ulong)inputLow) << 32);

return Rrmxmx(input64 ^ bitflip, length);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ulong HashLength9To16(byte* source, uint length, ulong seed)
{
Debug.Assert(length >= 9 && length <= 16);

const ulong SecretXorL = DefaultSecretUInt64_3 ^ DefaultSecretUInt64_4;
const ulong SecretXorR = DefaultSecretUInt64_5 ^ DefaultSecretUInt64_6;
ulong bitflipLow = SecretXorL + seed;
ulong bitflipHigh = SecretXorR - seed;

ulong inputLow = ReadUInt64LE(source) ^ bitflipLow;
ulong inputHigh = ReadUInt64LE(source + length - sizeof(ulong)) ^ bitflipHigh;

return FastAvalanche(
length +
BinaryPrimitives.ReverseEndianness(inputLow) +
inputHigh +
Multiply64To128ThenFold(inputLow, inputHigh));
}

private static ulong HashLength17To128(byte* source, uint length, ulong seed)
{
Debug.Assert(length >= 17 && length <= 128);

ulong hash = length * Prime64_1;

switch ((length - 1) / 32)
{
default: // case 3
hash += Mix16Bytes(source + 48, DefaultSecretUInt64_12, DefaultSecretUInt64_13, seed);
hash += Mix16Bytes(source + length - 64, DefaultSecretUInt64_14, DefaultSecretUInt64_15, seed);
goto case 2;
case 2:
hash += Mix16Bytes(source + 32, DefaultSecretUInt64_8, DefaultSecretUInt64_9, seed);
hash += Mix16Bytes(source + length - 48, DefaultSecretUInt64_10, DefaultSecretUInt64_11, seed);
goto case 1;
case 1:
hash += Mix16Bytes(source + 16, DefaultSecretUInt64_4, DefaultSecretUInt64_5, seed);
hash += Mix16Bytes(source + length - 32, DefaultSecretUInt64_6, DefaultSecretUInt64_7, seed);
goto case 0;
case 0:
hash += Mix16Bytes(source, DefaultSecretUInt64_0, DefaultSecretUInt64_1, seed);
hash += Mix16Bytes(source + length - 16, DefaultSecretUInt64_2, DefaultSecretUInt64_3, seed);
break;
}

return FastAvalanche(hash);
}

private static ulong HashLength129To240(byte* source, uint length, ulong seed)
{
Debug.Assert(length >= 129 && length <= 240);

ulong hash = length * Prime64_1;

hash += Mix16Bytes(source + (16 * 0), DefaultSecretUInt64_0, DefaultSecretUInt64_1, seed);
hash += Mix16Bytes(source + (16 * 1), DefaultSecretUInt64_2, DefaultSecretUInt64_3, seed);
hash += Mix16Bytes(source + (16 * 2), DefaultSecretUInt64_4, DefaultSecretUInt64_5, seed);
hash += Mix16Bytes(source + (16 * 3), DefaultSecretUInt64_6, DefaultSecretUInt64_7, seed);
hash += Mix16Bytes(source + (16 * 4), DefaultSecretUInt64_8, DefaultSecretUInt64_9, seed);
hash += Mix16Bytes(source + (16 * 5), DefaultSecretUInt64_10, DefaultSecretUInt64_11, seed);
hash += Mix16Bytes(source + (16 * 6), DefaultSecretUInt64_12, DefaultSecretUInt64_13, seed);
hash += Mix16Bytes(source + (16 * 7), DefaultSecretUInt64_14, DefaultSecretUInt64_15, seed);

hash = FastAvalanche(hash);

switch ((length - (16 * 8)) / 16)
{
default: // case 7
Debug.Assert((length - 16 * 8) / 16 == 7);
hash += Mix16Bytes(source + (16 * 14), DefaultSecret3UInt64_12, DefaultSecret3UInt64_13, seed);
goto case 6;
case 6:
hash += Mix16Bytes(source + (16 * 13), DefaultSecret3UInt64_10, DefaultSecret3UInt64_11, seed);
goto case 5;
case 5:
hash += Mix16Bytes(source + (16 * 12), DefaultSecret3UInt64_8, DefaultSecret3UInt64_9, seed);
goto case 4;
case 4:
hash += Mix16Bytes(source + (16 * 11), DefaultSecret3UInt64_6, DefaultSecret3UInt64_7, seed);
goto case 3;
case 3:
hash += Mix16Bytes(source + (16 * 10), DefaultSecret3UInt64_4, DefaultSecret3UInt64_5, seed);
goto case 2;
case 2:
hash += Mix16Bytes(source + (16 * 9), DefaultSecret3UInt64_2, DefaultSecret3UInt64_3, seed);
goto case 1;
case 1:
hash += Mix16Bytes(source + (16 * 8), DefaultSecret3UInt64_0, DefaultSecret3UInt64_1, seed);
goto case 0;
case 0:
hash += Mix16Bytes(source + length - 16, 0x7378D9C97E9FC831, 0xEBD33483ACC5EA64, seed); // DefaultSecret[119], DefaultSecret[127]
break;
}

return FastAvalanche(hash);
}

private static ulong HashLengthOver240(byte* source, uint length, ulong seed)
{
Debug.Assert(length > 240);

fixed (byte* defaultSecret = &MemoryMarshal.GetReference(DefaultSecret))
{
byte* secret = defaultSecret;
if (seed != 0)
{
byte* customSecret = stackalloc byte[SecretLengthBytes];
DeriveSecretFromSeed(customSecret, seed);
secret = customSecret;
}

ulong* accumulators = stackalloc ulong[AccumulatorCount];
InitializeAccumulators(accumulators);

HashInternalLoop(accumulators, source, length, secret);

return MergeAccumulators(accumulators, secret + 11, length * Prime64_1);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

namespace System.IO.Hashing
{
#if !SYSTEM_PRIVATE_CORELIB
/// <summary>Shared implementation of the XXH3 hash algorithm for 64-bit in <see cref="XxHash3"/> and <see cref="XxHash128"/> version.</summary>
#endif
#if NET
[SkipLocalsInit]
#endif
Expand Down Expand Up @@ -426,7 +428,7 @@ public static ulong MergeAccumulators(ulong* accumulators, byte* secret, ulong s
result64 += Multiply64To128ThenFold(accumulators[4] ^ ReadUInt64LE(secret + 32), accumulators[5] ^ ReadUInt64LE(secret + 40));
result64 += Multiply64To128ThenFold(accumulators[6] ^ ReadUInt64LE(secret + 48), accumulators[7] ^ ReadUInt64LE(secret + 56));

return Avalanche(result64);
return FastAvalanche(result64);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -439,9 +441,20 @@ public static ulong Mix16Bytes(byte* source, ulong secretLow, ulong secretHigh,
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong Multiply32To64(uint v1, uint v2) => (ulong)v1 * v2;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static ulong Avalanche(ulong hash)
{
hash ^= hash >> 33;
hash *= Prime64_2;
hash ^= hash >> 29;
hash *= Prime64_3;
hash ^= hash >> 32;
return hash;
}

/// <summary>"This is a fast avalanche stage, suitable when input bits are already partially mixed."</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong Avalanche(ulong hash)
public static ulong FastAvalanche(ulong hash)
{
hash = XorShift(hash, 37);
hash *= 0x165667919E3779F9;
Expand Down
7 changes: 6 additions & 1 deletion src/libraries/System.IO.Hashing/src/System.IO.Hashing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ System.IO.Hashing.XxHash32</PackageDescription>
<Compile Include="System\IO\Hashing\Crc64.Table.cs" />
<Compile Include="System\IO\Hashing\NonCryptographicHashAlgorithm.cs" />
<Compile Include="System\IO\Hashing\XxHash128.cs" />
<Compile Include="$(CommonPath)System\IO\Hashing\XxHash3.Common.cs">
<Link>Common\System\IO\Hashing\XxHash3.Common.cs</Link>
</Compile>
<Compile Include="System\IO\Hashing\XxHash3.cs" />
<Compile Include="System\IO\Hashing\XxHash32.cs" />
<Compile Include="System\IO\Hashing\XxHash32.State.cs" />
<Compile Include="System\IO\Hashing\XxHash64.cs" />
<Compile Include="System\IO\Hashing\XxHash64.State.cs" />
<Compile Include="System\IO\Hashing\XxHashShared.cs" />
<Compile Include="$(CommonPath)System\IO\Hashing\XxHashShared.cs">
<Link>Common\System\IO\Hashing\XxHashShared.cs</Link>
</Compile>
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
Expand Down
Loading
Loading