-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathHelpers.cs
675 lines (595 loc) · 25.3 KB
/
Helpers.cs
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
using System;
using System.Runtime.CompilerServices;
using size_t = BrotliSharpLib.Brotli.SizeT;
namespace BrotliSharpLib {
public static partial class Brotli {
// Use preprocessor directives to optimise away some of the IL to encourage
// inlining by JIT compiler
#if X64
private const bool Is64Bit = true;
private const int WordSize = 64;
#elif X86
private const bool Is64Bit = false;
private const int WordSize = 32;
#else
private static readonly bool Is64Bit = IntPtr.Size == 8;
private static readonly int WordSize = IntPtr.Size * 8;
#endif
// https://github.com/dotnet/coreclr/blob/master/src/mscorlib/src/System/Buffer.cs
private static unsafe void memcpy(void* destination, void* source, size_t length) {
// This is portable version of memcpy. It mirrors what the hand optimized assembly versions of memcpy typically do.
//
// Ideally, we would just use the cpblk IL instruction here. Unfortunately, cpblk IL instruction is not as efficient as
// possible yet and so we have this implementation here for now.
// Note: It's important that this switch handles lengths at least up to 22.
// See notes below near the main loop for why.
// The switch will be very fast since it can be implemented using a jump
// table in assembly. See http://stackoverflow.com/a/449297/4077294 for more info.
var dest = (byte*) destination;
var src = (byte*) source;
var len = (uint) length;
switch (len) {
case 0:
return;
case 1:
*dest = *src;
return;
case 2:
*(short*) dest = *(short*) src;
return;
case 3:
*(short*) dest = *(short*) src;
*(dest + 2) = *(src + 2);
return;
case 4:
*(int*) dest = *(int*) src;
return;
case 5:
*(int*) dest = *(int*) src;
*(dest + 4) = *(src + 4);
return;
case 6:
*(int*) dest = *(int*) src;
*(short*) (dest + 4) = *(short*) (src + 4);
return;
case 7:
*(int*) dest = *(int*) src;
*(short*) (dest + 4) = *(short*) (src + 4);
*(dest + 6) = *(src + 6);
return;
case 8:
if (Is64Bit)
*(long*) dest = *(long*) src;
else {
*(int*) dest = *(int*) src;
*(int*) (dest + 4) = *(int*) (src + 4);
}
return;
case 9:
if (Is64Bit)
*(long*) dest = *(long*) src;
else {
*(int*) dest = *(int*) src;
*(int*) (dest + 4) = *(int*) (src + 4);
}
*(dest + 8) = *(src + 8);
return;
case 10:
if (Is64Bit)
*(long*) dest = *(long*) src;
else {
*(int*) dest = *(int*) src;
*(int*) (dest + 4) = *(int*) (src + 4);
}
*(short*) (dest + 8) = *(short*) (src + 8);
return;
case 11:
if (Is64Bit)
*(long*) dest = *(long*) src;
else {
*(int*) dest = *(int*) src;
*(int*) (dest + 4) = *(int*) (src + 4);
}
*(short*) (dest + 8) = *(short*) (src + 8);
*(dest + 10) = *(src + 10);
return;
case 12:
if (Is64Bit)
*(long*) dest = *(long*) src;
else {
*(int*) dest = *(int*) src;
*(int*) (dest + 4) = *(int*) (src + 4);
}
*(int*) (dest + 8) = *(int*) (src + 8);
return;
case 13:
if (Is64Bit)
*(long*) dest = *(long*) src;
else {
*(int*) dest = *(int*) src;
*(int*) (dest + 4) = *(int*) (src + 4);
}
*(int*) (dest + 8) = *(int*) (src + 8);
*(dest + 12) = *(src + 12);
return;
case 14:
if (Is64Bit)
*(long*) dest = *(long*) src;
else {
*(int*) dest = *(int*) src;
*(int*) (dest + 4) = *(int*) (src + 4);
}
*(int*) (dest + 8) = *(int*) (src + 8);
*(short*) (dest + 12) = *(short*) (src + 12);
return;
case 15:
if (Is64Bit)
*(long*) dest = *(long*) src;
else {
*(int*) dest = *(int*) src;
*(int*) (dest + 4) = *(int*) (src + 4);
}
*(int*) (dest + 8) = *(int*) (src + 8);
*(short*) (dest + 12) = *(short*) (src + 12);
*(dest + 14) = *(src + 14);
return;
case 16:
if (Is64Bit) {
*(long*) dest = *(long*) src;
*(long*) (dest + 8) = *(long*) (src + 8);
}
else {
*(int*) dest = *(int*) src;
*(int*) (dest + 4) = *(int*) (src + 4);
*(int*) (dest + 8) = *(int*) (src + 8);
*(int*) (dest + 12) = *(int*) (src + 12);
}
return;
case 17:
if (Is64Bit) {
*(long*) dest = *(long*) src;
*(long*) (dest + 8) = *(long*) (src + 8);
}
else {
*(int*) dest = *(int*) src;
*(int*) (dest + 4) = *(int*) (src + 4);
*(int*) (dest + 8) = *(int*) (src + 8);
*(int*) (dest + 12) = *(int*) (src + 12);
}
*(dest + 16) = *(src + 16);
return;
case 18:
if (Is64Bit) {
*(long*) dest = *(long*) src;
*(long*) (dest + 8) = *(long*) (src + 8);
}
else {
*(int*) dest = *(int*) src;
*(int*) (dest + 4) = *(int*) (src + 4);
*(int*) (dest + 8) = *(int*) (src + 8);
*(int*) (dest + 12) = *(int*) (src + 12);
}
*(short*) (dest + 16) = *(short*) (src + 16);
return;
case 19:
if (Is64Bit) {
*(long*) dest = *(long*) src;
*(long*) (dest + 8) = *(long*) (src + 8);
}
else {
*(int*) dest = *(int*) src;
*(int*) (dest + 4) = *(int*) (src + 4);
*(int*) (dest + 8) = *(int*) (src + 8);
*(int*) (dest + 12) = *(int*) (src + 12);
}
*(short*) (dest + 16) = *(short*) (src + 16);
*(dest + 18) = *(src + 18);
return;
case 20:
if (Is64Bit) {
*(long*) dest = *(long*) src;
*(long*) (dest + 8) = *(long*) (src + 8);
}
else {
*(int*) dest = *(int*) src;
*(int*) (dest + 4) = *(int*) (src + 4);
*(int*) (dest + 8) = *(int*) (src + 8);
*(int*) (dest + 12) = *(int*) (src + 12);
}
*(int*) (dest + 16) = *(int*) (src + 16);
return;
case 21:
if (Is64Bit) {
*(long*) dest = *(long*) src;
*(long*) (dest + 8) = *(long*) (src + 8);
}
else {
*(int*) dest = *(int*) src;
*(int*) (dest + 4) = *(int*) (src + 4);
*(int*) (dest + 8) = *(int*) (src + 8);
*(int*) (dest + 12) = *(int*) (src + 12);
}
*(int*) (dest + 16) = *(int*) (src + 16);
*(dest + 20) = *(src + 20);
return;
case 22:
if (Is64Bit) {
*(long*) dest = *(long*) src;
*(long*) (dest + 8) = *(long*) (src + 8);
}
else {
*(int*) dest = *(int*) src;
*(int*) (dest + 4) = *(int*) (src + 4);
*(int*) (dest + 8) = *(int*) (src + 8);
*(int*) (dest + 12) = *(int*) (src + 12);
}
*(int*) (dest + 16) = *(int*) (src + 16);
*(short*) (dest + 20) = *(short*) (src + 20);
return;
}
size_t i = 0; // byte offset at which we're copying
if (((int) dest & 3) != 0) {
if (((int) dest & 1) != 0) {
*(dest + i) = *(src + i);
i += 1;
if (((int) dest & 2) != 0)
goto IntAligned;
}
*(short*) (dest + i) = *(short*) (src + i);
i += 2;
}
IntAligned:
if (Is64Bit) {
// On 64-bit IntPtr.Size == 8, so we want to advance to the next 8-aligned address. If
// (int)dest % 8 is 0, 5, 6, or 7, we will already have advanced by 0, 3, 2, or 1
// bytes to the next aligned address (respectively), so do nothing. On the other hand,
// if it is 1, 2, 3, or 4 we will want to copy-and-advance another 4 bytes until
// we're aligned.
// The thing 1, 2, 3, and 4 have in common that the others don't is that if you
// subtract one from them, their 3rd lsb will not be set. Hence, the below check.
if ((((int) dest - 1) & 4) == 0) {
*(int*) (dest + i) = *(int*) (src + i);
i += 4;
}
} // BIT64
var end = len - 16;
len -= i; // lower 4 bits of len represent how many bytes are left *after* the unrolled loop
// This is separated out into a different variable, so the i + 16 addition can be
// performed at the start of the pipeline and the loop condition does not have
// a dependency on the writes.
uint counter;
do {
counter = i + 16;
// This loop looks very costly since there appear to be a bunch of temporary values
// being created with the adds, but the jit (for x86 anyways) will convert each of
// these to use memory addressing operands.
// So the only cost is a bit of code size, which is made up for by the fact that
// we save on writes to dest/src.
if (Is64Bit) {
*(long*) (dest + i) = *(long*) (src + i);
*(long*) (dest + i + 8) = *(long*) (src + i + 8);
}
else {
*(int*) (dest + i) = *(int*) (src + i);
*(int*) (dest + i + 4) = *(int*) (src + i + 4);
*(int*) (dest + i + 8) = *(int*) (src + i + 8);
*(int*) (dest + i + 12) = *(int*) (src + i + 12);
}
i = counter;
// See notes above for why this wasn't used instead
// i += 16;
} while (counter <= end);
if ((len & 8) != 0) {
if (Is64Bit)
*(long*) (dest + i) = *(long*) (src + i);
else {
*(int*) (dest + i) = *(int*) (src + i);
*(int*) (dest + i + 4) = *(int*) (src + i + 4);
}
i += 8;
}
if ((len & 4) != 0) {
*(int*) (dest + i) = *(int*) (src + i);
i += 4;
}
if ((len & 2) != 0) {
*(short*) (dest + i) = *(short*) (src + i);
i += 2;
}
if ((len & 1) != 0) {
*(dest + i) = *(src + i);
// We're not using i after this, so not needed
// i += 1;
}
}
#if NET_45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
private static unsafe void memmove16(byte* dst, byte* src) {
if (Is64Bit) {
*(long*) dst = *(long*) src;
*(long*) (dst + 8) = *(long*) (src + 8);
}
else {
*(int*) dst = *(int*) src;
*(int*) (dst + 4) = *(int*) (src + 4);
*(int*) (dst + 8) = *(int*) (src + 8);
*(int*) (dst + 12) = *(int*) (src + 12);
}
}
// https://github.com/Smattr/memset
private static unsafe void* memset(void* ptr, int value, size_t num) {
size_t x = value & 0xff;
var pp = (byte*) ptr;
var xx = (byte) (value & 0xff);
int i;
for (i = 3; 1 << i < WordSize; ++i)
x |= x << (1 << i);
var bytes_per_word = 1 << (i - 3);
/* Prologue. */
while ((((uint) pp & (bytes_per_word - 1)) != 0) && (num-- != 0))
*pp++ = xx;
var tail = num & (bytes_per_word - 1);
var p = (size_t*) pp;
/* Main loop. */
num >>= i - 3;
while (num-- != 0)
*p++ = x;
/* Epilogue. */
pp = (byte*) p;
while (tail-- != 0)
*pp++ = xx;
return ptr;
}
internal unsafe struct SizeT {
private void* Value;
public SizeT(void* v) {
Value = v;
}
public static implicit operator UIntPtr(size_t s) {
return (UIntPtr) s.Value;
}
public static implicit operator size_t(UIntPtr s) {
return new size_t(s.ToPointer());
}
public static implicit operator ulong(size_t s) {
return (ulong) s.Value;
}
public static implicit operator uint(size_t s) {
return (uint) s.Value;
}
public static explicit operator ushort(size_t s) {
return (ushort) s.Value;
}
public static explicit operator short(size_t s) {
return (short) s.Value;
}
public static explicit operator long(size_t s) {
return (long) s.Value;
}
public static explicit operator int(size_t s) {
return (int) s.Value;
}
public static explicit operator byte(size_t s) {
return (byte) s.Value;
}
public static explicit operator sbyte(size_t s) {
return (sbyte) s.Value;
}
public static explicit operator void*(size_t s) {
return s.Value;
}
public static implicit operator size_t(int i) {
return new size_t((void*) i);
}
public static implicit operator size_t(uint i) {
return new size_t((void*) i);
}
public static implicit operator size_t(long i) {
return new size_t((void*) i);
}
public static implicit operator size_t(ulong i) {
return new size_t((void*) i);
}
public static explicit operator size_t(void* p) {
return new size_t(p);
}
#if NET_45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static size_t operator +(size_t a, size_t b) {
return Is64Bit
? new size_t((byte*) a + (ulong) b)
: new size_t((byte*) a + b);
}
public static size_t operator +(size_t a, int b) {
return new size_t((byte*) a + b);
}
public static size_t operator +(size_t a, uint b) {
return new size_t((byte*) a + b);
}
public static size_t operator +(size_t a, long b) {
return new size_t((byte*) a + b);
}
public static size_t operator +(size_t a, ulong b) {
return new size_t((byte*) a + b);
}
#if NET_45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static size_t operator -(size_t a, size_t b) {
return Is64Bit
? new size_t((byte*) a - (ulong) b)
: new size_t((byte*) a - b);
}
public static size_t operator -(size_t a, int b) {
return new size_t((byte*) a.Value - b);
}
public static size_t operator -(size_t a, uint b) {
return new size_t((byte*) a.Value - b);
}
public static size_t operator -(size_t a, long b) {
return new size_t((byte*) a.Value - b);
}
public static size_t operator -(size_t a, ulong b) {
return new size_t((byte*) a.Value - b);
}
#if NET_45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static size_t operator /(size_t a, size_t b) {
return new size_t(Is64Bit ? (void*) ((ulong) a / (ulong) b) : (void*) ((uint) a / (uint) b));
}
#if NET_45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static size_t operator /(size_t a, int b) {
return new size_t(Is64Bit ? (void*) ((ulong) a / (uint) b) : (void*) ((uint) a / (uint) b));
}
#if NET_45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static size_t operator /(size_t a, uint b) {
return new size_t(Is64Bit ? (void*) ((ulong) a / b) : (void*) ((uint) a / b));
}
#if NET_45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static size_t operator /(size_t a, long b) {
return new size_t(Is64Bit ? (void*) ((ulong) a / (ulong) b) : (void*) ((uint) a / (uint) b));
}
#if NET_45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static size_t operator /(size_t a, ulong b) {
return new size_t(Is64Bit ? (void*) ((ulong) a / b) : (void*) ((uint) a / (uint) b));
}
#if NET_45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static size_t operator *(size_t a, size_t b) {
return new size_t(Is64Bit ? (void*) ((ulong) a * (ulong) b) : (void*) ((uint) a * (uint) b));
}
#if NET_45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static size_t operator *(size_t a, int b) {
return new size_t(Is64Bit ? (void*) ((ulong) a * (ulong) b) : (void*) ((uint) a * (uint) b));
}
#if NET_45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static size_t operator *(size_t a, uint b) {
return new size_t(Is64Bit ? (void*) ((ulong) a * b) : (void*) ((uint) a * b));
}
#if NET_45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static size_t operator *(size_t a, long b) {
return new size_t(Is64Bit ? (void*) ((ulong) a * (ulong) b) : (void*) ((uint) a * (uint) b));
}
#if NET_45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static size_t operator *(size_t a, ulong b) {
return new size_t(Is64Bit ? (void*) ((ulong) a * b) : (void*) ((uint) a * (uint) b));
}
#if NET_45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static size_t operator %(size_t a, size_t b) {
return new size_t(Is64Bit ? (void*) ((ulong) a % (ulong) b) : (void*) ((uint) a % (uint) b));
}
#if NET_45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static size_t operator %(size_t a, int b) {
return new size_t(Is64Bit ? (void*) ((ulong) a % (ulong) b) : (void*) ((uint) a % (uint) b));
}
#if NET_45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static size_t operator %(size_t a, uint b) {
return new size_t(Is64Bit ? (void*) ((ulong) a % b) : (void*) ((uint) a % b));
}
#if NET_45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static size_t operator %(size_t a, long b) {
return new size_t(Is64Bit ? (void*) ((ulong) a % (ulong) b) : (void*) ((uint) a % (uint) b));
}
#if NET_45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static size_t operator %(size_t a, ulong b) {
return new size_t(Is64Bit ? (void*) ((ulong) a % b) : (void*) ((uint) a % (uint) b));
}
#if NET_45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static size_t operator &(size_t a, size_t b) {
return new size_t(Is64Bit ? (void*) ((ulong) a & (ulong) b) : (void*) ((uint) a & (uint) b));
}
public static size_t operator &(size_t a, int b) {
return new size_t((void*) ((uint)a & (uint) b));
}
public static size_t operator &(size_t a, uint b) {
return new size_t((void*)((uint)a & b));
}
#if NET_45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static size_t operator &(size_t a, long b) {
return new size_t(Is64Bit ? (void*) ((ulong) a & (uint) b) : (void*) ((uint) a & (uint) b));
}
#if NET_45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static size_t operator &(size_t a, ulong b) {
return new size_t(Is64Bit ? (void*) ((ulong) a & b) : (void*) ((uint) a & b));
}
#if NET_45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static size_t operator |(size_t a, size_t b) {
return new size_t(Is64Bit ? (void*) ((ulong) a | (ulong) b) : (void*) ((uint) a | (uint) b));
}
#if NET_45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static size_t operator |(size_t a, int b) {
return new size_t(Is64Bit ? (void*) ((ulong) a | (uint) b) : (void*) ((uint) a | (uint) b));
}
#if NET_45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static size_t operator |(size_t a, uint b) {
return new size_t(Is64Bit ? (void*) ((ulong) a | b) : (void*) ((uint) a | b));
}
#if NET_45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static size_t operator |(size_t a, long b) {
return new size_t(Is64Bit ? (void*) ((ulong) a | (uint) b) : (void*) ((uint) a | (uint) b));
}
#if NET_45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static size_t operator |(size_t a, ulong b) {
return new size_t(Is64Bit ? (void*) ((ulong) a | b) : (void*) ((uint) a | b));
}
#if NET_45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static size_t operator >>(size_t a, int b) {
return new size_t(Is64Bit ? (void*) ((ulong) a >> b) : (void*) ((uint) a >> b));
}
#if NET_45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static size_t operator <<(size_t a, int b) {
return new size_t(Is64Bit ? (void*) ((ulong) a << b) : (void*) ((uint) a << b));
}
}
}
}