-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBase64.cs
More file actions
77 lines (67 loc) · 3.27 KB
/
Base64.cs
File metadata and controls
77 lines (67 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.Buffers;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics.Arm;
using System.Runtime.Intrinsics.X86;
namespace SimdBase64
{
public static class Base64
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int MaximalBinaryLengthFromBase64<T>(ReadOnlySpan<T> input)
{
return Scalar.Base64.MaximalBinaryLengthFromBase64Scalar(input);
}
public static byte[] FromBase64String(string s)
{
ReadOnlySpan<char> base64 = s.AsSpan();
byte[] newBytes = new byte[SimdBase64.Base64.MaximalBinaryLengthFromBase64<char>(base64)];
int bytesConsumed = 0;
int bytesWritten = 0;
SimdBase64.Base64.DecodeFromBase64(base64, newBytes, out bytesConsumed, out bytesWritten, false);
Array.Resize(ref newBytes, bytesWritten);
return newBytes;
}
public unsafe static OperationStatus DecodeFromBase64(ReadOnlySpan<byte> source, Span<byte> dest, out int bytesConsumed, out int bytesWritten, bool isUrl = false)
{
if (AdvSimd.Arm64.IsSupported && BitConverter.IsLittleEndian)
{
return Arm.Base64.DecodeFromBase64ARM(source, dest, out bytesConsumed, out bytesWritten, isUrl);
}
// To be comleted, this may have to wait for .NET 10.
//if (Vector512.IsHardwareAccelerated && Avx512Vbmi2.IsSupported)
//{
//}
if (Avx2.IsSupported && Popcnt.IsSupported && Bmi1.IsSupported)
{
return AVX2.Base64.DecodeFromBase64AVX2(source, dest, out bytesConsumed, out bytesWritten, isUrl);
}
if (Ssse3.IsSupported && Popcnt.IsSupported)
{
return SSE.Base64.DecodeFromBase64SSE(source, dest, out bytesConsumed, out bytesWritten, isUrl);
}
return Scalar.Base64.DecodeFromBase64Scalar(source, dest, out bytesConsumed, out bytesWritten, isUrl);
}
public unsafe static OperationStatus DecodeFromBase64(ReadOnlySpan<char> source, Span<byte> dest, out int bytesConsumed, out int bytesWritten, bool isUrl = false)
{
if (AdvSimd.Arm64.IsSupported && BitConverter.IsLittleEndian)
{
return Arm.Base64.DecodeFromBase64ARM(source, dest, out bytesConsumed, out bytesWritten, isUrl);
}
// To be comleted, this may have to wait for .NET 10.
//if (Vector512.IsHardwareAccelerated && Avx512Vbmi.IsSupported)
//{
// return GetPointerToFirstInvalidByteAvx512(pInputBuffer, inputLength, out Utf16CodeUnitCountAdjustment, out ScalarCodeUnitCountAdjustment);
//}
if (Avx2.IsSupported && Popcnt.IsSupported && Bmi1.IsSupported)
{
return AVX2.Base64.DecodeFromBase64AVX2(source, dest, out bytesConsumed, out bytesWritten, isUrl);
}
if (Ssse3.IsSupported && Popcnt.IsSupported)
{
return SSE.Base64.DecodeFromBase64SSE(source, dest, out bytesConsumed, out bytesWritten, isUrl);
}
return Scalar.Base64.DecodeFromBase64Scalar(source, dest, out bytesConsumed, out bytesWritten, isUrl);
}
}
}