Coverage Report

Created: 2026-12-01 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/c-blosc2/blosc/shuffle-avx2.c
Line
Count
Source
1
/*********************************************************************
2
  Blosc - Blocked Shuffling and Compression Library
3
4
  Copyright (c) 2021  Blosc Development Team <blosc@blosc.org>
5
  https://blosc.org
6
  License: BSD 3-Clause (see LICENSE.txt)
7
8
  See LICENSE.txt for details about copyright and rights to use.
9
**********************************************************************/
10
11
#include "shuffle-avx2.h"
12
#include "shuffle-generic.h"
13
#include <stdlib.h>
14
15
/* Make sure AVX2 is available for the compilation target and compiler. */
16
#if defined(__AVX2__)
17
18
#include <immintrin.h>
19
20
#include <stdint.h>
21
22
/* The next is useful for debugging purposes */
23
#if 0
24
#include <stdio.h>
25
#include <string.h>
26
27
static void printymm32(__m256i ymm0)
28
{
29
  uint32_t buf[8];
30
31
  ((__m256i *)buf)[0] = ymm0;
32
  fprintf(stderr, "%x,%x,%x,%x,%x,%x,%x,%x\n",
33
          buf[0], buf[1], buf[2], buf[3],
34
          buf[4], buf[5], buf[6], buf[7]);
35
}
36
37
static void printymm(__m256i ymm0)
38
{
39
  uint8_t buf[32];
40
41
  ((__m256i *)buf)[0] = ymm0;
42
  fprintf(stderr, "%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x\n",
43
          buf[0], buf[1], buf[2], buf[3],
44
          buf[4], buf[5], buf[6], buf[7],
45
          buf[8], buf[9], buf[10], buf[11],
46
          buf[12], buf[13], buf[14], buf[15],
47
          buf[16], buf[17], buf[18], buf[19],
48
          buf[20], buf[21], buf[22], buf[23],
49
          buf[24], buf[25], buf[26], buf[27],
50
          buf[28], buf[29], buf[30], buf[31]);
51
}
52
#endif
53
54
/* GCC doesn't include the split load/store intrinsics
55
   needed for the tiled shuffle, so define them here. */
