Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Base64.cs
Go to the documentation of this file.
6
8
9public static class Base64
10{
11 private static ReadOnlySpan<sbyte> DecodingMap => new sbyte[256]
12 {
13 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
14 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
15 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
16 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
17 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53,
18 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
19 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4,
20 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
21 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
22 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
23 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
24 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
25 49, 50, 51, -1, -1, -1, -1, -1, -1, -1,
26 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
27 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
28 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
29 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
30 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
31 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
32 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
33 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
34 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
35 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38 -1, -1, -1, -1, -1, -1
39 };
40
41 private static ReadOnlySpan<byte> EncodingMap => "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"u8;
42
43 public unsafe static OperationStatus DecodeFromUtf8(ReadOnlySpan<byte> utf8, Span<byte> bytes, out int bytesConsumed, out int bytesWritten, bool isFinalBlock = true)
44 {
45 if (utf8.IsEmpty)
46 {
47 bytesConsumed = 0;
48 bytesWritten = 0;
49 return OperationStatus.Done;
50 }
51 fixed (byte* ptr = &MemoryMarshal.GetReference(utf8))
52 {
53 fixed (byte* ptr2 = &MemoryMarshal.GetReference(bytes))
54 {
55 int num = utf8.Length & -4;
56 int length = bytes.Length;
57 int num2 = num;
58 int maxDecodedFromUtf8Length = GetMaxDecodedFromUtf8Length(num);
59 if (length < maxDecodedFromUtf8Length - 2)
60 {
61 num2 = length / 3 * 4;
62 }
63 byte* srcBytes = ptr;
64 byte* destBytes = ptr2;
65 byte* ptr3 = ptr + (uint)num;
66 byte* ptr4 = ptr + (uint)num2;
67 if (num2 >= 24)
68 {
69 byte* ptr5 = ptr4 - 45;
70 if (Avx2.IsSupported && ptr5 >= srcBytes)
71 {
72 Avx2Decode(ref srcBytes, ref destBytes, ptr5, num2, length, ptr, ptr2);
73 if (srcBytes == ptr3)
74 {
75 goto IL_029d;
76 }
77 }
78 ptr5 = ptr4 - 24;
79 if (Ssse3.IsSupported && ptr5 >= srcBytes)
80 {
81 Ssse3Decode(ref srcBytes, ref destBytes, ptr5, num2, length, ptr, ptr2);
82 if (srcBytes == ptr3)
83 {
84 goto IL_029d;
85 }
86 }
87 }
88 int num3 = (isFinalBlock ? 4 : 0);
89 num2 = ((length < maxDecodedFromUtf8Length) ? (length / 3 * 4) : (num - num3));
90 ref sbyte reference = ref MemoryMarshal.GetReference(DecodingMap);
91 ptr4 = ptr + (uint)num2;
92 while (true)
93 {
94 if (srcBytes < ptr4)
95 {
96 int num4 = Decode(srcBytes, ref reference);
97 if (num4 >= 0)
98 {
99 WriteThreeLowOrderBytes(destBytes, num4);
100 srcBytes += 4;
101 destBytes += 3;
102 continue;
103 }
104 }
105 else
106 {
107 if (num2 != num - num3)
108 {
109 goto IL_02b3;
110 }
111 if (srcBytes == ptr3)
112 {
113 if (!isFinalBlock)
114 {
115 if (srcBytes == ptr + utf8.Length)
116 {
117 break;
118 }
119 bytesConsumed = (int)(srcBytes - ptr);
120 bytesWritten = (int)(destBytes - ptr2);
121 return OperationStatus.NeedMoreData;
122 }
123 }
124 else
125 {
126 uint num5 = ptr3[-4];
127 uint num6 = ptr3[-3];
128 uint num7 = ptr3[-2];
129 uint num8 = ptr3[-1];
130 int num9 = Internal.Runtime.CompilerServices.Unsafe.Add(ref reference, (IntPtr)num5);
131 int num10 = Internal.Runtime.CompilerServices.Unsafe.Add(ref reference, (IntPtr)num6);
132 num9 <<= 18;
133 num10 <<= 12;
134 num9 |= num10;
135 byte* ptr6 = ptr2 + (uint)length;
136 if (num8 != 61)
137 {
138 int num11 = Internal.Runtime.CompilerServices.Unsafe.Add(ref reference, (IntPtr)num7);
139 int num12 = Internal.Runtime.CompilerServices.Unsafe.Add(ref reference, (IntPtr)num8);
140 num11 <<= 6;
141 num9 |= num12;
142 num9 |= num11;
143 if (num9 >= 0)
144 {
145 if (destBytes + 3 <= ptr6)
146 {
147 WriteThreeLowOrderBytes(destBytes, num9);
148 destBytes += 3;
149 goto IL_028c;
150 }
151 goto IL_02b3;
152 }
153 }
154 else if (num7 != 61)
155 {
156 int num13 = Internal.Runtime.CompilerServices.Unsafe.Add(ref reference, (IntPtr)num7);
157 num13 <<= 6;
158 num9 |= num13;
159 if (num9 >= 0)
160 {
161 if (destBytes + 2 <= ptr6)
162 {
163 *destBytes = (byte)(num9 >> 16);
164 destBytes[1] = (byte)(num9 >> 8);
165 destBytes += 2;
166 goto IL_028c;
167 }
168 goto IL_02b3;
169 }
170 }
171 else if (num9 >= 0)
172 {
173 if (destBytes + 1 <= ptr6)
174 {
175 *destBytes = (byte)(num9 >> 16);
176 destBytes++;
177 goto IL_028c;
178 }
179 goto IL_02b3;
180 }
181 }
182 }
183 goto IL_02f2;
184 IL_028c:
185 srcBytes += 4;
186 if (num == utf8.Length)
187 {
188 break;
189 }
190 goto IL_02f2;
191 IL_02b3:
192 if (!(num != utf8.Length && isFinalBlock))
193 {
194 bytesConsumed = (int)(srcBytes - ptr);
195 bytesWritten = (int)(destBytes - ptr2);
196 return OperationStatus.DestinationTooSmall;
197 }
198 goto IL_02f2;
199 IL_02f2:
200 bytesConsumed = (int)(srcBytes - ptr);
201 bytesWritten = (int)(destBytes - ptr2);
202 return OperationStatus.InvalidData;
203 }
204 goto IL_029d;
205 IL_029d:
206 bytesConsumed = (int)(srcBytes - ptr);
207 bytesWritten = (int)(destBytes - ptr2);
208 return OperationStatus.Done;
209 }
210 }
211 }
212
213 [MethodImpl(MethodImplOptions.AggressiveInlining)]
214 public static int GetMaxDecodedFromUtf8Length(int length)
215 {
216 if (length < 0)
217 {
219 }
220 return (length >> 2) * 3;
221 }
222
223 public unsafe static OperationStatus DecodeFromUtf8InPlace(Span<byte> buffer, out int bytesWritten)
224 {
225 if (buffer.IsEmpty)
226 {
227 bytesWritten = 0;
228 return OperationStatus.Done;
229 }
230 fixed (byte* ptr = &MemoryMarshal.GetReference(buffer))
231 {
232 int length = buffer.Length;
233 uint num = 0u;
234 uint num2 = 0u;
235 if (length == (length >> 2) * 4)
236 {
237 if (length == 0)
238 {
239 goto IL_0189;
240 }
241 ref sbyte reference = ref MemoryMarshal.GetReference(DecodingMap);
242 while (num < length - 4)
243 {
244 int num3 = Decode(ptr + num, ref reference);
245 if (num3 >= 0)
246 {
247 WriteThreeLowOrderBytes(ptr + num2, num3);
248 num2 += 3;
249 num += 4;
250 continue;
251 }
252 goto IL_018f;
253 }
254 uint num4 = ptr[length - 4];
255 uint num5 = ptr[length - 3];
256 uint num6 = ptr[length - 2];
257 uint num7 = ptr[length - 1];
258 int num8 = Internal.Runtime.CompilerServices.Unsafe.Add(ref reference, (IntPtr)num4);
259 int num9 = Internal.Runtime.CompilerServices.Unsafe.Add(ref reference, (IntPtr)num5);
260 num8 <<= 18;
261 num9 <<= 12;
262 num8 |= num9;
263 if (num7 != 61)
264 {
265 int num10 = Internal.Runtime.CompilerServices.Unsafe.Add(ref reference, (IntPtr)num6);
266 int num11 = Internal.Runtime.CompilerServices.Unsafe.Add(ref reference, (IntPtr)num7);
267 num10 <<= 6;
268 num8 |= num11;
269 num8 |= num10;
270 if (num8 >= 0)
271 {
272 WriteThreeLowOrderBytes(ptr + num2, num8);
273 num2 += 3;
274 goto IL_0189;
275 }
276 }
277 else if (num6 != 61)
278 {
279 int num12 = Internal.Runtime.CompilerServices.Unsafe.Add(ref reference, (IntPtr)num6);
280 num12 <<= 6;
281 num8 |= num12;
282 if (num8 >= 0)
283 {
284 ptr[num2] = (byte)(num8 >> 16);
285 ptr[num2 + 1] = (byte)(num8 >> 8);
286 num2 += 2;
287 goto IL_0189;
288 }
289 }
290 else if (num8 >= 0)
291 {
292 ptr[num2] = (byte)(num8 >> 16);
293 num2++;
294 goto IL_0189;
295 }
296 }
297 goto IL_018f;
298 IL_018f:
299 bytesWritten = (int)num2;
300 return OperationStatus.InvalidData;
301 IL_0189:
302 bytesWritten = (int)num2;
303 return OperationStatus.Done;
304 }
305 }
306
307 [MethodImpl(MethodImplOptions.AggressiveInlining)]
308 private unsafe static void Avx2Decode(ref byte* srcBytes, ref byte* destBytes, byte* srcEnd, int sourceLength, int destLength, byte* srcStart, byte* destStart)
309 {
310 Vector256<sbyte> value = Vector256.Create(16, 16, 1, 2, 4, 8, 4, 8, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 1, 2, 4, 8, 4, 8, 16, 16, 16, 16, 16, 16, 16, 16);
311 Vector256<sbyte> value2 = Vector256.Create(21, 17, 17, 17, 17, 17, 17, 17, 17, 17, 19, 26, 27, 27, 27, 26, 21, 17, 17, 17, 17, 17, 17, 17, 17, 17, 19, 26, 27, 27, 27, 26);
312 Vector256<sbyte> value3 = Vector256.Create(0, 16, 19, 4, -65, -65, -71, -71, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 19, 4, -65, -65, -71, -71, 0, 0, 0, 0, 0, 0, 0, 0);
313 Vector256<sbyte> mask = Vector256.Create(2, 1, 0, 6, 5, 4, 10, 9, 8, 14, 13, 12, -1, -1, -1, -1, 2, 1, 0, 6, 5, 4, 10, 9, 8, 14, 13, 12, -1, -1, -1, -1);
314 Vector256<int> control = Vector256.Create(0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 4, 0, 0, 0, 5, 0, 0, 0, 6, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1).AsInt32();
315 Vector256<sbyte> right = Vector256.Create((sbyte)47);
316 Vector256<sbyte> right2 = Vector256.Create(20971840).AsSByte();
317 Vector256<short> right3 = Vector256.Create(69632).AsInt16();
318 byte* ptr = srcBytes;
319 byte* ptr2 = destBytes;
320 do
321 {
322 Vector256<sbyte> vector = Avx.LoadVector256(ptr).AsSByte();
323 Vector256<sbyte> vector2 = Avx2.And(Avx2.ShiftRightLogical(vector.AsInt32(), 4).AsSByte(), right);
324 Vector256<sbyte> mask2 = Avx2.And(vector, right);
325 Vector256<sbyte> right4 = Avx2.Shuffle(value, vector2);
326 Vector256<sbyte> left = Avx2.Shuffle(value2, mask2);
327 if (!Avx.TestZ(left, right4))
328 {
329 break;
330 }
331 Vector256<sbyte> left2 = Avx2.CompareEqual(vector, right);
332 Vector256<sbyte> right5 = Avx2.Shuffle(value3, Avx2.Add(left2, vector2));
333 vector = Avx2.Add(vector, right5);
334 Vector256<short> left3 = Avx2.MultiplyAddAdjacent(vector.AsByte(), right2);
335 Vector256<int> vector3 = Avx2.MultiplyAddAdjacent(left3, right3);
336 vector3 = Avx2.Shuffle(vector3.AsSByte(), mask).AsInt32();
337 vector = Avx2.PermuteVar8x32(vector3, control).AsSByte();
338 Avx.Store(ptr2, vector.AsByte());
339 ptr += 32;
340 ptr2 += 24;
341 }
342 while (ptr <= srcEnd);
343 srcBytes = ptr;
344 destBytes = ptr2;
345 }
346
347 [MethodImpl(MethodImplOptions.AggressiveInlining)]
348 private unsafe static void Ssse3Decode(ref byte* srcBytes, ref byte* destBytes, byte* srcEnd, int sourceLength, int destLength, byte* srcStart, byte* destStart)
349 {
350 Vector128<sbyte> value = Vector128.Create(16, 16, 1, 2, 4, 8, 4, 8, 16, 16, 16, 16, 16, 16, 16, 16);
351 Vector128<sbyte> value2 = Vector128.Create(21, 17, 17, 17, 17, 17, 17, 17, 17, 17, 19, 26, 27, 27, 27, 26);
352 Vector128<sbyte> value3 = Vector128.Create(0, 16, 19, 4, -65, -65, -71, -71, 0, 0, 0, 0, 0, 0, 0, 0);
353 Vector128<sbyte> mask = Vector128.Create(2, 1, 0, 6, 5, 4, 10, 9, 8, 14, 13, 12, -1, -1, -1, -1);
354 Vector128<sbyte> right = Vector128.Create((sbyte)47);
355 Vector128<sbyte> right2 = Vector128.Create(20971840).AsSByte();
356 Vector128<short> right3 = Vector128.Create(69632).AsInt16();
358 byte* ptr = srcBytes;
359 byte* ptr2 = destBytes;
360 do
361 {
362 Vector128<sbyte> vector = Sse2.LoadVector128(ptr).AsSByte();
363 Vector128<sbyte> vector2 = Sse2.And(Sse2.ShiftRightLogical(vector.AsInt32(), 4).AsSByte(), right);
364 Vector128<sbyte> mask2 = Sse2.And(vector, right);
365 Vector128<sbyte> right4 = Ssse3.Shuffle(value, vector2);
366 Vector128<sbyte> left = Ssse3.Shuffle(value2, mask2);
367 if (Sse2.MoveMask(Sse2.CompareGreaterThan(Sse2.And(left, right4), zero)) != 0)
368 {
369 break;
370 }
371 Vector128<sbyte> left2 = Sse2.CompareEqual(vector, right);
372 Vector128<sbyte> right5 = Ssse3.Shuffle(value3, Sse2.Add(left2, vector2));
373 vector = Sse2.Add(vector, right5);
374 Vector128<short> left3 = Ssse3.MultiplyAddAdjacent(vector.AsByte(), right2);
375 Vector128<int> vector3 = Sse2.MultiplyAddAdjacent(left3, right3);
376 vector = Ssse3.Shuffle(vector3.AsSByte(), mask);
377 Sse2.Store(ptr2, vector.AsByte());
378 ptr += 16;
379 ptr2 += 12;
380 }
381 while (ptr <= srcEnd);
382 srcBytes = ptr;
383 destBytes = ptr2;
384 }
385
386 [MethodImpl(MethodImplOptions.AggressiveInlining)]
387 private unsafe static int Decode(byte* encodedBytes, ref sbyte decodingMap)
388 {
389 uint num = *encodedBytes;
390 uint num2 = encodedBytes[1];
391 uint num3 = encodedBytes[2];
392 uint num4 = encodedBytes[3];
393 int num5 = Internal.Runtime.CompilerServices.Unsafe.Add(ref decodingMap, (IntPtr)num);
394 int num6 = Internal.Runtime.CompilerServices.Unsafe.Add(ref decodingMap, (IntPtr)num2);
395 int num7 = Internal.Runtime.CompilerServices.Unsafe.Add(ref decodingMap, (IntPtr)num3);
396 int num8 = Internal.Runtime.CompilerServices.Unsafe.Add(ref decodingMap, (IntPtr)num4);
397 num5 <<= 18;
398 num6 <<= 12;
399 num7 <<= 6;
400 num5 |= num8;
401 num6 |= num7;
402 return num5 | num6;
403 }
404
405 [MethodImpl(MethodImplOptions.AggressiveInlining)]
406 private unsafe static void WriteThreeLowOrderBytes(byte* destination, int value)
407 {
408 *destination = (byte)(value >> 16);
409 destination[1] = (byte)(value >> 8);
410 destination[2] = (byte)value;
411 }
412
413 public unsafe static OperationStatus EncodeToUtf8(ReadOnlySpan<byte> bytes, Span<byte> utf8, out int bytesConsumed, out int bytesWritten, bool isFinalBlock = true)
414 {
415 if (bytes.IsEmpty)
416 {
417 bytesConsumed = 0;
418 bytesWritten = 0;
419 return OperationStatus.Done;
420 }
421 fixed (byte* ptr = &MemoryMarshal.GetReference(bytes))
422 {
423 fixed (byte* ptr2 = &MemoryMarshal.GetReference(utf8))
424 {
425 int length = bytes.Length;
426 int length2 = utf8.Length;
427 int num = ((length > 1610612733 || length2 < GetMaxEncodedToUtf8Length(length)) ? ((length2 >> 2) * 3) : length);
428 byte* srcBytes = ptr;
429 byte* destBytes = ptr2;
430 byte* ptr3 = ptr + (uint)length;
431 byte* ptr4 = ptr + (uint)num;
432 if (num >= 16)
433 {
434 byte* ptr5 = ptr4 - 32;
435 if (Avx2.IsSupported && ptr5 >= srcBytes)
436 {
437 Avx2Encode(ref srcBytes, ref destBytes, ptr5, num, length2, ptr, ptr2);
438 if (srcBytes == ptr3)
439 {
440 goto IL_0175;
441 }
442 }
443 ptr5 = ptr4 - 16;
444 if (Ssse3.IsSupported && ptr5 >= srcBytes)
445 {
446 Ssse3Encode(ref srcBytes, ref destBytes, ptr5, num, length2, ptr, ptr2);
447 if (srcBytes == ptr3)
448 {
449 goto IL_0175;
450 }
451 }
452 }
453 ref byte reference = ref MemoryMarshal.GetReference(EncodingMap);
454 uint num2 = 0u;
455 ptr4 -= 2;
456 while (srcBytes < ptr4)
457 {
458 num2 = Encode(srcBytes, ref reference);
459 Internal.Runtime.CompilerServices.Unsafe.WriteUnaligned(destBytes, num2);
460 srcBytes += 3;
461 destBytes += 4;
462 }
463 if (ptr4 + 2 == ptr3)
464 {
465 if (!isFinalBlock)
466 {
467 if (srcBytes != ptr3)
468 {
469 bytesConsumed = (int)(srcBytes - ptr);
470 bytesWritten = (int)(destBytes - ptr2);
471 return OperationStatus.NeedMoreData;
472 }
473 }
474 else if (srcBytes + 1 == ptr3)
475 {
476 num2 = EncodeAndPadTwo(srcBytes, ref reference);
477 Internal.Runtime.CompilerServices.Unsafe.WriteUnaligned(destBytes, num2);
478 srcBytes++;
479 destBytes += 4;
480 }
481 else if (srcBytes + 2 == ptr3)
482 {
483 num2 = EncodeAndPadOne(srcBytes, ref reference);
484 Internal.Runtime.CompilerServices.Unsafe.WriteUnaligned(destBytes, num2);
485 srcBytes += 2;
486 destBytes += 4;
487 }
488 goto IL_0175;
489 }
490 bytesConsumed = (int)(srcBytes - ptr);
491 bytesWritten = (int)(destBytes - ptr2);
492 return OperationStatus.DestinationTooSmall;
493 IL_0175:
494 bytesConsumed = (int)(srcBytes - ptr);
495 bytesWritten = (int)(destBytes - ptr2);
496 return OperationStatus.Done;
497 }
498 }
499 }
500
501 [MethodImpl(MethodImplOptions.AggressiveInlining)]
502 public static int GetMaxEncodedToUtf8Length(int length)
503 {
504 if ((uint)length > 1610612733u)
505 {
507 }
508 return (length + 2) / 3 * 4;
509 }
510
511 public unsafe static OperationStatus EncodeToUtf8InPlace(Span<byte> buffer, int dataLength, out int bytesWritten)
512 {
513 if (buffer.IsEmpty)
514 {
515 bytesWritten = 0;
516 return OperationStatus.Done;
517 }
518 fixed (byte* ptr = &MemoryMarshal.GetReference(buffer))
519 {
520 int maxEncodedToUtf8Length = GetMaxEncodedToUtf8Length(dataLength);
521 if (buffer.Length >= maxEncodedToUtf8Length)
522 {
523 int num = dataLength - dataLength / 3 * 3;
524 uint num2 = (uint)(maxEncodedToUtf8Length - 4);
525 uint num3 = (uint)(dataLength - num);
526 uint num4 = 0u;
527 ref byte reference = ref MemoryMarshal.GetReference(EncodingMap);
528 if (num != 0)
529 {
530 num4 = ((num != 1) ? EncodeAndPadOne(ptr + num3, ref reference) : EncodeAndPadTwo(ptr + num3, ref reference));
531 Internal.Runtime.CompilerServices.Unsafe.WriteUnaligned(ptr + num2, num4);
532 num2 -= 4;
533 }
534 num3 -= 3;
535 while ((int)num3 >= 0)
536 {
537 num4 = Encode(ptr + num3, ref reference);
538 Internal.Runtime.CompilerServices.Unsafe.WriteUnaligned(ptr + num2, num4);
539 num2 -= 4;
540 num3 -= 3;
541 }
542 bytesWritten = maxEncodedToUtf8Length;
543 return OperationStatus.Done;
544 }
545 bytesWritten = 0;
546 return OperationStatus.DestinationTooSmall;
547 }
548 }
549
550 [MethodImpl(MethodImplOptions.AggressiveInlining)]
551 private unsafe static void Avx2Encode(ref byte* srcBytes, ref byte* destBytes, byte* srcEnd, int sourceLength, int destLength, byte* srcStart, byte* destStart)
552 {
553 Vector256<sbyte> mask = Vector256.Create(5, 4, 6, 5, 8, 7, 9, 8, 11, 10, 12, 11, 14, 13, 15, 14, 1, 0, 2, 1, 4, 3, 5, 4, 7, 6, 8, 7, 10, 9, 11, 10);
554 Vector256<sbyte> value = Vector256.Create(65, 71, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -19, -16, 0, 0, 65, 71, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -19, -16, 0, 0);
555 Vector256<sbyte> right = Vector256.Create(264305664).AsSByte();
556 Vector256<sbyte> right2 = Vector256.Create(4129776).AsSByte();
557 Vector256<ushort> right3 = Vector256.Create(67108928).AsUInt16();
558 Vector256<short> right4 = Vector256.Create(16777232).AsInt16();
559 Vector256<byte> right5 = Vector256.Create((byte)51);
560 Vector256<sbyte> right6 = Vector256.Create((sbyte)25);
561 byte* ptr = srcBytes;
562 byte* ptr2 = destBytes;
563 Vector256<sbyte> vector = Avx.LoadVector256(ptr).AsSByte();
564 vector = Avx2.PermuteVar8x32(vector.AsInt32(), Vector256.Create(0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5, 0, 0, 0, 6, 0, 0, 0).AsInt32()).AsSByte();
565 ptr -= 4;
566 while (true)
567 {
568 vector = Avx2.Shuffle(vector, mask);
569 Vector256<sbyte> vector2 = Avx2.And(vector, right);
570 Vector256<sbyte> vector3 = Avx2.And(vector, right2);
571 Vector256<ushort> vector4 = Avx2.MultiplyHigh(vector2.AsUInt16(), right3);
572 Vector256<short> vector5 = Avx2.MultiplyLow(vector3.AsInt16(), right4);
573 vector = Avx2.Or(vector4.AsSByte(), vector5.AsSByte());
574 Vector256<byte> vector6 = Avx2.SubtractSaturate(vector.AsByte(), right5);
575 Vector256<sbyte> right7 = Avx2.CompareGreaterThan(vector, right6);
576 Vector256<sbyte> mask2 = Avx2.Subtract(vector6.AsSByte(), right7);
577 vector = Avx2.Add(vector, Avx2.Shuffle(value, mask2));
578 Avx.Store(ptr2, vector.AsByte());
579 ptr += 24;
580 ptr2 += 32;
581 if (ptr > srcEnd)
582 {
583 break;
584 }
585 vector = Avx.LoadVector256(ptr).AsSByte();
586 }
587 srcBytes = ptr + 4;
588 destBytes = ptr2;
589 }
590
591 [MethodImpl(MethodImplOptions.AggressiveInlining)]
592 private unsafe static void Ssse3Encode(ref byte* srcBytes, ref byte* destBytes, byte* srcEnd, int sourceLength, int destLength, byte* srcStart, byte* destStart)
593 {
594 Vector128<sbyte> mask = Vector128.Create(1, 0, 2, 1, 4, 3, 5, 4, 7, 6, 8, 7, 10, 9, 11, 10);
595 Vector128<sbyte> value = Vector128.Create(65, 71, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -19, -16, 0, 0);
596 Vector128<sbyte> right = Vector128.Create(264305664).AsSByte();
597 Vector128<sbyte> right2 = Vector128.Create(4129776).AsSByte();
598 Vector128<ushort> right3 = Vector128.Create(67108928).AsUInt16();
599 Vector128<short> right4 = Vector128.Create(16777232).AsInt16();
600 Vector128<byte> right5 = Vector128.Create((byte)51);
601 Vector128<sbyte> right6 = Vector128.Create((sbyte)25);
602 byte* ptr = srcBytes;
603 byte* ptr2 = destBytes;
604 do
605 {
606 Vector128<sbyte> value2 = Sse2.LoadVector128(ptr).AsSByte();
607 value2 = Ssse3.Shuffle(value2, mask);
608 Vector128<sbyte> vector = Sse2.And(value2, right);
609 Vector128<sbyte> vector2 = Sse2.And(value2, right2);
610 Vector128<ushort> vector3 = Sse2.MultiplyHigh(vector.AsUInt16(), right3);
611 Vector128<short> vector4 = Sse2.MultiplyLow(vector2.AsInt16(), right4);
612 value2 = Sse2.Or(vector3.AsSByte(), vector4.AsSByte());
613 Vector128<byte> vector5 = Sse2.SubtractSaturate(value2.AsByte(), right5);
614 Vector128<sbyte> right7 = Sse2.CompareGreaterThan(value2, right6);
615 Vector128<sbyte> mask2 = Sse2.Subtract(vector5.AsSByte(), right7);
616 value2 = Sse2.Add(value2, Ssse3.Shuffle(value, mask2));
617 Sse2.Store(ptr2, value2.AsByte());
618 ptr += 12;
619 ptr2 += 16;
620 }
621 while (ptr <= srcEnd);
622 srcBytes = ptr;
623 destBytes = ptr2;
624 }
625
626 [MethodImpl(MethodImplOptions.AggressiveInlining)]
627 private unsafe static uint Encode(byte* threeBytes, ref byte encodingMap)
628 {
629 uint num = *threeBytes;
630 uint num2 = threeBytes[1];
631 uint num3 = threeBytes[2];
632 uint num4 = (num << 16) | (num2 << 8) | num3;
633 uint num5 = Internal.Runtime.CompilerServices.Unsafe.Add(ref encodingMap, (IntPtr)(num4 >> 18));
634 uint num6 = Internal.Runtime.CompilerServices.Unsafe.Add(ref encodingMap, (IntPtr)((num4 >> 12) & 0x3F));
635 uint num7 = Internal.Runtime.CompilerServices.Unsafe.Add(ref encodingMap, (IntPtr)((num4 >> 6) & 0x3F));
636 uint num8 = Internal.Runtime.CompilerServices.Unsafe.Add(ref encodingMap, (IntPtr)(num4 & 0x3F));
638 {
639 return num5 | (num6 << 8) | (num7 << 16) | (num8 << 24);
640 }
641 return (num5 << 24) | (num6 << 16) | (num7 << 8) | num8;
642 }
643
644 [MethodImpl(MethodImplOptions.AggressiveInlining)]
645 private unsafe static uint EncodeAndPadOne(byte* twoBytes, ref byte encodingMap)
646 {
647 uint num = *twoBytes;
648 uint num2 = twoBytes[1];
649 uint num3 = (num << 16) | (num2 << 8);
650 uint num4 = Internal.Runtime.CompilerServices.Unsafe.Add(ref encodingMap, (IntPtr)(num3 >> 18));
651 uint num5 = Internal.Runtime.CompilerServices.Unsafe.Add(ref encodingMap, (IntPtr)((num3 >> 12) & 0x3F));
652 uint num6 = Internal.Runtime.CompilerServices.Unsafe.Add(ref encodingMap, (IntPtr)((num3 >> 6) & 0x3F));
654 {
655 return num4 | (num5 << 8) | (num6 << 16) | 0x3D000000u;
656 }
657 return (num4 << 24) | (num5 << 16) | (num6 << 8) | 0x3Du;
658 }
659
660 [MethodImpl(MethodImplOptions.AggressiveInlining)]
661 private unsafe static uint EncodeAndPadTwo(byte* oneByte, ref byte encodingMap)
662 {
663 uint num = *oneByte;
664 uint num2 = num << 8;
665 uint num3 = Internal.Runtime.CompilerServices.Unsafe.Add(ref encodingMap, (IntPtr)(num2 >> 10));
666 uint num4 = Internal.Runtime.CompilerServices.Unsafe.Add(ref encodingMap, (IntPtr)((num2 >> 4) & 0x3F));
668 {
669 return num3 | (num4 << 8) | 0x3D0000u | 0x3D000000u;
670 }
671 return (num3 << 24) | (num4 << 16) | 0x3D00u | 0x3Du;
672 }
673}
static readonly bool IsLittleEndian
static unsafe uint Encode(byte *threeBytes, ref byte encodingMap)
Definition Base64.cs:627
static unsafe void Avx2Encode(ref byte *srcBytes, ref byte *destBytes, byte *srcEnd, int sourceLength, int destLength, byte *srcStart, byte *destStart)
Definition Base64.cs:551
static unsafe int Decode(byte *encodedBytes, ref sbyte decodingMap)
Definition Base64.cs:387
static int GetMaxDecodedFromUtf8Length(int length)
Definition Base64.cs:214
static unsafe uint EncodeAndPadOne(byte *twoBytes, ref byte encodingMap)
Definition Base64.cs:645
static ReadOnlySpan< sbyte > DecodingMap
Definition Base64.cs:11
static unsafe OperationStatus DecodeFromUtf8InPlace(Span< byte > buffer, out int bytesWritten)
Definition Base64.cs:223
static unsafe void Ssse3Encode(ref byte *srcBytes, ref byte *destBytes, byte *srcEnd, int sourceLength, int destLength, byte *srcStart, byte *destStart)
Definition Base64.cs:592
static unsafe OperationStatus EncodeToUtf8(ReadOnlySpan< byte > bytes, Span< byte > utf8, out int bytesConsumed, out int bytesWritten, bool isFinalBlock=true)
Definition Base64.cs:413
static unsafe void Ssse3Decode(ref byte *srcBytes, ref byte *destBytes, byte *srcEnd, int sourceLength, int destLength, byte *srcStart, byte *destStart)
Definition Base64.cs:348
static unsafe void WriteThreeLowOrderBytes(byte *destination, int value)
Definition Base64.cs:406
static int GetMaxEncodedToUtf8Length(int length)
Definition Base64.cs:502
static ReadOnlySpan< byte > EncodingMap
Definition Base64.cs:41
static unsafe uint EncodeAndPadTwo(byte *oneByte, ref byte encodingMap)
Definition Base64.cs:661
static unsafe void Avx2Decode(ref byte *srcBytes, ref byte *destBytes, byte *srcEnd, int sourceLength, int destLength, byte *srcStart, byte *destStart)
Definition Base64.cs:308
static unsafe OperationStatus DecodeFromUtf8(ReadOnlySpan< byte > utf8, Span< byte > bytes, out int bytesConsumed, out int bytesWritten, bool isFinalBlock=true)
Definition Base64.cs:43
static unsafe OperationStatus EncodeToUtf8InPlace(Span< byte > buffer, int dataLength, out int bytesWritten)
Definition Base64.cs:511
static Vector128< byte > Create(byte value)
Definition Vector128.cs:138
static Vector256< byte > Create(byte value)
Definition Vector256.cs:105
static Vector256< sbyte > CompareGreaterThan(Vector256< sbyte > left, Vector256< sbyte > right)
Definition Avx2.cs:552
static Vector256< sbyte > SubtractSaturate(Vector256< sbyte > left, Vector256< sbyte > right)
Definition Avx2.cs:2173
static Vector256< sbyte > Add(Vector256< sbyte > left, Vector256< sbyte > right)
Definition Avx2.cs:32
static Vector256< sbyte > And(Vector256< sbyte > left, Vector256< sbyte > right)
Definition Avx2.cs:132
static Vector256< sbyte > CompareEqual(Vector256< sbyte > left, Vector256< sbyte > right)
Definition Avx2.cs:512
static new bool IsSupported
Definition Avx2.cs:15
static Vector256< sbyte > Subtract(Vector256< sbyte > left, Vector256< sbyte > right)
Definition Avx2.cs:2133
static Vector256< int > MultiplyAddAdjacent(Vector256< short > left, Vector256< short > right)
Definition Avx2.cs:1508
static Vector256< short > MultiplyLow(Vector256< short > left, Vector256< short > right)
Definition Avx2.cs:1618
static Vector256< short > ShiftRightLogical(Vector256< short > value, Vector128< short > count)
Definition Avx2.cs:1938
static Vector256< sbyte > Or(Vector256< sbyte > left, Vector256< sbyte > right)
Definition Avx2.cs:1638
static Vector256< short > MultiplyHigh(Vector256< short > left, Vector256< short > right)
Definition Avx2.cs:1603
static Vector256< sbyte > Shuffle(Vector256< sbyte > value, Vector256< sbyte > mask)
Definition Avx2.cs:2078
static Vector256< int > PermuteVar8x32(Vector256< int > left, Vector256< int > control)
Definition Avx2.cs:1753
static unsafe Vector256< sbyte > LoadVector256(sbyte *address)
Definition Avx.cs:462
static unsafe void Store(sbyte *address, Vector256< sbyte > source)
Definition Avx.cs:962
static bool TestZ(Vector128< float > left, Vector128< float > right)
Definition Avx.cs:1142
static unsafe Vector128< sbyte > LoadVector128(sbyte *address)
Definition Sse2.cs:582
static Vector128< sbyte > SubtractSaturate(Vector128< sbyte > left, Vector128< sbyte > right)
Definition Sse2.cs:1402
static int MoveMask(Vector128< sbyte > value)
Definition Sse2.cs:772
static Vector128< sbyte > CompareEqual(Vector128< sbyte > left, Vector128< sbyte > right)
Definition Sse2.cs:232
static unsafe void Store(sbyte *address, Vector128< sbyte > source)
Definition Sse2.cs:1287
static Vector128< int > MultiplyAddAdjacent(Vector128< short > left, Vector128< short > right)
Definition Sse2.cs:822
static Vector128< byte > Or(Vector128< byte > left, Vector128< byte > right)
Definition Sse2.cs:837
static Vector128< sbyte > CompareGreaterThan(Vector128< sbyte > left, Vector128< sbyte > right)
Definition Sse2.cs:282
static Vector128< byte > And(Vector128< byte > left, Vector128< byte > right)
Definition Sse2.cs:132
static Vector128< short > MultiplyLow(Vector128< short > left, Vector128< short > right)
Definition Sse2.cs:827
static Vector128< byte > Subtract(Vector128< byte > left, Vector128< byte > right)
Definition Sse2.cs:1352
static Vector128< byte > Add(Vector128< byte > left, Vector128< byte > right)
Definition Sse2.cs:62
static Vector128< short > MultiplyHigh(Vector128< short > left, Vector128< short > right)
Definition Sse2.cs:812
static Vector128< short > ShiftRightLogical(Vector128< short > value, Vector128< short > count)
Definition Sse2.cs:1057
static Vector128< short > MultiplyAddAdjacent(Vector128< byte > left, Vector128< sbyte > right)
Definition Ssse3.cs:102
static Vector128< sbyte > Shuffle(Vector128< sbyte > value, Vector128< sbyte > mask)
Definition Ssse3.cs:112
static void ThrowArgumentOutOfRangeException(System.ExceptionArgument argument)
int Length
Definition Span.cs:70