-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FNV-1a-128 high-speed implementation using LX4Cnh
- Loading branch information
Showing
16 changed files
with
575 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 16 | ||
VisualStudioVersion = 16.0.31402.337 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FNV-1a", "src\FNV-1a.csproj", "{5A2DDFC2-9402-44FB-BFF6-5071F15615B2}" | ||
EndProject | ||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests", "tests\UnitTest\UnitTest.csproj", "{A93ACA73-DA16-461D-BF06-9941671C7924}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Benchmark", "tests\Benchmark\Benchmark.csproj", "{39EEFDAE-C950-497A-A435-A05175D62800}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{5A2DDFC2-9402-44FB-BFF6-5071F15615B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{5A2DDFC2-9402-44FB-BFF6-5071F15615B2}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{5A2DDFC2-9402-44FB-BFF6-5071F15615B2}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{5A2DDFC2-9402-44FB-BFF6-5071F15615B2}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{A93ACA73-DA16-461D-BF06-9941671C7924}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{A93ACA73-DA16-461D-BF06-9941671C7924}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{A93ACA73-DA16-461D-BF06-9941671C7924}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{A93ACA73-DA16-461D-BF06-9941671C7924}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{39EEFDAE-C950-497A-A435-A05175D62800}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{39EEFDAE-C950-497A-A435-A05175D62800}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{39EEFDAE-C950-497A-A435-A05175D62800}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{39EEFDAE-C950-497A-A435-A05175D62800}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {577340DB-CBCA-4482-8E93-9FFB42035889} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2021 Denis Kuzmin <x-3F@outlook.com> github/3F | ||
Copyright (c) sandbox contributors https://github.com/3F/sandbox/graphs/contributors | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
*FNV-1a* high-speed implementations | ||
|
||
``` | ||
Copyright (c) 2021 Denis Kuzmin <x-3F@outlook.com> github/3F | ||
``` | ||
|
||
[ [ ☕ ](https://3F.github.io/Donation/) ] | ||
|
||
Part of https://twitter.com/github3F/status/1403748080760111106 | ||
|
||
## speed | ||
|
||
### FNV-1a-128 using LX4Cnh | ||
|
||
120 Unicode (UTF-16) characters as follow: | ||
|
||
`*LodgeX4CorrNoHigh* (LX4Cnh) algorithm of the high-speed multiplications of **128-bit** numbers (full range, 128 × 128).` | ||
|
||
[](#) | ||
|
||
> 128-bit hash value: 0x8e719ac9080952dec9c90a46279bfcc9 | ||
[](#) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net472;netcoreapp2.1</TargetFrameworks> | ||
<RootNamespace>net.r_eg.sandbox.Hash</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<CheckForOverflowUnderflow>false</CheckForOverflowUnderflow> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="..\..\..\algorithms\LodgeX4CorrNoHigh\src\LodgeX4CorrNoHigh.cs" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2021 Denis Kuzmin <x-3F@outlook.com> github/3F | ||
* Copyright (c) sandbox contributors https://github.com/3F/sandbox/graphs/contributors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
using net.r_eg.sandbox.algorithms; | ||
|
||
namespace net.r_eg.sandbox.Hash | ||
{ | ||
public static class FNV1a | ||
{ | ||
private const ulong OFS64 = 0xcbf29ce484222325; | ||
private const ulong PRIME64 = 0x00000100000001B3; | ||
|
||
private const uint P128_B = 0x01000000, P128_D = 0x0000013B, P128_BD = 0xFFFEC5; | ||
|
||
public static ulong GetHash128LX4Cnh(string input, out ulong low) | ||
{ | ||
ulong a = 0x6c62272e, b = 0x07bb0142, c = 0x62b82175, d = 0x6295c58d; | ||
|
||
ulong f = 0, fLm = 0; | ||
unchecked | ||
{ | ||
for(int i = 0; i < input.Length; ++i) | ||
{ | ||
d ^= input[i]; | ||
|
||
// Below is an optimized implementation (limited) of the LX4Cnh algorithm specially for Fnv1a128 | ||
// (c) Denis Kuzmin <x-3F@outlook.com> github/3F | ||
|
||
f = b * P128_B; | ||
|
||
ulong v = (uint)f; | ||
|
||
f = (f >> 32) + v; | ||
|
||
if(a > b) | ||
{ | ||
f += (uint)((a - b) * P128_B); | ||
} | ||
else if(a < b) | ||
{ | ||
f -= (uint)((b - a) * P128_B); | ||
} | ||
|
||
ulong fHigh = (f << 32) + (uint)v; | ||
ulong r2 = d * P128_D; | ||
|
||
v = (r2 >> 32) + (r2 & 0xFFF_FFFF_FFFF_FFFF); | ||
|
||
f = (r2 & 0xF000_0000_0000_0000) >> 32; | ||
|
||
if(c > d) | ||
{ | ||
fLm = v; | ||
v += (c - d) * P128_D; | ||
if(fLm > v) f += 0x100000000; | ||
} | ||
else if(c < d) | ||
{ | ||
fLm = v; | ||
v -= (d - c) * P128_D; | ||
if(fLm < v) f -= 0x100000000; | ||
} | ||
|
||
fLm = (((ulong)(uint)v) << 32) + (uint)r2; | ||
|
||
f = fHigh + fLm + f + (v >> 32); | ||
|
||
fHigh = (a << 32) + b; //fa | ||
v = (c << 32) + d; //fb | ||
|
||
if(fHigh < v) | ||
{ | ||
f += (v - fHigh) * P128_BD; | ||
} | ||
else if(fHigh > v) | ||
{ | ||
f -= (fHigh - v) * P128_BD; | ||
} | ||
|
||
a = f >> 32; | ||
b = (uint)f; | ||
c = fLm >> 32; | ||
d = (uint)fLm; | ||
} | ||
} | ||
|
||
low = fLm; | ||
return f; | ||
} | ||
|
||
public static ulong GetHash64(string input) | ||
{ | ||
ulong hash = OFS64; | ||
|
||
unchecked | ||
{ | ||
for(int i = 0; i < input.Length; ++i) | ||
{ | ||
hash = (hash ^ input[i]) * PRIME64; | ||
} | ||
} | ||
|
||
return hash; | ||
} | ||
|
||
public static ulong GetHash128Call(string input, out ulong low) | ||
{ | ||
uint a = 0x6c62272e, b = 0x07bb0142, c = 0x62b82175, d = 0x6295c58d; | ||
|
||
ulong f = 0; low = 0; | ||
unchecked | ||
{ | ||
for(int i = 0; i < input.Length; ++i) | ||
{ | ||
d ^= input[i]; | ||
|
||
f = LodgeX4CorrNoHigh.Multiply(a, b, c, d, 0, P128_B, 0, P128_D, out low); | ||
|
||
a = (uint)(f >> 32); | ||
b = (uint)f; | ||
c = (uint)(low >> 32); | ||
d = (uint)low; | ||
} | ||
} | ||
|
||
return f; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFrameworks>net472;net5.0</TargetFrameworks> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<IsPackable>false</IsPackable> | ||
<RootNamespace>net.r_eg.sandbox.Hash.Tests</RootNamespace> | ||
<NoWarn>1701;1702;IDE0059</NoWarn> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="BenchmarkDotNet" Version="0.13.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\FNV-1a.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.