56
#if defined(__GNUC__) && !defined(__clang__) && !defined(__ICC)
57
static inline __m256i
58
__attribute__((__always_inline__))
59
_mm256_loadu2_m128i(const __m128i* const hiaddr, const __m128i* const loaddr) {
60
  return _mm256_inserti128_si256(
61
      _mm256_castsi128_si256(_mm_loadu_si128(loaddr)), _mm_loadu_si128(hiaddr), 1);
62
}
63
64
static inline void
65
__attribute__((__always_inline__))
66
_mm256_storeu2_m128i(__m128i* const hiaddr, __m128i* const loaddr, const __m256i a) {
67
  _mm_storeu_si128(loaddr, _mm256_castsi256_si128(a));
68
  _mm_storeu_si128(hiaddr, _mm256_extracti128_si256(a, 1));
69
}
70
#endif  /* defined(__GNUC__) */
71
72
/* Routine optimized for shuffling a buffer for a type size of 2 bytes. */
73
static void
74
shuffle2_avx2(uint8_t* const dest, const uint8_t* const src,
75
1.69k
              const int32_t vectorizable_elements, const int32_t total_elements) {
76
1.69k
  static const int32_t bytesoftype = 2;
77
1.69k
  int32_t j;
78
1.69k
  const __m256i mask = _mm256_set1_epi16(0x00ff);
79
80
35.2k
  for (j = 0; j < vectorizable_elements; j += sizeof(__m256i)) {
81
    /* Fetch 32 elements (64 bytes), pack low/high bytes, and fix cross-lane order. */
82
33.5k
    __m256i y0 = _mm256_loadu_si256((__m256i*)(src + (j * bytesoftype)));
83
33.5k
    __m256i y1 = _mm256_loadu_si256((__m256i*)(src + (j * bytesoftype) + sizeof(__m256i)));
84
33.5k
    __m256i lo = _mm256_permute4x64_epi64(
85
33.5k
        _mm256_packus_epi16(_mm256_and_si256(y0, mask), _mm256_and_si256(y1, mask)), 0xd8);
86
33.5k
    __m256i hi = _mm256_permute4x64_epi64(
87
33.5k
        _mm256_packus_epi16(_mm256_srli_epi16(y0, 8), _mm256_srli_epi16(y1, 8)), 0xd8);
88
89
    /* Store the result vectors */
90
33.5k
    _mm256_storeu_si256((__m256i*)(dest + j), lo);
91
33.5k
    _mm256_storeu_si256((__m256i*)(dest + j + total_elements), hi);
92
33.5k
  }
93
1.69k
}
94
95
/* Routine optimized for shuffling a buffer for a type size of 4 bytes. */
96
static void
97
shuffle4_avx2(uint8_t* const dest, const uint8_t* const src,
98
6.85k
              const int32_t vectorizable_elements, const int32_t total_elements) {
99
6.85k
  static const int32_t bytesoftype = 4;
100
6.85k
  int32_t i;
101
6.85k
  int j;
102
6.85k
  __m256i ymm0[4], ymm1[4];
103
104
  /* Create the shuffle mask.
105
     NOTE: The XMM/YMM 'set' intrinsics require the arguments to be ordered from
106
     most to least significant (i.e., their order is reversed when compared to
107
     loading the mask from an array). */
108
6.85k
  const __m256i shmask = _mm256_set_epi8(
109
6.85k
      0x0f, 0x0b, 0x07, 0x03, 0x0e, 0x0a, 0x06, 0x02,
110
6.85k
      0x0d, 0x09, 0x05, 0x01, 0x0c, 0x08, 0x04, 0x00,
111
6.85k
      0x0f, 0x0b, 0x07, 0x03, 0x0e, 0x0a, 0x06, 0x02,
112
6.85k
      0x0d, 0x09, 0x05, 0x01, 0x0c, 0x08, 0x04, 0x00);
113
6.85k
  const __m256i mask = _mm256_set_epi32(
114
6.85k
      0x07, 0x03, 0x06, 0x02, 0x05, 0x01, 0x04, 0x00);
115
116
16.9M
  for (i = 0; i < vectorizable_elements; i += sizeof(__m256i)) {
117
    /* Fetch 32 elements (128 bytes) then transpose bytes and words within 128-bit lanes. */
118
84.6M
    for (j = 0; j < 4; j++) {
119
67.7M
      ymm0[j] = _mm256_shuffle_epi8(
120
67.7M
          _mm256_loadu_si256((__m256i*)(src + (i * bytesoftype) + (j * sizeof(__m256i)))),
121
67.7M
          shmask);
122
67.7M
    }
123
    /* Transpose double words */
124
50.8M
    for (j = 0; j < 2; j++) {
125
33.8M
      ymm1[j * 2] = _mm256_unpacklo_epi32(ymm0[j * 2], ymm0[j * 2 + 1]);
126
33.8M
      ymm1[j * 2 + 1] = _mm256_unpackhi_epi32(ymm0[j * 2], ymm0[j * 2 + 1]);
127
33.8M
    }
128
    /* Transpose quad words */
129
50.8M
    for (j = 0; j < 2; j++) {
130
33.8M
      ymm0[j * 2] = _mm256_unpacklo_epi64(ymm1[j], ymm1[j + 2]);
131
33.8M
      ymm0[j * 2 + 1] = _mm256_unpackhi_epi64(ymm1[j], ymm1[j + 2]);
132
33.8M
    }
133
84.6M
    for (j = 0; j < 4; j++) {
134
67.7M
      ymm0[j] = _mm256_permutevar8x32_epi32(ymm0[j], mask);
135
67.7M
    }
136
    /* Store the result vectors */
137
16.9M
    uint8_t* const dest_for_ith_element = dest + i;
138
84.6M
    for (j = 0; j < 4; j++) {
139
67.7M
      _mm256_storeu_si256((__m256i*)(dest_for_ith_element + (j * total_elements)), ymm0[j]);
140
67.7M
    }
141
16.9M
  }
142
6.85k
}
143
144
/* Routine optimized for shuffling a buffer for a type size of 8 bytes. */
145
static void
146
shuffle8_avx2(uint8_t* const dest, const uint8_t* const src,
147
1.98M
              const int32_t vectorizable_elements, const int32_t total_elements) {
148
1.98M
  static const int32_t bytesoftype = 8;
149
1.98M
  int32_t j;
150
1.98M
  int k, l;
151
1.98M
  __m256i ymm0[8], ymm1[8];
152
1.98M
  const __m256i shmask_in = _mm256_set_epi8(
153
1.98M
      0x0f, 0x07, 0x0e, 0x06, 0x0d, 0x05, 0x0c, 0x04,
154
1.98M
      0x0b, 0x03, 0x0a, 0x02, 0x09, 0x01, 0x08, 0x00,
155
1.98M
      0x0f, 0x07, 0x0e, 0x06, 0x0d, 0x05, 0x0c, 0x04,
156
1.98M
      0x0b, 0x03, 0x0a, 0x02, 0x09, 0x01, 0x08, 0x00);
157
1.98M
  const __m256i shmask_out = _mm256_set_epi8(
158
1.98M
      0x0f, 0x0e, 0x07, 0x06, 0x0d, 0x0c, 0x05, 0x04,
159
1.98M
      0x0b, 0x0a, 0x03, 0x02, 0x09, 0x08, 0x01, 0x00,
160
1.98M
      0x0f, 0x0e, 0x07, 0x06, 0x0d, 0x0c, 0x05, 0x04,
161
1.98M
      0x0b, 0x0a, 0x03, 0x02, 0x09, 0x08, 0x01, 0x00);
162
163
81.3M
  for (j = 0; j < vectorizable_elements; j += sizeof(__m256i)) {
164
    /* Fetch 32 elements (256 bytes) then transpose bytes. */
165
710M
    for (k = 0; k < 8; k++) {
166
631M
      ymm1[k] = _mm256_shuffle_epi8(
167
631M
          _mm256_loadu_si256((__m256i*)(src + (j * bytesoftype) + (k * sizeof(__m256i)))),
168
631M
          shmask_in);
169
631M
    }
170
    /* Transpose words */
171
392M
    for (k = 0, l = 0; k < 4; k++, l += 2) {
172
312M
      ymm0[k * 2] = _mm256_unpacklo_epi16(ymm1[l], ymm1[l + 1]);
173
312M
      ymm0[k * 2 + 1] = _mm256_unpackhi_epi16(ymm1[l], ymm1[l + 1]);
174
312M
    }
175
    /* Transpose double words */
176
392M
    for (k = 0, l = 0; k < 4; k++, l++) {
177
312M
      if (k == 2) l += 2;
178
312M
      ymm1[k * 2] = _mm256_unpacklo_epi32(ymm0[l], ymm0[l + 2]);
179
312M
      ymm1[k * 2 + 1] = _mm256_unpackhi_epi32(ymm0[l], ymm0[l + 2]);
180
312M
    }
181
    /* Transpose quad words */
182
386M
    for (k = 0; k < 4; k++) {
183
307M
      ymm0[k * 2] = _mm256_unpacklo_epi64(ymm1[k], ymm1[k + 4]);
184
307M
      ymm0[k * 2 + 1] = _mm256_unpackhi_epi64(ymm1[k], ymm1[k + 4]);
185
307M
    }
186
700M
    for (k = 0; k < 8; k++) {
187
621M
      ymm0[k] = _mm256_shuffle_epi8(
188
621M
          _mm256_permute4x64_epi64(ymm0[k], 0xD8), shmask_out);
189
621M
    }
190
    /* Store the result vectors */
191
79.3M
    uint8_t* const dest_for_jth_element = dest + j;
192
697M
    for (k = 0; k < 8; k++) {
193
617M
      _mm256_storeu_si256((__m256i*)(dest_for_jth_element + (k * total_elements)), ymm0[k]);
194
617M
    }
195
79.3M
  }
196
1.98M
}
197
198
/* Routine optimized for shuffling a buffer for a type size of 16 bytes. */
199
static void
200
shuffle16_avx2(uint8_t* const dest, const uint8_t* const src,
201
2.91k
               const int32_t vectorizable_elements, const int32_t total_elements) {
202
2.91k
  static const int32_t bytesoftype = 16;
203
2.91k
  int32_t j;
204
2.91k
  int k, l;
205
2.91k
  __m256i ymm0[16], ymm1[16];
206
207
  /* Create the shuffle mask.
208
     NOTE: The XMM/YMM 'set' intrinsics require the arguments to be ordered from
209
     most to least significant (i.e., their order is reversed when compared to
210
     loading the mask from an array). */
211
2.91k
  const __m256i shmask = _mm256_set_epi8(
212
2.91k
      0x0f, 0x07, 0x0e, 0x06, 0x0d, 0x05, 0x0c, 0x04,
213
2.91k
      0x0b, 0x03, 0x0a, 0x02, 0x09, 0x01, 0x08, 0x00,
214
2.91k
      0x0f, 0x07, 0x0e, 0x06, 0x0d, 0x05, 0x0c, 0x04,
215
2.91k
      0x0b, 0x03, 0x0a, 0x02, 0x09, 0x01, 0x08, 0x00);
216
217
6.73k
  for (j = 0; j < vectorizable_elements; j += sizeof(__m256i)) {
218
    /* Fetch 32 elements (512 bytes) into 16 YMM registers. */
219
64.9k
    for (k = 0; k < 16; k++) {
220
61.0k
      ymm0[k] = _mm256_loadu_si256((__m256i*)(src + (j * bytesoftype) + (k * sizeof(__m256i))));
221
61.0k
    }
222
    /* Transpose bytes */
223
34.3k
    for (k = 0, l = 0; k < 8; k++, l += 2) {
224
30.5k
      ymm1[k * 2] = _mm256_unpacklo_epi8(ymm0[l], ymm0[l + 1]);
225
30.5k
      ymm1[k * 2 + 1] = _mm256_unpackhi_epi8(ymm0[l], ymm0[l + 1]);
226
30.5k
    }
227
    /* Transpose words */
228
34.3k
    for (k = 0, l = -2; k < 8; k++, l++) {
229
30.5k
      if ((k % 2) == 0) l += 2;
230
30.5k
      ymm0[k * 2] = _mm256_unpacklo_epi16(ymm1[l], ymm1[l + 2]);
231
30.5k
      ymm0[k * 2 + 1] = _mm256_unpackhi_epi16(ymm1[l], ymm1[l + 2]);
232
30.5k
    }
233
    /* Transpose double words */
234
34.3k
    for (k = 0, l = -4; k < 8; k++, l++) {
235
30.5k
      if ((k % 4) == 0) l += 4;
236
30.5k
      ymm1[k * 2] = _mm256_unpacklo_epi32(ymm0[l], ymm0[l + 4]);
237
30.5k
      ymm1[k * 2 + 1] = _mm256_unpackhi_epi32(ymm0[l], ymm0[l + 4]);
238
30.5k
    }
239
    /* Transpose quad words */
240
34.3k
    for (k = 0; k < 8; k++) {
241
30.5k
      ymm0[k * 2] = _mm256_unpacklo_epi64(ymm1[k], ymm1[k + 8]);
242
30.5k
      ymm0[k * 2 + 1] = _mm256_unpackhi_epi64(ymm1[k], ymm1[k + 8]);
243
30.5k
    }
244
64.9k
    for (k = 0; k < 16; k++) {
245
61.0k
      ymm0[k] = _mm256_permute4x64_epi64(ymm0[k], 0xd8);
246
61.0k
      ymm0[k] = _mm256_shuffle_epi8(ymm0[k], shmask);
247
61.0k
    }
248
    /* Store the result vectors */
249
3.81k
    uint8_t* const dest_for_jth_element = dest + j;
250
64.9k
    for (k = 0; k < 16; k++) {
251
61.0k
      _mm256_storeu_si256((__m256i*)(dest_for_jth_element + (k * total_elements)), ymm0[k]);
252
61.0k
    }
253
3.81k
  }
254
2.91k
}
255
256
/* Routine optimized for shuffling a buffer for a type size larger than 16 bytes. */
257
static void
258
shuffle16_tiled_avx2(uint8_t* const dest, const uint8_t* const src,
259
4.02k
                     const int32_t vectorizable_elements, const int32_t total_elements, const int32_t bytesoftype) {
260
4.02k
  int32_t j;
261
4.02k
  int k, l;
262
4.02k
  __m256i ymm0[16], ymm1[16];
263
264
4.02k
  const lldiv_t vecs_per_el = lldiv(bytesoftype, sizeof(__m128i));
265
4.02k
  const int32_t vecs_rem = (int32_t)vecs_per_el.rem;
266
267
  /* Create the shuffle mask.
268
     NOTE: The XMM/YMM 'set' intrinsics require the arguments to be ordered from
269
     most to least significant (i.e., their order is reversed when compared to
270
     loading the mask from an array). */
271
4.02k
  const __m256i shmask = _mm256_set_epi8(
272
4.02k
      0x0f, 0x07, 0x0e, 0x06, 0x0d, 0x05, 0x0c, 0x04,
273
4.02k
      0x0b, 0x03, 0x0a, 0x02, 0x09, 0x01, 0x08, 0x00,
274
4.02k
      0x0f, 0x07, 0x0e, 0x06, 0x0d, 0x05, 0x0c, 0x04,
275
4.02k
      0x0b, 0x03, 0x0a, 0x02, 0x09, 0x01, 0x08, 0x00);
276
277
16.9k
  for (j = 0; j < vectorizable_elements; j += sizeof(__m256i)) {
278
    /* Advance the offset into the type by the vector size (in bytes), unless this is
279
    the initial iteration and the type size is not a multiple of the vector size.
280
    In that case, only advance by the number of bytes necessary so that the number
281
    of remaining bytes in the type will be a multiple of the vector size. */
282
12.8k
    int32_t offset_into_type;
283
74.1k
    for (offset_into_type = 0; offset_into_type < bytesoftype;
284
61.3k
         offset_into_type += (offset_into_type == 0 && vecs_rem > 0 ? vecs_rem : (int32_t)sizeof(__m128i))) {
285
286
      /* Fetch elements in groups of 512 bytes */
287
61.3k
      const uint8_t* const src_with_offset = src + offset_into_type;
288
1.04M
      for (k = 0; k < 16; k++) {
289
980k
        ymm0[k] = _mm256_loadu2_m128i(
290
980k
            (__m128i*)(src_with_offset + (j + (2 * k) + 1) * bytesoftype),
291
980k
            (__m128i*)(src_with_offset + (j + (2 * k)) * bytesoftype));
292
980k
      }
293
      /* Transpose bytes */
294
551k
      for (k = 0, l = 0; k < 8; k++, l += 2) {
295
490k
        ymm1[k * 2] = _mm256_unpacklo_epi8(ymm0[l], ymm0[l + 1]);
296
490k
        ymm1[k * 2 + 1] = _mm256_unpackhi_epi8(ymm0[l], ymm0[l + 1]);
297
490k
      }
298
      /* Transpose words */
299
551k
      for (k = 0, l = -2; k < 8; k++, l++) {
300
490k
        if ((k % 2) == 0) l += 2;
301
490k
        ymm0[k * 2] = _mm256_unpacklo_epi16(ymm1[l], ymm1[l + 2]);
302
490k
        ymm0[k * 2 + 1] = _mm256_unpackhi_epi16(ymm1[l], ymm1[l + 2]);
303
490k
      }
304
      /* Transpose double words */
305
551k
      for (k = 0, l = -4; k < 8; k++, l++) {
306
490k
        if ((k % 4) == 0) l += 4;
307
490k
        ymm1[k * 2] = _mm256_unpacklo_epi32(ymm0[l], ymm0[l + 4]);
308
490k
        ymm1[k * 2 + 1] = _mm256_unpackhi_epi32(ymm0[l], ymm0[l + 4]);
309
490k
      }
310
      /* Transpose quad words */
311
551k
      for (k = 0; k < 8; k++) {
312
490k
        ymm0[k * 2] = _mm256_unpacklo_epi64(ymm1[k], ymm1[k + 8]);
313
490k
        ymm0[k * 2 + 1] = _mm256_unpackhi_epi64(ymm1[k], ymm1[k + 8]);
314
490k
      }
315
1.04M
      for (k = 0; k < 16; k++) {
316
980k
        ymm0[k] = _mm256_permute4x64_epi64(ymm0[k], 0xd8);
317
980k
        ymm0[k] = _mm256_shuffle_epi8(ymm0[k], shmask);
318
980k
      }
319
      /* Store the result vectors */
320
61.3k
      uint8_t* const dest_for_jth_element = dest + j;
321
1.04M
      for (k = 0; k < 16; k++) {
322
980k
        _mm256_storeu_si256((__m256i*)(dest_for_jth_element + (total_elements * (offset_into_type + k))), ymm0[k]);
323
980k
      }
324
61.3k
    }
325
12.8k
  }
326
4.02k
}
327
328
/* Routine optimized for unshuffling a buffer for a type size of 2 bytes. */
329
static void
330
unshuffle2_avx2(uint8_t* const dest, const uint8_t* const src,
331
13.4k
                const int32_t vectorizable_elements, const int32_t total_elements) {
332
13.4k
  static const int32_t bytesoftype = 2;
333
13.4k
  int32_t i;
334
13.4k
  int j;
335
13.4k
  __m256i ymm0[2], ymm1[2];
336
337
71.2M
  for (i = 0; i < vectorizable_elements; i += sizeof(__m256i)) {
338
    /* Load 32 elements (64 bytes) into 2 YMM registers. */
339
71.1M
    const uint8_t* const src_for_ith_element = src + i;
340
213M
    for (j = 0; j < 2; j++) {
341
142M
      ymm0[j] = _mm256_loadu_si256((__m256i*)(src_for_ith_element + (j * total_elements)));
342
142M
    }
343
    /* Shuffle bytes */
344
213M
    for (j = 0; j < 2; j++) {
345
142M
      ymm0[j] = _mm256_permute4x64_epi64(ymm0[j], 0xd8);
346
142M
    }
347
    /* Compute the low 64 bytes */
348
71.1M
    ymm1[0] = _mm256_unpacklo_epi8(ymm0[0], ymm0[1]);
349
    /* Compute the hi 64 bytes */
350
71.1M
    ymm1[1] = _mm256_unpackhi_epi8(ymm0[0], ymm0[1]);
351
    /* Store the result vectors in proper order */
352
71.1M
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (0 * sizeof(__m256i))), ymm1[0]);
353
71.1M
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (1 * sizeof(__m256i))), ymm1[1]);
354
71.1M
  }
