未验证 提交 1adda686 编写于 作者: S Stephen Toub 提交者: GitHub

Add XxHash3 to System.IO.Hashing (#76641)

* Add XxHash3

* Add more XxHash3 test cases

* Cull back volume of test cases, and line wrap to help editor performance

* Address PR feedback
Co-authored-by: NEugene Sirkiza <esirkiza@microsoft.com>
上级 fc0af103
......@@ -602,30 +602,38 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
License notice for xxHash
-------------------------
xxHash Library
Copyright (c) 2012-2014, Yann Collet
All rights reserved.
xxHash - Extremely Fast Hash algorithm
Header File
Copyright (C) 2012-2021 Yann Collet
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
BSD 2-Clause License (https://www.opensource.org/licenses/bsd-license.php)
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
You can contact the author at:
- xxHash homepage: https://www.xxhash.com
- xxHash source repository: https://github.com/Cyan4973/xxHash
License notice for Berkeley SoftFloat Release 3e
------------------------------------------------
......
......@@ -15,6 +15,7 @@ System.IO.Hashing.XxHash32</PackageDescription>
<Compile Include="System\IO\Hashing\Crc32.Table.cs" />
<Compile Include="System\IO\Hashing\Crc64.cs" />
<Compile Include="System\IO\Hashing\Crc64.Table.cs" />
<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" />
......
......@@ -142,7 +142,9 @@ public static bool TryHash(ReadOnlySpan<byte> source, Span<byte> destination, ou
public static int Hash(ReadOnlySpan<byte> source, Span<byte> destination)
{
if (destination.Length < Size)
throw new ArgumentException(SR.Argument_DestinationTooShort, nameof(destination));
{
ThrowDestinationTooShort();
}
return StaticHash(source, destination);
}
......
......@@ -140,7 +140,9 @@ public static bool TryHash(ReadOnlySpan<byte> source, Span<byte> destination, ou
public static int Hash(ReadOnlySpan<byte> source, Span<byte> destination)
{
if (destination.Length < Size)
throw new ArgumentException(SR.Argument_DestinationTooShort, nameof(destination));
{
ThrowDestinationTooShort();
}
return StaticHash(source, destination);
}
......
......@@ -4,6 +4,7 @@
using System.Buffers;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
......@@ -226,7 +227,7 @@ public int GetCurrentHash(Span<byte> destination)
{
if (destination.Length < HashLengthInBytes)
{
throw new ArgumentException(SR.Argument_DestinationTooShort, nameof(destination));
ThrowDestinationTooShort();
}
GetCurrentHashCore(destination.Slice(0, HashLengthInBytes));
......@@ -288,7 +289,7 @@ public int GetHashAndReset(Span<byte> destination)
{
if (destination.Length < HashLengthInBytes)
{
throw new ArgumentException(SR.Argument_DestinationTooShort, nameof(destination));
ThrowDestinationTooShort();
}
GetHashAndResetCore(destination.Slice(0, HashLengthInBytes));
......@@ -341,5 +342,9 @@ public override int GetHashCode()
{
throw new NotSupportedException(SR.NotSupported_GetHashCode);
}
[DoesNotReturn]
private protected static void ThrowDestinationTooShort() =>
throw new ArgumentException(SR.Argument_DestinationTooShort, "destination");
}
}
......@@ -13,14 +13,14 @@ namespace System.IO.Hashing
{
public sealed partial class XxHash32
{
internal const uint Prime32_1 = 0x9E3779B1;
internal const uint Prime32_2 = 0x85EBCA77;
internal const uint Prime32_3 = 0xC2B2AE3D;
internal const uint Prime32_4 = 0x27D4EB2F;
internal const uint Prime32_5 = 0x165667B1;
private struct State
{
private const uint Prime32_1 = 0x9E3779B1;
private const uint Prime32_2 = 0x85EBCA77;
private const uint Prime32_3 = 0xC2B2AE3D;
private const uint Prime32_4 = 0x27D4EB2F;
private const uint Prime32_5 = 0x165667B1;
private uint _acc1;
private uint _acc2;
private uint _acc3;
......
......@@ -209,7 +209,9 @@ public static bool TryHash(ReadOnlySpan<byte> source, Span<byte> destination, ou
public static int Hash(ReadOnlySpan<byte> source, Span<byte> destination, int seed = 0)
{
if (destination.Length < HashSize)
throw new ArgumentException(SR.Argument_DestinationTooShort, nameof(destination));
{
ThrowDestinationTooShort();
}
return StaticHash(source, destination, seed);
}
......
......@@ -4,6 +4,7 @@
using System.Buffers.Binary;
using System.Diagnostics;
using System.Numerics;
using System.Runtime.CompilerServices;
// Implemented from the specification at
// https://github.com/Cyan4973/xxHash/blob/f9155bd4c57e2270a4ffbb176485e5d713de1c9b/doc/xxhash_spec.md
......@@ -12,14 +13,25 @@ namespace System.IO.Hashing
{
public sealed partial class XxHash64
{
private struct State
internal const ulong Prime64_1 = 0x9E3779B185EBCA87;
internal const ulong Prime64_2 = 0xC2B2AE3D27D4EB4F;
internal const ulong Prime64_3 = 0x165667B19E3779F9;
internal const ulong Prime64_4 = 0x85EBCA77C2B2AE63;
internal const ulong Prime64_5 = 0x27D4EB2F165667C5;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static ulong Avalanche(ulong hash)
{
private const ulong Prime64_1 = 0x9E3779B185EBCA87;
private const ulong Prime64_2 = 0xC2B2AE3D27D4EB4F;
private const ulong Prime64_3 = 0x165667B19E3779F9;
private const ulong Prime64_4 = 0x85EBCA77C2B2AE63;
private const ulong Prime64_5 = 0x27D4EB2F165667C5;
hash ^= hash >> 33;
hash *= Prime64_2;
hash ^= hash >> 29;
hash *= Prime64_3;
hash ^= hash >> 32;
return hash;
}
private struct State
{
private ulong _acc1;
private ulong _acc2;
private ulong _acc3;
......@@ -127,13 +139,7 @@ private static ulong ApplyRound(ulong acc, ulong lane)
acc *= Prime64_1;
}
acc ^= (acc >> 33);
acc *= Prime64_2;
acc ^= (acc >> 29);
acc *= Prime64_3;
acc ^= (acc >> 32);
return acc;
return Avalanche(acc);
}
}
}
......
......@@ -209,7 +209,9 @@ public static bool TryHash(ReadOnlySpan<byte> source, Span<byte> destination, ou
public static int Hash(ReadOnlySpan<byte> source, Span<byte> destination, long seed = 0)
{
if (destination.Length < HashSize)
throw new ArgumentException(SR.Argument_DestinationTooShort, nameof(destination));
{
ThrowDestinationTooShort();
}
return StaticHash(source, destination, seed);
}
......
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetFrameworkMinimum)</TargetFrameworks>
......@@ -8,8 +8,8 @@
<Compile Include="Crc64Tests.cs" />
<Compile Include="NonCryptoHashBaseTests.cs" />
<Compile Include="NonCryptoHashTestDriver.cs" />
<Compile Include="XxHash3Tests.cs" />
<Compile Include="XxHash32Tests.cs" />
<Compile Include="XxHash32Tests.007.cs" />
<Compile Include="XxHash32Tests.f00d.cs" />
<Compile Include="XxHash64Tests.cs" />
<Compile Include="XxHash64Tests.007.cs" />
......@@ -21,4 +21,7 @@
<ItemGroup>
<ProjectReference Include="..\src\System.IO.Hashing.csproj" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
<PackageReference Include="System.ValueTuple" Version="$(SystemValueTupleVersion)" />
</ItemGroup>
</Project>
因为 它太大了无法显示 source diff 。你可以改为 查看blob
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册