Debian Codegraph @

src/c-blosc2/blosc/shuffle-avx2.c

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