355
13.4k
}
356
357
/* Routine optimized for unshuffling a buffer for a type size of 4 bytes. */
358
static void
359
unshuffle4_avx2(uint8_t* const dest, const uint8_t* const src,
360
5.64k
                const int32_t vectorizable_elements, const int32_t total_elements) {
361
5.64k
  static const int32_t bytesoftype = 4;
362
5.64k
  int32_t i;
363
5.64k
  int j;
364
5.64k
  __m256i ymm0[4], ymm1[4];
365
366
55.4M
  for (i = 0; i < vectorizable_elements; i += sizeof(__m256i)) {
367
    /* Load 32 elements (128 bytes) into 4 YMM registers. */
368
55.4M
    const uint8_t* const src_for_ith_element = src + i;
369
277M
    for (j = 0; j < 4; j++) {
370
221M
      ymm0[j] = _mm256_loadu_si256((__m256i*)(src_for_ith_element + (j * total_elements)));
371
221M
    }
372
    /* Shuffle bytes */
373
166M
    for (j = 0; j < 2; j++) {
374
      /* Compute the low 64 bytes */
375
110M
      ymm1[j] = _mm256_unpacklo_epi8(ymm0[j * 2], ymm0[j * 2 + 1]);
376
      /* Compute the hi 64 bytes */
377
110M
      ymm1[2 + j] = _mm256_unpackhi_epi8(ymm0[j * 2], ymm0[j * 2 + 1]);
378
110M
    }
379
    /* Shuffle 2-byte words */
380
166M
    for (j = 0; j < 2; j++) {
381
      /* Compute the low 64 bytes */
382
110M
      ymm0[j] = _mm256_unpacklo_epi16(ymm1[j * 2], ymm1[j * 2 + 1]);
383
      /* Compute the hi 64 bytes */
384
110M
      ymm0[2 + j] = _mm256_unpackhi_epi16(ymm1[j * 2], ymm1[j * 2 + 1]);
385
110M
    }
386
55.4M
    ymm1[0] = _mm256_permute2x128_si256(ymm0[0], ymm0[2], 0x20);
387
55.4M
    ymm1[1] = _mm256_permute2x128_si256(ymm0[1], ymm0[3], 0x20);
388
55.4M
    ymm1[2] = _mm256_permute2x128_si256(ymm0[0], ymm0[2], 0x31);
389
55.4M
    ymm1[3] = _mm256_permute2x128_si256(ymm0[1], ymm0[3], 0x31);
390
391
    /* Store the result vectors in proper order */
392
277M
    for (j = 0; j < 4; j++) {
393
221M
      _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (j * sizeof(__m256i))), ymm1[j]);
394
221M
    }
