Skip to content

Commit

Permalink
Merge pull request #47 from koculu/46-enhancement-x86-support
Browse files Browse the repository at this point in the history
Add Crc32 x86 support.
  • Loading branch information
koculu authored Sep 13, 2023
2 parents 58a5dcf + fbdea76 commit 2ae6b30
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/ZoneTree/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<Authors>Ahmed Yasin Koculu</Authors>
<PackageId>ZoneTree</PackageId>
<Title>ZoneTree</Title>
<ProductVersion>1.6.6.0</ProductVersion>
<Version>1.6.6.0</Version>
<ProductVersion>1.6.7.0</ProductVersion>
<Version>1.6.7.0</Version>
<Authors>Ahmed Yasin Koculu</Authors>
<AssemblyTitle>ZoneTree</AssemblyTitle>
<Description>ZoneTree is a persistent, high-performance, transactional, ACID-compliant ordered key-value database for NET. It can operate in memory or on local/cloud storage.</Description>
Expand Down
49 changes: 49 additions & 0 deletions src/ZoneTree/WAL/Crc32Computer_SSE42_X86.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics.X86;

namespace Tenray.ZoneTree.WAL;

public sealed class Crc32Computer_SSE42_X86
{
public static bool IsSupported => Sse42.IsSupported;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint Compute(uint crc, ulong data)
{
var result = (uint)Sse42.Crc32(crc, (uint)data);
return Sse42.Crc32(crc, (uint)(data >> 32));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint Compute(uint crc, uint data)
{
return Sse42.Crc32(crc, data);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint Compute(uint crc, int data)
{
return Sse42.Crc32(crc, (uint)data);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint Compute(uint crc, byte[] data)
{
var off = 0;
var len = data.Length;
while (len >= 4)
{
crc = (uint)Sse42.X64.Crc32(crc, BitConverter.ToUInt32(data, off));
off += 4;
len -= 4;
}

while (len > 0)
{
crc = Sse42.Crc32(crc, data[off]);
off++;
len--;
}
return crc;
}
}
10 changes: 10 additions & 0 deletions src/ZoneTree/WAL/LogEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ public uint CreateChecksum()
return crc32;
}

if (Crc32Computer_SSE42_X86.IsSupported)
{
crc32 = Crc32Computer_SSE42_X86.Compute(crc32, (ulong)OpIndex);
crc32 = Crc32Computer_SSE42_X86.Compute(crc32, KeyLength);
crc32 = Crc32Computer_SSE42_X86.Compute(crc32, ValueLength);
crc32 = Crc32Computer_SSE42_X86.Compute(crc32, Key);
crc32 = Crc32Computer_SSE42_X86.Compute(crc32, Value);
return crc32;
}

if (Crc32Computer_ARM64.IsSupported)
{
crc32 = Crc32Computer_ARM64.Compute(crc32, (ulong)OpIndex);
Expand Down

0 comments on commit 2ae6b30

Please sign in to comment.