Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions src/libraries/System.Memory/src/System/Buffers/Text/Base64Decoder.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,12 +5,14 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.Arm;
using System.Runtime.Intrinsics.X86;

namespace System.Buffers.Text
{
// AVX2 version based on https://github.com/aklomp/base64/tree/e516d769a2a432c08404f1981e73b431566057be/lib/arch/avx2
// SSSE3 version based on https://github.com/aklomp/base64/tree/e516d769a2a432c08404f1981e73b431566057be/lib/arch/ssse3
// AdvSimd version based on https://github.com/aklomp/base64/blob/e516d769a2a432c08404f1981e73b431566057be/lib/arch/neon64

public static partial class Base64
{
Expand DownExpand Up@@ -81,6 +83,15 @@ public static unsafe OperationStatus DecodeFromUtf8(ReadOnlySpan<byte> utf8, Spa
if (src == srcEnd)
goto DoneExit;
}

end = srcMax - 96;
if (BitConverter.IsLittleEndian && AdvSimd.Arm64.IsSupported && (end >= src))
{
AdvSimdDecode(ref src, ref dest, end, maxSrcLength, destLength, srcBytes, destBytes);

if (src == srcEnd)
goto DoneExit;
}
}

// Last bytes could have padding characters, so process them separately and treat them as valid only if isFinalBlock is true
Expand DownExpand Up@@ -644,6 +655,133 @@ private static unsafe void Ssse3Decode(ref byte* srcBytes, ref byte* destBytes,
destBytes = dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe Vector128<byte> AdvSimdTbx8Byte(Vector128<byte> defaults, Vector128<byte> table0, Vector128<byte> table1, Vector128<byte> table2, Vector128<byte> table3, Vector128<byte> table4, Vector128<byte> table5, Vector128<byte> table6, Vector128<byte> table7, Vector128<byte> indicies, Vector128<byte> offset)
{
// Implement an 8 way table lookup.
// This could be reduced by using two NEON TBX4 instructions.

Debug.Assert(AdvSimd.Arm64.IsSupported && BitConverter.IsLittleEndian);

Vector128<byte> dest = defaults;
Vector128<byte> indicies_sub = indicies;

dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table0, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table1, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table2, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table3, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table4, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table5, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table6, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table7, indicies_sub);

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this using perf, a lot of time is spent in this function. One reason for that is due to the chain of dependencies.

Splitting the indicies_sub into separate variables didn't make any noticeable difference:
var indicies1 = AdvSimd.Subtract(indicies, Vector128.Create((byte)(16U1)));
var indicies2 = AdvSimd.Subtract(indicies, Vector128.Create((byte)(16U
2)));
var indicies3 = AdvSimd.Subtract(indicies, Vector128.Create((byte)(16U*3)));
etc

The TBXs could be split out:
increment everything in the lookup table so that it starts from 1. Do the lookups using TBLs, meaning failures are 0. Combine all the results with ORs. Then subtract 1 from the result.
That would add more complexity, and I very much doubt it's going to give much benefit overall.


return dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe Vector128<byte> AdvSimdTbx3Byte(Vector128<byte> defaults, Vector128<byte> table0, Vector128<byte> table1, Vector128<byte> table2, Vector128<byte> indicies, Vector128<byte> offset)
{
// Implement a 3 way table lookup.

Debug.Assert(AdvSimd.Arm64.IsSupported && BitConverter.IsLittleEndian);

Vector128<byte> dest = defaults;
Vector128<byte> indicies_sub = indicies;

dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table0, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table1, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table2, indicies_sub);

return dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe void AdvSimdDecode(ref byte* srcBytes, ref byte* destBytes, byte* srcEnd, int sourceLength, int destLength, byte* srcStart, byte* destStart)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious - does AggressiveInlining here show benefits in the benchmarks?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remove this one it doesn't make a difference.
If I remove the two around AdvSimdTbx3Byte and AdvSimdTbx8Byte it gets 2x worse.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those definitely make sense while this one is a bit questionable

{
Debug.Assert(AdvSimd.Arm64.IsSupported && BitConverter.IsLittleEndian);

// Complete lookup table - similar to that used in the SS3 decode.
Vector128<byte> dec_lut0 = Vector128.Create(255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut1 = Vector128.Create(255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut2 = Vector128.Create(255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63);
Vector128<byte> dec_lut3 = Vector128.Create( 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut4 = Vector128.Create(255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);
Vector128<byte> dec_lut5 = Vector128.Create( 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut6 = Vector128.Create(255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40);
Vector128<byte> dec_lut7 = Vector128.Create( 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255);

// Interleave pattern for the ST3.
Vector128<byte> st3_interleave_index0 = Vector128.Create((byte) 0, 16, 32, 1, 17, 33, 2, 18, 34, 3, 19, 35, 4, 20, 36, 5);
Vector128<byte> st3_interleave_index1 = Vector128.Create((byte)21, 37, 6, 22, 38, 7, 23, 39, 8, 24, 40, 9, 25, 41, 10, 26);
Vector128<byte> st3_interleave_index2 = Vector128.Create((byte)42, 11, 27, 43, 12, 28, 44, 13, 29, 45, 14, 30, 46, 15, 31, 47);

// Some constants.
Vector128<byte> vzero = Vector128.Create((byte)0);
Vector128<byte> v255 = Vector128.Create((byte)255U);
Vector128<byte> v16 = Vector128.Create((byte)16U);

byte* src = srcBytes;
byte* dest = destBytes;

do
{
// Load 64 bytes of data and deinterleave the result.
// This is equivalent to a NEON LD4 instruction.
Vector128<byte> str0 = Vector128.LoadUnsafe(ref *src);
Vector128<byte> str1 = Vector128.LoadUnsafe(ref *src, 16);
Vector128<byte> str2 = Vector128.LoadUnsafe(ref *src, 32);
Vector128<byte> str3 = Vector128.LoadUnsafe(ref *src, 48);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect you can use AdvSimd.Arm64.LoadPairVector128 (and even nontemporal if it makes sense) here - jit is not smart enough yet to do it by itself

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing this worked, but didn't give any improvement

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect you can use AdvSimd.Arm64.LoadPairVector128 (and even nontemporal if it makes sense) here - jit is not smart enough yet to do it by itself

Vector128<short> tmp0 = AdvSimd.Arm64.UnzipEven(str0.AsInt16(), str1.AsInt16());
Vector128<short> tmp1 = AdvSimd.Arm64.UnzipOdd(str0.AsInt16(), str1.AsInt16());
Vector128<short> tmp2 = AdvSimd.Arm64.UnzipEven(str2.AsInt16(), str3.AsInt16());
Vector128<short> tmp3 = AdvSimd.Arm64.UnzipOdd(str2.AsInt16(), str3.AsInt16());
str0 = AdvSimd.Arm64.UnzipEven(tmp0.AsByte(), tmp2.AsByte());
str1 = AdvSimd.Arm64.UnzipOdd(tmp0.AsByte(), tmp2.AsByte());
str2 = AdvSimd.Arm64.UnzipEven(tmp1.AsByte(), tmp3.AsByte());
str3 = AdvSimd.Arm64.UnzipOdd(tmp1.AsByte(), tmp3.AsByte());

// Table lookup on each 16 bytes.
str0 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str0, v16);
str1 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str1, v16);
str2 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str2, v16);
str3 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str3, v16);

// Check for invalid input, any value larger than 63.
Vector128<byte> classified0 = AdvSimd.Arm64.MaxPairwise(str0, str1);
Vector128<byte> classified1 = AdvSimd.Arm64.MaxPairwise(str2, str3);
Vector128<byte> maxChars = AdvSimd.Arm64.MaxPairwise(classified0, classified1);
if ((maxChars.AsUInt64().ToScalar() & 0xc0c0c0c0c0c0c0c0) != 0)
break;

// Compress each four bytes into three.
Vector128<byte> dec0 = Vector128.BitwiseOr(Vector128.ShiftLeft(str0, 2), Vector128.ShiftRightLogical(str1, 4));
Vector128<byte> dec1 = Vector128.BitwiseOr(Vector128.ShiftLeft(str1, 4), Vector128.ShiftRightLogical(str2, 2));
Vector128<byte> dec2 = Vector128.BitwiseOr(Vector128.ShiftLeft(str2, 6), str3);

@EgorBoEgorBoJun 7, 2022

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jit doesn't do instruction selection so if you want your shifts to be side-by-side for better pipelining you need to extract them to temp locals, e.g.:

varsl0=Vector128.ShiftLeft(str0,2);varsl1=Vector128.ShiftLeft(str1,4);varsl2=Vector128.ShiftLeft(str2,6);varsr1=Vector128.ShiftRightLogical(str1,4);varsr2=Vector128.ShiftRightLogical(str2,2);Vector128<byte>dec0=Vector128.BitwiseOr(sl0,sr1);Vector128<byte>dec1=Vector128.BitwiseOr(sl1,sr2);Vector128<byte>dec2=Vector128.BitwiseOr(sl2,str3);

not sure it matters much in terms of perf

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copying that verbatim didn't make any difference.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another way to do this might be to treat the vector register as a single value and do something fancy with a masks and a single value shift. Not quite sure how that would look. Might get a some performance, but it'd be messy.


// Interleave the decoded result and store out.
// This is equivalent to a NEON ST3 instruction.
AdvSimdTbx3Byte(vzero, dec0, dec1, dec2, st3_interleave_index0, v16).Store(dest);
AdvSimdTbx3Byte(vzero, dec0, dec1, dec2, st3_interleave_index1, v16).Store(dest + 16);
AdvSimdTbx3Byte(vzero, dec0, dec1, dec2, st3_interleave_index2, v16).Store(dest + 32);

src += 64;
dest += 48;
}
while (src <= srcEnd);

srcBytes = src;
destBytes = dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe int Decode(byte* encodedBytes, ref sbyte decodingMap)
{
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions src/libraries/System.Memory/src/System/Buffers/Text/Base64Decoder.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,12 +5,14 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.Arm;
using System.Runtime.Intrinsics.X86;

namespace System.Buffers.Text
{
// AVX2 version based on https://github.com/aklomp/base64/tree/e516d769a2a432c08404f1981e73b431566057be/lib/arch/avx2
// SSSE3 version based on https://github.com/aklomp/base64/tree/e516d769a2a432c08404f1981e73b431566057be/lib/arch/ssse3
// AdvSimd version based on https://github.com/aklomp/base64/blob/e516d769a2a432c08404f1981e73b431566057be/lib/arch/neon64

public static partial class Base64
{
Expand DownExpand Up@@ -81,6 +83,15 @@ public static unsafe OperationStatus DecodeFromUtf8(ReadOnlySpan<byte> utf8, Spa
if (src == srcEnd)
goto DoneExit;
}

end = srcMax - 96;
if (BitConverter.IsLittleEndian && AdvSimd.Arm64.IsSupported && (end >= src))
{
AdvSimdDecode(ref src, ref dest, end, maxSrcLength, destLength, srcBytes, destBytes);

if (src == srcEnd)
goto DoneExit;
}
}

// Last bytes could have padding characters, so process them separately and treat them as valid only if isFinalBlock is true
Expand DownExpand Up@@ -644,6 +655,133 @@ private static unsafe void Ssse3Decode(ref byte* srcBytes, ref byte* destBytes,
destBytes = dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe Vector128<byte> AdvSimdTbx8Byte(Vector128<byte> defaults, Vector128<byte> table0, Vector128<byte> table1, Vector128<byte> table2, Vector128<byte> table3, Vector128<byte> table4, Vector128<byte> table5, Vector128<byte> table6, Vector128<byte> table7, Vector128<byte> indicies, Vector128<byte> offset)
{
// Implement an 8 way table lookup.
// This could be reduced by using two NEON TBX4 instructions.

Debug.Assert(AdvSimd.Arm64.IsSupported && BitConverter.IsLittleEndian);

Vector128<byte> dest = defaults;
Vector128<byte> indicies_sub = indicies;

dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table0, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table1, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table2, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table3, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table4, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table5, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table6, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table7, indicies_sub);

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this using perf, a lot of time is spent in this function. One reason for that is due to the chain of dependencies.

Splitting the indicies_sub into separate variables didn't make any noticeable difference:
var indicies1 = AdvSimd.Subtract(indicies, Vector128.Create((byte)(16U1)));
var indicies2 = AdvSimd.Subtract(indicies, Vector128.Create((byte)(16U
2)));
var indicies3 = AdvSimd.Subtract(indicies, Vector128.Create((byte)(16U*3)));
etc

The TBXs could be split out:
increment everything in the lookup table so that it starts from 1. Do the lookups using TBLs, meaning failures are 0. Combine all the results with ORs. Then subtract 1 from the result.
That would add more complexity, and I very much doubt it's going to give much benefit overall.


return dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe Vector128<byte> AdvSimdTbx3Byte(Vector128<byte> defaults, Vector128<byte> table0, Vector128<byte> table1, Vector128<byte> table2, Vector128<byte> indicies, Vector128<byte> offset)
{
// Implement a 3 way table lookup.

Debug.Assert(AdvSimd.Arm64.IsSupported && BitConverter.IsLittleEndian);

Vector128<byte> dest = defaults;
Vector128<byte> indicies_sub = indicies;

dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table0, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table1, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table2, indicies_sub);

return dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe void AdvSimdDecode(ref byte* srcBytes, ref byte* destBytes, byte* srcEnd, int sourceLength, int destLength, byte* srcStart, byte* destStart)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious - does AggressiveInlining here show benefits in the benchmarks?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remove this one it doesn't make a difference.
If I remove the two around AdvSimdTbx3Byte and AdvSimdTbx8Byte it gets 2x worse.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those definitely make sense while this one is a bit questionable

{
Debug.Assert(AdvSimd.Arm64.IsSupported && BitConverter.IsLittleEndian);

// Complete lookup table - similar to that used in the SS3 decode.
Vector128<byte> dec_lut0 = Vector128.Create(255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut1 = Vector128.Create(255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut2 = Vector128.Create(255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63);
Vector128<byte> dec_lut3 = Vector128.Create( 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut4 = Vector128.Create(255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);
Vector128<byte> dec_lut5 = Vector128.Create( 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut6 = Vector128.Create(255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40);
Vector128<byte> dec_lut7 = Vector128.Create( 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255);

// Interleave pattern for the ST3.
Vector128<byte> st3_interleave_index0 = Vector128.Create((byte) 0, 16, 32, 1, 17, 33, 2, 18, 34, 3, 19, 35, 4, 20, 36, 5);
Vector128<byte> st3_interleave_index1 = Vector128.Create((byte)21, 37, 6, 22, 38, 7, 23, 39, 8, 24, 40, 9, 25, 41, 10, 26);
Vector128<byte> st3_interleave_index2 = Vector128.Create((byte)42, 11, 27, 43, 12, 28, 44, 13, 29, 45, 14, 30, 46, 15, 31, 47);

// Some constants.
Vector128<byte> vzero = Vector128.Create((byte)0);
Vector128<byte> v255 = Vector128.Create((byte)255U);
Vector128<byte> v16 = Vector128.Create((byte)16U);

byte* src = srcBytes;
byte* dest = destBytes;

do
{
// Load 64 bytes of data and deinterleave the result.
// This is equivalent to a NEON LD4 instruction.
Vector128<byte> str0 = Vector128.LoadUnsafe(ref *src);
Vector128<byte> str1 = Vector128.LoadUnsafe(ref *src, 16);
Vector128<byte> str2 = Vector128.LoadUnsafe(ref *src, 32);
Vector128<byte> str3 = Vector128.LoadUnsafe(ref *src, 48);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect you can use AdvSimd.Arm64.LoadPairVector128 (and even nontemporal if it makes sense) here - jit is not smart enough yet to do it by itself

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing this worked, but didn't give any improvement

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect you can use AdvSimd.Arm64.LoadPairVector128 (and even nontemporal if it makes sense) here - jit is not smart enough yet to do it by itself

Vector128<short> tmp0 = AdvSimd.Arm64.UnzipEven(str0.AsInt16(), str1.AsInt16());
Vector128<short> tmp1 = AdvSimd.Arm64.UnzipOdd(str0.AsInt16(), str1.AsInt16());
Vector128<short> tmp2 = AdvSimd.Arm64.UnzipEven(str2.AsInt16(), str3.AsInt16());
Vector128<short> tmp3 = AdvSimd.Arm64.UnzipOdd(str2.AsInt16(), str3.AsInt16());
str0 = AdvSimd.Arm64.UnzipEven(tmp0.AsByte(), tmp2.AsByte());
str1 = AdvSimd.Arm64.UnzipOdd(tmp0.AsByte(), tmp2.AsByte());
str2 = AdvSimd.Arm64.UnzipEven(tmp1.AsByte(), tmp3.AsByte());
str3 = AdvSimd.Arm64.UnzipOdd(tmp1.AsByte(), tmp3.AsByte());

// Table lookup on each 16 bytes.
str0 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str0, v16);
str1 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str1, v16);
str2 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str2, v16);
str3 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str3, v16);

// Check for invalid input, any value larger than 63.
Vector128<byte> classified0 = AdvSimd.Arm64.MaxPairwise(str0, str1);
Vector128<byte> classified1 = AdvSimd.Arm64.MaxPairwise(str2, str3);
Vector128<byte> maxChars = AdvSimd.Arm64.MaxPairwise(classified0, classified1);
if ((maxChars.AsUInt64().ToScalar() & 0xc0c0c0c0c0c0c0c0) != 0)
break;

// Compress each four bytes into three.
Vector128<byte> dec0 = Vector128.BitwiseOr(Vector128.ShiftLeft(str0, 2), Vector128.ShiftRightLogical(str1, 4));
Vector128<byte> dec1 = Vector128.BitwiseOr(Vector128.ShiftLeft(str1, 4), Vector128.ShiftRightLogical(str2, 2));
Vector128<byte> dec2 = Vector128.BitwiseOr(Vector128.ShiftLeft(str2, 6), str3);

@EgorBoEgorBoJun 7, 2022

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jit doesn't do instruction selection so if you want your shifts to be side-by-side for better pipelining you need to extract them to temp locals, e.g.:

varsl0=Vector128.ShiftLeft(str0,2);varsl1=Vector128.ShiftLeft(str1,4);varsl2=Vector128.ShiftLeft(str2,6);varsr1=Vector128.ShiftRightLogical(str1,4);varsr2=Vector128.ShiftRightLogical(str2,2);Vector128<byte>dec0=Vector128.BitwiseOr(sl0,sr1);Vector128<byte>dec1=Vector128.BitwiseOr(sl1,sr2);Vector128<byte>dec2=Vector128.BitwiseOr(sl2,str3);

not sure it matters much in terms of perf

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copying that verbatim didn't make any difference.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another way to do this might be to treat the vector register as a single value and do something fancy with a masks and a single value shift. Not quite sure how that would look. Might get a some performance, but it'd be messy.


// Interleave the decoded result and store out.
// This is equivalent to a NEON ST3 instruction.
AdvSimdTbx3Byte(vzero, dec0, dec1, dec2, st3_interleave_index0, v16).Store(dest);
AdvSimdTbx3Byte(vzero, dec0, dec1, dec2, st3_interleave_index1, v16).Store(dest + 16);
AdvSimdTbx3Byte(vzero, dec0, dec1, dec2, st3_interleave_index2, v16).Store(dest + 32);

src += 64;
dest += 48;
}
while (src <= srcEnd);

srcBytes = src;
destBytes = dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe int Decode(byte* encodedBytes, ref sbyte decodingMap)
{
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions src/libraries/System.Memory/src/System/Buffers/Text/Base64Decoder.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,12 +5,14 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.Arm;
using System.Runtime.Intrinsics.X86;

namespace System.Buffers.Text
{
// AVX2 version based on https://github.com/aklomp/base64/tree/e516d769a2a432c08404f1981e73b431566057be/lib/arch/avx2
// SSSE3 version based on https://github.com/aklomp/base64/tree/e516d769a2a432c08404f1981e73b431566057be/lib/arch/ssse3
// AdvSimd version based on https://github.com/aklomp/base64/blob/e516d769a2a432c08404f1981e73b431566057be/lib/arch/neon64

public static partial class Base64
{
Expand DownExpand Up@@ -81,6 +83,15 @@ public static unsafe OperationStatus DecodeFromUtf8(ReadOnlySpan<byte> utf8, Spa
if (src == srcEnd)
goto DoneExit;
}

end = srcMax - 96;
if (BitConverter.IsLittleEndian && AdvSimd.Arm64.IsSupported && (end >= src))
{
AdvSimdDecode(ref src, ref dest, end, maxSrcLength, destLength, srcBytes, destBytes);

if (src == srcEnd)
goto DoneExit;
}
}

// Last bytes could have padding characters, so process them separately and treat them as valid only if isFinalBlock is true
Expand DownExpand Up@@ -644,6 +655,133 @@ private static unsafe void Ssse3Decode(ref byte* srcBytes, ref byte* destBytes,
destBytes = dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe Vector128<byte> AdvSimdTbx8Byte(Vector128<byte> defaults, Vector128<byte> table0, Vector128<byte> table1, Vector128<byte> table2, Vector128<byte> table3, Vector128<byte> table4, Vector128<byte> table5, Vector128<byte> table6, Vector128<byte> table7, Vector128<byte> indicies, Vector128<byte> offset)
{
// Implement an 8 way table lookup.
// This could be reduced by using two NEON TBX4 instructions.

Debug.Assert(AdvSimd.Arm64.IsSupported && BitConverter.IsLittleEndian);

Vector128<byte> dest = defaults;
Vector128<byte> indicies_sub = indicies;

dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table0, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table1, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table2, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table3, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table4, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table5, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table6, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table7, indicies_sub);

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this using perf, a lot of time is spent in this function. One reason for that is due to the chain of dependencies.

Splitting the indicies_sub into separate variables didn't make any noticeable difference:
var indicies1 = AdvSimd.Subtract(indicies, Vector128.Create((byte)(16U1)));
var indicies2 = AdvSimd.Subtract(indicies, Vector128.Create((byte)(16U
2)));
var indicies3 = AdvSimd.Subtract(indicies, Vector128.Create((byte)(16U*3)));
etc

The TBXs could be split out:
increment everything in the lookup table so that it starts from 1. Do the lookups using TBLs, meaning failures are 0. Combine all the results with ORs. Then subtract 1 from the result.
That would add more complexity, and I very much doubt it's going to give much benefit overall.


return dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe Vector128<byte> AdvSimdTbx3Byte(Vector128<byte> defaults, Vector128<byte> table0, Vector128<byte> table1, Vector128<byte> table2, Vector128<byte> indicies, Vector128<byte> offset)
{
// Implement a 3 way table lookup.

Debug.Assert(AdvSimd.Arm64.IsSupported && BitConverter.IsLittleEndian);

Vector128<byte> dest = defaults;
Vector128<byte> indicies_sub = indicies;

dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table0, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table1, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table2, indicies_sub);

return dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe void AdvSimdDecode(ref byte* srcBytes, ref byte* destBytes, byte* srcEnd, int sourceLength, int destLength, byte* srcStart, byte* destStart)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious - does AggressiveInlining here show benefits in the benchmarks?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remove this one it doesn't make a difference.
If I remove the two around AdvSimdTbx3Byte and AdvSimdTbx8Byte it gets 2x worse.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those definitely make sense while this one is a bit questionable

{
Debug.Assert(AdvSimd.Arm64.IsSupported && BitConverter.IsLittleEndian);

// Complete lookup table - similar to that used in the SS3 decode.
Vector128<byte> dec_lut0 = Vector128.Create(255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut1 = Vector128.Create(255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut2 = Vector128.Create(255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63);
Vector128<byte> dec_lut3 = Vector128.Create( 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut4 = Vector128.Create(255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);
Vector128<byte> dec_lut5 = Vector128.Create( 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut6 = Vector128.Create(255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40);
Vector128<byte> dec_lut7 = Vector128.Create( 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255);

// Interleave pattern for the ST3.
Vector128<byte> st3_interleave_index0 = Vector128.Create((byte) 0, 16, 32, 1, 17, 33, 2, 18, 34, 3, 19, 35, 4, 20, 36, 5);
Vector128<byte> st3_interleave_index1 = Vector128.Create((byte)21, 37, 6, 22, 38, 7, 23, 39, 8, 24, 40, 9, 25, 41, 10, 26);
Vector128<byte> st3_interleave_index2 = Vector128.Create((byte)42, 11, 27, 43, 12, 28, 44, 13, 29, 45, 14, 30, 46, 15, 31, 47);

// Some constants.
Vector128<byte> vzero = Vector128.Create((byte)0);
Vector128<byte> v255 = Vector128.Create((byte)255U);
Vector128<byte> v16 = Vector128.Create((byte)16U);

byte* src = srcBytes;
byte* dest = destBytes;

do
{
// Load 64 bytes of data and deinterleave the result.
// This is equivalent to a NEON LD4 instruction.
Vector128<byte> str0 = Vector128.LoadUnsafe(ref *src);
Vector128<byte> str1 = Vector128.LoadUnsafe(ref *src, 16);
Vector128<byte> str2 = Vector128.LoadUnsafe(ref *src, 32);
Vector128<byte> str3 = Vector128.LoadUnsafe(ref *src, 48);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect you can use AdvSimd.Arm64.LoadPairVector128 (and even nontemporal if it makes sense) here - jit is not smart enough yet to do it by itself

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing this worked, but didn't give any improvement

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect you can use AdvSimd.Arm64.LoadPairVector128 (and even nontemporal if it makes sense) here - jit is not smart enough yet to do it by itself

Vector128<short> tmp0 = AdvSimd.Arm64.UnzipEven(str0.AsInt16(), str1.AsInt16());
Vector128<short> tmp1 = AdvSimd.Arm64.UnzipOdd(str0.AsInt16(), str1.AsInt16());
Vector128<short> tmp2 = AdvSimd.Arm64.UnzipEven(str2.AsInt16(), str3.AsInt16());
Vector128<short> tmp3 = AdvSimd.Arm64.UnzipOdd(str2.AsInt16(), str3.AsInt16());
str0 = AdvSimd.Arm64.UnzipEven(tmp0.AsByte(), tmp2.AsByte());
str1 = AdvSimd.Arm64.UnzipOdd(tmp0.AsByte(), tmp2.AsByte());
str2 = AdvSimd.Arm64.UnzipEven(tmp1.AsByte(), tmp3.AsByte());
str3 = AdvSimd.Arm64.UnzipOdd(tmp1.AsByte(), tmp3.AsByte());

// Table lookup on each 16 bytes.
str0 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str0, v16);
str1 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str1, v16);
str2 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str2, v16);
str3 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str3, v16);

// Check for invalid input, any value larger than 63.
Vector128<byte> classified0 = AdvSimd.Arm64.MaxPairwise(str0, str1);
Vector128<byte> classified1 = AdvSimd.Arm64.MaxPairwise(str2, str3);
Vector128<byte> maxChars = AdvSimd.Arm64.MaxPairwise(classified0, classified1);
if ((maxChars.AsUInt64().ToScalar() & 0xc0c0c0c0c0c0c0c0) != 0)
break;

// Compress each four bytes into three.
Vector128<byte> dec0 = Vector128.BitwiseOr(Vector128.ShiftLeft(str0, 2), Vector128.ShiftRightLogical(str1, 4));
Vector128<byte> dec1 = Vector128.BitwiseOr(Vector128.ShiftLeft(str1, 4), Vector128.ShiftRightLogical(str2, 2));
Vector128<byte> dec2 = Vector128.BitwiseOr(Vector128.ShiftLeft(str2, 6), str3);

@EgorBoEgorBoJun 7, 2022

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jit doesn't do instruction selection so if you want your shifts to be side-by-side for better pipelining you need to extract them to temp locals, e.g.:

varsl0=Vector128.ShiftLeft(str0,2);varsl1=Vector128.ShiftLeft(str1,4);varsl2=Vector128.ShiftLeft(str2,6);varsr1=Vector128.ShiftRightLogical(str1,4);varsr2=Vector128.ShiftRightLogical(str2,2);Vector128<byte>dec0=Vector128.BitwiseOr(sl0,sr1);Vector128<byte>dec1=Vector128.BitwiseOr(sl1,sr2);Vector128<byte>dec2=Vector128.BitwiseOr(sl2,str3);

not sure it matters much in terms of perf

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copying that verbatim didn't make any difference.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another way to do this might be to treat the vector register as a single value and do something fancy with a masks and a single value shift. Not quite sure how that would look. Might get a some performance, but it'd be messy.


// Interleave the decoded result and store out.
// This is equivalent to a NEON ST3 instruction.
AdvSimdTbx3Byte(vzero, dec0, dec1, dec2, st3_interleave_index0, v16).Store(dest);
AdvSimdTbx3Byte(vzero, dec0, dec1, dec2, st3_interleave_index1, v16).Store(dest + 16);
AdvSimdTbx3Byte(vzero, dec0, dec1, dec2, st3_interleave_index2, v16).Store(dest + 32);

src += 64;
dest += 48;
}
while (src <= srcEnd);

srcBytes = src;
destBytes = dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe int Decode(byte* encodedBytes, ref sbyte decodingMap)
{
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions src/libraries/System.Memory/src/System/Buffers/Text/Base64Decoder.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,12 +5,14 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.Arm;
using System.Runtime.Intrinsics.X86;

namespace System.Buffers.Text
{
// AVX2 version based on https://github.com/aklomp/base64/tree/e516d769a2a432c08404f1981e73b431566057be/lib/arch/avx2
// SSSE3 version based on https://github.com/aklomp/base64/tree/e516d769a2a432c08404f1981e73b431566057be/lib/arch/ssse3
// AdvSimd version based on https://github.com/aklomp/base64/blob/e516d769a2a432c08404f1981e73b431566057be/lib/arch/neon64

public static partial class Base64
{
Expand DownExpand Up@@ -81,6 +83,15 @@ public static unsafe OperationStatus DecodeFromUtf8(ReadOnlySpan<byte> utf8, Spa
if (src == srcEnd)
goto DoneExit;
}

end = srcMax - 96;
if (BitConverter.IsLittleEndian && AdvSimd.Arm64.IsSupported && (end >= src))
{
AdvSimdDecode(ref src, ref dest, end, maxSrcLength, destLength, srcBytes, destBytes);

if (src == srcEnd)
goto DoneExit;
}
}

// Last bytes could have padding characters, so process them separately and treat them as valid only if isFinalBlock is true
Expand DownExpand Up@@ -644,6 +655,133 @@ private static unsafe void Ssse3Decode(ref byte* srcBytes, ref byte* destBytes,
destBytes = dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe Vector128<byte> AdvSimdTbx8Byte(Vector128<byte> defaults, Vector128<byte> table0, Vector128<byte> table1, Vector128<byte> table2, Vector128<byte> table3, Vector128<byte> table4, Vector128<byte> table5, Vector128<byte> table6, Vector128<byte> table7, Vector128<byte> indicies, Vector128<byte> offset)
{
// Implement an 8 way table lookup.
// This could be reduced by using two NEON TBX4 instructions.

Debug.Assert(AdvSimd.Arm64.IsSupported && BitConverter.IsLittleEndian);

Vector128<byte> dest = defaults;
Vector128<byte> indicies_sub = indicies;

dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table0, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table1, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table2, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table3, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table4, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table5, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table6, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table7, indicies_sub);

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this using perf, a lot of time is spent in this function. One reason for that is due to the chain of dependencies.

Splitting the indicies_sub into separate variables didn't make any noticeable difference:
var indicies1 = AdvSimd.Subtract(indicies, Vector128.Create((byte)(16U1)));
var indicies2 = AdvSimd.Subtract(indicies, Vector128.Create((byte)(16U
2)));
var indicies3 = AdvSimd.Subtract(indicies, Vector128.Create((byte)(16U*3)));
etc

The TBXs could be split out:
increment everything in the lookup table so that it starts from 1. Do the lookups using TBLs, meaning failures are 0. Combine all the results with ORs. Then subtract 1 from the result.
That would add more complexity, and I very much doubt it's going to give much benefit overall.


return dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe Vector128<byte> AdvSimdTbx3Byte(Vector128<byte> defaults, Vector128<byte> table0, Vector128<byte> table1, Vector128<byte> table2, Vector128<byte> indicies, Vector128<byte> offset)
{
// Implement a 3 way table lookup.

Debug.Assert(AdvSimd.Arm64.IsSupported && BitConverter.IsLittleEndian);

Vector128<byte> dest = defaults;
Vector128<byte> indicies_sub = indicies;

dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table0, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table1, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table2, indicies_sub);

return dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe void AdvSimdDecode(ref byte* srcBytes, ref byte* destBytes, byte* srcEnd, int sourceLength, int destLength, byte* srcStart, byte* destStart)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious - does AggressiveInlining here show benefits in the benchmarks?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remove this one it doesn't make a difference.
If I remove the two around AdvSimdTbx3Byte and AdvSimdTbx8Byte it gets 2x worse.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those definitely make sense while this one is a bit questionable

{
Debug.Assert(AdvSimd.Arm64.IsSupported && BitConverter.IsLittleEndian);

// Complete lookup table - similar to that used in the SS3 decode.
Vector128<byte> dec_lut0 = Vector128.Create(255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut1 = Vector128.Create(255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut2 = Vector128.Create(255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63);
Vector128<byte> dec_lut3 = Vector128.Create( 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut4 = Vector128.Create(255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);
Vector128<byte> dec_lut5 = Vector128.Create( 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut6 = Vector128.Create(255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40);
Vector128<byte> dec_lut7 = Vector128.Create( 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255);

// Interleave pattern for the ST3.
Vector128<byte> st3_interleave_index0 = Vector128.Create((byte) 0, 16, 32, 1, 17, 33, 2, 18, 34, 3, 19, 35, 4, 20, 36, 5);
Vector128<byte> st3_interleave_index1 = Vector128.Create((byte)21, 37, 6, 22, 38, 7, 23, 39, 8, 24, 40, 9, 25, 41, 10, 26);
Vector128<byte> st3_interleave_index2 = Vector128.Create((byte)42, 11, 27, 43, 12, 28, 44, 13, 29, 45, 14, 30, 46, 15, 31, 47);

// Some constants.
Vector128<byte> vzero = Vector128.Create((byte)0);
Vector128<byte> v255 = Vector128.Create((byte)255U);
Vector128<byte> v16 = Vector128.Create((byte)16U);

byte* src = srcBytes;
byte* dest = destBytes;

do
{
// Load 64 bytes of data and deinterleave the result.
// This is equivalent to a NEON LD4 instruction.
Vector128<byte> str0 = Vector128.LoadUnsafe(ref *src);
Vector128<byte> str1 = Vector128.LoadUnsafe(ref *src, 16);
Vector128<byte> str2 = Vector128.LoadUnsafe(ref *src, 32);
Vector128<byte> str3 = Vector128.LoadUnsafe(ref *src, 48);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect you can use AdvSimd.Arm64.LoadPairVector128 (and even nontemporal if it makes sense) here - jit is not smart enough yet to do it by itself

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing this worked, but didn't give any improvement

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect you can use AdvSimd.Arm64.LoadPairVector128 (and even nontemporal if it makes sense) here - jit is not smart enough yet to do it by itself

Vector128<short> tmp0 = AdvSimd.Arm64.UnzipEven(str0.AsInt16(), str1.AsInt16());
Vector128<short> tmp1 = AdvSimd.Arm64.UnzipOdd(str0.AsInt16(), str1.AsInt16());
Vector128<short> tmp2 = AdvSimd.Arm64.UnzipEven(str2.AsInt16(), str3.AsInt16());
Vector128<short> tmp3 = AdvSimd.Arm64.UnzipOdd(str2.AsInt16(), str3.AsInt16());
str0 = AdvSimd.Arm64.UnzipEven(tmp0.AsByte(), tmp2.AsByte());
str1 = AdvSimd.Arm64.UnzipOdd(tmp0.AsByte(), tmp2.AsByte());
str2 = AdvSimd.Arm64.UnzipEven(tmp1.AsByte(), tmp3.AsByte());
str3 = AdvSimd.Arm64.UnzipOdd(tmp1.AsByte(), tmp3.AsByte());

// Table lookup on each 16 bytes.
str0 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str0, v16);
str1 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str1, v16);
str2 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str2, v16);
str3 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str3, v16);

// Check for invalid input, any value larger than 63.
Vector128<byte> classified0 = AdvSimd.Arm64.MaxPairwise(str0, str1);
Vector128<byte> classified1 = AdvSimd.Arm64.MaxPairwise(str2, str3);
Vector128<byte> maxChars = AdvSimd.Arm64.MaxPairwise(classified0, classified1);
if ((maxChars.AsUInt64().ToScalar() & 0xc0c0c0c0c0c0c0c0) != 0)
break;

// Compress each four bytes into three.
Vector128<byte> dec0 = Vector128.BitwiseOr(Vector128.ShiftLeft(str0, 2), Vector128.ShiftRightLogical(str1, 4));
Vector128<byte> dec1 = Vector128.BitwiseOr(Vector128.ShiftLeft(str1, 4), Vector128.ShiftRightLogical(str2, 2));
Vector128<byte> dec2 = Vector128.BitwiseOr(Vector128.ShiftLeft(str2, 6), str3);

@EgorBoEgorBoJun 7, 2022

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jit doesn't do instruction selection so if you want your shifts to be side-by-side for better pipelining you need to extract them to temp locals, e.g.:

varsl0=Vector128.ShiftLeft(str0,2);varsl1=Vector128.ShiftLeft(str1,4);varsl2=Vector128.ShiftLeft(str2,6);varsr1=Vector128.ShiftRightLogical(str1,4);varsr2=Vector128.ShiftRightLogical(str2,2);Vector128<byte>dec0=Vector128.BitwiseOr(sl0,sr1);Vector128<byte>dec1=Vector128.BitwiseOr(sl1,sr2);Vector128<byte>dec2=Vector128.BitwiseOr(sl2,str3);

not sure it matters much in terms of perf

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copying that verbatim didn't make any difference.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another way to do this might be to treat the vector register as a single value and do something fancy with a masks and a single value shift. Not quite sure how that would look. Might get a some performance, but it'd be messy.


// Interleave the decoded result and store out.
// This is equivalent to a NEON ST3 instruction.
AdvSimdTbx3Byte(vzero, dec0, dec1, dec2, st3_interleave_index0, v16).Store(dest);
AdvSimdTbx3Byte(vzero, dec0, dec1, dec2, st3_interleave_index1, v16).Store(dest + 16);
AdvSimdTbx3Byte(vzero, dec0, dec1, dec2, st3_interleave_index2, v16).Store(dest + 32);

src += 64;
dest += 48;
}
while (src <= srcEnd);

srcBytes = src;
destBytes = dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe int Decode(byte* encodedBytes, ref sbyte decodingMap)
{
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions src/libraries/System.Memory/src/System/Buffers/Text/Base64Decoder.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,12 +5,14 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.Arm;
using System.Runtime.Intrinsics.X86;

namespace System.Buffers.Text
{
// AVX2 version based on https://github.com/aklomp/base64/tree/e516d769a2a432c08404f1981e73b431566057be/lib/arch/avx2
// SSSE3 version based on https://github.com/aklomp/base64/tree/e516d769a2a432c08404f1981e73b431566057be/lib/arch/ssse3
// AdvSimd version based on https://github.com/aklomp/base64/blob/e516d769a2a432c08404f1981e73b431566057be/lib/arch/neon64

public static partial class Base64
{
Expand DownExpand Up@@ -81,6 +83,15 @@ public static unsafe OperationStatus DecodeFromUtf8(ReadOnlySpan<byte> utf8, Spa
if (src == srcEnd)
goto DoneExit;
}

end = srcMax - 96;
if (BitConverter.IsLittleEndian && AdvSimd.Arm64.IsSupported && (end >= src))
{
AdvSimdDecode(ref src, ref dest, end, maxSrcLength, destLength, srcBytes, destBytes);

if (src == srcEnd)
goto DoneExit;
}
}

// Last bytes could have padding characters, so process them separately and treat them as valid only if isFinalBlock is true
Expand DownExpand Up@@ -644,6 +655,133 @@ private static unsafe void Ssse3Decode(ref byte* srcBytes, ref byte* destBytes,
destBytes = dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe Vector128<byte> AdvSimdTbx8Byte(Vector128<byte> defaults, Vector128<byte> table0, Vector128<byte> table1, Vector128<byte> table2, Vector128<byte> table3, Vector128<byte> table4, Vector128<byte> table5, Vector128<byte> table6, Vector128<byte> table7, Vector128<byte> indicies, Vector128<byte> offset)
{
// Implement an 8 way table lookup.
// This could be reduced by using two NEON TBX4 instructions.

Debug.Assert(AdvSimd.Arm64.IsSupported && BitConverter.IsLittleEndian);

Vector128<byte> dest = defaults;
Vector128<byte> indicies_sub = indicies;

dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table0, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table1, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table2, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table3, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table4, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table5, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table6, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table7, indicies_sub);

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this using perf, a lot of time is spent in this function. One reason for that is due to the chain of dependencies.

Splitting the indicies_sub into separate variables didn't make any noticeable difference:
var indicies1 = AdvSimd.Subtract(indicies, Vector128.Create((byte)(16U1)));
var indicies2 = AdvSimd.Subtract(indicies, Vector128.Create((byte)(16U
2)));
var indicies3 = AdvSimd.Subtract(indicies, Vector128.Create((byte)(16U*3)));
etc

The TBXs could be split out:
increment everything in the lookup table so that it starts from 1. Do the lookups using TBLs, meaning failures are 0. Combine all the results with ORs. Then subtract 1 from the result.
That would add more complexity, and I very much doubt it's going to give much benefit overall.


return dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe Vector128<byte> AdvSimdTbx3Byte(Vector128<byte> defaults, Vector128<byte> table0, Vector128<byte> table1, Vector128<byte> table2, Vector128<byte> indicies, Vector128<byte> offset)
{
// Implement a 3 way table lookup.

Debug.Assert(AdvSimd.Arm64.IsSupported && BitConverter.IsLittleEndian);

Vector128<byte> dest = defaults;
Vector128<byte> indicies_sub = indicies;

dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table0, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table1, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table2, indicies_sub);

return dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe void AdvSimdDecode(ref byte* srcBytes, ref byte* destBytes, byte* srcEnd, int sourceLength, int destLength, byte* srcStart, byte* destStart)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious - does AggressiveInlining here show benefits in the benchmarks?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remove this one it doesn't make a difference.
If I remove the two around AdvSimdTbx3Byte and AdvSimdTbx8Byte it gets 2x worse.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those definitely make sense while this one is a bit questionable

{
Debug.Assert(AdvSimd.Arm64.IsSupported && BitConverter.IsLittleEndian);

// Complete lookup table - similar to that used in the SS3 decode.
Vector128<byte> dec_lut0 = Vector128.Create(255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut1 = Vector128.Create(255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut2 = Vector128.Create(255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63);
Vector128<byte> dec_lut3 = Vector128.Create( 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut4 = Vector128.Create(255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);
Vector128<byte> dec_lut5 = Vector128.Create( 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut6 = Vector128.Create(255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40);
Vector128<byte> dec_lut7 = Vector128.Create( 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255);

// Interleave pattern for the ST3.
Vector128<byte> st3_interleave_index0 = Vector128.Create((byte) 0, 16, 32, 1, 17, 33, 2, 18, 34, 3, 19, 35, 4, 20, 36, 5);
Vector128<byte> st3_interleave_index1 = Vector128.Create((byte)21, 37, 6, 22, 38, 7, 23, 39, 8, 24, 40, 9, 25, 41, 10, 26);
Vector128<byte> st3_interleave_index2 = Vector128.Create((byte)42, 11, 27, 43, 12, 28, 44, 13, 29, 45, 14, 30, 46, 15, 31, 47);

// Some constants.
Vector128<byte> vzero = Vector128.Create((byte)0);
Vector128<byte> v255 = Vector128.Create((byte)255U);
Vector128<byte> v16 = Vector128.Create((byte)16U);

byte* src = srcBytes;
byte* dest = destBytes;

do
{
// Load 64 bytes of data and deinterleave the result.
// This is equivalent to a NEON LD4 instruction.
Vector128<byte> str0 = Vector128.LoadUnsafe(ref *src);
Vector128<byte> str1 = Vector128.LoadUnsafe(ref *src, 16);
Vector128<byte> str2 = Vector128.LoadUnsafe(ref *src, 32);
Vector128<byte> str3 = Vector128.LoadUnsafe(ref *src, 48);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect you can use AdvSimd.Arm64.LoadPairVector128 (and even nontemporal if it makes sense) here - jit is not smart enough yet to do it by itself

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing this worked, but didn't give any improvement

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect you can use AdvSimd.Arm64.LoadPairVector128 (and even nontemporal if it makes sense) here - jit is not smart enough yet to do it by itself

Vector128<short> tmp0 = AdvSimd.Arm64.UnzipEven(str0.AsInt16(), str1.AsInt16());
Vector128<short> tmp1 = AdvSimd.Arm64.UnzipOdd(str0.AsInt16(), str1.AsInt16());
Vector128<short> tmp2 = AdvSimd.Arm64.UnzipEven(str2.AsInt16(), str3.AsInt16());
Vector128<short> tmp3 = AdvSimd.Arm64.UnzipOdd(str2.AsInt16(), str3.AsInt16());
str0 = AdvSimd.Arm64.UnzipEven(tmp0.AsByte(), tmp2.AsByte());
str1 = AdvSimd.Arm64.UnzipOdd(tmp0.AsByte(), tmp2.AsByte());
str2 = AdvSimd.Arm64.UnzipEven(tmp1.AsByte(), tmp3.AsByte());
str3 = AdvSimd.Arm64.UnzipOdd(tmp1.AsByte(), tmp3.AsByte());

// Table lookup on each 16 bytes.
str0 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str0, v16);
str1 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str1, v16);
str2 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str2, v16);
str3 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str3, v16);

// Check for invalid input, any value larger than 63.
Vector128<byte> classified0 = AdvSimd.Arm64.MaxPairwise(str0, str1);
Vector128<byte> classified1 = AdvSimd.Arm64.MaxPairwise(str2, str3);
Vector128<byte> maxChars = AdvSimd.Arm64.MaxPairwise(classified0, classified1);
if ((maxChars.AsUInt64().ToScalar() & 0xc0c0c0c0c0c0c0c0) != 0)
break;

// Compress each four bytes into three.
Vector128<byte> dec0 = Vector128.BitwiseOr(Vector128.ShiftLeft(str0, 2), Vector128.ShiftRightLogical(str1, 4));
Vector128<byte> dec1 = Vector128.BitwiseOr(Vector128.ShiftLeft(str1, 4), Vector128.ShiftRightLogical(str2, 2));
Vector128<byte> dec2 = Vector128.BitwiseOr(Vector128.ShiftLeft(str2, 6), str3);

@EgorBoEgorBoJun 7, 2022

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jit doesn't do instruction selection so if you want your shifts to be side-by-side for better pipelining you need to extract them to temp locals, e.g.:

varsl0=Vector128.ShiftLeft(str0,2);varsl1=Vector128.ShiftLeft(str1,4);varsl2=Vector128.ShiftLeft(str2,6);varsr1=Vector128.ShiftRightLogical(str1,4);varsr2=Vector128.ShiftRightLogical(str2,2);Vector128<byte>dec0=Vector128.BitwiseOr(sl0,sr1);Vector128<byte>dec1=Vector128.BitwiseOr(sl1,sr2);Vector128<byte>dec2=Vector128.BitwiseOr(sl2,str3);

not sure it matters much in terms of perf

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copying that verbatim didn't make any difference.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another way to do this might be to treat the vector register as a single value and do something fancy with a masks and a single value shift. Not quite sure how that would look. Might get a some performance, but it'd be messy.


// Interleave the decoded result and store out.
// This is equivalent to a NEON ST3 instruction.
AdvSimdTbx3Byte(vzero, dec0, dec1, dec2, st3_interleave_index0, v16).Store(dest);
AdvSimdTbx3Byte(vzero, dec0, dec1, dec2, st3_interleave_index1, v16).Store(dest + 16);
AdvSimdTbx3Byte(vzero, dec0, dec1, dec2, st3_interleave_index2, v16).Store(dest + 32);

src += 64;
dest += 48;
}
while (src <= srcEnd);

srcBytes = src;
destBytes = dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe int Decode(byte* encodedBytes, ref sbyte decodingMap)
{
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions src/libraries/System.Memory/src/System/Buffers/Text/Base64Decoder.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,12 +5,14 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.Arm;
using System.Runtime.Intrinsics.X86;

namespace System.Buffers.Text
{
// AVX2 version based on https://github.com/aklomp/base64/tree/e516d769a2a432c08404f1981e73b431566057be/lib/arch/avx2
// SSSE3 version based on https://github.com/aklomp/base64/tree/e516d769a2a432c08404f1981e73b431566057be/lib/arch/ssse3
// AdvSimd version based on https://github.com/aklomp/base64/blob/e516d769a2a432c08404f1981e73b431566057be/lib/arch/neon64

public static partial class Base64
{
Expand DownExpand Up@@ -81,6 +83,15 @@ public static unsafe OperationStatus DecodeFromUtf8(ReadOnlySpan<byte> utf8, Spa
if (src == srcEnd)
goto DoneExit;
}

end = srcMax - 96;
if (BitConverter.IsLittleEndian && AdvSimd.Arm64.IsSupported && (end >= src))
{
AdvSimdDecode(ref src, ref dest, end, maxSrcLength, destLength, srcBytes, destBytes);

if (src == srcEnd)
goto DoneExit;
}
}

// Last bytes could have padding characters, so process them separately and treat them as valid only if isFinalBlock is true
Expand DownExpand Up@@ -644,6 +655,133 @@ private static unsafe void Ssse3Decode(ref byte* srcBytes, ref byte* destBytes,
destBytes = dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe Vector128<byte> AdvSimdTbx8Byte(Vector128<byte> defaults, Vector128<byte> table0, Vector128<byte> table1, Vector128<byte> table2, Vector128<byte> table3, Vector128<byte> table4, Vector128<byte> table5, Vector128<byte> table6, Vector128<byte> table7, Vector128<byte> indicies, Vector128<byte> offset)
{
// Implement an 8 way table lookup.
// This could be reduced by using two NEON TBX4 instructions.

Debug.Assert(AdvSimd.Arm64.IsSupported && BitConverter.IsLittleEndian);

Vector128<byte> dest = defaults;
Vector128<byte> indicies_sub = indicies;

dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table0, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table1, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table2, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table3, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table4, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table5, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table6, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table7, indicies_sub);

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this using perf, a lot of time is spent in this function. One reason for that is due to the chain of dependencies.

Splitting the indicies_sub into separate variables didn't make any noticeable difference:
var indicies1 = AdvSimd.Subtract(indicies, Vector128.Create((byte)(16U1)));
var indicies2 = AdvSimd.Subtract(indicies, Vector128.Create((byte)(16U
2)));
var indicies3 = AdvSimd.Subtract(indicies, Vector128.Create((byte)(16U*3)));
etc

The TBXs could be split out:
increment everything in the lookup table so that it starts from 1. Do the lookups using TBLs, meaning failures are 0. Combine all the results with ORs. Then subtract 1 from the result.
That would add more complexity, and I very much doubt it's going to give much benefit overall.


return dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe Vector128<byte> AdvSimdTbx3Byte(Vector128<byte> defaults, Vector128<byte> table0, Vector128<byte> table1, Vector128<byte> table2, Vector128<byte> indicies, Vector128<byte> offset)
{
// Implement a 3 way table lookup.

Debug.Assert(AdvSimd.Arm64.IsSupported && BitConverter.IsLittleEndian);

Vector128<byte> dest = defaults;
Vector128<byte> indicies_sub = indicies;

dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table0, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table1, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table2, indicies_sub);

return dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe void AdvSimdDecode(ref byte* srcBytes, ref byte* destBytes, byte* srcEnd, int sourceLength, int destLength, byte* srcStart, byte* destStart)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious - does AggressiveInlining here show benefits in the benchmarks?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remove this one it doesn't make a difference.
If I remove the two around AdvSimdTbx3Byte and AdvSimdTbx8Byte it gets 2x worse.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those definitely make sense while this one is a bit questionable

{
Debug.Assert(AdvSimd.Arm64.IsSupported && BitConverter.IsLittleEndian);

// Complete lookup table - similar to that used in the SS3 decode.
Vector128<byte> dec_lut0 = Vector128.Create(255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut1 = Vector128.Create(255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut2 = Vector128.Create(255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63);
Vector128<byte> dec_lut3 = Vector128.Create( 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut4 = Vector128.Create(255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);
Vector128<byte> dec_lut5 = Vector128.Create( 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut6 = Vector128.Create(255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40);
Vector128<byte> dec_lut7 = Vector128.Create( 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255);

// Interleave pattern for the ST3.
Vector128<byte> st3_interleave_index0 = Vector128.Create((byte) 0, 16, 32, 1, 17, 33, 2, 18, 34, 3, 19, 35, 4, 20, 36, 5);
Vector128<byte> st3_interleave_index1 = Vector128.Create((byte)21, 37, 6, 22, 38, 7, 23, 39, 8, 24, 40, 9, 25, 41, 10, 26);
Vector128<byte> st3_interleave_index2 = Vector128.Create((byte)42, 11, 27, 43, 12, 28, 44, 13, 29, 45, 14, 30, 46, 15, 31, 47);

// Some constants.
Vector128<byte> vzero = Vector128.Create((byte)0);
Vector128<byte> v255 = Vector128.Create((byte)255U);
Vector128<byte> v16 = Vector128.Create((byte)16U);

byte* src = srcBytes;
byte* dest = destBytes;

do
{
// Load 64 bytes of data and deinterleave the result.
// This is equivalent to a NEON LD4 instruction.
Vector128<byte> str0 = Vector128.LoadUnsafe(ref *src);
Vector128<byte> str1 = Vector128.LoadUnsafe(ref *src, 16);
Vector128<byte> str2 = Vector128.LoadUnsafe(ref *src, 32);
Vector128<byte> str3 = Vector128.LoadUnsafe(ref *src, 48);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect you can use AdvSimd.Arm64.LoadPairVector128 (and even nontemporal if it makes sense) here - jit is not smart enough yet to do it by itself

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing this worked, but didn't give any improvement

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect you can use AdvSimd.Arm64.LoadPairVector128 (and even nontemporal if it makes sense) here - jit is not smart enough yet to do it by itself

Vector128<short> tmp0 = AdvSimd.Arm64.UnzipEven(str0.AsInt16(), str1.AsInt16());
Vector128<short> tmp1 = AdvSimd.Arm64.UnzipOdd(str0.AsInt16(), str1.AsInt16());
Vector128<short> tmp2 = AdvSimd.Arm64.UnzipEven(str2.AsInt16(), str3.AsInt16());
Vector128<short> tmp3 = AdvSimd.Arm64.UnzipOdd(str2.AsInt16(), str3.AsInt16());
str0 = AdvSimd.Arm64.UnzipEven(tmp0.AsByte(), tmp2.AsByte());
str1 = AdvSimd.Arm64.UnzipOdd(tmp0.AsByte(), tmp2.AsByte());
str2 = AdvSimd.Arm64.UnzipEven(tmp1.AsByte(), tmp3.AsByte());
str3 = AdvSimd.Arm64.UnzipOdd(tmp1.AsByte(), tmp3.AsByte());

// Table lookup on each 16 bytes.
str0 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str0, v16);
str1 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str1, v16);
str2 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str2, v16);
str3 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str3, v16);

// Check for invalid input, any value larger than 63.
Vector128<byte> classified0 = AdvSimd.Arm64.MaxPairwise(str0, str1);
Vector128<byte> classified1 = AdvSimd.Arm64.MaxPairwise(str2, str3);
Vector128<byte> maxChars = AdvSimd.Arm64.MaxPairwise(classified0, classified1);
if ((maxChars.AsUInt64().ToScalar() & 0xc0c0c0c0c0c0c0c0) != 0)
break;

// Compress each four bytes into three.
Vector128<byte> dec0 = Vector128.BitwiseOr(Vector128.ShiftLeft(str0, 2), Vector128.ShiftRightLogical(str1, 4));
Vector128<byte> dec1 = Vector128.BitwiseOr(Vector128.ShiftLeft(str1, 4), Vector128.ShiftRightLogical(str2, 2));
Vector128<byte> dec2 = Vector128.BitwiseOr(Vector128.ShiftLeft(str2, 6), str3);

@EgorBoEgorBoJun 7, 2022

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jit doesn't do instruction selection so if you want your shifts to be side-by-side for better pipelining you need to extract them to temp locals, e.g.:

varsl0=Vector128.ShiftLeft(str0,2);varsl1=Vector128.ShiftLeft(str1,4);varsl2=Vector128.ShiftLeft(str2,6);varsr1=Vector128.ShiftRightLogical(str1,4);varsr2=Vector128.ShiftRightLogical(str2,2);Vector128<byte>dec0=Vector128.BitwiseOr(sl0,sr1);Vector128<byte>dec1=Vector128.BitwiseOr(sl1,sr2);Vector128<byte>dec2=Vector128.BitwiseOr(sl2,str3);

not sure it matters much in terms of perf

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copying that verbatim didn't make any difference.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another way to do this might be to treat the vector register as a single value and do something fancy with a masks and a single value shift. Not quite sure how that would look. Might get a some performance, but it'd be messy.


// Interleave the decoded result and store out.
// This is equivalent to a NEON ST3 instruction.
AdvSimdTbx3Byte(vzero, dec0, dec1, dec2, st3_interleave_index0, v16).Store(dest);
AdvSimdTbx3Byte(vzero, dec0, dec1, dec2, st3_interleave_index1, v16).Store(dest + 16);
AdvSimdTbx3Byte(vzero, dec0, dec1, dec2, st3_interleave_index2, v16).Store(dest + 32);

src += 64;
dest += 48;
}
while (src <= srcEnd);

srcBytes = src;
destBytes = dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe int Decode(byte* encodedBytes, ref sbyte decodingMap)
{
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions src/libraries/System.Memory/src/System/Buffers/Text/Base64Decoder.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,12 +5,14 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.Arm;
using System.Runtime.Intrinsics.X86;

namespace System.Buffers.Text
{
// AVX2 version based on https://github.com/aklomp/base64/tree/e516d769a2a432c08404f1981e73b431566057be/lib/arch/avx2
// SSSE3 version based on https://github.com/aklomp/base64/tree/e516d769a2a432c08404f1981e73b431566057be/lib/arch/ssse3
// AdvSimd version based on https://github.com/aklomp/base64/blob/e516d769a2a432c08404f1981e73b431566057be/lib/arch/neon64

public static partial class Base64
{
Expand DownExpand Up@@ -81,6 +83,15 @@ public static unsafe OperationStatus DecodeFromUtf8(ReadOnlySpan<byte> utf8, Spa
if (src == srcEnd)
goto DoneExit;
}

end = srcMax - 96;
if (BitConverter.IsLittleEndian && AdvSimd.Arm64.IsSupported && (end >= src))
{
AdvSimdDecode(ref src, ref dest, end, maxSrcLength, destLength, srcBytes, destBytes);

if (src == srcEnd)
goto DoneExit;
}
}

// Last bytes could have padding characters, so process them separately and treat them as valid only if isFinalBlock is true
Expand DownExpand Up@@ -644,6 +655,133 @@ private static unsafe void Ssse3Decode(ref byte* srcBytes, ref byte* destBytes,
destBytes = dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe Vector128<byte> AdvSimdTbx8Byte(Vector128<byte> defaults, Vector128<byte> table0, Vector128<byte> table1, Vector128<byte> table2, Vector128<byte> table3, Vector128<byte> table4, Vector128<byte> table5, Vector128<byte> table6, Vector128<byte> table7, Vector128<byte> indicies, Vector128<byte> offset)
{
// Implement an 8 way table lookup.
// This could be reduced by using two NEON TBX4 instructions.

Debug.Assert(AdvSimd.Arm64.IsSupported && BitConverter.IsLittleEndian);

Vector128<byte> dest = defaults;
Vector128<byte> indicies_sub = indicies;

dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table0, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table1, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table2, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table3, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table4, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table5, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table6, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table7, indicies_sub);

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this using perf, a lot of time is spent in this function. One reason for that is due to the chain of dependencies.

Splitting the indicies_sub into separate variables didn't make any noticeable difference:
var indicies1 = AdvSimd.Subtract(indicies, Vector128.Create((byte)(16U1)));
var indicies2 = AdvSimd.Subtract(indicies, Vector128.Create((byte)(16U
2)));
var indicies3 = AdvSimd.Subtract(indicies, Vector128.Create((byte)(16U*3)));
etc

The TBXs could be split out:
increment everything in the lookup table so that it starts from 1. Do the lookups using TBLs, meaning failures are 0. Combine all the results with ORs. Then subtract 1 from the result.
That would add more complexity, and I very much doubt it's going to give much benefit overall.


return dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe Vector128<byte> AdvSimdTbx3Byte(Vector128<byte> defaults, Vector128<byte> table0, Vector128<byte> table1, Vector128<byte> table2, Vector128<byte> indicies, Vector128<byte> offset)
{
// Implement a 3 way table lookup.

Debug.Assert(AdvSimd.Arm64.IsSupported && BitConverter.IsLittleEndian);

Vector128<byte> dest = defaults;
Vector128<byte> indicies_sub = indicies;

dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table0, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table1, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table2, indicies_sub);

return dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe void AdvSimdDecode(ref byte* srcBytes, ref byte* destBytes, byte* srcEnd, int sourceLength, int destLength, byte* srcStart, byte* destStart)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious - does AggressiveInlining here show benefits in the benchmarks?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remove this one it doesn't make a difference.
If I remove the two around AdvSimdTbx3Byte and AdvSimdTbx8Byte it gets 2x worse.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those definitely make sense while this one is a bit questionable

{
Debug.Assert(AdvSimd.Arm64.IsSupported && BitConverter.IsLittleEndian);

// Complete lookup table - similar to that used in the SS3 decode.
Vector128<byte> dec_lut0 = Vector128.Create(255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut1 = Vector128.Create(255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut2 = Vector128.Create(255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63);
Vector128<byte> dec_lut3 = Vector128.Create( 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut4 = Vector128.Create(255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);
Vector128<byte> dec_lut5 = Vector128.Create( 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut6 = Vector128.Create(255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40);
Vector128<byte> dec_lut7 = Vector128.Create( 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255);

// Interleave pattern for the ST3.
Vector128<byte> st3_interleave_index0 = Vector128.Create((byte) 0, 16, 32, 1, 17, 33, 2, 18, 34, 3, 19, 35, 4, 20, 36, 5);
Vector128<byte> st3_interleave_index1 = Vector128.Create((byte)21, 37, 6, 22, 38, 7, 23, 39, 8, 24, 40, 9, 25, 41, 10, 26);
Vector128<byte> st3_interleave_index2 = Vector128.Create((byte)42, 11, 27, 43, 12, 28, 44, 13, 29, 45, 14, 30, 46, 15, 31, 47);

// Some constants.
Vector128<byte> vzero = Vector128.Create((byte)0);
Vector128<byte> v255 = Vector128.Create((byte)255U);
Vector128<byte> v16 = Vector128.Create((byte)16U);

byte* src = srcBytes;
byte* dest = destBytes;

do
{
// Load 64 bytes of data and deinterleave the result.
// This is equivalent to a NEON LD4 instruction.
Vector128<byte> str0 = Vector128.LoadUnsafe(ref *src);
Vector128<byte> str1 = Vector128.LoadUnsafe(ref *src, 16);
Vector128<byte> str2 = Vector128.LoadUnsafe(ref *src, 32);
Vector128<byte> str3 = Vector128.LoadUnsafe(ref *src, 48);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect you can use AdvSimd.Arm64.LoadPairVector128 (and even nontemporal if it makes sense) here - jit is not smart enough yet to do it by itself

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing this worked, but didn't give any improvement

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect you can use AdvSimd.Arm64.LoadPairVector128 (and even nontemporal if it makes sense) here - jit is not smart enough yet to do it by itself

Vector128<short> tmp0 = AdvSimd.Arm64.UnzipEven(str0.AsInt16(), str1.AsInt16());
Vector128<short> tmp1 = AdvSimd.Arm64.UnzipOdd(str0.AsInt16(), str1.AsInt16());
Vector128<short> tmp2 = AdvSimd.Arm64.UnzipEven(str2.AsInt16(), str3.AsInt16());
Vector128<short> tmp3 = AdvSimd.Arm64.UnzipOdd(str2.AsInt16(), str3.AsInt16());
str0 = AdvSimd.Arm64.UnzipEven(tmp0.AsByte(), tmp2.AsByte());
str1 = AdvSimd.Arm64.UnzipOdd(tmp0.AsByte(), tmp2.AsByte());
str2 = AdvSimd.Arm64.UnzipEven(tmp1.AsByte(), tmp3.AsByte());
str3 = AdvSimd.Arm64.UnzipOdd(tmp1.AsByte(), tmp3.AsByte());

// Table lookup on each 16 bytes.
str0 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str0, v16);
str1 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str1, v16);
str2 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str2, v16);
str3 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str3, v16);

// Check for invalid input, any value larger than 63.
Vector128<byte> classified0 = AdvSimd.Arm64.MaxPairwise(str0, str1);
Vector128<byte> classified1 = AdvSimd.Arm64.MaxPairwise(str2, str3);
Vector128<byte> maxChars = AdvSimd.Arm64.MaxPairwise(classified0, classified1);
if ((maxChars.AsUInt64().ToScalar() & 0xc0c0c0c0c0c0c0c0) != 0)
break;

// Compress each four bytes into three.
Vector128<byte> dec0 = Vector128.BitwiseOr(Vector128.ShiftLeft(str0, 2), Vector128.ShiftRightLogical(str1, 4));
Vector128<byte> dec1 = Vector128.BitwiseOr(Vector128.ShiftLeft(str1, 4), Vector128.ShiftRightLogical(str2, 2));
Vector128<byte> dec2 = Vector128.BitwiseOr(Vector128.ShiftLeft(str2, 6), str3);

@EgorBoEgorBoJun 7, 2022

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jit doesn't do instruction selection so if you want your shifts to be side-by-side for better pipelining you need to extract them to temp locals, e.g.:

varsl0=Vector128.ShiftLeft(str0,2);varsl1=Vector128.ShiftLeft(str1,4);varsl2=Vector128.ShiftLeft(str2,6);varsr1=Vector128.ShiftRightLogical(str1,4);varsr2=Vector128.ShiftRightLogical(str2,2);Vector128<byte>dec0=Vector128.BitwiseOr(sl0,sr1);Vector128<byte>dec1=Vector128.BitwiseOr(sl1,sr2);Vector128<byte>dec2=Vector128.BitwiseOr(sl2,str3);

not sure it matters much in terms of perf

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copying that verbatim didn't make any difference.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another way to do this might be to treat the vector register as a single value and do something fancy with a masks and a single value shift. Not quite sure how that would look. Might get a some performance, but it'd be messy.


// Interleave the decoded result and store out.
// This is equivalent to a NEON ST3 instruction.
AdvSimdTbx3Byte(vzero, dec0, dec1, dec2, st3_interleave_index0, v16).Store(dest);
AdvSimdTbx3Byte(vzero, dec0, dec1, dec2, st3_interleave_index1, v16).Store(dest + 16);
AdvSimdTbx3Byte(vzero, dec0, dec1, dec2, st3_interleave_index2, v16).Store(dest + 32);

src += 64;
dest += 48;
}
while (src <= srcEnd);

srcBytes = src;
destBytes = dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe int Decode(byte* encodedBytes, ref sbyte decodingMap)
{
Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions src/libraries/System.Memory/src/System/Buffers/Text/Base64Decoder.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,12 +5,14 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.Arm;
using System.Runtime.Intrinsics.X86;

namespace System.Buffers.Text
{
// AVX2 version based on https://github.com/aklomp/base64/tree/e516d769a2a432c08404f1981e73b431566057be/lib/arch/avx2
// SSSE3 version based on https://github.com/aklomp/base64/tree/e516d769a2a432c08404f1981e73b431566057be/lib/arch/ssse3
// AdvSimd version based on https://github.com/aklomp/base64/blob/e516d769a2a432c08404f1981e73b431566057be/lib/arch/neon64

public static partial class Base64
{
Expand DownExpand Up@@ -81,6 +83,15 @@ public static unsafe OperationStatus DecodeFromUtf8(ReadOnlySpan<byte> utf8, Spa
if (src == srcEnd)
goto DoneExit;
}

end = srcMax - 96;
if (BitConverter.IsLittleEndian && AdvSimd.Arm64.IsSupported && (end >= src))
{
AdvSimdDecode(ref src, ref dest, end, maxSrcLength, destLength, srcBytes, destBytes);

if (src == srcEnd)
goto DoneExit;
}
}

// Last bytes could have padding characters, so process them separately and treat them as valid only if isFinalBlock is true
Expand DownExpand Up@@ -644,6 +655,133 @@ private static unsafe void Ssse3Decode(ref byte* srcBytes, ref byte* destBytes,
destBytes = dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe Vector128<byte> AdvSimdTbx8Byte(Vector128<byte> defaults, Vector128<byte> table0, Vector128<byte> table1, Vector128<byte> table2, Vector128<byte> table3, Vector128<byte> table4, Vector128<byte> table5, Vector128<byte> table6, Vector128<byte> table7, Vector128<byte> indicies, Vector128<byte> offset)
{
// Implement an 8 way table lookup.
// This could be reduced by using two NEON TBX4 instructions.

Debug.Assert(AdvSimd.Arm64.IsSupported && BitConverter.IsLittleEndian);

Vector128<byte> dest = defaults;
Vector128<byte> indicies_sub = indicies;

dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table0, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table1, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table2, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table3, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table4, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table5, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table6, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table7, indicies_sub);

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this using perf, a lot of time is spent in this function. One reason for that is due to the chain of dependencies.

Splitting the indicies_sub into separate variables didn't make any noticeable difference:
var indicies1 = AdvSimd.Subtract(indicies, Vector128.Create((byte)(16U1)));
var indicies2 = AdvSimd.Subtract(indicies, Vector128.Create((byte)(16U
2)));
var indicies3 = AdvSimd.Subtract(indicies, Vector128.Create((byte)(16U*3)));
etc

The TBXs could be split out:
increment everything in the lookup table so that it starts from 1. Do the lookups using TBLs, meaning failures are 0. Combine all the results with ORs. Then subtract 1 from the result.
That would add more complexity, and I very much doubt it's going to give much benefit overall.


return dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe Vector128<byte> AdvSimdTbx3Byte(Vector128<byte> defaults, Vector128<byte> table0, Vector128<byte> table1, Vector128<byte> table2, Vector128<byte> indicies, Vector128<byte> offset)
{
// Implement a 3 way table lookup.

Debug.Assert(AdvSimd.Arm64.IsSupported && BitConverter.IsLittleEndian);

Vector128<byte> dest = defaults;
Vector128<byte> indicies_sub = indicies;

dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table0, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table1, indicies_sub);
indicies_sub = AdvSimd.Subtract(indicies_sub, offset);
dest = AdvSimd.Arm64.VectorTableLookupExtension(dest, table2, indicies_sub);

return dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe void AdvSimdDecode(ref byte* srcBytes, ref byte* destBytes, byte* srcEnd, int sourceLength, int destLength, byte* srcStart, byte* destStart)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious - does AggressiveInlining here show benefits in the benchmarks?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remove this one it doesn't make a difference.
If I remove the two around AdvSimdTbx3Byte and AdvSimdTbx8Byte it gets 2x worse.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those definitely make sense while this one is a bit questionable

{
Debug.Assert(AdvSimd.Arm64.IsSupported && BitConverter.IsLittleEndian);

// Complete lookup table - similar to that used in the SS3 decode.
Vector128<byte> dec_lut0 = Vector128.Create(255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut1 = Vector128.Create(255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut2 = Vector128.Create(255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63);
Vector128<byte> dec_lut3 = Vector128.Create( 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut4 = Vector128.Create(255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);
Vector128<byte> dec_lut5 = Vector128.Create( 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255);
Vector128<byte> dec_lut6 = Vector128.Create(255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40);
Vector128<byte> dec_lut7 = Vector128.Create( 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255);

// Interleave pattern for the ST3.
Vector128<byte> st3_interleave_index0 = Vector128.Create((byte) 0, 16, 32, 1, 17, 33, 2, 18, 34, 3, 19, 35, 4, 20, 36, 5);
Vector128<byte> st3_interleave_index1 = Vector128.Create((byte)21, 37, 6, 22, 38, 7, 23, 39, 8, 24, 40, 9, 25, 41, 10, 26);
Vector128<byte> st3_interleave_index2 = Vector128.Create((byte)42, 11, 27, 43, 12, 28, 44, 13, 29, 45, 14, 30, 46, 15, 31, 47);

// Some constants.
Vector128<byte> vzero = Vector128.Create((byte)0);
Vector128<byte> v255 = Vector128.Create((byte)255U);
Vector128<byte> v16 = Vector128.Create((byte)16U);

byte* src = srcBytes;
byte* dest = destBytes;

do
{
// Load 64 bytes of data and deinterleave the result.
// This is equivalent to a NEON LD4 instruction.
Vector128<byte> str0 = Vector128.LoadUnsafe(ref *src);
Vector128<byte> str1 = Vector128.LoadUnsafe(ref *src, 16);
Vector128<byte> str2 = Vector128.LoadUnsafe(ref *src, 32);
Vector128<byte> str3 = Vector128.LoadUnsafe(ref *src, 48);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect you can use AdvSimd.Arm64.LoadPairVector128 (and even nontemporal if it makes sense) here - jit is not smart enough yet to do it by itself

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing this worked, but didn't give any improvement

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect you can use AdvSimd.Arm64.LoadPairVector128 (and even nontemporal if it makes sense) here - jit is not smart enough yet to do it by itself

Vector128<short> tmp0 = AdvSimd.Arm64.UnzipEven(str0.AsInt16(), str1.AsInt16());
Vector128<short> tmp1 = AdvSimd.Arm64.UnzipOdd(str0.AsInt16(), str1.AsInt16());
Vector128<short> tmp2 = AdvSimd.Arm64.UnzipEven(str2.AsInt16(), str3.AsInt16());
Vector128<short> tmp3 = AdvSimd.Arm64.UnzipOdd(str2.AsInt16(), str3.AsInt16());
str0 = AdvSimd.Arm64.UnzipEven(tmp0.AsByte(), tmp2.AsByte());
str1 = AdvSimd.Arm64.UnzipOdd(tmp0.AsByte(), tmp2.AsByte());
str2 = AdvSimd.Arm64.UnzipEven(tmp1.AsByte(), tmp3.AsByte());
str3 = AdvSimd.Arm64.UnzipOdd(tmp1.AsByte(), tmp3.AsByte());

// Table lookup on each 16 bytes.
str0 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str0, v16);
str1 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str1, v16);
str2 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str2, v16);
str3 = AdvSimdTbx8Byte(v255, dec_lut0, dec_lut1, dec_lut2, dec_lut3, dec_lut4, dec_lut5, dec_lut6, dec_lut7, str3, v16);

// Check for invalid input, any value larger than 63.
Vector128<byte> classified0 = AdvSimd.Arm64.MaxPairwise(str0, str1);
Vector128<byte> classified1 = AdvSimd.Arm64.MaxPairwise(str2, str3);
Vector128<byte> maxChars = AdvSimd.Arm64.MaxPairwise(classified0, classified1);
if ((maxChars.AsUInt64().ToScalar() & 0xc0c0c0c0c0c0c0c0) != 0)
break;

// Compress each four bytes into three.
Vector128<byte> dec0 = Vector128.BitwiseOr(Vector128.ShiftLeft(str0, 2), Vector128.ShiftRightLogical(str1, 4));
Vector128<byte> dec1 = Vector128.BitwiseOr(Vector128.ShiftLeft(str1, 4), Vector128.ShiftRightLogical(str2, 2));
Vector128<byte> dec2 = Vector128.BitwiseOr(Vector128.ShiftLeft(str2, 6), str3);

@EgorBoEgorBoJun 7, 2022

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jit doesn't do instruction selection so if you want your shifts to be side-by-side for better pipelining you need to extract them to temp locals, e.g.:

varsl0=Vector128.ShiftLeft(str0,2);varsl1=Vector128.ShiftLeft(str1,4);varsl2=Vector128.ShiftLeft(str2,6);varsr1=Vector128.ShiftRightLogical(str1,4);varsr2=Vector128.ShiftRightLogical(str2,2);Vector128<byte>dec0=Vector128.BitwiseOr(sl0,sr1);Vector128<byte>dec1=Vector128.BitwiseOr(sl1,sr2);Vector128<byte>dec2=Vector128.BitwiseOr(sl2,str3);

not sure it matters much in terms of perf

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copying that verbatim didn't make any difference.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another way to do this might be to treat the vector register as a single value and do something fancy with a masks and a single value shift. Not quite sure how that would look. Might get a some performance, but it'd be messy.


// Interleave the decoded result and store out.
// This is equivalent to a NEON ST3 instruction.
AdvSimdTbx3Byte(vzero, dec0, dec1, dec2, st3_interleave_index0, v16).Store(dest);
AdvSimdTbx3Byte(vzero, dec0, dec1, dec2, st3_interleave_index1, v16).Store(dest + 16);
AdvSimdTbx3Byte(vzero, dec0, dec1, dec2, st3_interleave_index2, v16).Store(dest + 32);

src += 64;
dest += 48;
}
while (src <= srcEnd);

srcBytes = src;
destBytes = dest;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe int Decode(byte* encodedBytes, ref sbyte decodingMap)
{
Expand Down