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-bytedelta-avx2.c
Line
Count
Source
1
/*********************************************************************
2
  Blosc - Blocked Shuffling and Compression Library
3
  Copyright (c) 2026 Blosc Development Team <blosc@blosc.org>
4
  License: BSD 3-Clause (see LICENSE.txt)
5
**********************************************************************/
6
7
/* This translation unit alone is compiled for AVX2. The baseline shuffle
8
 * dispatcher must check CPUID/OSXSAVE before calling either entry point. */
9
#include "../plugins/filters/bytedelta/bytedelta.h"
10
11
#if defined(__AVX2__)
12
13
#include <immintrin.h>
14
#include <string.h>
15
16
/* Forward SHUFFLE + BYTEDELTA:
17
 * Per-byte delta commutes with byte-plane transposition because subtraction is
18
 * modulo 256 per byte lane:
19
 *   delta(shuffle(e))[p, i] = e[i].b[p] - e[i-1].b[p] = shuffle(e -_byte prev(e))[p, i].
20
 * Computing the byte difference before transposing uses a single unaligned load
21
 * at (src - N) and a port-0/1 vpsubb, eliminating all cross-tile carry state,
22
 * vpalignr/vinserti128 shuffles, and register spills. */
23
24
/* Transpose 32 two-byte elements (64B) into 2 contiguous 32-byte planes and store */
25
0
static inline void transpose_store_2(uint8_t *output, int count, int i, __m256i y0, __m256i y1) {
26
0
  const __m256i mask = _mm256_set1_epi16(0x00ff);
27
0
  __m256i p0 = _mm256_permute4x64_epi64(
28
0
      _mm256_packus_epi16(_mm256_and_si256(y0, mask), _mm256_and_si256(y1, mask)), 0xd8);
29
0
  __m256i p1 = _mm256_permute4x64_epi64(
30
0
      _mm256_packus_epi16(_mm256_srli_epi16(y0, 8), _mm256_srli_epi16(y1, 8)), 0xd8);
31
0
  _mm256_storeu_si256((__m256i *)(output + i), p0);
32
0
  _mm256_storeu_si256((__m256i *)(output + count + i), p1);
33
0
}
34
35
/* Transpose 32 four-byte elements (128B) into 4 contiguous 32-byte planes and store */
36
0
static inline void transpose_store_4(uint8_t *output, int count, int i, const __m256i d[4]) {
37
0
  const __m256i mask = _mm256_setr_epi8(
38
0
      0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15,
39
0
      0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15);
40
0
  __m256i a[4], b[4];
41
  /* Place elements 0..15 in lane 0 and 16..31 in lane 1 so the transpose is purely in-lane. */
42
0
  a[0] = _mm256_shuffle_epi8(_mm256_permute2x128_si256(d[0], d[2], 0x20), mask);
43
0
  a[1] = _mm256_shuffle_epi8(_mm256_permute2x128_si256(d[0], d[2], 0x31), mask);
44
0
  a[2] = _mm256_shuffle_epi8(_mm256_permute2x128_si256(d[1], d[3], 0x20), mask);
45
0
  a[3] = _mm256_shuffle_epi8(_mm256_permute2x128_si256(d[1], d[3], 0x31), mask);
46
0
  b[0] = _mm256_unpacklo_epi32(a[0], a[1]);
47
0
  b[1] = _mm256_unpackhi_epi32(a[0], a[1]);
48
0
  b[2] = _mm256_unpacklo_epi32(a[2], a[3]);
49
0
  b[3] = _mm256_unpackhi_epi32(a[2], a[3]);
50
0
  _mm256_storeu_si256((__m256i *)(output + 0 * count + i), _mm256_unpacklo_epi64(b[0], b[2]));
51
0
  _mm256_storeu_si256((__m256i *)(output + 1 * count + i), _mm256_unpackhi_epi64(b[0], b[2]));
52
0
  _mm256_storeu_si256((__m256i *)(output + 2 * count + i), _mm256_unpacklo_epi64(b[1], b[3]));
53
0
  _mm256_storeu_si256((__m256i *)(output + 3 * count + i), _mm256_unpackhi_epi64(b[1], b[3]));
54
0
}
55
56
/* Transpose 32 eight-byte elements (256B) into 8 contiguous 32-byte planes and store */
57
0
static inline void transpose_store_8(uint8_t *output, int count, int i, const __m256i d[8]) {
58
0
  const __m256i mask = _mm256_setr_epi8(
59
0
      0, 8, 1, 9, 2, 10, 3, 11, 4, 12, 5, 13, 6, 14, 7, 15,
60
0
      0, 8, 1, 9, 2, 10, 3, 11, 4, 12, 5, 13, 6, 14, 7, 15);
61
0
  __m256i a[8], b[8];
62
  /* Pair d[p] and d[p+4] so elements 0..15 land in lane 0 and 16..31 in lane 1 */
63
0
  for (int p = 0; p < 4; p++) {
64
0
    b[p * 2] = _mm256_shuffle_epi8(_mm256_permute2x128_si256(d[p], d[p + 4], 0x20), mask);
65
0
    b[p * 2 + 1] = _mm256_shuffle_epi8(_mm256_permute2x128_si256(d[p], d[p + 4], 0x31), mask);
66
0
  }
67
  /* In-lane 16-bit, 32-bit, and 64-bit unpack stages complete the 8x16 transpose */
68
0
  for (int p = 0; p < 4; p++) {
69
0
    a[p * 2] = _mm256_unpacklo_epi16(b[p * 2], b[p * 2 + 1]);
70
0
    a[p * 2 + 1] = _mm256_unpackhi_epi16(b[p * 2], b[p * 2 + 1]);
71
0
  }
72
0
  for (int p = 0; p < 4; p++) {
73
0
    int k = p < 2 ? p : p + 2;
74
0
    b[p * 2] = _mm256_unpacklo_epi32(a[k], a[k + 2]);
75
0
    b[p * 2 + 1] = _mm256_unpackhi_epi32(a[k], a[k + 2]);
76
0
  }
77
0
  for (int p = 0; p < 4; p++) {
78
0
    _mm256_storeu_si256((__m256i *)(output + (p * 2) * count + i),
79
0
                        _mm256_unpacklo_epi64(b[p], b[p + 4]));
80
0
    _mm256_storeu_si256((__m256i *)(output + (p * 2 + 1) * count + i),
81
0
                        _mm256_unpackhi_epi64(b[p], b[p + 4]));
82
0
  }
83
0
}
84
85
/* Record-parallel byte prefix sum helpers for backward SHUFFLE + BYTEDELTA.
86
 * Transposing into interleaved records first lets us run a tree prefix sum
87
 * across complete records in parallel, eliminating per-plane 32-byte Kogge-Stone
88
 * loops and reducing carry state from N registers (which spilled on typesize=8)
89
 * down to a single 128-bit register. */
