-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
fastcopy.c
639 lines (580 loc) · 16.2 KB
/
fastcopy.c
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
/*********************************************************************
Blosc - Blocked Shuffling and Compression Library
Author: Francesc Alted <francesc@blosc.org>
Creation date: 2018-01-03
See LICENSE.txt for details about copyright and rights to use.
**********************************************************************/
/*********************************************************************
The code in this file is heavily based on memcopy.h, from the
zlib-ng compression library. See LICENSES/ZLIB-NG.txt for details.
See also: https://github.com/zlib-ng/zlib-ng/blob/develop/zlib.h.in
New implementations by Francesc Alted:
* fast_copy() and copy_run() functions
* Support for SSE2/AVX2 copy instructions for these routines
**********************************************************************/
#include <assert.h>
#include "blosc-common.h"
/*
* Use inlined functions for supported systems.
*/
#if defined(_MSC_VER) && !defined(__cplusplus) /* Visual Studio */
#define inline __inline /* Visual C is not C99, but supports some kind of inline */
#endif
static inline unsigned char *copy_1_bytes(unsigned char *out, const unsigned char *from) {
*out++ = *from;
return out;
}
static inline unsigned char *copy_2_bytes(unsigned char *out, const unsigned char *from) {
#if defined(BLOSC_STRICT_ALIGN)
uint16_t chunk;
memcpy(&chunk, from, 2);
memcpy(out, &chunk, 2);
#else
*(uint16_t *) out = *(uint16_t *) from;
#endif
return out + 2;
}
static inline unsigned char *copy_3_bytes(unsigned char *out, const unsigned char *from) {
out = copy_1_bytes(out, from);
return copy_2_bytes(out, from + 1);
}
static inline unsigned char *copy_4_bytes(unsigned char *out, const unsigned char *from) {
#if defined(BLOSC_STRICT_ALIGN)
uint32_t chunk;
memcpy(&chunk, from, 4);
memcpy(out, &chunk, 4);
#else
*(uint32_t *) out = *(uint32_t *) from;
#endif
return out + 4;
}
static inline unsigned char *copy_5_bytes(unsigned char *out, const unsigned char *from) {
out = copy_1_bytes(out, from);
return copy_4_bytes(out, from + 1);
}
static inline unsigned char *copy_6_bytes(unsigned char *out, const unsigned char *from) {
out = copy_2_bytes(out, from);
return copy_4_bytes(out, from + 2);
}
static inline unsigned char *copy_7_bytes(unsigned char *out, const unsigned char *from) {
out = copy_3_bytes(out, from);
return copy_4_bytes(out, from + 3);
}
static inline unsigned char *copy_8_bytes(unsigned char *out, const unsigned char *from) {
#if defined(BLOSC_STRICT_ALIGN)
uint64_t chunk;
memcpy(&chunk, from, 8);
memcpy(out, &chunk, 8);
#else
*(uint64_t *) out = *(uint64_t *) from;
#endif
return out + 8;
}
static inline unsigned char *copy_16_bytes(unsigned char *out, const unsigned char *from) {
#if defined(__SSE2__)
__m128i chunk;
chunk = _mm_loadu_si128((__m128i*)from);
_mm_storeu_si128((__m128i*)out, chunk);
out += 16;
#elif !defined(BLOSC_STRICT_ALIGN)
*(uint64_t*)out = *(uint64_t*)from;
from += 8; out += 8;
*(uint64_t*)out = *(uint64_t*)from;
from += 8; out += 8;
#else
int i;
for (i = 0; i < 16; i++) {
*out++ = *from++;
}
#endif
return out;
}
static inline unsigned char *copy_32_bytes(unsigned char *out, const unsigned char *from) {
#if defined(__AVX2__)
__m256i chunk;
chunk = _mm256_loadu_si256((__m256i*)from);
_mm256_storeu_si256((__m256i*)out, chunk);
out += 32;
#elif defined(__SSE2__)
__m128i chunk;
chunk = _mm_loadu_si128((__m128i*)from);
_mm_storeu_si128((__m128i*)out, chunk);
from += 16; out += 16;
chunk = _mm_loadu_si128((__m128i*)from);
_mm_storeu_si128((__m128i*)out, chunk);
out += 16;
#elif !defined(BLOSC_STRICT_ALIGN)
*(uint64_t*)out = *(uint64_t*)from;
from += 8; out += 8;
*(uint64_t*)out = *(uint64_t*)from;
from += 8; out += 8;
*(uint64_t*)out = *(uint64_t*)from;
from += 8; out += 8;
*(uint64_t*)out = *(uint64_t*)from;
from += 8; out += 8;
#else
int i;
for (i = 0; i < 32; i++) {
*out++ = *from++;
}
#endif
return out;
}
// This is never used, so comment it out
//#if defined(__AVX2__)
//static inline unsigned char *copy_32_bytes_aligned(unsigned char *out, const unsigned char *from) {
// __m256i chunk;
// chunk = _mm256_load_si256((__m256i*)from);
// _mm256_storeu_si256((__m256i*)out, chunk);
// return out + 32;
//}
//#endif // __AVX2__
/* Copy LEN bytes (7 or fewer) from FROM into OUT. Return OUT + LEN. */
static inline unsigned char *copy_bytes(unsigned char *out, const unsigned char *from, unsigned len) {
assert(len < 8);
#ifdef BLOSC_STRICT_ALIGN
while (len--) {
*out++ = *from++;
}
#else
switch (len) {
case 7:
return copy_7_bytes(out, from);
case 6:
return copy_6_bytes(out, from);
case 5:
return copy_5_bytes(out, from);
case 4:
return copy_4_bytes(out, from);
case 3:
return copy_3_bytes(out, from);
case 2:
return copy_2_bytes(out, from);
case 1:
return copy_1_bytes(out, from);
case 0:
return out;
default:
assert(0);
}
#endif /* BLOSC_STRICT_ALIGN */
return out;
}
// Define a symbol for avoiding fall-through warnings emitted by gcc >= 7.0
#if ((defined(__GNUC__) && BLOSC_GCC_VERSION >= 700) && !defined(__clang__) && \
!defined(__ICC) && !defined(__ICL))
#define AVOID_FALLTHROUGH_WARNING
#endif
/* Byte by byte semantics: copy LEN bytes from FROM and write them to OUT. Return OUT + LEN. */
static inline unsigned char *chunk_memcpy(unsigned char *out, const unsigned char *from, unsigned len) {
unsigned sz = sizeof(uint64_t);
unsigned rem = len % sz;
unsigned by8;
assert(len >= sz);
/* Copy a few bytes to make sure the loop below has a multiple of SZ bytes to be copied. */
copy_8_bytes(out, from);
len /= sz;
out += rem;
from += rem;
by8 = len % 8;
len -= by8;
switch (by8) {
case 7:
out = copy_8_bytes(out, from);
from += sz;
#ifdef AVOID_FALLTHROUGH_WARNING
__attribute__ ((fallthrough)); // Shut-up -Wimplicit-fallthrough warning in GCC
#endif
case 6:
out = copy_8_bytes(out, from);
from += sz;
#ifdef AVOID_FALLTHROUGH_WARNING
__attribute__ ((fallthrough));
#endif
case 5:
out = copy_8_bytes(out, from);
from += sz;
#ifdef AVOID_FALLTHROUGH_WARNING
__attribute__ ((fallthrough));
#endif
case 4:
out = copy_8_bytes(out, from);
from += sz;
#ifdef AVOID_FALLTHROUGH_WARNING
__attribute__ ((fallthrough));
#endif
case 3:
out = copy_8_bytes(out, from);
from += sz;
#ifdef AVOID_FALLTHROUGH_WARNING
__attribute__ ((fallthrough));
#endif
case 2:
out = copy_8_bytes(out, from);
from += sz;
#ifdef AVOID_FALLTHROUGH_WARNING
__attribute__ ((fallthrough));
#endif
case 1:
out = copy_8_bytes(out, from);
from += sz;
#ifdef AVOID_FALLTHROUGH_WARNING
__attribute__ ((fallthrough));
#endif
default:
break;
}
while (len) {
out = copy_8_bytes(out, from);
from += sz;
out = copy_8_bytes(out, from);
from += sz;
out = copy_8_bytes(out, from);
from += sz;
out = copy_8_bytes(out, from);
from += sz;
out = copy_8_bytes(out, from);
from += sz;
out = copy_8_bytes(out, from);
from += sz;
out = copy_8_bytes(out, from);
from += sz;
out = copy_8_bytes(out, from);
from += sz;
len -= 8;
}
return out;
}
#if (defined(__SSE2__) && defined(__AVX2__))
/* 16-byte version of chunk_memcpy() */
static inline unsigned char *chunk_memcpy_16(unsigned char *out, const unsigned char *from, unsigned len) {
unsigned sz = 16;
unsigned rem = len % sz;
unsigned ilen;
assert(len >= sz);
/* Copy a few bytes to make sure the loop below has a multiple of SZ bytes to be copied. */
copy_16_bytes(out, from);
len /= sz;
out += rem;
from += rem;
for (ilen = 0; ilen < len; ilen++) {
copy_16_bytes(out, from);
out += sz;
from += sz;
}
return out;
}
#endif
// NOTE: chunk_memcpy_32() and chunk_memcpy_32_unrolled() are not used, so commenting them
///* 32-byte version of chunk_memcpy() */
//static inline unsigned char *chunk_memcpy_32(unsigned char *out, const unsigned char *from, unsigned len) {
// unsigned sz = 32;
// unsigned rem = len % sz;
// unsigned ilen;
//
// assert(len >= sz);
//
// /* Copy a few bytes to make sure the loop below has a multiple of SZ bytes to be copied. */
// copy_32_bytes(out, from);
//
// len /= sz;
// out += rem;
// from += rem;
//
// for (ilen = 0; ilen < len; ilen++) {
// copy_32_bytes(out, from);
// out += sz;
// from += sz;
// }
//
// return out;
//}
//
///* 32-byte *unrolled* version of chunk_memcpy() */
//static inline unsigned char *chunk_memcpy_32_unrolled(unsigned char *out, const unsigned char *from, unsigned len) {
// unsigned sz = 32;
// unsigned rem = len % sz;
// unsigned by8;
//
// assert(len >= sz);
//
// /* Copy a few bytes to make sure the loop below has a multiple of SZ bytes to be copied. */
// copy_32_bytes(out, from);
//
// len /= sz;
// out += rem;
// from += rem;
//
// by8 = len % 8;
// len -= by8;
// switch (by8) {
// case 7:
// out = copy_32_bytes(out, from);
// from += sz;
// case 6:
// out = copy_32_bytes(out, from);
// from += sz;
// case 5:
// out = copy_32_bytes(out, from);
// from += sz;
// case 4:
// out = copy_32_bytes(out, from);
// from += sz;
// case 3:
// out = copy_32_bytes(out, from);
// from += sz;
// case 2:
// out = copy_32_bytes(out, from);
// from += sz;
// case 1:
// out = copy_32_bytes(out, from);
// from += sz;
// default:
// break;
// }
//
// while (len) {
// out = copy_32_bytes(out, from);
// from += sz;
// out = copy_32_bytes(out, from);
// from += sz;
// out = copy_32_bytes(out, from);
// from += sz;
// out = copy_32_bytes(out, from);
// from += sz;
// out = copy_32_bytes(out, from);
// from += sz;
// out = copy_32_bytes(out, from);
// from += sz;
// out = copy_32_bytes(out, from);
// from += sz;
// out = copy_32_bytes(out, from);
// from += sz;
//
// len -= 8;
// }
//
// return out;
//}
/* SSE2/AVX2 *unaligned* version of chunk_memcpy() */
#if defined(__SSE2__) || defined(__AVX2__)
static inline unsigned char *chunk_memcpy_unaligned(unsigned char *out, const unsigned char *from, unsigned len) {
#if defined(__AVX2__)
unsigned sz = sizeof(__m256i);
#elif defined(__SSE2__)
unsigned sz = sizeof(__m128i);
#endif
unsigned rem = len % sz;
unsigned ilen;
assert(len >= sz);
/* Copy a few bytes to make sure the loop below has a multiple of SZ bytes to be copied. */
#if defined(__AVX2__)
copy_32_bytes(out, from);
#elif defined(__SSE2__)
copy_16_bytes(out, from);
#endif
len /= sz;
out += rem;
from += rem;
for (ilen = 0; ilen < len; ilen++) {
#if defined(__AVX2__)
copy_32_bytes(out, from);
#elif defined(__SSE2__)
copy_16_bytes(out, from);
#endif
out += sz;
from += sz;
}
return out;
}
#endif // __SSE2__ || __AVX2__
// NOTE: chunk_memcpy_aligned() is not used, so commenting it
//#if defined(__SSE2__) || defined(__AVX2__)
///* SSE2/AVX2 *aligned* version of chunk_memcpy() */
//static inline unsigned char *chunk_memcpy_aligned(unsigned char *out, const unsigned char *from, unsigned len) {
//#if defined(__AVX2__)
// unsigned sz = sizeof(__m256i);
// __m256i chunk;
//#elif defined(__SSE2__)
// unsigned sz = sizeof(__m128i);
// __m128i chunk;
//#endif
// unsigned bytes_to_align = sz - (unsigned)(((uintptr_t)(const void *)(from)) % sz);
// unsigned corrected_len = len - bytes_to_align;
// unsigned rem = corrected_len % sz;
// unsigned ilen;
//
// assert(len >= sz);
//
// /* Copy a few bytes to make sure the loop below has aligned access. */
//#if defined(__AVX2__)
// chunk = _mm256_loadu_si256((__m256i *) from);
// _mm256_storeu_si256((__m256i *) out, chunk);
//#elif defined(__SSE2__)
// chunk = _mm_loadu_si128((__m128i *) from);
// _mm_storeu_si128((__m128i *) out, chunk);
//#endif
// out += bytes_to_align;
// from += bytes_to_align;
//
// len = corrected_len / sz;
// for (ilen = 0; ilen < len; ilen++) {
//#if defined(__AVX2__)
// chunk = _mm256_load_si256((__m256i *) from); /* *aligned* load */
// _mm256_storeu_si256((__m256i *) out, chunk);
//#elif defined(__SSE2__)
// chunk = _mm_load_si128((__m128i *) from); /* *aligned* load */
// _mm_storeu_si128((__m128i *) out, chunk);
//#endif
// out += sz;
// from += sz;
// }
//
// /* Copy remaining bytes */
// if (rem < 8) {
// out = copy_bytes(out, from, rem);
// }
// else {
// out = chunk_memcpy(out, from, rem);
// }
//
// return out;
//}
//#endif // __AVX2__ || __SSE2__
/* Byte by byte semantics: copy LEN bytes from FROM and write them to OUT. Return OUT + LEN. */
unsigned char *fastcopy(unsigned char *out, const unsigned char *from, unsigned len) {
switch (len) {
case 32:
return copy_32_bytes(out, from);
case 16:
return copy_16_bytes(out, from);
case 8:
return copy_8_bytes(out, from);
default: {
}
}
if (len < 8) {
return copy_bytes(out, from, len);
}
#if defined(__SSE2__)
if (len < 16) {
return chunk_memcpy(out, from, len);
}
#if !defined(__AVX2__)
return chunk_memcpy_unaligned(out, from, len);
#else
if (len < 32) {
return chunk_memcpy_16(out, from, len);
}
return chunk_memcpy_unaligned(out, from, len);
#endif // !__AVX2__
#else
return chunk_memcpy(out, from, len);
#endif // __SSE2__
}
/* Copy a run */
unsigned char* copy_match(unsigned char *out, const unsigned char *from, unsigned len) {
#if defined(__AVX2__)
unsigned sz = sizeof(__m256i);
#elif defined(__SSE2__)
unsigned sz = sizeof(__m128i);
#else
unsigned sz = sizeof(uint64_t);
#endif
#if ((defined(__GNUC__) && BLOSC_GCC_VERSION < 800) && !defined(__clang__) && !defined(__ICC) && !defined(__ICL))
// GCC < 8 in fully optimization mode seems to have problems with the code further below so stop here
for (; len > 0; len--) {
*out++ = *from++;
}
return out;
#endif
// If out and from are away more than the size of the copy, then a fastcopy is safe
unsigned overlap_dist = (unsigned) (out - from);
if (overlap_dist > sz) {
return fastcopy(out, from, len);
}
// Otherwise we need to be more careful so as not to overwrite destination
switch (overlap_dist) {
case 32:
for (; len >= 32; len -= 32) {
out = copy_32_bytes(out, from);
}
break;
case 30:
for (; len >= 30; len -= 30) {
out = copy_16_bytes(out, from);
out = copy_8_bytes(out, from + 16);
out = copy_4_bytes(out, from + 24);
out = copy_2_bytes(out, from + 28);
}
break;
case 28:
for (; len >= 28; len -= 28) {
out = copy_16_bytes(out, from);
out = copy_8_bytes(out, from + 16);
out = copy_4_bytes(out, from + 24);
}
break;
case 26:
for (; len >= 26; len -= 26) {
out = copy_16_bytes(out, from);
out = copy_8_bytes(out, from + 16);
out = copy_2_bytes(out, from + 24);
}
break;
case 24:
for (; len >= 24; len -= 24) {
out = copy_16_bytes(out, from);
out = copy_8_bytes(out, from + 16);
}
break;
case 22:
for (; len >= 22; len -= 22) {
out = copy_16_bytes(out, from);
out = copy_4_bytes(out, from + 16);
out = copy_2_bytes(out, from + 20);
}
break;
case 20:
for (; len >= 20; len -= 20) {
out = copy_16_bytes(out, from);
out = copy_4_bytes(out, from + 16);
}
break;
case 18:
for (; len >= 18; len -= 18) {
out = copy_16_bytes(out, from);
out = copy_2_bytes(out, from + 16);
}
break;
case 16:
for (; len >= 16; len -= 16) {
out = copy_16_bytes(out, from);
}
break;
case 8:
for (; len >= 8; len -= 8) {
out = copy_8_bytes(out, from);
}
break;
case 4:
for (; len >= 4; len -= 4) {
out = copy_4_bytes(out, from);
}
break;
case 2:
for (; len >= 2; len -= 2) {
out = copy_2_bytes(out, from);
}
break;
default:
for (; len > 0; len--) {
*out++ = *from++;
}
}
// Copy the leftovers
for (; len > 0; len--) {
*out++ = *from++;
}
return out;
}