395
55.4M
  }
396
5.64k
}
397
398
/* Routine optimized for unshuffling a buffer for a type size of 8 bytes. */
399
static void
400
unshuffle8_avx2(uint8_t* const dest, const uint8_t* const src,
401
4.10M
                const int32_t vectorizable_elements, const int32_t total_elements) {
402
4.10M
  static const int32_t bytesoftype = 8;
403
4.10M
  int32_t i;
404
4.10M
  int j;
405
4.10M
  __m256i ymm0[8], ymm1[8];
406
407
155M
  for (i = 0; i < vectorizable_elements; i += sizeof(__m256i)) {
408
    /* Fetch 32 elements (256 bytes) into 8 YMM registers. */
409
151M
    const uint8_t* const src_for_ith_element = src + i;
410
1.36G
    for (j = 0; j < 8; j++) {
411
1.21G
      ymm0[j] = _mm256_loadu_si256((__m256i*)(src_for_ith_element + (j * total_elements)));
412
1.21G
    }
413
    /* Shuffle bytes */
414
756M
    for (j = 0; j < 4; j++) {
415
      /* Compute the low 32 bytes */
416
605M
      ymm1[j] = _mm256_unpacklo_epi8(ymm0[j * 2], ymm0[j * 2 + 1]);
417
      /* Compute the hi 32 bytes */
418
605M
      ymm1[4 + j] = _mm256_unpackhi_epi8(ymm0[j * 2], ymm0[j * 2 + 1]);
419
605M
    }
420
    /* Shuffle words */
421
756M
    for (j = 0; j < 4; j++) {
422
      /* Compute the low 32 bytes */
423
605M
      ymm0[j] = _mm256_unpacklo_epi16(ymm1[j * 2], ymm1[j * 2 + 1]);
424
      /* Compute the hi 32 bytes */
425
605M
      ymm0[4 + j] = _mm256_unpackhi_epi16(ymm1[j * 2], ymm1[j * 2 + 1]);
426
605M
    }
427
1.36G
    for (j = 0; j < 8; j++) {
428
1.21G
      ymm0[j] = _mm256_permute4x64_epi64(ymm0[j], 0xd8);
429
1.21G
    }
430
431
    /* Shuffle 4-byte dwords */
432
756M
    for (j = 0; j < 4; j++) {
433
      /* Compute the low 32 bytes */
434
605M
      ymm1[j] = _mm256_unpacklo_epi32(ymm0[j * 2], ymm0[j * 2 + 1]);
435
      /* Compute the hi 32 bytes */
436
605M
      ymm1[4 + j] = _mm256_unpackhi_epi32(ymm0[j * 2], ymm0[j * 2 + 1]);
437
605M
    }
438
439
    /* Store the result vectors in proper order */
440
151M
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (0 * sizeof(__m256i))), ymm1[0]);
441
151M
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (1 * sizeof(__m256i))), ymm1[2]);
442
151M
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (2 * sizeof(__m256i))), ymm1[1]);
443
151M
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (3 * sizeof(__m256i))), ymm1[3]);
444
151M
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (4 * sizeof(__m256i))), ymm1[4]);
445
151M
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (5 * sizeof(__m256i))), ymm1[6]);
446
151M
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (6 * sizeof(__m256i))), ymm1[5]);
447
151M
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (7 * sizeof(__m256i))), ymm1[7]);
448
151M
  }