90
165k
static inline __m256i dup_hi64_256(__m256i v) {
91
  /* Broadcast the upper 64-bit record of each 128-bit lane across that lane. */
92
165k
  return _mm256_unpackhi_epi64(v, v);
93
165k
}
94
95
5.85M
static inline __m256i dup_hi32_256(__m256i v) {
96
  /* Broadcast the top 32-bit record of each 128-bit lane across that lane. */
97
5.85M
  return _mm256_shuffle_epi32(v, 0xff);
98
5.85M
}
99
100
0
static int forward2_avx2(const uint8_t *input, uint8_t *output, int32_t length) {
101
0
  int count = length / 2, vector_count = count / 32 * 32;
102
0
  if (vector_count >= 32) {
103
    /* First tile: zero-pad the missing predecessor of element 0 without reading before input. */
104
0
    __m256i y0 = _mm256_loadu_si256((const __m256i *)input);
105
0
    __m128i lo_prev = _mm_slli_si128(_mm256_castsi256_si128(y0), 2);
106
0
    __m128i hi_prev = _mm_loadu_si128((const __m128i *)(input + 14));
107
0
    __m256i d0 = _mm256_sub_epi8(
108
0
        y0, _mm256_inserti128_si256(_mm256_castsi128_si256(lo_prev), hi_prev, 1));
109
0
    __m256i d1 = _mm256_sub_epi8(
110
0
        _mm256_loadu_si256((const __m256i *)(input + 32)),
111
0
        _mm256_loadu_si256((const __m256i *)(input + 30)));
112
0
    transpose_store_2(output, count, 0, d0, d1);
113
0
    for (int i = 32; i < vector_count; i += 32) {
114
0
      const uint8_t *src = input + i * 2;
115
0
      d0 = _mm256_sub_epi8(_mm256_loadu_si256((const __m256i *)src),
116
0
                           _mm256_loadu_si256((const __m256i *)(src - 2)));
117
0
      d1 = _mm256_sub_epi8(_mm256_loadu_si256((const __m256i *)(src + 32)),
118
0
                           _mm256_loadu_si256((const __m256i *)(src + 30)));
119
0
      transpose_store_2(output, count, i, d0, d1);
120
0
    }
121
0
  }
122
0
  for (int p = 0; p < 2; p++) {
123
0
    uint8_t carry = vector_count > 0 ? input[(vector_count - 1) * 2 + p] : 0;
124
0
    for (int i = vector_count; i < count; i++) {
125
0
      uint8_t value = input[i * 2 + p];
126
0
      output[p * count + i] = (uint8_t)(value - carry);
127
0
      carry = value;
128
0
    }
129
0
  }
130
0
  memcpy(output + count * 2, input + count * 2, (size_t)(length % 2));
131
0
  return 1;
132
0
}
133
134
0
static int forward4_avx2(const uint8_t *input, uint8_t *output, int32_t length) {
135
0
  int count = length / 4, vector_count = count / 32 * 32;
136
0
  if (vector_count >= 32) {
137
0
    __m256i d[4];
138
0
    __m256i y0 = _mm256_loadu_si256((const __m256i *)input);
139
0
    __m128i lo_prev = _mm_slli_si128(_mm256_castsi256_si128(y0), 4);
140
0
    __m128i hi_prev = _mm_loadu_si128((const __m128i *)(input + 12));
141
0
    d[0] = _mm256_sub_epi8(
142
0
        y0, _mm256_inserti128_si256(_mm256_castsi128_si256(lo_prev), hi_prev, 1));
143
0
    for (int k = 1; k < 4; k++) {
144
0
      d[k] = _mm256_sub_epi8(_mm256_loadu_si256((const __m256i *)(input + k * 32)),
145
0
                             _mm256_loadu_si256((const __m256i *)(input + k * 32 - 4)));
146
0
    }
147
0
    transpose_store_4(output, count, 0, d);
148
0
    for (int i = 32; i < vector_count; i += 32) {
149
0
      const uint8_t *src = input + i * 4;
150
0
      for (int k = 0; k < 4; k++) {
151
0
        d[k] = _mm256_sub_epi8(_mm256_loadu_si256((const __m256i *)(src + k * 32)),
152
0
                               _mm256_loadu_si256((const __m256i *)(src + k * 32 - 4)));
153
0
      }
154
0
      transpose_store_4(output, count, i, d);
155
0
    }
156
0
  }
157
0
  for (int p = 0; p < 4; p++) {
158
0
    uint8_t carry = vector_count > 0 ? input[(vector_count - 1) * 4 + p] : 0;
159
0
    for (int i = vector_count; i < count; i++) {
160
0
      uint8_t value = input[i * 4 + p];
161
0
      output[p * count + i] = (uint8_t)(value - carry);
162
0
      carry = value;
163
0
    }
164
0
  }
165
0
  memcpy(output + count * 4, input + count * 4, (size_t)(length % 4));
166
0
  return 1;
167
0
}
168
169
0
static int forward8_avx2(const uint8_t *input, uint8_t *output, int32_t length) {
170
0
  int count = length / 8, vector_count = count / 32 * 32;
171
0
  if (vector_count >= 32) {
172
0
    __m256i d[8];
173
0
    __m256i y0 = _mm256_loadu_si256((const __m256i *)input);
174
0
    __m128i lo_prev = _mm_slli_si128(_mm256_castsi256_si128(y0), 8);
175
0
    __m128i hi_prev = _mm_loadu_si128((const __m128i *)(input + 8));
176
0
    d[0] = _mm256_sub_epi8(
177
0
        y0, _mm256_inserti128_si256(_mm256_castsi128_si256(lo_prev), hi_prev, 1));
178
0
    for (int k = 1; k < 8; k++) {
179
0
      d[k] = _mm256_sub_epi8(_mm256_loadu_si256((const __m256i *)(input + k * 32)),
180
0
                             _mm256_loadu_si256((const __m256i *)(input + k * 32 - 8)));
181
0
    }
182
0
    transpose_store_8(output, count, 0, d);
183
0
    for (int i = 32; i < vector_count; i += 32) {
184
0
      const uint8_t *src = input + i * 8;
185
0
      for (int k = 0; k < 8; k++) {
186
0
        d[k] = _mm256_sub_epi8(_mm256_loadu_si256((const __m256i *)(src + k * 32)),
187
0
                               _mm256_loadu_si256((const __m256i *)(src + k * 32 - 8)));
188
0
      }
189
0
      transpose_store_8(output, count, i, d);
190
0
    }
191
0
  }
192
0
  for (int p = 0; p < 8; p++) {
193
0
    uint8_t carry = vector_count > 0 ? input[(vector_count - 1) * 8 + p] : 0;
194
0
    for (int i = vector_count; i < count; i++) {
195
0
      uint8_t value = input[i * 8 + p];
196
0
      output[p * count + i] = (uint8_t)(value - carry);
197
0
      carry = value;
198
0
    }
199
0
  }
200
0
  memcpy(output + count * 8, input + count * 8, (size_t)(length % 8));
201
0
  return 1;
202
0
}
203
204
12.3k
static int backward2_avx2(const uint8_t *input, uint8_t *output, int32_t length) {
205
12.3k
  int count = length / 2, vector_count = count / 32 * 32;
206
  /* Shuffle mask broadcasting the 8th 16-bit record (bytes 14..15) in each 128-bit lane. */
207
12.3k
  const __m256i mask_hi16 = _mm256_setr_epi8(
208
12.3k
      14, 15, 14, 15, 14, 15, 14, 15, 14, 15, 14, 15, 14, 15, 14, 15,
209
12.3k
      14, 15, 14, 15, 14, 15, 14, 15, 14, 15, 14, 15, 14, 15, 14, 15);
210
12.3k
  const __m128i mask_hi16_128 = _mm256_castsi256_si128(mask_hi16);
211
12.3k
  __m128i carry128 = _mm_setzero_si128();
212
1.60M
  for (int i = 0; i < vector_count; i += 32) {
213
    /* Load 32 delta bytes from each of the 2 byte planes. */
214
1.59M
    __m256i p0 = _mm256_loadu_si256((const __m256i *)(input + i));
215
1.59M
    __m256i p1 = _mm256_loadu_si256((const __m256i *)(input + count + i));
216
217
    /* Interleave into 2-byte records: lo lanes hold 0..7 and 8..15, hi lanes 16..23 and 24..31. */
218
1.59M
    __m256i r0 = _mm256_unpacklo_epi8(p0, p1);
219
1.59M
    __m256i r1 = _mm256_unpackhi_epi8(p0, p1);
220
221
    /* Kogge-Stone prefix sum across the 8 interleaved 2-byte records in each 128-bit lane. */
222
1.59M
    r0 = _mm256_add_epi8(r0, _mm256_slli_si256(r0, 2));
223
1.59M
    r1 = _mm256_add_epi8(r1, _mm256_slli_si256(r1, 2));
224
1.59M
    r0 = _mm256_add_epi8(r0, _mm256_slli_si256(r0, 4));
225
1.59M
    r1 = _mm256_add_epi8(r1, _mm256_slli_si256(r1, 4));
226
1.59M
    r0 = _mm256_add_epi8(r0, _mm256_slli_si256(r0, 8));
227
1.59M
    r1 = _mm256_add_epi8(r1, _mm256_slli_si256(r1, 8));
228
229
    /* Propagate each 8-record half-lane total from r0 into r1. */
230
1.59M
    r1 = _mm256_add_epi8(r1, _mm256_shuffle_epi8(r0, mask_hi16));
231
232
    /* Build per-lane tile carry [carry, carry + sum_lo] and advance running carry128. */
233
1.59M
    __m128i sum_lo = _mm_shuffle_epi8(_mm256_castsi256_si128(r1), mask_hi16_128);
234
1.59M
    __m128i sum_hi = _mm_shuffle_epi8(_mm256_extracti128_si256(r1, 1), mask_hi16_128);
235
1.59M
    __m128i carry_hi = _mm_add_epi8(carry128, sum_lo);
236
1.59M
    __m256i tile_carry = _mm256_inserti128_si256(_mm256_castsi128_si256(carry128), carry_hi, 1);
237
1.59M
    carry128 = _mm_add_epi8(carry128, _mm_add_epi8(sum_lo, sum_hi));
238
239
    /* Apply incoming cross-tile and cross-lane carry to all 32 records. */
240
1.59M
    r0 = _mm256_add_epi8(r0, tile_carry);
241
1.59M
    r1 = _mm256_add_epi8(r1, tile_carry);
242
243
    /* Reorder lanes into two contiguous 32-byte stores (records 0..15, then 16..31). */
244
1.59M
    uint8_t *dst = output + i * 2;
245
1.59M
    _mm256_storeu_si256((__m256i *)dst, _mm256_permute2x128_si256(r0, r1, 0x20));
246
1.59M
    _mm256_storeu_si256((__m256i *)(dst + 32), _mm256_permute2x128_si256(r0, r1, 0x31));
247
1.59M
  }
248
  /* Scalar tail for remaining <32 records and any incomplete trailing bytes. */
249
12.3k
  uint8_t tail_carry[16];
250
12.3k
  _mm_storeu_si128((__m128i *)tail_carry, carry128);
251
37.0k
  for (int p = 0; p < 2; p++) {
252
24.7k
    uint8_t carry = tail_carry[p];
253
368k
    for (int i = vector_count; i < count; i++) {
254
343k
      carry = (uint8_t)(carry + input[p * count + i]);
255
343k
      output[i * 2 + p] = carry;
256
343k
    }
257
24.7k
  }
258
12.3k
  memcpy(output + count * 2, input + count * 2, (size_t)(length % 2));
259
12.3k
  return 1;
260
12.3k
}
261
262
5.59k
static int backward4_avx2(const uint8_t *input, uint8_t *output, int32_t length) {
263
5.59k
  int count = length / 4, vector_count = count / 32 * 32;
264
5.59k
  __m128i carry128 = _mm_setzero_si128();
265
1.42M
  for (int i = 0; i < vector_count; i += 32) {
266
    /* Load 32 delta bytes from each of the 4 byte planes. */
267
1.42M
    __m256i p0 = _mm256_loadu_si256((const __m256i *)(input + 0 * count + i));
268
1.42M
    __m256i p1 = _mm256_loadu_si256((const __m256i *)(input + 1 * count + i));
269
1.42M
    __m256i p2 = _mm256_loadu_si256((const __m256i *)(input + 2 * count + i));
270
1.42M
    __m256i p3 = _mm256_loadu_si256((const __m256i *)(input + 3 * count + i));
271
272
    /* Stage 1 & 2 in-lane transpose: interleave 4 planes into 4-byte records in r0..r3. */
273
1.42M
    __m256i a0 = _mm256_unpacklo_epi8(p0, p1);
274
1.42M
    __m256i a1 = _mm256_unpackhi_epi8(p0, p1);
275
1.42M
    __m256i a2 = _mm256_unpacklo_epi8(p2, p3);
276
1.42M
    __m256i a3 = _mm256_unpackhi_epi8(p2, p3);
277
278
1.42M
    __m256i r0 = _mm256_unpacklo_epi16(a0, a2);
279
1.42M
    __m256i r1 = _mm256_unpackhi_epi16(a0, a2);
280
1.42M
    __m256i r2 = _mm256_unpacklo_epi16(a1, a3);
281
1.42M
    __m256i r3 = _mm256_unpackhi_epi16(a1, a3);
282
283
    /* Prefix sum across the 4 interleaved 4-byte records within each 128-bit lane. */
284
1.42M
    r0 = _mm256_add_epi8(r0, _mm256_slli_si256(r0, 4));
285
1.42M
    r1 = _mm256_add_epi8(r1, _mm256_slli_si256(r1, 4));
286
1.42M
    r2 = _mm256_add_epi8(r2, _mm256_slli_si256(r2, 4));
287
1.42M
    r3 = _mm256_add_epi8(r3, _mm256_slli_si256(r3, 4));
288
289
1.42M
    r0 = _mm256_add_epi8(r0, _mm256_slli_si256(r0, 8));
290
1.42M
    r1 = _mm256_add_epi8(r1, _mm256_slli_si256(r1, 8));
291
1.42M
    r2 = _mm256_add_epi8(r2, _mm256_slli_si256(r2, 8));
292
1.42M
    r3 = _mm256_add_epi8(r3, _mm256_slli_si256(r3, 8));
293
294
    /* Tree-reduce quad totals across r0..r3 to get full 16-record local sums per lane. */
295
1.42M
    r1 = _mm256_add_epi8(r1, dup_hi32_256(r0));
296
1.42M
    r3 = _mm256_add_epi8(r3, dup_hi32_256(r2));
297
1.42M
    __m256i d1 = dup_hi32_256(r1);
298
1.42M
    __m256i r3_local = _mm256_add_epi8(r3, d1);
299
300
    /* Build per-lane tile carry [carry, carry + sum_lo] and advance running carry128. */
301
1.42M
    __m128i sum_lo = _mm_shuffle_epi32(_mm256_castsi256_si128(r3_local), 0xff);
302
1.42M
    __m128i sum_hi = _mm_shuffle_epi32(_mm256_extracti128_si256(r3_local, 1), 0xff);
303
1.42M
    __m128i carry_hi = _mm_add_epi8(carry128, sum_lo);
304
1.42M
    __m256i tile_carry = _mm256_inserti128_si256(_mm256_castsi128_si256(carry128), carry_hi, 1);
305
1.42M
    carry128 = _mm_add_epi8(carry128, _mm_add_epi8(sum_lo, sum_hi));
306
307
    /* Fold tile_carry with d1 for r2 and apply carries to all 4 vectors. */
308
1.42M
    __m256i d1c = _mm256_add_epi8(d1, tile_carry);
309
1.42M
    r0 = _mm256_add_epi8(r0, tile_carry);
310
1.42M
    r1 = _mm256_add_epi8(r1, tile_carry);
311
1.42M
    r2 = _mm256_add_epi8(r2, d1c);
312
1.42M
    r3 = _mm256_add_epi8(r3_local, tile_carry);
313
314
    /* Reorder lanes into four contiguous 32-byte stores (records 0..31). */
315
1.42M
    uint8_t *dst = output + i * 4;
316
1.42M
    _mm256_storeu_si256((__m256i *)(dst + 0), _mm256_permute2x128_si256(r0, r1, 0x20));
317
1.42M
    _mm256_storeu_si256((__m256i *)(dst + 32), _mm256_permute2x128_si256(r2, r3, 0x20));
318
1.42M
    _mm256_storeu_si256((__m256i *)(dst + 64), _mm256_permute2x128_si256(r0, r1, 0x31));
319
1.42M
    _mm256_storeu_si256((__m256i *)(dst + 96), _mm256_permute2x128_si256(r2, r3, 0x31));
320
1.42M
  }
321
  /* Scalar tail for remaining <32 records and any incomplete trailing bytes. */
322
5.59k
  uint8_t tail_carry[16];
323
5.59k
  _mm_storeu_si128((__m128i *)tail_carry, carry128);
324
27.9k
  for (int p = 0; p < 4; p++) {
325
22.3k
    uint8_t carry = tail_carry[p];
326
336k
    for (int i = vector_count; i < count; i++) {
327
313k
      carry = (uint8_t)(carry + input[p * count + i]);
328
313k
      output[i * 4 + p] = carry;
329
313k
    }
330
22.3k
  }
331
5.59k
  memcpy(output + count * 4, input + count * 4, (size_t)(length % 4));
332
5.59k
  return 1;
333
5.59k
}
334
335
20.6k
static int backward8_avx2(const uint8_t *input, uint8_t *output, int32_t length) {
336
20.6k
  int count = length / 8, vector_count = count / 32 * 32;
337
20.6k
  __m128i carry128 = _mm_setzero_si128();
338
572k
  for (int i = 0; i < vector_count; i += 32) {
339
    /* Load 32 delta bytes from each of the 8 byte planes. */
340
552k
    __m256i p0 = _mm256_loadu_si256((const __m256i *)(input + 0 * count + i));
341
552k
    __m256i p1 = _mm256_loadu_si256((const __m256i *)(input + 1 * count + i));
342
552k
    __m256i p2 = _mm256_loadu_si256((const __m256i *)(input + 2 * count + i));
343
552k
    __m256i p3 = _mm256_loadu_si256((const __m256i *)(input + 3 * count + i));
344
552k
    __m256i p4 = _mm256_loadu_si256((const __m256i *)(input + 4 * count + i));
345
552k
    __m256i p5 = _mm256_loadu_si256((const __m256i *)(input + 5 * count + i));
346
552k
    __m256i p6 = _mm256_loadu_si256((const __m256i *)(input + 6 * count + i));
347
552k
    __m256i p7 = _mm256_loadu_si256((const __m256i *)(input + 7 * count + i));
348
349
    /* Stage 1 (8-bit) transpose of low 4 planes; skip upper 4 planes when all zero. */
350
552k
    __m256i a0 = _mm256_unpacklo_epi8(p0, p1);
351
552k
    __m256i a1 = _mm256_unpackhi_epi8(p0, p1);
352
552k
    __m256i a2 = _mm256_unpacklo_epi8(p2, p3);
353
552k
    __m256i a3 = _mm256_unpackhi_epi8(p2, p3);
354
355
552k
    __m256i hi = _mm256_or_si256(_mm256_or_si256(p4, p5), _mm256_or_si256(p6, p7));
356
552k
    if (_mm256_testz_si256(hi, hi)) {
357
528k
      __m256i b0 = _mm256_unpacklo_epi16(a0, a2);
358
528k
      __m256i b1 = _mm256_unpackhi_epi16(a0, a2);
359
528k
      __m256i b2 = _mm256_unpacklo_epi16(a1, a3);
360
528k
      __m256i b3 = _mm256_unpackhi_epi16(a1, a3);
361
362
528k
      b0 = _mm256_add_epi8(b0, _mm256_slli_si256(b0, 4));
363
528k
      b1 = _mm256_add_epi8(b1, _mm256_slli_si256(b1, 4));
364
528k
      b2 = _mm256_add_epi8(b2, _mm256_slli_si256(b2, 4));
365
528k
      b3 = _mm256_add_epi8(b3, _mm256_slli_si256(b3, 4));
366
367
528k
      b0 = _mm256_add_epi8(b0, _mm256_slli_si256(b0, 8));
368
528k
      b1 = _mm256_add_epi8(b1, _mm256_slli_si256(b1, 8));
369
528k
      b2 = _mm256_add_epi8(b2, _mm256_slli_si256(b2, 8));
370
528k
      b3 = _mm256_add_epi8(b3, _mm256_slli_si256(b3, 8));
371
372
528k
      b1 = _mm256_add_epi8(b1, dup_hi32_256(b0));
373
528k
      b3 = _mm256_add_epi8(b3, dup_hi32_256(b2));
374
528k
      __m256i d1 = dup_hi32_256(b1);
375
528k
      __m256i b3_local = _mm256_add_epi8(b3, d1);
376
377
528k
      __m128i carry_lo128 = _mm_shuffle_epi32(carry128, 0x00);
378
528k
      __m128i carry_hi128 = _mm_shuffle_epi32(carry128, 0x55);
379
528k
      __m128i sum_lo = _mm_shuffle_epi32(_mm256_castsi256_si128(b3_local), 0xff);
380
528k
      __m128i sum_hi = _mm_shuffle_epi32(_mm256_extracti128_si256(b3_local, 1), 0xff);
381
528k
      __m128i carry_mid = _mm_add_epi8(carry_lo128, sum_lo);
382
528k
      __m256i tile_carry_lo = _mm256_inserti128_si256(
383
528k
          _mm256_castsi128_si256(carry_lo128), carry_mid, 1);
384
528k
      carry_lo128 = _mm_add_epi8(carry_mid, sum_hi);
385
528k
      carry128 = _mm_unpacklo_epi32(carry_lo128, carry_hi128);
386
387
528k
      __m256i d1c = _mm256_add_epi8(d1, tile_carry_lo);
388
528k
      b0 = _mm256_add_epi8(b0, tile_carry_lo);
389
528k
      b1 = _mm256_add_epi8(b1, tile_carry_lo);
390
528k
      b2 = _mm256_add_epi8(b2, d1c);
391
528k
      b3 = _mm256_add_epi8(b3_local, tile_carry_lo);
392
393
528k
      __m256i carry_hi256 = _mm256_inserti128_si256(
394
528k
          _mm256_castsi128_si256(carry_hi128), carry_hi128, 1);
395
528k
      __m256i r0 = _mm256_unpacklo_epi32(b0, carry_hi256);
396
528k
      __m256i r1 = _mm256_unpackhi_epi32(b0, carry_hi256);
397
528k
      __m256i r2 = _mm256_unpacklo_epi32(b1, carry_hi256);
398
528k
      __m256i r3 = _mm256_unpackhi_epi32(b1, carry_hi256);
399
528k
      __m256i r4 = _mm256_unpacklo_epi32(b2, carry_hi256);
400
528k
      __m256i r5 = _mm256_unpackhi_epi32(b2, carry_hi256);
401
528k
      __m256i r6 = _mm256_unpacklo_epi32(b3, carry_hi256);
402
528k
      __m256i r7 = _mm256_unpackhi_epi32(b3, carry_hi256);
403
404
528k
      uint8_t *dst = output + i * 8;
405
528k
      _mm_storeu_si128((__m128i *)(dst + 0), _mm256_castsi256_si128(r0));
406
528k
      _mm_storeu_si128((__m128i *)(dst + 16), _mm256_castsi256_si128(r1));
407
528k
      _mm_storeu_si128((__m128i *)(dst + 32), _mm256_castsi256_si128(r2));
408
528k
      _mm_storeu_si128((__m128i *)(dst + 48), _mm256_castsi256_si128(r3));
409
528k
      _mm_storeu_si128((__m128i *)(dst + 64), _mm256_castsi256_si128(r4));
410
528k
      _mm_storeu_si128((__m128i *)(dst + 80), _mm256_castsi256_si128(r5));
411
528k
      _mm_storeu_si128((__m128i *)(dst + 96), _mm256_castsi256_si128(r6));
412
528k
      _mm_storeu_si128((__m128i *)(dst + 112), _mm256_castsi256_si128(r7));
413
528k
      _mm_storeu_si128((__m128i *)(dst + 128), _mm256_extracti128_si256(r0, 1));
414
528k
      _mm_storeu_si128((__m128i *)(dst + 144), _mm256_extracti128_si256(r1, 1));
415
528k
      _mm_storeu_si128((__m128i *)(dst + 160), _mm256_extracti128_si256(r2, 1));
416
528k
      _mm_storeu_si128((__m128i *)(dst + 176), _mm256_extracti128_si256(r3, 1));
417
528k
      _mm_storeu_si128((__m128i *)(dst + 192), _mm256_extracti128_si256(r4, 1));
418
528k
      _mm_storeu_si128((__m128i *)(dst + 208), _mm256_extracti128_si256(r5, 1));
419
528k
      _mm_storeu_si128((__m128i *)(dst + 224), _mm256_extracti128_si256(r6, 1));
420
528k
      _mm_storeu_si128((__m128i *)(dst + 240), _mm256_extracti128_si256(r7, 1));
421
528k
      continue;
422
528k
    }
423
424
23.6k
    __m256i a4 = _mm256_unpacklo_epi8(p4, p5);
425
23.6k
    __m256i a5 = _mm256_unpackhi_epi8(p4, p5);
426
23.6k
    __m256i a6 = _mm256_unpacklo_epi8(p6, p7);
427
23.6k
    __m256i a7 = _mm256_unpackhi_epi8(p6, p7);
428
429
23.6k
    __m256i b0 = _mm256_unpacklo_epi16(a0, a2);
430
23.6k
    __m256i b1 = _mm256_unpackhi_epi16(a0, a2);
431
23.6k
    __m256i b2 = _mm256_unpacklo_epi16(a1, a3);
432
23.6k
    __m256i b3 = _mm256_unpackhi_epi16(a1, a3);
433
23.6k
    __m256i b4 = _mm256_unpacklo_epi16(a4, a6);
434
23.6k
    __m256i b5 = _mm256_unpackhi_epi16(a4, a6);
435
23.6k
    __m256i b6 = _mm256_unpacklo_epi16(a5, a7);
436
23.6k
    __m256i b7 = _mm256_unpackhi_epi16(a5, a7);
437
438
23.6k
    __m256i r0 = _mm256_unpacklo_epi32(b0, b4);
439
23.6k
    __m256i r1 = _mm256_unpackhi_epi32(b0, b4);
440
23.6k
    __m256i r2 = _mm256_unpacklo_epi32(b1, b5);
441
23.6k
    __m256i r3 = _mm256_unpackhi_epi32(b1, b5);
442
23.6k
    __m256i r4 = _mm256_unpacklo_epi32(b2, b6);
443
23.6k
    __m256i r5 = _mm256_unpackhi_epi32(b2, b6);
444
23.6k
    __m256i r6 = _mm256_unpacklo_epi32(b3, b7);
445
23.6k
    __m256i r7 = _mm256_unpackhi_epi32(b3, b7);
446
447
    /* Prefix sum depth 1: combine adjacent pairs of 8-byte records within each 128-bit lane. */
448
23.6k
    r0 = _mm256_add_epi8(r0, _mm256_slli_si256(r0, 8));
449
23.6k
    r1 = _mm256_add_epi8(r1, _mm256_slli_si256(r1, 8));
450
23.6k
    r2 = _mm256_add_epi8(r2, _mm256_slli_si256(r2, 8));
451
23.6k
    r3 = _mm256_add_epi8(r3, _mm256_slli_si256(r3, 8));
452
23.6k
    r4 = _mm256_add_epi8(r4, _mm256_slli_si256(r4, 8));
453
23.6k
    r5 = _mm256_add_epi8(r5, _mm256_slli_si256(r5, 8));
454
23.6k
    r6 = _mm256_add_epi8(r6, _mm256_slli_si256(r6, 8));
455
23.6k
    r7 = _mm256_add_epi8(r7, _mm256_slli_si256(r7, 8));
456
457
    /* Prefix sum depth 2: combine pairs into 4-record groups. */
458
23.6k
    r1 = _mm256_add_epi8(r1, dup_hi64_256(r0));
459
23.6k
    r3 = _mm256_add_epi8(r3, dup_hi64_256(r2));
460
23.6k
    r5 = _mm256_add_epi8(r5, dup_hi64_256(r4));
461
23.6k
    r7 = _mm256_add_epi8(r7, dup_hi64_256(r6));
462
463
    /* Prefix sum depth 3: combine 4-record groups into 8-record halves. */
464
23.6k
    __m256i d1 = dup_hi64_256(r1);
465
23.6k
    __m256i d5 = dup_hi64_256(r5);
466
23.6k
    r2 = _mm256_add_epi8(r2, d1);
467
23.6k
    r3 = _mm256_add_epi8(r3, d1);
468
23.6k
    r6 = _mm256_add_epi8(r6, d5);
469
23.6k
    r7 = _mm256_add_epi8(r7, d5);
470
471
    /* Prefix sum depth 4: compute 16-record local totals in r7_local before applying carries. */
472
23.6k
    __m256i d3 = dup_hi64_256(r3);
473
23.6k
    __m256i r7_local = _mm256_add_epi8(r7, d3);
474
475
    /* Build per-lane tile carry [carry, carry + sum_lo] and advance running carry128. */
476
23.6k
    __m128i r7_lo = _mm256_castsi256_si128(r7_local);
477
23.6k
    __m128i r7_hi = _mm256_extracti128_si256(r7_local, 1);
478
23.6k
    __m128i sum_lo = _mm_unpackhi_epi64(r7_lo, r7_lo);
479
23.6k
    __m128i sum_hi = _mm_unpackhi_epi64(r7_hi, r7_hi);
480
23.6k
    __m128i carry_hi = _mm_add_epi8(carry128, sum_lo);
481
23.6k
    __m256i tile_carry = _mm256_inserti128_si256(_mm256_castsi128_si256(carry128), carry_hi, 1);
482
23.6k
    carry128 = _mm_add_epi8(carry128, _mm_add_epi8(sum_lo, sum_hi));
483
484
    /* Fold tile_carry with d3 for r4..r6 to save vector adds, then finalize r0..r7. */
485
23.6k
    __m256i d3c = _mm256_add_epi8(d3, tile_carry);
486
23.6k
    r0 = _mm256_add_epi8(r0, tile_carry);
487
23.6k
    r1 = _mm256_add_epi8(r1, tile_carry);
488
23.6k
    r2 = _mm256_add_epi8(r2, tile_carry);
489
23.6k
    r3 = _mm256_add_epi8(r3, tile_carry);
490
23.6k
    r4 = _mm256_add_epi8(r4, d3c);
491
23.6k
    r5 = _mm256_add_epi8(r5, d3c);
492
23.6k
    r6 = _mm256_add_epi8(r6, d3c);
493
23.6k
    r7 = _mm256_add_epi8(r7_local, tile_carry);
494
495
    /* Split-store low 128-bit lanes (records 0..15) and high lanes (16..31) without vperm2i128. */
496
23.6k
    uint8_t *dst = output + i * 8;
497
23.6k
    _mm_storeu_si128((__m128i *)(dst + 0), _mm256_castsi256_si128(r0));
498
23.6k
    _mm_storeu_si128((__m128i *)(dst + 16), _mm256_castsi256_si128(r1));
499
23.6k
    _mm_storeu_si128((__m128i *)(dst + 32), _mm256_castsi256_si128(r2));
500
23.6k
    _mm_storeu_si128((__m128i *)(dst + 48), _mm256_castsi256_si128(r3));
501
23.6k
    _mm_storeu_si128((__m128i *)(dst + 64), _mm256_castsi256_si128(r4));
502
23.6k
    _mm_storeu_si128((__m128i *)(dst + 80), _mm256_castsi256_si128(r5));
503
23.6k
    _mm_storeu_si128((__m128i *)(dst + 96), _mm256_castsi256_si128(r6));
504
23.6k
    _mm_storeu_si128((__m128i *)(dst + 112), _mm256_castsi256_si128(r7));
505
23.6k
    _mm_storeu_si128((__m128i *)(dst + 128), _mm256_extracti128_si256(r0, 1));
506
23.6k
    _mm_storeu_si128((__m128i *)(dst + 144), _mm256_extracti128_si256(r1, 1));
507
23.6k
    _mm_storeu_si128((__m128i *)(dst + 160), _mm256_extracti128_si256(r2, 1));
508
23.6k
    _mm_storeu_si128((__m128i *)(dst + 176), _mm256_extracti128_si256(r3, 1));
509
23.6k
    _mm_storeu_si128((__m128i *)(dst + 192), _mm256_extracti128_si256(r4, 1));
510
23.6k
    _mm_storeu_si128((__m128i *)(dst + 208), _mm256_extracti128_si256(r5, 1));
511
23.6k
    _mm_storeu_si128((__m128i *)(dst + 224), _mm256_extracti128_si256(r6, 1));
512
23.6k
    _mm_storeu_si128((__m128i *)(dst + 240), _mm256_extracti128_si256(r7, 1));
513
23.6k
  }
514
  /* Scalar tail for remaining <32 records and any incomplete trailing bytes. */
515
20.6k
  uint8_t tail_carry[16];
516
20.6k
  _mm_storeu_si128((__m128i *)tail_carry, carry128);
517
185k
  for (int p = 0; p < 8; p++) {
518
164k
    uint8_t carry = tail_carry[p];
519
2.39M
    for (int i = vector_count; i < count; i++) {
520
2.23M
      carry = (uint8_t)(carry + input[p * count + i]);
521
2.23M
      output[i * 8 + p] = carry;
522
2.23M
    }
523
164k
  }
524
20.6k
  memcpy(output + count * 8, input + count * 8, (size_t)(length % 8));
525
20.6k
  return 1;
526
20.6k
}
527
528
0
int shuffle_bytedelta_avx2_forward(const uint8_t *input, uint8_t *output, int32_t length, int typesize) {
529
0
  switch (typesize) {
530
0
    case 2: return forward2_avx2(input, output, length);
531
0
    case 4: return forward4_avx2(input, output, length);
532
0
    case 8: return forward8_avx2(input, output, length);
533
0
    default: return 0;
534
0
  }
535
0
}
536
537
38.5k
int shuffle_bytedelta_avx2_backward(const uint8_t *input, uint8_t *output, int32_t length, int typesize) {
538
38.5k
  switch (typesize) {
539
12.3k
    case 2: return backward2_avx2(input, output, length);
540
5.59k
    case 4: return backward4_avx2(input, output, length);
541
20.6k
    case 8: return backward8_avx2(input, output, length);
542
0
    default: return 0;
543
38.5k
  }
544
38.5k
}
545
546
#else
547
548
int shuffle_bytedelta_avx2_forward(const uint8_t *input, uint8_t *output, int32_t length, int typesize) {
549
  BLOSC_UNUSED_PARAM(input);
550
  BLOSC_UNUSED_PARAM(output);
551
  BLOSC_UNUSED_PARAM(length);
552
  BLOSC_UNUSED_PARAM(typesize);
553
  return 0;
554
}
555
556
int shuffle_bytedelta_avx2_backward(const uint8_t *input, uint8_t *output, int32_t length, int typesize) {
557
  BLOSC_UNUSED_PARAM(input);
558
  BLOSC_UNUSED_PARAM(output);
559
  BLOSC_UNUSED_PARAM(length);
560
  BLOSC_UNUSED_PARAM(typesize);
561
  return 0;
562
}
563
564
#endif /* defined(__AVX2__) */