Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 5.6k
Adding AVX512 path to Base64 encoding/Decoding#92241
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
911b517
Adding AVX512 path to Base64 encoding/Decoding
DeepakRajendrakumaran 7b351ad
Addressing review Comments.
DeepakRajendrakumaran 7fb00e3
Removing fallback path.
DeepakRajendrakumaran 2969ddb
Updating Third Party Notice.
DeepakRajendrakumaran fc05beb
Addressing review comments
DeepakRajendrakumaran File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
85 changes: 84 additions & 1 deletion
85 src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Decoder.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
85 changes: 84 additions & 1 deletion
85 src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Encoder.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 |
|---|---|---|
| @@ -67,7 +67,16 @@ public static unsafe OperationStatus EncodeToUtf8(ReadOnlySpan<byte> bytes, Span | ||
| if (maxSrcLength >= 16) | ||
| { | ||
| byte* end = srcMax - 32; | ||
| byte* end = srcMax - 64; | ||
| if (Vector512.IsHardwareAccelerated && Avx512Vbmi.IsSupported && (end >= src)) | ||
| { | ||
| Avx512Encode(ref src, ref dest, end, maxSrcLength, destLength, srcBytes, destBytes); | ||
| if (src == srcEnd) | ||
| goto DoneExit; | ||
| } | ||
| end = srcMax - 64; | ||
| if (Avx2.IsSupported && (end >= src)) | ||
| { | ||
| Avx2Encode(ref src, ref dest, end, maxSrcLength, destLength, srcBytes, destBytes); | ||
| @@ -226,6 +235,80 @@ public static unsafe OperationStatus EncodeToUtf8InPlace(Span<byte> buffer, int | ||
| } | ||
| } | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| [CompExactlyDependsOn(typeof(Avx512BW))] | ||
| [CompExactlyDependsOn(typeof(Avx512Vbmi))] | ||
| private static unsafe void Avx512Encode(ref byte* srcBytes, ref byte* destBytes, byte* srcEnd, int sourceLength, int destLength, byte* srcStart, byte* destStart) | ||
| { | ||
| // Reference for VBMI implementation : https://github.com/WojciechMula/base64simd/tree/master/encode | ||
| // If we have AVX512 support, pick off 48 bytes at a time for as long as we can. | ||
| // But because we read 64 bytes at a time, ensure we have enough room to do a | ||
| // full 64-byte read without segfaulting. | ||
| byte* src = srcBytes; | ||
| byte* dest = destBytes; | ||
| // The JIT won't hoist these "constants", so help it | ||
| Vector512<sbyte> shuffleVecVbmi = Vector512.Create( | ||
| 0x01020001, 0x04050304, 0x07080607, 0x0a0b090a, | ||
| 0x0d0e0c0d, 0x10110f10, 0x13141213, 0x16171516, | ||
| 0x191a1819, 0x1c1d1b1c, 0x1f201e1f, 0x22232122, | ||
| 0x25262425, 0x28292728, 0x2b2c2a2b, 0x2e2f2d2e).AsSByte(); | ||
| Vector512<sbyte> vbmiLookup = Vector512.Create("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"u8).AsSByte(); | ||
| Vector512<ushort> maskAC = Vector512.Create((uint)0x0fc0fc00).AsUInt16(); | ||
| Vector512<uint> maskBB = Vector512.Create((uint)0x3f003f00); | ||
| Vector512<ushort> shiftAC = Vector512.Create((uint)0x0006000a).AsUInt16(); | ||
| Vector512<ushort> shiftBB = Vector512.Create((uint)0x00080004).AsUInt16(); | ||
| AssertRead<Vector256<sbyte>>(src, srcStart, sourceLength); | ||
| // This algorithm requires AVX512VBMI support. | ||
| // Vbmi was first introduced in CannonLake and is avaialable from IceLake on. | ||
| // str = [...|PONM|LKJI|HGFE|DCBA] | ||
| Vector512<sbyte> str = Vector512.Load(src).AsSByte(); | ||
| while (true) | ||
MihaZupan marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| { | ||
| // Step 1 : Split 48 bytes into 64 bytes with each byte using 6-bits from input | ||
| // str = [...|KLJK|HIGH|EFDE|BCAB] | ||
| str = Avx512Vbmi.PermuteVar64x8(str, shuffleVecVbmi); | ||
| // TO-DO- This can be achieved faster with multishift | ||
| // Consider the first 4 bytes - BCAB | ||
| // temp1 = [...|0000cccc|cc000000|aaaaaa00|00000000] | ||
| Vector512<ushort> temp1 = (str.AsUInt16() & maskAC); | ||
| // temp2 = [...|00000000|00cccccc|00000000|00aaaaaa] | ||
| Vector512<ushort> temp2 = Avx512BW.ShiftRightLogicalVariable(temp1, shiftAC).AsUInt16(); | ||
| // temp3 = [...|ccdddddd|00000000|aabbbbbb|cccc0000] | ||
| Vector512<ushort> temp3 = Avx512BW.ShiftLeftLogicalVariable(str.AsUInt16(), shiftBB).AsUInt16(); | ||
| // str = [...|00dddddd|00cccccc|00bbbbbb|00aaaaaa] | ||
| str = Vector512.ConditionalSelect(maskBB, temp3.AsUInt32(), temp2.AsUInt32()).AsSByte(); | ||
| // Step 2: Now we have the indices calculated. Next step is to use these indices to translate. | ||
| str = Avx512Vbmi.PermuteVar64x8(vbmiLookup, str); | ||
| AssertWrite<Vector512<sbyte>>(dest, destStart, destLength); | ||
| str.Store((sbyte*)dest); | ||
| src += 48; | ||
| dest += 64; | ||
| if (src > srcEnd) | ||
| break; | ||
| AssertRead<Vector512<sbyte>>(src, srcStart, sourceLength); | ||
| str = Vector512.Load(src).AsSByte(); | ||
| } | ||
| srcBytes = src; | ||
| destBytes = dest; | ||
| } | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| [CompExactlyDependsOn(typeof(Avx2))] | ||
| private static unsafe void Avx2Encode(ref byte* srcBytes, ref byte* destBytes, byte* srcEnd, int sourceLength, int destLength, byte* srcStart, byte* destStart) | ||
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.