449
4.10M
}
450
451
/* Routine optimized for unshuffling a buffer for a type size of 16 bytes. */
452
static void
453
unshuffle16_avx2(uint8_t* const dest, const uint8_t* const src,
454
1.57k
                 const int32_t vectorizable_elements, const int32_t total_elements) {
455
1.57k
  static const int32_t bytesoftype = 16;
456
1.57k
  int32_t i;
457
1.57k
  int j;
458
1.57k
  __m256i ymm0[16], ymm1[16];
459
460
3.33M
  for (i = 0; i < vectorizable_elements; i += sizeof(__m256i)) {
461
    /* Fetch 32 elements (512 bytes) into 16 YMM registers. */
462
3.33M
    const uint8_t* const src_for_ith_element = src + i;
463
56.6M
    for (j = 0; j < 16; j++) {
464
53.3M
      ymm0[j] = _mm256_loadu_si256((__m256i*)(src_for_ith_element + (j * total_elements)));
465
53.3M
    }
466
467
    /* Shuffle bytes */
468
29.9M
    for (j = 0; j < 8; j++) {
469
      /* Compute the low 32 bytes */
470
26.6M
      ymm1[j] = _mm256_unpacklo_epi8(ymm0[j * 2], ymm0[j * 2 + 1]);
471
      /* Compute the hi 32 bytes */
472
26.6M
      ymm1[8 + j] = _mm256_unpackhi_epi8(ymm0[j * 2], ymm0[j * 2 + 1]);
473
26.6M
    }
474
    /* Shuffle 2-byte words */
475
29.9M
    for (j = 0; j < 8; j++) {
476
      /* Compute the low 32 bytes */
477
26.6M
      ymm0[j] = _mm256_unpacklo_epi16(ymm1[j * 2], ymm1[j * 2 + 1]);
478
      /* Compute the hi 32 bytes */
479
26.6M
      ymm0[8 + j] = _mm256_unpackhi_epi16(ymm1[j * 2], ymm1[j * 2 + 1]);
480
26.6M
    }
481
    /* Shuffle 4-byte dwords */
482
29.9M
    for (j = 0; j < 8; j++) {
483
      /* Compute the low 32 bytes */
484
26.6M
      ymm1[j] = _mm256_unpacklo_epi32(ymm0[j * 2], ymm0[j * 2 + 1]);
485
      /* Compute the hi 32 bytes */
486
26.6M
      ymm1[8 + j] = _mm256_unpackhi_epi32(ymm0[j * 2], ymm0[j * 2 + 1]);
487
26.6M
    }
488
489
    /* Shuffle 8-byte qwords */
490
29.9M
    for (j = 0; j < 8; j++) {
491
      /* Compute the low 32 bytes */
492
26.6M
      ymm0[j] = _mm256_unpacklo_epi64(ymm1[j * 2], ymm1[j * 2 + 1]);
493
      /* Compute the hi 32 bytes */
494
26.6M
      ymm0[8 + j] = _mm256_unpackhi_epi64(ymm1[j * 2], ymm1[j * 2 + 1]);
495
26.6M
    }
496
497
29.9M
    for (j = 0; j < 8; j++) {
498
26.6M
      ymm1[j] = _mm256_permute2x128_si256(ymm0[j], ymm0[j + 8], 0x20);
499
26.6M
      ymm1[j + 8] = _mm256_permute2x128_si256(ymm0[j], ymm0[j + 8], 0x31);
500
26.6M
    }
501
502
    /* Store the result vectors in proper order */
503
3.33M
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (0 * sizeof(__m256i))), ymm1[0]);
504
3.33M
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (1 * sizeof(__m256i))), ymm1[4]);
505
3.33M
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (2 * sizeof(__m256i))), ymm1[2]);
506
3.33M
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (3 * sizeof(__m256i))), ymm1[6]);
507
3.33M
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (4 * sizeof(__m256i))), ymm1[1]);
508
3.33M
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (5 * sizeof(__m256i))), ymm1[5]);
509
3.33M
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (6 * sizeof(__m256i))), ymm1[3]);
510
3.33M
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (7 * sizeof(__m256i))), ymm1[7]);
511
3.33M
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (8 * sizeof(__m256i))), ymm1[8]);
512
3.33M
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (9 * sizeof(__m256i))), ymm1[12]);
513
3.33M
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (10 * sizeof(__m256i))), ymm1[10]);
514
3.33M
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (11 * sizeof(__m256i))), ymm1[14]);
515
3.33M
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (12 * sizeof(__m256i))), ymm1[9]);
516
3.33M
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (13 * sizeof(__m256i))), ymm1[13]);
517
3.33M
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (14 * sizeof(__m256i))), ymm1[11]);
518
3.33M
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (15 * sizeof(__m256i))), ymm1[15]);
519
3.33M
  }
520
1.57k
}
521
522
/* Routine optimized for unshuffling a buffer for a type size of 12 bytes.
523
 * Based off 16-byte implementation*/
