-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Add vectorization to improve CRC32 performance #83321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+389
−0
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
03dbc18
Add x86 intrinsics to improve CRC32 performance
brantburnett 24114dc
Use vector operator overloads and ref byte indexing
brantburnett 7df1df4
Fix error and remove ref ROS
brantburnett ddfbe79
Drop aggressive inlining and legibility improvements
brantburnett 340317e
Don't overcheck intrinsics
brantburnett 49f970d
First pass at ARM support
brantburnett 6bacc2a
Merge branch 'main' into crc32-x86
brantburnett 068e79f
ARM tweaks
brantburnett c38eac8
A bit of cleanup for legibility
brantburnett 12563ec
A little more cleanup
brantburnett 3c9e1d7
Add license notices
brantburnett 3b7c981
Move vector shift right to helper function
brantburnett eebae5e
A bit of cleanup
brantburnett 7122e02
Use System.Diagnostics.UnreachableException
brantburnett ec5ed7c
Use ReadUnaligned for ARM CRC
brantburnett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
69 changes: 69 additions & 0 deletions
69
src/libraries/System.IO.Hashing/src/System/IO/Hashing/Crc32.Arm.cs
This file contains hidden or 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,69 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using ArmCrc = System.Runtime.Intrinsics.Arm.Crc32; | ||
|
||
namespace System.IO.Hashing | ||
{ | ||
public partial class Crc32 | ||
{ | ||
private static uint UpdateScalarArm64(uint crc, ReadOnlySpan<byte> source) | ||
{ | ||
Debug.Assert(ArmCrc.Arm64.IsSupported, "ARM CRC support is required."); | ||
|
||
// Compute in 8 byte chunks | ||
if (source.Length >= sizeof(ulong)) | ||
{ | ||
ref byte ptr = ref MemoryMarshal.GetReference(source); | ||
int longLength = source.Length & ~0x7; // Exclude trailing bytes not a multiple of 8 | ||
|
||
for (int i = 0; i < longLength; i += sizeof(ulong)) | ||
{ | ||
crc = ArmCrc.Arm64.ComputeCrc32(crc, | ||
Unsafe.ReadUnaligned<ulong>(ref Unsafe.Add(ref ptr, i))); | ||
} | ||
|
||
source = source.Slice(longLength); | ||
} | ||
|
||
// Compute remaining bytes | ||
for (int i = 0; i < source.Length; i++) | ||
{ | ||
crc = ArmCrc.ComputeCrc32(crc, source[i]); | ||
} | ||
|
||
return crc; | ||
} | ||
|
||
private static uint UpdateScalarArm32(uint crc, ReadOnlySpan<byte> source) | ||
{ | ||
Debug.Assert(ArmCrc.IsSupported, "ARM CRC support is required."); | ||
|
||
// Compute in 4 byte chunks | ||
if (source.Length >= sizeof(uint)) | ||
{ | ||
ref byte ptr = ref MemoryMarshal.GetReference(source); | ||
int intLength = source.Length & ~0x3; // Exclude trailing bytes not a multiple of 4 | ||
|
||
for (int i = 0; i < intLength; i += sizeof(uint)) | ||
{ | ||
crc = ArmCrc.ComputeCrc32(crc, | ||
Unsafe.ReadUnaligned<uint>(ref Unsafe.Add(ref ptr, i))); | ||
} | ||
|
||
source = source.Slice(intLength); | ||
} | ||
|
||
// Compute remaining bytes | ||
for (int i = 0; i < source.Length; i++) | ||
{ | ||
crc = ArmCrc.ComputeCrc32(crc, source[i]); | ||
} | ||
|
||
return crc; | ||
} | ||
} | ||
} |
216 changes: 216 additions & 0 deletions
216
src/libraries/System.IO.Hashing/src/System/IO/Hashing/Crc32.Vectorized.cs
This file contains hidden or 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,216 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.Intrinsics; | ||
using System.Runtime.Intrinsics.X86; | ||
using System.Runtime.InteropServices; | ||
using System.Runtime.Intrinsics.Arm; | ||
using Aes = System.Runtime.Intrinsics.Arm.Aes; | ||
|
||
namespace System.IO.Hashing | ||
{ | ||
public partial class Crc32 | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static Vector128<ulong> CarrylessMultiplyLower(Vector128<ulong> left, Vector128<ulong> right) | ||
{ | ||
if (Pclmulqdq.IsSupported) | ||
{ | ||
return Pclmulqdq.CarrylessMultiply(left, right, 0x00); | ||
} | ||
|
||
if (Aes.IsSupported) | ||
{ | ||
return Aes.PolynomialMultiplyWideningLower(left.GetLower(), right.GetLower()); | ||
} | ||
|
||
ThrowHelper.ThrowUnreachableException(); | ||
return default; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static Vector128<ulong> CarrylessMultiplyUpper(Vector128<ulong> left, Vector128<ulong> right) | ||
{ | ||
if (Pclmulqdq.IsSupported) | ||
{ | ||
return Pclmulqdq.CarrylessMultiply(left, right, 0x11); | ||
} | ||
|
||
if (Aes.IsSupported) | ||
{ | ||
return Aes.PolynomialMultiplyWideningUpper(left, right); | ||
} | ||
|
||
ThrowHelper.ThrowUnreachableException(); | ||
return default; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static Vector128<ulong> CarrylessMultiplyLeftLowerRightUpper(Vector128<ulong> left, Vector128<ulong> right) | ||
{ | ||
if (Pclmulqdq.IsSupported) | ||
{ | ||
return Pclmulqdq.CarrylessMultiply(left, right, 0x10); | ||
} | ||
|
||
if (Aes.IsSupported) | ||
{ | ||
return Aes.PolynomialMultiplyWideningLower(left.GetLower(), right.GetUpper()); | ||
} | ||
|
||
ThrowHelper.ThrowUnreachableException(); | ||
return default; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static Vector128<ulong> ShiftRightBytesInVector(Vector128<ulong> operand, | ||
[ConstantExpected(Max = (byte)15)] byte numBytesToShift) | ||
{ | ||
if (Sse2.IsSupported) | ||
{ | ||
return Sse2.ShiftRightLogical128BitLane(operand, numBytesToShift); | ||
} | ||
|
||
if (AdvSimd.IsSupported) | ||
{ | ||
return AdvSimd.ExtractVector128(operand.AsByte(), Vector128<byte>.Zero, numBytesToShift).AsUInt64(); | ||
} | ||
|
||
ThrowHelper.ThrowUnreachableException(); | ||
return default; | ||
} | ||
|
||
// We check for little endian byte order here in case we're ever on ARM in big endian mode. | ||
// All of these checks except the length check are elided by JIT, so the JITted implementation | ||
// will be either a return false or a length check against a constant. This means this method | ||
// should be inlined into the caller. | ||
private static bool CanBeVectorized(ReadOnlySpan<byte> source) => | ||
BitConverter.IsLittleEndian | ||
&& (Pclmulqdq.IsSupported || (Aes.IsSupported && AdvSimd.IsSupported)) | ||
&& source.Length >= Vector128<byte>.Count * 4; | ||
|
||
// Processes the bytes in source in 64 byte chunks using carryless/polynomial multiplication intrinsics, | ||
// followed by processing 16 byte chunks, and then processing remaining bytes individually. Requires | ||
// little endian byte order and support for PCLMULQDQ intrinsics on Intel architecture or AES and | ||
// AdvSimd intrinsics on ARM architecture. Based on the algorithm put forth in the Intel paper "Fast CRC | ||
// Computation for Generic Polynomials Using PCLMULQDQ Instruction" in December, 2009. | ||
// https://github.com/intel/isa-l/blob/33a2d9484595c2d6516c920ce39a694c144ddf69/crc/crc32_ieee_by4.asm | ||
// https://github.com/SixLabors/ImageSharp/blob/f4f689ce67ecbcc35cebddba5aacb603e6d1068a/src/ImageSharp/Formats/Png/Zlib/Crc32.cs#L80 | ||
private static uint UpdateVectorized(uint crc, ReadOnlySpan<byte> source) | ||
{ | ||
Debug.Assert(CanBeVectorized(source), "source cannot be vectorized."); | ||
|
||
// Work with a reference to where we're at in the ReadOnlySpan and a local length | ||
// to avoid extraneous range checks. | ||
ref byte srcRef = ref MemoryMarshal.GetReference(source); | ||
int length = source.Length; | ||
|
||
Vector128<ulong> x1 = Vector128.LoadUnsafe(ref srcRef).AsUInt64(); | ||
Vector128<ulong> x2 = Vector128.LoadUnsafe(ref srcRef, 16).AsUInt64(); | ||
Vector128<ulong> x3 = Vector128.LoadUnsafe(ref srcRef, 32).AsUInt64(); | ||
Vector128<ulong> x4 = Vector128.LoadUnsafe(ref srcRef, 48).AsUInt64(); | ||
Vector128<ulong> x5; | ||
|
||
x1 ^= Vector128.CreateScalar(crc).AsUInt64(); | ||
Vector128<ulong> x0 = Vector128.Create(0x0154442bd4UL, 0x01c6e41596UL); // k1, k2 | ||
|
||
srcRef = ref Unsafe.Add(ref srcRef, Vector128<byte>.Count * 4); | ||
length -= Vector128<byte>.Count * 4; | ||
|
||
// Parallel fold blocks of 64, if any. | ||
while (length >= Vector128<byte>.Count * 4) | ||
{ | ||
x5 = CarrylessMultiplyLower(x1, x0); | ||
Vector128<ulong> x6 = CarrylessMultiplyLower(x2, x0); | ||
Vector128<ulong> x7 = CarrylessMultiplyLower(x3, x0); | ||
Vector128<ulong> x8 = CarrylessMultiplyLower(x4, x0); | ||
|
||
x1 = CarrylessMultiplyUpper(x1, x0); | ||
x2 = CarrylessMultiplyUpper(x2, x0); | ||
x3 = CarrylessMultiplyUpper(x3, x0); | ||
x4 = CarrylessMultiplyUpper(x4, x0); | ||
|
||
Vector128<ulong> y5 = Vector128.LoadUnsafe(ref srcRef).AsUInt64(); | ||
Vector128<ulong> y6 = Vector128.LoadUnsafe(ref srcRef, 16).AsUInt64(); | ||
Vector128<ulong> y7 = Vector128.LoadUnsafe(ref srcRef, 32).AsUInt64(); | ||
Vector128<ulong> y8 = Vector128.LoadUnsafe(ref srcRef, 48).AsUInt64(); | ||
|
||
x1 ^= x5; | ||
x2 ^= x6; | ||
x3 ^= x7; | ||
x4 ^= x8; | ||
|
||
x1 ^= y5; | ||
x2 ^= y6; | ||
x3 ^= y7; | ||
x4 ^= y8; | ||
|
||
srcRef = ref Unsafe.Add(ref srcRef, Vector128<byte>.Count * 4); | ||
length -= Vector128<byte>.Count * 4; | ||
} | ||
|
||
// Fold into 128-bits. | ||
x0 = Vector128.Create(0x01751997d0UL, 0x00ccaa009eUL); // k3, k4 | ||
|
||
x5 = CarrylessMultiplyLower(x1, x0); | ||
x1 = CarrylessMultiplyUpper(x1, x0); | ||
x1 ^= x2; | ||
x1 ^= x5; | ||
|
||
x5 = CarrylessMultiplyLower(x1, x0); | ||
x1 = CarrylessMultiplyUpper(x1, x0); | ||
x1 ^= x3; | ||
x1 ^= x5; | ||
|
||
x5 = CarrylessMultiplyLower(x1, x0); | ||
x1 = CarrylessMultiplyUpper(x1, x0); | ||
x1 ^= x4; | ||
x1 ^= x5; | ||
|
||
// Single fold blocks of 16, if any. | ||
while (length >= Vector128<byte>.Count) | ||
{ | ||
x2 = Vector128.LoadUnsafe(ref srcRef).AsUInt64(); | ||
|
||
x5 = CarrylessMultiplyLower(x1, x0); | ||
x1 = CarrylessMultiplyUpper(x1, x0); | ||
x1 ^= x2; | ||
x1 ^= x5; | ||
|
||
srcRef = ref Unsafe.Add(ref srcRef, Vector128<byte>.Count); | ||
length -= Vector128<byte>.Count; | ||
} | ||
|
||
// Fold 128 bits to 64 bits. | ||
x2 = CarrylessMultiplyLeftLowerRightUpper(x1, x0); | ||
x3 = Vector128.Create(~0, 0, ~0, 0).AsUInt64(); | ||
x1 = ShiftRightBytesInVector(x1, 8); | ||
x1 ^= x2; | ||
|
||
x0 = Vector128.CreateScalar(0x0163cd6124UL); // k5, k0 | ||
|
||
x2 = ShiftRightBytesInVector(x1, 4); | ||
x1 &= x3; | ||
x1 = CarrylessMultiplyLower(x1, x0); | ||
x1 ^= x2; | ||
|
||
// Reduce to 32 bits. | ||
x0 = Vector128.Create(0x01db710641UL, 0x01f7011641UL); // polynomial | ||
|
||
x2 = x1 & x3; | ||
x2 = CarrylessMultiplyLeftLowerRightUpper(x2, x0); | ||
x2 &= x3; | ||
x2 = CarrylessMultiplyLower(x2, x0); | ||
x1 ^= x2; | ||
|
||
// Process the remaining bytes, if any | ||
uint result = x1.AsUInt32().GetElement(1); | ||
return length > 0 | ||
? UpdateScalar(result, MemoryMarshal.CreateReadOnlySpan(ref srcRef, length)) | ||
: result; | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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,14 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace System | ||
{ | ||
internal static partial class ThrowHelper | ||
{ | ||
[DoesNotReturn] | ||
internal static void ThrowUnreachableException() => throw new UnreachableException(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.