From 911b517834f6306b253f8bf0031b9a7248387042 Mon Sep 17 00:00:00 2001 From: Deepak Rajendrakumaran Date: Mon, 18 Sep 2023 12:18:55 -0700 Subject: [PATCH 1/5] Adding AVX512 path to Base64 encoding/Decoding --- .../src/System/Buffers/Text/Base64Decoder.cs | 144 +++++++++++++++++- .../src/System/Buffers/Text/Base64Encoder.cs | 125 ++++++++++++++- 2 files changed, 267 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Decoder.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Decoder.cs index 47cf012c0a733..ba1097bb67351 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Decoder.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Decoder.cs @@ -68,7 +68,18 @@ private static unsafe OperationStatus DecodeFromUtf8(ReadOnlySpan utf8, Sp if (maxSrcLength >= 24) { - byte* end = srcMax - 45; + byte* end = srcMax - 88; + if (Vector512.IsHardwareAccelerated && Avx512BW.IsSupported && (end >= src)) + { + Avx512Decode(ref src, ref dest, end, maxSrcLength, destLength, srcBytes, destBytes); + + if (src == srcEnd) + { + goto DoneExit; + } + } + + end = srcMax - 45; if (Avx2.IsSupported && (end >= src)) { Avx2Decode(ref src, ref dest, end, maxSrcLength, destLength, srcBytes, destBytes); @@ -616,6 +627,137 @@ private static OperationStatus DecodeWithWhiteSpaceFromUtf8InPlace(Span ut return status; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [CompExactlyDependsOn(typeof(Avx512BW))] + [CompExactlyDependsOn(typeof(Avx512Vbmi))] + private static unsafe void Avx512Decode(ref byte* srcBytes, ref byte* destBytes, byte* srcEnd, int sourceLength, int destLength, byte* srcStart, byte* destStart) + { + // Reference for VBMI implementation : https://arxiv.org/pdf/1910.05109.pdf + // If we have AVX512 support, pick off 64 bytes at a time for as long as we can, + // but make sure that we quit before seeing any == markers at the end of the + // string. Also, because we write 16 zeroes at the end of the output, ensure + // that there are at least 22 valid bytes of input data remaining to close the + // gap. 64 + 2 + 22 = 88 bytes. + byte* src = srcBytes; + byte* dest = destBytes; + + // The JIT won't hoist these "constants", so help it + // JIT will remove the Non VBMI path constants when VBMI is available and vice-versa. + + // Vbmi path constants + Vector512 vbmiLookup0 = Vector512.Create( + 0x80808080, 0x80808080, 0x80808080, 0x80808080, + 0x80808080, 0x80808080, 0x80808080, 0x80808080, + 0x80808080, 0x80808080, 0x3e808080, 0x3f808080, + 0x37363534, 0x3b3a3938, 0x80803d3c, 0x80808080).AsSByte(); + Vector512 vbmiLookup1 = Vector512.Create( + 0x02010080, 0x06050403, 0x0a090807, 0x0e0d0c0b, + 0x1211100f, 0x16151413, 0x80191817, 0x80808080, + 0x1c1b1a80, 0x201f1e1d, 0x24232221, 0x28272625, + 0x2c2b2a29, 0x302f2e2d, 0x80333231, 0x80808080).AsSByte(); + Vector512 vbmiPackedLanesControl = Vector512.Create( + 0x06000102, 0x090a0405, 0x0c0d0e08, 0x16101112, + 0x191a1415, 0x1c1d1e18, 0x26202122, 0x292a2425, + 0x2c2d2e28, 0x36303132, 0x393a3435, 0x3c3d3e38, + 0x00000000, 0x00000000, 0x00000000, 0x00000000).AsByte(); + + // Non Vbmi path constants + Vector512 lutHi = Vector512.Create( + 0x10, 0x10, 0x01, 0x02, 0x04, 0x08, 0x04, 0x08, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, + 0x10, 0x10, 0x01, 0x02, 0x04, 0x08, 0x04, 0x08, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, + 0x10, 0x10, 0x01, 0x02, 0x04, 0x08, 0x04, 0x08, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, + 0x10, 0x10, 0x01, 0x02, 0x04, 0x08, 0x04, 0x08, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10); + + Vector512 lutLo = Vector512.Create( + 0x15, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x13, 0x1A, 0x1B, 0x1B, 0x1B, 0x1A, + 0x15, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x13, 0x1A, 0x1B, 0x1B, 0x1B, 0x1A, + 0x15, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x13, 0x1A, 0x1B, 0x1B, 0x1B, 0x1A, + 0x15, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x13, 0x1A, 0x1B, 0x1B, 0x1B, 0x1A); + Vector512 mask2F = Vector512.Create((sbyte)'/'); + Vector512 lutShift = Vector512.Create( + 0, 16, 19, 4, -65, -65, -71, -71, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 16, 19, 4, -65, -65, -71, -71, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 16, 19, 4, -65, -65, -71, -71, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 16, 19, 4, -65, -65, -71, -71, 0, 0, 0, 0, 0, 0, 0, 0); + Vector512 packBytesInLaneMask = Vector512.Create( + 2, 1, 0, 6, 5, 4, 10, 9, 8, 14, 13, 12, -1, -1, -1, -1, + 18, 1, 0, 6, 5, 4, 10, 9, 8, 14, 13, 12, -1, -1, -1, -1, + 2, 1, 0, 6, 5, 4, 10, 9, 8, 14, 13, 12, -1, -1, -1, -1, + 2, 1, 0, 6, 5, 4, 10, 9, 8, 14, 13, 12, -1, -1, -1, -1); + Vector512 packLanesControl = Vector512.Create(0, 1, 2, 4, 5, 6, 8, 9, 10, 12, 13, 14, 0, 0, 0, 0); + + // Common path constants + Vector512 mergeConstant0 = Vector512.Create(0x01400140).AsSByte(); + Vector512 mergeConstant1 = Vector512.Create(0x00011000).AsInt16(); + + // The fastest version of this algorithm requires AVX512VBMI support. + // The fallback path uses AVX512BW and AVX512F instructions. + do + { + AssertRead>(src, srcStart, sourceLength); + Vector512 str = Vector512.Load(src).AsSByte(); + + // Step 1: Translate encoded Base64 input to their original indices + // This step also checks for invalid inputs and exits. + // After this, we have indices which are verified to have upper 2 bits set to 0 in each byte. + // origIndex = [...|00dddddd|00cccccc|00bbbbbb|00aaaaaa] + Vector512 origIndex; + if (Avx512Vbmi.IsSupported) + { + origIndex = Avx512Vbmi.PermuteVar64x8x2(vbmiLookup0, str, vbmiLookup1); + ulong errorMask = (Vector512.BitwiseOr(origIndex.AsInt32(), str.AsInt32()).AsSByte()).ExtractMostSignificantBits(); + if (errorMask != 0) + { + break; + } + } + else + { + Vector512 hiNibbles = Vector512.BitwiseAnd(Avx512F.ShiftRightLogical(str.AsInt32(), 4).AsSByte(), mask2F); + + Vector512 loNibbles = Vector512.BitwiseAnd(str, mask2F); + Vector512 hi = Avx512BW.Shuffle(lutHi, hiNibbles); + Vector512 lo = Avx512BW.Shuffle(lutLo, loNibbles); + if (!Vector512.EqualsAll(Vector512.BitwiseAnd(hi, lo), Vector512.Zero)) + { + break; + } + Vector512 eq2F = Vector512.Equals(str, mask2F); + Vector512 shift = Avx512BW.Shuffle(lutShift, Vector512.Add(eq2F, hiNibbles)); + origIndex = Vector512.Add(str, shift); + } + + + + // Step 2: Now we need to reshuffle bits to remove the 0 bits. + // multiAdd1: [...|0000cccc|ccdddddd|0000aaaa|aabbbbbb] + Vector512 multiAdd1 = Avx512BW.MultiplyAddAdjacent(origIndex.AsByte(), mergeConstant0); + // multiAdd1: [...|00000000|aaaaaabb|bbbbcccc|ccdddddd] + Vector512 multiAdd2 = Avx512BW.MultiplyAddAdjacent(multiAdd1, mergeConstant1); + + // Step 3: Pack 48 bytes + if (Avx512Vbmi.IsSupported) + { + str = Avx512Vbmi.PermuteVar64x8(multiAdd2.AsByte(), vbmiPackedLanesControl).AsSByte(); + } + else + { + Vector512 packed = Avx512BW.Shuffle(multiAdd2.AsSByte(), packBytesInLaneMask).AsInt32(); + str = Avx512F.PermuteVar16x32(packed, packLanesControl).AsSByte(); + + } + + AssertWrite>(dest, destStart, destLength); + Vector512.Store(str.AsByte(), dest); + src += 64; + dest += 48; + } + while (src <= srcEnd); + + srcBytes = src; + destBytes = dest; + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] [CompExactlyDependsOn(typeof(Avx2))] private static unsafe void Avx2Decode(ref byte* srcBytes, ref byte* destBytes, byte* srcEnd, int sourceLength, int destLength, byte* srcStart, byte* destStart) diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Encoder.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Encoder.cs index 077dbde6f00e1..2d0fa1b14e341 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Encoder.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Encoder.cs @@ -67,7 +67,16 @@ public static unsafe OperationStatus EncodeToUtf8(ReadOnlySpan bytes, Span if (maxSrcLength >= 16) { - byte* end = srcMax - 32; + byte* end = srcMax - 64; + if (Vector512.IsHardwareAccelerated && Avx512BW.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,120 @@ public static unsafe OperationStatus EncodeToUtf8InPlace(Span 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://arxiv.org/pdf/1910.05109.pdf + // 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 + // JIT will remove the Non VBMI path constants when VBMI is available and vice-versa. + + // VBMI path constants + Vector512 shuffleVecVbmi = Vector512.Create( + 0x01020001, 0x04050304, 0x07080607, 0x0a0b090a, + 0x0d0e0c0d, 0x10110f10, 0x13141213, 0x16171516, + 0x191a1819, 0x1c1d1b1c, 0x1f201e1f, 0x22232122, + 0x25262425, 0x28292728, 0x2b2c2a2b, 0x2e2f2d2e).AsSByte(); + Vector512 vbmiLookup = Vector512.Create( + (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F', (byte)'G', (byte)'H', (byte)'I', (byte)'J', (byte)'K', (byte)'L', (byte)'M', (byte)'N', (byte)'O', (byte)'P', + (byte)'Q', (byte)'R', (byte)'S', (byte)'T', (byte)'U', (byte)'V', (byte)'W', (byte)'X', (byte)'Y', (byte)'Z', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f', + (byte)'g', (byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l', (byte)'m', (byte)'n', (byte)'o', (byte)'p', (byte)'q', (byte)'r', (byte)'s', (byte)'t', (byte)'u', (byte)'v', + (byte)'w', (byte)'x', (byte)'y', (byte)'z', (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7', (byte)'8', (byte)'9', (byte)'+', (byte)'/').AsSByte(); + + // Non VBMI path constants + Vector512 rearrangeVec = Vector512.Create(0, 1, 2, -1, 3, 4, 5, -1, 6, 7, 8, -1, 9, 10, 11, -1); + Vector512 shuffleVec = Vector512.Create(0x01020001, 0x04050304, 0x07080607, 0x0a0b090a, 0x01020001, 0x04050304, 0x07080607, 0x0a0b090a, + 0x01020001, 0x04050304, 0x07080607, 0x0a0b090a, 0x01020001, 0x04050304, 0x07080607, 0x0a0b090a).AsSByte(); + Vector512 lut = Vector512.Create( + 65, 71, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -19, -16, 0, 0, + 65, 71, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -19, -16, 0, 0, + 65, 71, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -19, -16, 0, 0, + 65, 71, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -19, -16, 0, 0); + Vector512 const51 = Vector512.Create((byte)51); + Vector512 const25 = Vector512.Create((sbyte)25); + + // Common constants + Vector512 maskAC = Vector512.Create((uint)0x0fc0fc00).AsUInt16(); + Vector512 maskBB = Vector512.Create((uint)0x3f003f00); + Vector512 shiftAC = Vector512.Create((uint)0x0006000a).AsUInt16(); + Vector512 shiftBB = Vector512.Create((uint)0x00080004).AsUInt16(); + + AssertRead>(src, srcStart, sourceLength); + // The fastest version of this algorithm requires AVX512VBMI support. + // The fallback path uses AVX512BW and AVX512F instructions. + // Both versions work on the input data as four 128 byte lanes. + // str = [...|PONM|LKJI|HGFE|DCBA] + Vector512 str = Vector512.Load(src).AsSByte(); + + while (true) + { + // Step 1 : Split 48 bytes into 64 bytes with each byte using 6-bits from input + if (Avx512Vbmi.IsSupported) + { + // str = [...|KLJK|HIGH|EFDE|BCAB] + str = Avx512Vbmi.PermuteVar64x8(str, shuffleVecVbmi); + } + else + { + // shuf1 = [...|0000|LKJI|HGFE|DCBA] + Vector512 shuf1 = Avx512F.PermuteVar16x32(str.AsInt32(), rearrangeVec).AsSByte(); + // str = [...|KLJK|HIGH|EFDE|BCAB] + str = Avx512BW.Shuffle(shuf1, shuffleVec); + } + + // TO-DO- This can be achieved faster with multishift + // Consider the first 4 bytes - BCAB + // temp1 = [...|0000cccc|cc000000|aaaaaa00|00000000] + Vector512 temp1 = (str.AsUInt16() & maskAC); + + // temp2 = [...|00000000|00cccccc|00000000|00aaaaaa] + Vector512 temp2 = Avx512BW.ShiftRightLogicalVariable(temp1, shiftAC).AsUInt16(); + + // temp3 = [...|ccdddddd|00000000|aabbbbbb|cccc0000] + Vector512 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. + if (Avx512Vbmi.IsSupported) + { + str = Avx512Vbmi.PermuteVar64x8(vbmiLookup, str); + } + else + { + Vector512 indices = Avx512BW.SubtractSaturate(str.AsByte(), const51); + Vector512 mask = Vector512.GreaterThan(str, const25); + Vector512 tmp = Vector512.Subtract(indices.AsSByte(), mask); + // Add offsets to input values: + str = Vector512.Add(str, Avx512BW.Shuffle(lut, tmp)); + } + + AssertWrite>(dest, destStart, destLength); + Vector512.Store(str.AsByte(), dest); + + src += 48; + dest += 64; + + if (src > srcEnd) + break; + + AssertRead>(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) From 7b351addab2d55b89be578d4b3b3c80c453a69b1 Mon Sep 17 00:00:00 2001 From: Deepak Rajendrakumaran Date: Mon, 18 Sep 2023 17:20:42 -0700 Subject: [PATCH 2/5] Addressing review Comments. Signed-off-by: Deepak Rajendrakumaran --- .../src/System/Buffers/Text/Base64Decoder.cs | 10 +++++----- .../src/System/Buffers/Text/Base64Encoder.cs | 6 +----- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Decoder.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Decoder.cs index ba1097bb67351..a1570c91372cf 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Decoder.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Decoder.cs @@ -705,20 +705,20 @@ private static unsafe void Avx512Decode(ref byte* srcBytes, ref byte* destBytes, if (Avx512Vbmi.IsSupported) { origIndex = Avx512Vbmi.PermuteVar64x8x2(vbmiLookup0, str, vbmiLookup1); - ulong errorMask = (Vector512.BitwiseOr(origIndex.AsInt32(), str.AsInt32()).AsSByte()).ExtractMostSignificantBits(); - if (errorMask != 0) + Vector512 errorVec = (origIndex.AsInt32() | str.AsInt32()).AsSByte(); + if (errorVec.ExtractMostSignificantBits() != 0) { break; } } else { - Vector512 hiNibbles = Vector512.BitwiseAnd(Avx512F.ShiftRightLogical(str.AsInt32(), 4).AsSByte(), mask2F); + Vector512 hiNibbles = (Avx512F.ShiftRightLogical(str.AsInt32(), 4).AsSByte() & mask2F); - Vector512 loNibbles = Vector512.BitwiseAnd(str, mask2F); + Vector512 loNibbles = (str & mask2F); Vector512 hi = Avx512BW.Shuffle(lutHi, hiNibbles); Vector512 lo = Avx512BW.Shuffle(lutLo, loNibbles); - if (!Vector512.EqualsAll(Vector512.BitwiseAnd(hi, lo), Vector512.Zero)) + if (!Vector512.EqualsAll((hi & lo), Vector512.Zero)) { break; } diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Encoder.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Encoder.cs index 2d0fa1b14e341..f7b82d90c90f1 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Encoder.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Encoder.cs @@ -257,11 +257,7 @@ private static unsafe void Avx512Encode(ref byte* srcBytes, ref byte* destBytes, 0x0d0e0c0d, 0x10110f10, 0x13141213, 0x16171516, 0x191a1819, 0x1c1d1b1c, 0x1f201e1f, 0x22232122, 0x25262425, 0x28292728, 0x2b2c2a2b, 0x2e2f2d2e).AsSByte(); - Vector512 vbmiLookup = Vector512.Create( - (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F', (byte)'G', (byte)'H', (byte)'I', (byte)'J', (byte)'K', (byte)'L', (byte)'M', (byte)'N', (byte)'O', (byte)'P', - (byte)'Q', (byte)'R', (byte)'S', (byte)'T', (byte)'U', (byte)'V', (byte)'W', (byte)'X', (byte)'Y', (byte)'Z', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f', - (byte)'g', (byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l', (byte)'m', (byte)'n', (byte)'o', (byte)'p', (byte)'q', (byte)'r', (byte)'s', (byte)'t', (byte)'u', (byte)'v', - (byte)'w', (byte)'x', (byte)'y', (byte)'z', (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7', (byte)'8', (byte)'9', (byte)'+', (byte)'/').AsSByte(); + Vector512 vbmiLookup = Vector512.Create("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"u8).AsSByte(); // Non VBMI path constants Vector512 rearrangeVec = Vector512.Create(0, 1, 2, -1, 3, 4, 5, -1, 6, 7, 8, -1, 9, 10, 11, -1); From 7fb00e3c3feed02957f77ca8c72bb59b71136e7a Mon Sep 17 00:00:00 2001 From: Deepak Rajendrakumaran Date: Wed, 20 Sep 2023 11:45:13 -0700 Subject: [PATCH 3/5] Removing fallback path. --- .../src/System/Buffers/Text/Base64Decoder.cs | 77 +++---------------- .../src/System/Buffers/Text/Base64Encoder.cs | 54 +++---------- 2 files changed, 21 insertions(+), 110 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Decoder.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Decoder.cs index a1570c91372cf..f2b4cab433e60 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Decoder.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Decoder.cs @@ -69,7 +69,7 @@ private static unsafe OperationStatus DecodeFromUtf8(ReadOnlySpan utf8, Sp if (maxSrcLength >= 24) { byte* end = srcMax - 88; - if (Vector512.IsHardwareAccelerated && Avx512BW.IsSupported && (end >= src)) + if (Vector512.IsHardwareAccelerated && Avx512Vbmi.IsSupported && (end >= src)) { Avx512Decode(ref src, ref dest, end, maxSrcLength, destLength, srcBytes, destBytes); @@ -632,7 +632,7 @@ private static OperationStatus DecodeWithWhiteSpaceFromUtf8InPlace(Span ut [CompExactlyDependsOn(typeof(Avx512Vbmi))] private static unsafe void Avx512Decode(ref byte* srcBytes, ref byte* destBytes, byte* srcEnd, int sourceLength, int destLength, byte* srcStart, byte* destStart) { - // Reference for VBMI implementation : https://arxiv.org/pdf/1910.05109.pdf + // Reference for VBMI implementation : https://github.com/WojciechMula/base64simd/tree/master/decode // If we have AVX512 support, pick off 64 bytes at a time for as long as we can, // but make sure that we quit before seeing any == markers at the end of the // string. Also, because we write 16 zeroes at the end of the output, ensure @@ -643,8 +643,6 @@ private static unsafe void Avx512Decode(ref byte* srcBytes, ref byte* destBytes, // The JIT won't hoist these "constants", so help it // JIT will remove the Non VBMI path constants when VBMI is available and vice-versa. - - // Vbmi path constants Vector512 vbmiLookup0 = Vector512.Create( 0x80808080, 0x80808080, 0x80808080, 0x80808080, 0x80808080, 0x80808080, 0x80808080, 0x80808080, @@ -661,37 +659,12 @@ private static unsafe void Avx512Decode(ref byte* srcBytes, ref byte* destBytes, 0x2c2d2e28, 0x36303132, 0x393a3435, 0x3c3d3e38, 0x00000000, 0x00000000, 0x00000000, 0x00000000).AsByte(); - // Non Vbmi path constants - Vector512 lutHi = Vector512.Create( - 0x10, 0x10, 0x01, 0x02, 0x04, 0x08, 0x04, 0x08, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, - 0x10, 0x10, 0x01, 0x02, 0x04, 0x08, 0x04, 0x08, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, - 0x10, 0x10, 0x01, 0x02, 0x04, 0x08, 0x04, 0x08, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, - 0x10, 0x10, 0x01, 0x02, 0x04, 0x08, 0x04, 0x08, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10); - - Vector512 lutLo = Vector512.Create( - 0x15, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x13, 0x1A, 0x1B, 0x1B, 0x1B, 0x1A, - 0x15, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x13, 0x1A, 0x1B, 0x1B, 0x1B, 0x1A, - 0x15, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x13, 0x1A, 0x1B, 0x1B, 0x1B, 0x1A, - 0x15, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x13, 0x1A, 0x1B, 0x1B, 0x1B, 0x1A); - Vector512 mask2F = Vector512.Create((sbyte)'/'); - Vector512 lutShift = Vector512.Create( - 0, 16, 19, 4, -65, -65, -71, -71, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 16, 19, 4, -65, -65, -71, -71, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 16, 19, 4, -65, -65, -71, -71, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 16, 19, 4, -65, -65, -71, -71, 0, 0, 0, 0, 0, 0, 0, 0); - Vector512 packBytesInLaneMask = Vector512.Create( - 2, 1, 0, 6, 5, 4, 10, 9, 8, 14, 13, 12, -1, -1, -1, -1, - 18, 1, 0, 6, 5, 4, 10, 9, 8, 14, 13, 12, -1, -1, -1, -1, - 2, 1, 0, 6, 5, 4, 10, 9, 8, 14, 13, 12, -1, -1, -1, -1, - 2, 1, 0, 6, 5, 4, 10, 9, 8, 14, 13, 12, -1, -1, -1, -1); - Vector512 packLanesControl = Vector512.Create(0, 1, 2, 4, 5, 6, 8, 9, 10, 12, 13, 14, 0, 0, 0, 0); - - // Common path constants Vector512 mergeConstant0 = Vector512.Create(0x01400140).AsSByte(); Vector512 mergeConstant1 = Vector512.Create(0x00011000).AsInt16(); - // The fastest version of this algorithm requires AVX512VBMI support. - // The fallback path uses AVX512BW and AVX512F instructions. + // This algorithm requires AVX512VBMI support. + // Vbmi was first introduced in CannonLake and is avaialable from IceLake on. + // This makes it okay to use Vbmi instructions since Vector512.IsHardwareAccelerated returns True only from IceLake on. do { AssertRead>(src, srcStart, sourceLength); @@ -702,33 +675,14 @@ private static unsafe void Avx512Decode(ref byte* srcBytes, ref byte* destBytes, // After this, we have indices which are verified to have upper 2 bits set to 0 in each byte. // origIndex = [...|00dddddd|00cccccc|00bbbbbb|00aaaaaa] Vector512 origIndex; - if (Avx512Vbmi.IsSupported) - { - origIndex = Avx512Vbmi.PermuteVar64x8x2(vbmiLookup0, str, vbmiLookup1); - Vector512 errorVec = (origIndex.AsInt32() | str.AsInt32()).AsSByte(); - if (errorVec.ExtractMostSignificantBits() != 0) - { - break; - } - } - else - { - Vector512 hiNibbles = (Avx512F.ShiftRightLogical(str.AsInt32(), 4).AsSByte() & mask2F); - Vector512 loNibbles = (str & mask2F); - Vector512 hi = Avx512BW.Shuffle(lutHi, hiNibbles); - Vector512 lo = Avx512BW.Shuffle(lutLo, loNibbles); - if (!Vector512.EqualsAll((hi & lo), Vector512.Zero)) - { - break; - } - Vector512 eq2F = Vector512.Equals(str, mask2F); - Vector512 shift = Avx512BW.Shuffle(lutShift, Vector512.Add(eq2F, hiNibbles)); - origIndex = Vector512.Add(str, shift); + origIndex = Avx512Vbmi.PermuteVar64x8x2(vbmiLookup0, str, vbmiLookup1); + Vector512 errorVec = (origIndex.AsInt32() | str.AsInt32()).AsSByte(); + if (errorVec.ExtractMostSignificantBits() != 0) + { + break; } - - // Step 2: Now we need to reshuffle bits to remove the 0 bits. // multiAdd1: [...|0000cccc|ccdddddd|0000aaaa|aabbbbbb] Vector512 multiAdd1 = Avx512BW.MultiplyAddAdjacent(origIndex.AsByte(), mergeConstant0); @@ -736,16 +690,7 @@ private static unsafe void Avx512Decode(ref byte* srcBytes, ref byte* destBytes, Vector512 multiAdd2 = Avx512BW.MultiplyAddAdjacent(multiAdd1, mergeConstant1); // Step 3: Pack 48 bytes - if (Avx512Vbmi.IsSupported) - { - str = Avx512Vbmi.PermuteVar64x8(multiAdd2.AsByte(), vbmiPackedLanesControl).AsSByte(); - } - else - { - Vector512 packed = Avx512BW.Shuffle(multiAdd2.AsSByte(), packBytesInLaneMask).AsInt32(); - str = Avx512F.PermuteVar16x32(packed, packLanesControl).AsSByte(); - - } + str = Avx512Vbmi.PermuteVar64x8(multiAdd2.AsByte(), vbmiPackedLanesControl).AsSByte(); AssertWrite>(dest, destStart, destLength); Vector512.Store(str.AsByte(), dest); diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Encoder.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Encoder.cs index f7b82d90c90f1..256126fe5353b 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Encoder.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Encoder.cs @@ -68,7 +68,7 @@ public static unsafe OperationStatus EncodeToUtf8(ReadOnlySpan bytes, Span if (maxSrcLength >= 16) { byte* end = srcMax - 64; - if (Vector512.IsHardwareAccelerated && Avx512BW.IsSupported && (end >= src)) + if (Vector512.IsHardwareAccelerated && Avx512Vbmi.IsSupported && (end >= src)) { Avx512Encode(ref src, ref dest, end, maxSrcLength, destLength, srcBytes, destBytes); @@ -240,7 +240,7 @@ public static unsafe OperationStatus EncodeToUtf8InPlace(Span buffer, int [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://arxiv.org/pdf/1910.05109.pdf + // 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. @@ -250,8 +250,6 @@ private static unsafe void Avx512Encode(ref byte* srcBytes, ref byte* destBytes, // The JIT won't hoist these "constants", so help it // JIT will remove the Non VBMI path constants when VBMI is available and vice-versa. - - // VBMI path constants Vector512 shuffleVecVbmi = Vector512.Create( 0x01020001, 0x04050304, 0x07080607, 0x0a0b090a, 0x0d0e0c0d, 0x10110f10, 0x13141213, 0x16171516, @@ -259,46 +257,25 @@ private static unsafe void Avx512Encode(ref byte* srcBytes, ref byte* destBytes, 0x25262425, 0x28292728, 0x2b2c2a2b, 0x2e2f2d2e).AsSByte(); Vector512 vbmiLookup = Vector512.Create("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"u8).AsSByte(); - // Non VBMI path constants - Vector512 rearrangeVec = Vector512.Create(0, 1, 2, -1, 3, 4, 5, -1, 6, 7, 8, -1, 9, 10, 11, -1); - Vector512 shuffleVec = Vector512.Create(0x01020001, 0x04050304, 0x07080607, 0x0a0b090a, 0x01020001, 0x04050304, 0x07080607, 0x0a0b090a, - 0x01020001, 0x04050304, 0x07080607, 0x0a0b090a, 0x01020001, 0x04050304, 0x07080607, 0x0a0b090a).AsSByte(); - Vector512 lut = Vector512.Create( - 65, 71, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -19, -16, 0, 0, - 65, 71, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -19, -16, 0, 0, - 65, 71, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -19, -16, 0, 0, - 65, 71, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -19, -16, 0, 0); - Vector512 const51 = Vector512.Create((byte)51); - Vector512 const25 = Vector512.Create((sbyte)25); - - // Common constants Vector512 maskAC = Vector512.Create((uint)0x0fc0fc00).AsUInt16(); Vector512 maskBB = Vector512.Create((uint)0x3f003f00); Vector512 shiftAC = Vector512.Create((uint)0x0006000a).AsUInt16(); Vector512 shiftBB = Vector512.Create((uint)0x00080004).AsUInt16(); AssertRead>(src, srcStart, sourceLength); - // The fastest version of this algorithm requires AVX512VBMI support. - // The fallback path uses AVX512BW and AVX512F instructions. - // Both versions work on the input data as four 128 byte lanes. + + // This algorithm requires AVX512VBMI support. + // Vbmi was first introduced in CannonLake and is avaialable from IceLake on. + // This makes it okay to use Vbmi instructions since Vector512.IsHardwareAccelerated returns True only from IceLake on. + // str = [...|PONM|LKJI|HGFE|DCBA] Vector512 str = Vector512.Load(src).AsSByte(); while (true) { // Step 1 : Split 48 bytes into 64 bytes with each byte using 6-bits from input - if (Avx512Vbmi.IsSupported) - { - // str = [...|KLJK|HIGH|EFDE|BCAB] - str = Avx512Vbmi.PermuteVar64x8(str, shuffleVecVbmi); - } - else - { - // shuf1 = [...|0000|LKJI|HGFE|DCBA] - Vector512 shuf1 = Avx512F.PermuteVar16x32(str.AsInt32(), rearrangeVec).AsSByte(); - // str = [...|KLJK|HIGH|EFDE|BCAB] - str = Avx512BW.Shuffle(shuf1, shuffleVec); - } + // 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 @@ -315,18 +292,7 @@ private static unsafe void Avx512Encode(ref byte* srcBytes, ref byte* destBytes, 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. - if (Avx512Vbmi.IsSupported) - { - str = Avx512Vbmi.PermuteVar64x8(vbmiLookup, str); - } - else - { - Vector512 indices = Avx512BW.SubtractSaturate(str.AsByte(), const51); - Vector512 mask = Vector512.GreaterThan(str, const25); - Vector512 tmp = Vector512.Subtract(indices.AsSByte(), mask); - // Add offsets to input values: - str = Vector512.Add(str, Avx512BW.Shuffle(lut, tmp)); - } + str = Avx512Vbmi.PermuteVar64x8(vbmiLookup, str); AssertWrite>(dest, destStart, destLength); Vector512.Store(str.AsByte(), dest); From 2969ddb28d388b33b933658b3075bb9b67ec59f8 Mon Sep 17 00:00:00 2001 From: Deepak Rajendrakumaran Date: Thu, 21 Sep 2023 11:10:36 -0700 Subject: [PATCH 4/5] Updating Third Party Notice. --- THIRD-PARTY-NOTICES.TXT | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/THIRD-PARTY-NOTICES.TXT b/THIRD-PARTY-NOTICES.TXT index 7026e2bc232f7..07a2b94ba5fc6 100644 --- a/THIRD-PARTY-NOTICES.TXT +++ b/THIRD-PARTY-NOTICES.TXT @@ -1297,3 +1297,37 @@ 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. + +License notice for Avx512Vbmi base64 encoding / decoding +-------------------------------------------------------- + +Copyright (c) 2015-2018, Wojciech Muła +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + +2. 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. + +-------------------------------------------------------- + +Aspects of base64 encoding / decoding are based on algorithm described in "Base64 encoding and decoding at almost the speed of a memory +copy", Wojciech Muła and Daniel Lemire. https://arxiv.org/pdf/1910.05109.pdf From fc05beb52ba51f9f87cbc0d660b56a3d4a0cdd38 Mon Sep 17 00:00:00 2001 From: Deepak Rajendrakumaran Date: Tue, 24 Oct 2023 13:31:40 -0700 Subject: [PATCH 5/5] Addressing review comments --- .../src/System/Buffers/Text/Base64Decoder.cs | 8 ++------ .../src/System/Buffers/Text/Base64Encoder.cs | 4 +--- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Decoder.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Decoder.cs index f2b4cab433e60..1ad2cf9faa9f3 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Decoder.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Decoder.cs @@ -642,7 +642,6 @@ private static unsafe void Avx512Decode(ref byte* srcBytes, ref byte* destBytes, byte* dest = destBytes; // The JIT won't hoist these "constants", so help it - // JIT will remove the Non VBMI path constants when VBMI is available and vice-versa. Vector512 vbmiLookup0 = Vector512.Create( 0x80808080, 0x80808080, 0x80808080, 0x80808080, 0x80808080, 0x80808080, 0x80808080, 0x80808080, @@ -664,7 +663,6 @@ private static unsafe void Avx512Decode(ref byte* srcBytes, ref byte* destBytes, // This algorithm requires AVX512VBMI support. // Vbmi was first introduced in CannonLake and is avaialable from IceLake on. - // This makes it okay to use Vbmi instructions since Vector512.IsHardwareAccelerated returns True only from IceLake on. do { AssertRead>(src, srcStart, sourceLength); @@ -674,9 +672,7 @@ private static unsafe void Avx512Decode(ref byte* srcBytes, ref byte* destBytes, // This step also checks for invalid inputs and exits. // After this, we have indices which are verified to have upper 2 bits set to 0 in each byte. // origIndex = [...|00dddddd|00cccccc|00bbbbbb|00aaaaaa] - Vector512 origIndex; - - origIndex = Avx512Vbmi.PermuteVar64x8x2(vbmiLookup0, str, vbmiLookup1); + Vector512 origIndex = Avx512Vbmi.PermuteVar64x8x2(vbmiLookup0, str, vbmiLookup1); Vector512 errorVec = (origIndex.AsInt32() | str.AsInt32()).AsSByte(); if (errorVec.ExtractMostSignificantBits() != 0) { @@ -693,7 +689,7 @@ private static unsafe void Avx512Decode(ref byte* srcBytes, ref byte* destBytes, str = Avx512Vbmi.PermuteVar64x8(multiAdd2.AsByte(), vbmiPackedLanesControl).AsSByte(); AssertWrite>(dest, destStart, destLength); - Vector512.Store(str.AsByte(), dest); + str.Store((sbyte*)dest); src += 64; dest += 48; } diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Encoder.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Encoder.cs index 256126fe5353b..08ca62b533f51 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Encoder.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Encoder.cs @@ -249,7 +249,6 @@ private static unsafe void Avx512Encode(ref byte* srcBytes, ref byte* destBytes, byte* dest = destBytes; // The JIT won't hoist these "constants", so help it - // JIT will remove the Non VBMI path constants when VBMI is available and vice-versa. Vector512 shuffleVecVbmi = Vector512.Create( 0x01020001, 0x04050304, 0x07080607, 0x0a0b090a, 0x0d0e0c0d, 0x10110f10, 0x13141213, 0x16171516, @@ -266,7 +265,6 @@ private static unsafe void Avx512Encode(ref byte* srcBytes, ref byte* destBytes, // This algorithm requires AVX512VBMI support. // Vbmi was first introduced in CannonLake and is avaialable from IceLake on. - // This makes it okay to use Vbmi instructions since Vector512.IsHardwareAccelerated returns True only from IceLake on. // str = [...|PONM|LKJI|HGFE|DCBA] Vector512 str = Vector512.Load(src).AsSByte(); @@ -295,7 +293,7 @@ private static unsafe void Avx512Encode(ref byte* srcBytes, ref byte* destBytes, str = Avx512Vbmi.PermuteVar64x8(vbmiLookup, str); AssertWrite>(dest, destStart, destLength); - Vector512.Store(str.AsByte(), dest); + str.Store((sbyte*)dest); src += 48; dest += 64;