524
static void
525
unshuffle12_avx2(uint8_t* const dest, const uint8_t* const src,
526
4.22k
    const int32_t vectorizable_elements, const int32_t total_elements) {
527
4.22k
  static const int32_t bytesoftype = 12;
528
4.22k
  int32_t i;
529
4.22k
  int j;
530
4.22k
  __m256i ymm0[16], ymm1[16];
531
532
4.22k
  __m256i permute = _mm256_set_epi32(0,0,6,5,4,2,1,0);
533
4.22k
  __m256i store_mask = _mm256_set_epi32(0x0, 0x0, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
534
4.22k
  int32_t jump = 2*bytesoftype;
535
25.9k
  for (i = 0; i < vectorizable_elements; i += sizeof(__m256i)) {
536
    /* Fetch 24 elements (384 bytes) into 12 YMM registers. */
537
21.7k
    const uint8_t* const src_for_ith_element = src + i;
538
282k
    for (j = 0; j < bytesoftype; j++) {
539
261k
      ymm0[j] = _mm256_loadu_si256((__m256i*)(src_for_ith_element + (j * total_elements)));
540
261k
    }
541
    /* Initialize the last 4 registers (128 bytes) to null */
542
108k
    for (j = bytesoftype; j < 16; j++) {
543
87.0k
      ymm0[j] = _mm256_setzero_si256();
544
87.0k
    }
545
546
    /* Shuffle bytes */
547
195k
    for (j = 0; j < 8; j++) {
548
      /* Compute the low 32 bytes */
549
174k
      ymm1[j] = _mm256_unpacklo_epi8(ymm0[j * 2], ymm0[j * 2 + 1]);
550
      /* Compute the hi 32 bytes */
551
174k
      ymm1[8 + j] = _mm256_unpackhi_epi8(ymm0[j * 2], ymm0[j * 2 + 1]);
552
174k
    }
553
    /* Shuffle 2-byte words */
554
195k
    for (j = 0; j < 8; j++) {
555
      /* Compute the low 32 bytes */
556
174k
      ymm0[j] = _mm256_unpacklo_epi16(ymm1[j * 2], ymm1[j * 2 + 1]);
557
      /* Compute the hi 32 bytes */
558
174k
      ymm0[8 + j] = _mm256_unpackhi_epi16(ymm1[j * 2], ymm1[j * 2 + 1]);
559
174k
    }
560
    /* Shuffle 4-byte dwords */
561
195k
    for (j = 0; j < 8; j++) {
562
      /* Compute the low 32 bytes */
563
174k
      ymm1[j] = _mm256_unpacklo_epi32(ymm0[j * 2], ymm0[j * 2 + 1]);
564
      /* Compute the hi 32 bytes */
565
174k
      ymm1[8 + j] = _mm256_unpackhi_epi32(ymm0[j * 2], ymm0[j * 2 + 1]);
566
174k
    }
567
568
    /* Shuffle 8-byte qwords */
569
195k
    for (j = 0; j < 8; j++) {
570
      /* Compute the low 32 bytes */
571
174k
      ymm0[j] = _mm256_unpacklo_epi64(ymm1[j * 2], ymm1[j * 2 + 1]);
572
      /* Compute the hi 32 bytes */
573
174k
      ymm0[8 + j] = _mm256_unpackhi_epi64(ymm1[j * 2], ymm1[j * 2 + 1]);
574
174k
    }
575
576
577
195k
    for (j = 0; j < 8; j++) {
578
174k
      ymm1[j] = _mm256_permute2x128_si256(ymm0[j], ymm0[j + 8], 0x20);
579
174k
      ymm1[j + 8] = _mm256_permute2x128_si256(ymm0[j], ymm0[j + 8], 0x31);
580
174k
      ymm1[j] = _mm256_permutevar8x32_epi32(ymm1[j], permute);
581
174k
      ymm1[j+8] = _mm256_permutevar8x32_epi32(ymm1[j+8], permute);
582
583
584
174k
    }
585
21.7k
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (0 * jump)), ymm1[0]);
586
21.7k
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (1 * jump)), ymm1[4]);
587
21.7k
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (2 * jump)), ymm1[2]);
588
21.7k
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (3 * jump)), ymm1[6]);
589
21.7k
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (4 * jump)), ymm1[1]);
590
21.7k
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (5 * jump)), ymm1[5]);
591
21.7k
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (6 * jump)), ymm1[3]);
592
21.7k
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (7 * jump)), ymm1[7]);
593
21.7k
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (8 * jump)), ymm1[8]);
594
21.7k
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (9 * jump)), ymm1[12]);
595
21.7k
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (10 * jump)), ymm1[10]);
596
21.7k
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (11 * jump)), ymm1[14]);
597
21.7k
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (12 * jump)), ymm1[9]);
598
21.7k
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (13 * jump)), ymm1[13]);
599
21.7k
    _mm256_storeu_si256((__m256i*)(dest + (i * bytesoftype) + (14 * jump)), ymm1[11]);
600
21.7k
    _mm256_maskstore_epi32((int *)(dest + (i * bytesoftype) + (15 * jump)), store_mask, ymm1[15]);
601
21.7k
  }
602
4.22k
}
603
/* Routine optimized for unshuffling a buffer for a type size larger than 16 bytes. */
604
static void
605
unshuffle16_tiled_avx2(const uint8_t *dest, const uint8_t* const src,
606
68.3k
                       const int32_t vectorizable_elements, const int32_t total_elements, const int32_t bytesoftype) {
607
68.3k
  int32_t i;
608
68.3k
  int j;
609
68.3k
  __m256i ymm0[16], ymm1[16];
610
611
68.3k
  const lldiv_t vecs_per_el = lldiv(bytesoftype, sizeof(__m128i));
612
68.3k
  const int32_t vecs_rem = (int32_t)vecs_per_el.rem;
613
614
  /* The unshuffle loops are inverted (compared to shuffle_tiled16_avx2)
615
     to optimize cache utilization. */
616
68.3k
  int32_t offset_into_type;
617
680k
  for (offset_into_type = 0; offset_into_type < bytesoftype;
618
611k
       offset_into_type += (offset_into_type == 0 && vecs_rem > 0 ? vecs_rem : (int32_t)sizeof(__m128i))) {
619
44.9M
    for (i = 0; i < vectorizable_elements; i += sizeof(__m256i)) {
620
      /* Load the first 16 bytes of 32 adjacent elements (512 bytes) into 16 YMM registers */
621
44.3M
      const uint8_t* const src_for_ith_element = src + i;
622
754M
      for (j = 0; j < 16; j++) {
623
709M
        ymm0[j] = _mm256_loadu_si256((__m256i*)(src_for_ith_element + (total_elements * (offset_into_type + j))));
624
709M
      }
625
626
      /* Shuffle bytes */
627
399M
      for (j = 0; j < 8; j++) {
628
        /* Compute the low 32 bytes */
629
354M
        ymm1[j] = _mm256_unpacklo_epi8(ymm0[j * 2], ymm0[j * 2 + 1]);
630
        /* Compute the hi 32 bytes */
631
354M
        ymm1[8 + j] = _mm256_unpackhi_epi8(ymm0[j * 2], ymm0[j * 2 + 1]);
632
354M
      }
633
      /* Shuffle 2-byte words */
634
399M
      for (j = 0; j < 8; j++) {
635
        /* Compute the low 32 bytes */
636
354M
        ymm0[j] = _mm256_unpacklo_epi16(ymm1[j * 2], ymm1[j * 2 + 1]);
637
        /* Compute the hi 32 bytes */
638
354M
        ymm0[8 + j] = _mm256_unpackhi_epi16(ymm1[j * 2], ymm1[j * 2 + 1]);
639
354M
      }
640
      /* Shuffle 4-byte dwords */
641
399M
      for (j = 0; j < 8; j++) {
642
        /* Compute the low 32 bytes */
643
354M
        ymm1[j] = _mm256_unpacklo_epi32(ymm0[j * 2], ymm0[j * 2 + 1]);
644
        /* Compute the hi 32 bytes */
645
354M
        ymm1[8 + j] = _mm256_unpackhi_epi32(ymm0[j * 2], ymm0[j * 2 + 1]);
646
354M
      }
647
648
      /* Shuffle 8-byte qwords */
649
399M
      for (j = 0; j < 8; j++) {
650
        /* Compute the low 32 bytes */
651
354M
        ymm0[j] = _mm256_unpacklo_epi64(ymm1[j * 2], ymm1[j * 2 + 1]);
652
        /* Compute the hi 32 bytes */
653
354M
        ymm0[8 + j] = _mm256_unpackhi_epi64(ymm1[j * 2], ymm1[j * 2 + 1]);
654
354M
      }
655
656
399M
      for (j = 0; j < 8; j++) {
657
354M
        ymm1[j] = _mm256_permute2x128_si256(ymm0[j], ymm0[j + 8], 0x20);
658
354M
        ymm1[j + 8] = _mm256_permute2x128_si256(ymm0[j], ymm0[j + 8], 0x31);
659
354M
      }
660
661
      /* Store the result vectors in proper order */
662
44.3M
      const uint8_t* const dest_with_offset = dest + offset_into_type;
663
44.3M
      _mm256_storeu2_m128i(
664
44.3M
          (__m128i*)(dest_with_offset + (i + 0x01) * bytesoftype),
665
44.3M
          (__m128i*)(dest_with_offset + (i + 0x00) * bytesoftype), ymm1[0]);
666
44.3M
      _mm256_storeu2_m128i(
667
44.3M
          (__m128i*)(dest_with_offset + (i + 0x03) * bytesoftype),
668
44.3M
          (__m128i*)(dest_with_offset + (i + 0x02) * bytesoftype), ymm1[4]);
669
44.3M
      _mm256_storeu2_m128i(
670
44.3M
          (__m128i*)(dest_with_offset + (i + 0x05) * bytesoftype),
671
44.3M
          (__m128i*)(dest_with_offset + (i + 0x04) * bytesoftype), ymm1[2]);
672
44.3M
      _mm256_storeu2_m128i(
673
44.3M
          (__m128i*)(dest_with_offset + (i + 0x07) * bytesoftype),
674
44.3M
          (__m128i*)(dest_with_offset + (i + 0x06) * bytesoftype), ymm1[6]);
675
44.3M
      _mm256_storeu2_m128i(
676
44.3M
          (__m128i*)(dest_with_offset + (i + 0x09) * bytesoftype),
677
44.3M
          (__m128i*)(dest_with_offset + (i + 0x08) * bytesoftype), ymm1[1]);
678
44.3M
      _mm256_storeu2_m128i(
679
44.3M
          (__m128i*)(dest_with_offset + (i + 0x0b) * bytesoftype),
680
44.3M
          (__m128i*)(dest_with_offset + (i + 0x0a) * bytesoftype), ymm1[5]);
681
44.3M
      _mm256_storeu2_m128i(
682
44.3M
          (__m128i*)(dest_with_offset + (i + 0x0d) * bytesoftype),
683
44.3M
          (__m128i*)(dest_with_offset + (i + 0x0c) * bytesoftype), ymm1[3]);
684
44.3M
      _mm256_storeu2_m128i(
685
44.3M
          (__m128i*)(dest_with_offset + (i + 0x0f) * bytesoftype),
686
44.3M
          (__m128i*)(dest_with_offset + (i + 0x0e) * bytesoftype), ymm1[7]);
687
44.3M
      _mm256_storeu2_m128i(
688
44.3M
          (__m128i*)(dest_with_offset + (i + 0x11) * bytesoftype),
689
44.3M
          (__m128i*)(dest_with_offset + (i + 0x10) * bytesoftype), ymm1[8]);
690
44.3M
      _mm256_storeu2_m128i(
691
44.3M
          (__m128i*)(dest_with_offset + (i + 0x13) * bytesoftype),
692
44.3M
          (__m128i*)(dest_with_offset + (i + 0x12) * bytesoftype), ymm1[12]);
693
44.3M
      _mm256_storeu2_m128i(
694
44.3M
          (__m128i*)(dest_with_offset + (i + 0x15) * bytesoftype),
695
44.3M
          (__m128i*)(dest_with_offset + (i + 0x14) * bytesoftype), ymm1[10]);
696
44.3M
      _mm256_storeu2_m128i(
697
44.3M
          (__m128i*)(dest_with_offset + (i + 0x17) * bytesoftype),
698
44.3M
          (__m128i*)(dest_with_offset + (i + 0x16) * bytesoftype), ymm1[14]);
699
44.3M
      _mm256_storeu2_m128i(
700
44.3M
          (__m128i*)(dest_with_offset + (i + 0x19) * bytesoftype),
701
44.3M
          (__m128i*)(dest_with_offset + (i + 0x18) * bytesoftype), ymm1[9]);
702
44.3M
      _mm256_storeu2_m128i(
703
44.3M
          (__m128i*)(dest_with_offset + (i + 0x1b) * bytesoftype),
704
44.3M
          (__m128i*)(dest_with_offset + (i + 0x1a) * bytesoftype), ymm1[13]);
705
44.3M
      _mm256_storeu2_m128i(
706
44.3M
          (__m128i*)(dest_with_offset + (i + 0x1d) * bytesoftype),
707
44.3M
          (__m128i*)(dest_with_offset + (i + 0x1c) * bytesoftype), ymm1[11]);
708
44.3M
      _mm256_storeu2_m128i(
709
44.3M
          (__m128i*)(dest_with_offset + (i + 0x1f) * bytesoftype),
710
44.3M
          (__m128i*)(dest_with_offset + (i + 0x1e) * bytesoftype), ymm1[15]);
711
44.3M
    }
712
611k
  }
713
68.3k
}
714
715
/* Shuffle a block.  This can never fail. */
716
void
717
shuffle_avx2(const int32_t bytesoftype, const int32_t blocksize,
718
2.97M
             const uint8_t *_src, uint8_t *_dest) {
719
2.97M
  const int32_t vectorized_chunk_size = bytesoftype * (int32_t)sizeof(__m256i);
720
721
  /* If the block size is too small to be vectorized,
722
     use the generic implementation. */
723
2.97M
  if (blocksize < vectorized_chunk_size) {
724
958k
    shuffle_generic(bytesoftype, blocksize, _src, _dest);
725
958k
    return;
726
958k
  }
727
728
  /* If the blocksize is not a multiple of both the typesize and
729
     the vector size, round the blocksize down to the next value
730
     which is a multiple of both. The vectorized shuffle can be
731
     used for that portion of the data, and the naive implementation
732
     can be used for the remaining portion. */
733
2.01M
  const int32_t vectorizable_bytes = blocksize - (blocksize % vectorized_chunk_size);
734
735
2.01M
  const int32_t vectorizable_elements = vectorizable_bytes / bytesoftype;
736
2.01M
  const int32_t total_elements = blocksize / bytesoftype;
737
738
  /* Optimized shuffle implementations */
739
2.01M
  switch (bytesoftype) {
740
1.69k
    case 2:
741
1.69k
      shuffle2_avx2(_dest, _src, vectorizable_elements, total_elements);
742
1.69k
      break;
743
6.85k
    case 4:
744
6.85k
      shuffle4_avx2(_dest, _src, vectorizable_elements, total_elements);
745
6.85k
      break;
746
1.98M
    case 8:
747
1.98M
      shuffle8_avx2(_dest, _src, vectorizable_elements, total_elements);
748
1.98M
      break;
749
2.91k
    case 16:
750
2.91k
      shuffle16_avx2(_dest, _src, vectorizable_elements, total_elements);
751
2.91k
      break;
752
15.7k
    default:
753
      /* For types larger than 16 bytes, use the AVX2 tiled shuffle. */
754
15.7k
      if (bytesoftype > (int32_t)sizeof(__m128i)) {
755
4.02k
        shuffle16_tiled_avx2(_dest, _src, vectorizable_elements, total_elements, bytesoftype);
756
4.02k
      }
757
11.7k
      else {
758
        /* Non-optimized shuffle */
759
11.7k
        shuffle_generic(bytesoftype, blocksize, _src, _dest);
760
        /* The non-optimized function covers the whole buffer,
761
           so we're done processing here. */
762
11.7k
        return;
763
11.7k
      }
764
2.01M
  }
765
766
  /* If the buffer had any bytes at the end which couldn't be handled
767
     by the vectorized implementations, use the non-optimized version
768
     to finish them up. */
769
2.00M
  if (vectorizable_bytes < blocksize) {
770
1.02M
    shuffle_generic_inline(bytesoftype, vectorizable_bytes, blocksize, _src, _dest);
771
1.02M
  }
772
2.00M
}
773
774
/* Unshuffle a block.  This can never fail. */
775
void
776
unshuffle_avx2(const int32_t bytesoftype, const int32_t blocksize,
777
4.51M
               const uint8_t *_src, uint8_t *_dest) {
778
4.51M
  const int32_t vectorized_chunk_size = bytesoftype * (int32_t)sizeof(__m256i);
779
780
  /* If the block size is too small to be vectorized,
781
     use the generic implementation. */
782
4.51M
  if (blocksize < vectorized_chunk_size) {
783
281k
    unshuffle_generic(bytesoftype, blocksize, _src, _dest);
784
281k
    return;
785
281k
  }
786
787
  /* If the blocksize is not a multiple of both the typesize and
788
     the vector size, round the blocksize down to the next value
789
     which is a multiple of both. The vectorized unshuffle can be
790
     used for that portion of the data, and the naive implementation
791
     can be used for the remaining portion. */
792
4.23M
  const int32_t vectorizable_bytes = blocksize - (blocksize % vectorized_chunk_size);
793
794
4.23M
  const int32_t vectorizable_elements = vectorizable_bytes / bytesoftype;
795
4.23M
  const int32_t total_elements = blocksize / bytesoftype;
796
797
  /* Optimized unshuffle implementations */
798
4.23M
  switch (bytesoftype) {
799
13.4k
    case 2:
800
13.4k
      unshuffle2_avx2(_dest, _src, vectorizable_elements, total_elements);
801
13.4k
      break;
802
5.64k
    case 4:
803
5.64k
      unshuffle4_avx2(_dest, _src, vectorizable_elements, total_elements);
804
5.64k
      break;
805
4.10M
    case 8:
806
4.10M
      unshuffle8_avx2(_dest, _src, vectorizable_elements, total_elements);
807
4.10M
      break;
808
4.22k
    case 12:
809
4.22k
      unshuffle12_avx2(_dest, _src, vectorizable_elements, total_elements);
810
4.22k
      break;
811
1.57k
    case 16:
812
1.57k
      unshuffle16_avx2(_dest, _src, vectorizable_elements, total_elements);
813
1.57k
      break;
814
98.4k
    default:
815
      /* For types larger than 16 bytes, use the AVX2 tiled unshuffle. */
816
98.4k
      if (bytesoftype > (int32_t)sizeof(__m128i)) {
817
68.3k
        unshuffle16_tiled_avx2(_dest, _src, vectorizable_elements, total_elements, bytesoftype);
818
68.3k
      }
819
30.0k
      else {
820
        /* Non-optimized unshuffle */
821
30.0k
        unshuffle_generic(bytesoftype, blocksize, _src, _dest);
822
        /* The non-optimized function covers the whole buffer,
823
           so we're done processing here. */
824
30.0k
        return;
825
30.0k
      }
826
4.23M
  }
827
828
  /* If the buffer had any bytes at the end which couldn't be handled
829
     by the vectorized implementations, use the non-optimized version
830
     to finish them up. */
831
4.20M
  if (vectorizable_bytes < blocksize) {
832
3.37M
    unshuffle_generic_inline(bytesoftype, vectorizable_bytes, blocksize, _src, _dest);
833
3.37M
  }
834
4.20M
}
835
836
const bool is_shuffle_avx2 = true;
837
838
#else
839
840
const bool is_shuffle_avx2 = false;
841
842
void shuffle_avx2(const int32_t bytesoftype, const int32_t blocksize,
843
                  const uint8_t *_src, uint8_t *_dest) {
844
  abort();
845
}
846
847
void unshuffle_avx2(const int32_t bytesoftype, const int32_t blocksize,
848
                    const uint8_t *_src, uint8_t *_dest) {
849
  abort();
850
}
851
852
#endif /* defined(__AVX2__) */