]> git.sesse.net Git - plocate/blob - turbopfor.h
Convert the SSE2 delta decoder state into a class.
[plocate] / turbopfor.h
1 #ifndef _TURBOPFOR_H
2 #define _TURBOPFOR_H 1
3
4 // A reimplementation of parts of the TurboPFor codecs, using the same
5 // storage format. These are not as fast as the reference implementation
6 // (about 60% of the performance, averaged over a real plocate corpus),
7 // and do not support the same breadth of codecs (in particular, only
8 // delta-plus-1 is implemented, and only 32-bit docids are tested),
9 // but aim to be more portable and (ideally) easier-to-understand.
10 // In particular, they will compile on x86 without SSE4.1 or AVX support.
11 //
12 // The main reference is https://michael.stapelberg.ch/posts/2019-02-05-turbopfor-analysis/,
13 // although some implementation details have been worked out by studying the
14 // TurboPFor code.
15
16 #include <assert.h>
17 #include <endian.h>
18 #include <stdint.h>
19 #include <string.h>
20 #include <limits.h>
21
22 #include <algorithm>
23
24 #if defined(__i386__) || defined(__x86_64__)
25 #define COULD_HAVE_SSE2
26 #include <immintrin.h>
27 #endif
28
29 // Forward declarations to declare to the template code below that they exist.
30 // (These must seemingly be non-templates for function multiversioning to work.)
31 __attribute__((target("default")))
32 const unsigned char *decode_for_interleaved_128_32(const unsigned char *in, uint32_t *out);
33 __attribute__((target("default")))
34 const unsigned char *decode_pfor_bitmap_interleaved_128_32(const unsigned char *in, uint32_t *out);
35 __attribute__((target("default")))
36 const unsigned char *decode_pfor_vb_interleaved_128_32(const unsigned char *in, uint32_t *out);
37
38 #ifdef COULD_HAVE_SSE2
39 __attribute__((target("sse2")))
40 const unsigned char *decode_for_interleaved_128_32(const unsigned char *in, uint32_t *out);
41 __attribute__((target("sse2")))
42 const unsigned char *decode_pfor_bitmap_interleaved_128_32(const unsigned char *in, uint32_t *out);
43 __attribute__((target("sse2")))
44 const unsigned char *decode_pfor_vb_interleaved_128_32(const unsigned char *in, uint32_t *out);
45 #endif
46
47 template<class Docid>
48 Docid read_le(const void *in)
49 {
50         Docid val;
51         memcpy(&val, in, sizeof(val));
52         if constexpr (sizeof(Docid) == 8) {
53                 return le64toh(val);
54         } else if constexpr (sizeof(Docid) == 4) {
55                 return le32toh(val);
56         } else if constexpr (sizeof(Docid) == 2) {
57                 return le16toh(val);
58         } else if constexpr (sizeof(Docid) == 1) {
59                 return val;
60         } else {
61                 assert(false);
62         }
63 }
64
65 // Reads a single value with an encoding that looks a bit like PrefixVarint.
66 // It's unclear why this doesn't use the varbyte encoding.
67 template<class Docid>
68 const unsigned char *read_baseval(const unsigned char *in, Docid *out)
69 {
70         //fprintf(stderr, "baseval: 0x%02x 0x%02x 0x%02x 0x%02x\n", in[0], in[1], in[2], in[3]);
71         if (*in < 128) {
72                 *out = *in;
73                 return in + 1;
74         } else if (*in < 192) {
75                 *out = ((uint32_t(in[0]) << 8) | uint32_t(in[1])) & 0x3fff;
76                 return in + 2;
77         } else if (*in < 224) {
78                 *out = ((uint32_t(in[0]) << 16) |
79                         (uint32_t(in[2]) << 8) |
80                         (uint32_t(in[1]))) & 0x1fffff;
81                 return in + 3;
82         } else {
83                 assert(false);  // Not implemented.
84         }
85 }
86
87 template<class Docid>
88 const unsigned char *read_vb(const unsigned char *in, Docid *out)
89 {
90         if (*in <= 176) {
91                 *out = *in;
92                 return in + 1;
93         } else if (*in <= 240) {
94                 *out = ((uint32_t(in[0] - 177) << 8) | uint32_t(in[1])) + 177;
95                 return in + 2;
96         } else if (*in <= 248) {
97                 *out = ((uint32_t(in[0] - 241) << 16) | read_le<uint16_t>(in + 1)) + 16561;
98                 return in + 3;
99         } else if (*in == 249) {
100                 *out = (uint32_t(in[1])) |
101                         (uint32_t(in[2]) << 8) |
102                         (uint32_t(in[3]) << 16);
103                 return in + 4;
104         } else if (*in == 250) {
105                 *out = read_le<uint32_t>(in + 1);
106                 return in + 5;
107         } else {
108                 assert(false);
109         }
110 }
111
112 struct BitReader {
113 public:
114         BitReader(const unsigned char *in, unsigned bits)
115                 : in(in), bits(bits), mask((1U << bits) - 1) {}
116         uint32_t read()
117         {
118                 uint32_t val = (read_le<uint32_t>(in) >> bits_used) & mask;
119
120                 bits_used += bits;
121                 in += bits_used / 8;
122                 bits_used %= 8;
123
124                 return val;
125         }
126
127 private:
128         const unsigned char *in;
129         const unsigned bits;
130         const unsigned mask;
131         unsigned bits_used = 0;
132 };
133
134 template<unsigned NumStreams>
135 struct InterleavedBitReader {
136 public:
137         InterleavedBitReader(const unsigned char *in, unsigned bits)
138                 : in(in), bits(bits), mask((1U << bits) - 1) {}
139         uint32_t read()
140         {
141                 uint32_t val;
142                 if (bits_used + bits > 32) {
143                         val = (read_le<uint32_t>(in) >> bits_used) | (read_le<uint32_t>(in + Stride) << (32 - bits_used));
144                 } else {
145                         val = (read_le<uint32_t>(in) >> bits_used);
146                 }
147
148                 bits_used += bits;
149                 in += Stride * (bits_used / 32);
150                 bits_used %= 32;
151
152                 return val & mask;
153         }
154
155 private:
156         static constexpr unsigned Stride = NumStreams * sizeof(uint32_t);
157         const unsigned char *in;
158         const unsigned bits;
159         const unsigned mask;
160         unsigned bits_used = 0;
161 };
162
163 // Does not properly account for overflow.
164 inline unsigned div_round_up(unsigned val, unsigned div)
165 {
166         return (val + div - 1) / div;
167 }
168
169 inline unsigned bytes_for_packed_bits(unsigned num, unsigned bit_width)
170 {
171         return div_round_up(num * bit_width, CHAR_BIT);
172 }
173
174 // Constant block. Layout:
175 //
176 //  - Bit width (6 bits) | type << 6
177 //  - Base values (<bits> bits, rounded up to nearest byte)
178 template<class Docid>
179 const unsigned char *decode_constant(const unsigned char *in, unsigned num, Docid *out)
180 {
181         const unsigned bit_width = *in++ & 0x3f;
182         Docid val = read_le<Docid>(in);
183         if (bit_width < sizeof(Docid) * 8) {
184                 val &= ((1U << bit_width) - 1);
185         }
186
187         Docid prev_val = out[-1];
188         for (unsigned i = 0; i < num; ++i) {
189                 out[i] = prev_val = val + prev_val + 1;
190         }
191         return in + div_round_up(bit_width, 8);
192 }
193
194 // FOR block (ie., PFor without exceptions). Layout:
195 //
196 //  - Bit width (6 bits) | type << 6
197 //  - Base values (<num> values of <bits> bits, rounded up to a multiple of 32 values)
198 template<class Docid>
199 const unsigned char *decode_for(const unsigned char *in, unsigned num, Docid *out)
200 {
201         const unsigned bit_width = *in++ & 0x3f;
202
203         Docid prev_val = out[-1];
204         BitReader bs(in, bit_width);
205         for (unsigned i = 0; i < num; ++i) {
206                 prev_val = out[i] = bs.read() + prev_val + 1;
207         }
208         return in + bytes_for_packed_bits(num, bit_width);
209 }
210
211 #ifdef COULD_HAVE_SSE2
212 class DeltaDecoderSSE2 {
213 public:
214         DeltaDecoderSSE2(uint32_t prev_val) : prev_val(_mm_set1_epi32(prev_val)) {}
215         __m128i decode(__m128i val) {
216                 val = _mm_add_epi32(val, _mm_slli_si128(val, 4));
217                 val = _mm_add_epi32(val, _mm_slli_si128(val, 8));
218                 val = _mm_add_epi32(val, _mm_add_epi32(prev_val, delta));
219                 prev_val = _mm_shuffle_epi32(val, _MM_SHUFFLE(3, 3, 3, 3));
220                 return val;
221         }
222
223 private:
224         // Use 4/3/2/1 as delta instead of fixed 1, so that we can do the prev_val + delta
225         // in parallel with something else.
226         const __m128i delta = _mm_set_epi32(4, 3, 2, 1);
227
228         __m128i prev_val;
229 };
230
231 template<unsigned BlockSize>
232 __attribute__((target("sse2")))
233 inline void delta_decode_sse2(uint32_t *out)
234 {
235         DeltaDecoderSSE2 delta(out[-1]);
236         __m128i *outvec = reinterpret_cast<__m128i *>(out);
237         for (unsigned i = 0; i < BlockSize / 4; ++i) {
238                 __m128i val = _mm_loadu_si128(outvec + i);
239                 _mm_storeu_si128(outvec + i, delta.decode(val));
240         }
241 }
242
243 template<unsigned BlockSize, bool OrWithExisting>
244 __attribute__((target("sse2")))
245 const unsigned char *decode_bitmap_sse2(const unsigned char *in, unsigned bit_width, uint32_t *out)
246 {
247         const __m128i *invec = reinterpret_cast<const __m128i *>(in);
248         __m128i *outvec = reinterpret_cast<__m128i *>(out);
249         const __m128i mask = _mm_set1_epi32((1U << bit_width) - 1);
250         unsigned bits_used = 0;
251         for (unsigned i = 0; i < BlockSize / 4; ++i) {
252                 __m128i val = _mm_srli_epi32(_mm_loadu_si128(invec), bits_used);
253                 if (bits_used + bit_width > 32) {
254                         __m128i val_upper = _mm_slli_epi32(_mm_loadu_si128(invec + 1), 32 - bits_used);
255                         val = _mm_or_si128(val, val_upper);
256                 }
257                 val = _mm_and_si128(val, mask);
258                 if constexpr (OrWithExisting) {
259                         val = _mm_or_si128(val, _mm_loadu_si128(outvec + i));
260                 }
261                 _mm_storeu_si128(outvec + i, val);
262
263                 bits_used += bit_width;
264                 invec += bits_used / 32;
265                 bits_used %= 32;
266         }
267         in += bytes_for_packed_bits(BlockSize, bit_width);
268         return in;
269 }
270 #endif
271
272 // Like decode_for(), but the values are organized in four independent streams,
273 // for SIMD (presumably SSE2). Supports a whole block only.
274 template<unsigned BlockSize, class Docid>
275 const unsigned char *decode_for_interleaved_generic(const unsigned char *in, Docid *out)
276 {
277         const unsigned bit_width = *in++ & 0x3f;
278
279         InterleavedBitReader<4> bs0(in + 0 * sizeof(uint32_t), bit_width);
280         InterleavedBitReader<4> bs1(in + 1 * sizeof(uint32_t), bit_width);
281         InterleavedBitReader<4> bs2(in + 2 * sizeof(uint32_t), bit_width);
282         InterleavedBitReader<4> bs3(in + 3 * sizeof(uint32_t), bit_width);
283         for (unsigned i = 0; i < BlockSize / 4; ++i) {
284                 out[i * 4 + 0] = bs0.read();
285                 out[i * 4 + 1] = bs1.read();
286                 out[i * 4 + 2] = bs2.read();
287                 out[i * 4 + 3] = bs3.read();
288         }
289         Docid prev_val = out[-1];
290         for (unsigned i = 0; i < BlockSize; ++i) {
291                 out[i] = prev_val = out[i] + prev_val + 1;
292         }
293         return in + bytes_for_packed_bits(BlockSize, bit_width);
294 }
295
296 template<unsigned BlockSize, class Docid>
297 const unsigned char *decode_for_interleaved(const unsigned char *in, Docid *out)
298 {
299         if constexpr (BlockSize == 128 && sizeof(Docid) == sizeof(uint32_t)) {
300                 return decode_for_interleaved_128_32(in, out);
301         } else {
302                 return decode_for_interleaved_generic(in, out);
303         }
304 }
305
306 __attribute__((target("default")))
307 const unsigned char *decode_for_interleaved_128_32(const unsigned char *in, uint32_t *out)
308 {
309         return decode_for_interleaved_generic<128>(in, out);
310 }
311
312 #ifdef COULD_HAVE_SSE2
313 // Specialized version for SSE2.
314 __attribute__((target("sse2")))
315 const unsigned char *decode_for_interleaved_128_32(const unsigned char *in, uint32_t *out)
316 {
317         constexpr unsigned BlockSize = 128;
318
319         const unsigned bit_width = *in++ & 0x3f;
320
321         in = decode_bitmap_sse2<BlockSize, false>(in, bit_width, out);
322         delta_decode_sse2<BlockSize>(out);
323
324         return in;
325 }
326 #endif
327
328 template<class Docid>
329 const unsigned char *decode_pfor_bitmap_exceptions(const unsigned char *in, unsigned num, unsigned bit_width, Docid *out)
330 {
331         const unsigned exception_bit_width = *in++;
332         const uint64_t *exception_bitmap_ptr = reinterpret_cast<const uint64_t *>(in);
333         in += div_round_up(num, 8);
334
335         int num_exceptions = 0;
336
337         BitReader bs(in, exception_bit_width);
338         for (unsigned i = 0; i < num; i += 64, ++exception_bitmap_ptr) {
339                 uint64_t exceptions = read_le<uint64_t>(exception_bitmap_ptr);
340                 if (num - i < 64) {
341                         // We've read some bytes past the end, so clear out the junk bits.
342                         exceptions &= (1ULL << (num - i)) - 1;
343                 }
344                 for (; exceptions != 0; exceptions &= exceptions - 1, ++num_exceptions) {
345                         unsigned idx = (ffsll(exceptions) - 1) + i;
346                         out[idx] = bs.read() << bit_width;
347                 }
348         }
349         in += bytes_for_packed_bits(num_exceptions, exception_bit_width);
350         return in;
351 }
352
353 // PFor block with bitmap exceptions. Layout:
354 //
355 //  - Bit width (6 bits) | type << 6
356 //  - Exception bit width (8 bits)
357 //  - Bitmap of which values have exceptions (<num> bits, rounded up to a byte)
358 //  - Exceptions (<num_exc> values of <bits_exc> bits, rounded up to a byte)
359 //  - Base values (<num> values of <bits> bits, rounded up to a byte)
360 template<class Docid>
361 const unsigned char *decode_pfor_bitmap(const unsigned char *in, unsigned num, Docid *out)
362 {
363         memset(out, 0, num * sizeof(Docid));
364
365         const unsigned bit_width = *in++ & 0x3f;
366
367         in = decode_pfor_bitmap_exceptions(in, num, bit_width, out);
368
369         // Decode the base values, and delta-decode.
370         Docid prev_val = out[-1];
371         BitReader bs(in, bit_width);
372         for (unsigned i = 0; i < num; ++i) {
373                 out[i] = prev_val = (out[i] | bs.read()) + prev_val + 1;
374         }
375         return in + bytes_for_packed_bits(num, bit_width);
376 }
377
378 // Like decode_pfor_bitmap(), but the base values are organized in four
379 // independent streams, for SIMD (presumably SSE2). Supports a whole block only.
380 template<unsigned BlockSize, class Docid>
381 const unsigned char *decode_pfor_bitmap_interleaved_generic(const unsigned char *in, Docid *out)
382 {
383         memset(out, 0, BlockSize * sizeof(Docid));
384
385         const unsigned bit_width = *in++ & 0x3f;
386
387         in = decode_pfor_bitmap_exceptions(in, BlockSize, bit_width, out);
388
389         // Decode the base values.
390         InterleavedBitReader<4> bs0(in + 0 * sizeof(uint32_t), bit_width);
391         InterleavedBitReader<4> bs1(in + 1 * sizeof(uint32_t), bit_width);
392         InterleavedBitReader<4> bs2(in + 2 * sizeof(uint32_t), bit_width);
393         InterleavedBitReader<4> bs3(in + 3 * sizeof(uint32_t), bit_width);
394         for (unsigned i = 0; i < BlockSize / 4; ++i) {
395                 out[i * 4 + 0] |= bs0.read();
396                 out[i * 4 + 1] |= bs1.read();
397                 out[i * 4 + 2] |= bs2.read();
398                 out[i * 4 + 3] |= bs3.read();
399         }
400
401         // Delta-decode.
402         Docid prev_val = out[-1];
403         for (unsigned i = 0; i < BlockSize; ++i) {
404                 out[i] = prev_val = out[i] + prev_val + 1;
405         }
406         return in + bytes_for_packed_bits(BlockSize, bit_width);
407 }
408
409 template<unsigned BlockSize, class Docid>
410 const unsigned char *decode_pfor_bitmap_interleaved(const unsigned char *in, Docid *out)
411 {
412         if constexpr (BlockSize == 128 && sizeof(Docid) == sizeof(uint32_t)) {
413                 return decode_pfor_bitmap_interleaved_128_32(in, out);
414         } else {
415                 return decode_pfor_bitmap_interleaved_generic(in, out);
416         }
417 }
418
419 __attribute__((target("default")))
420 const unsigned char *decode_pfor_bitmap_interleaved_128_32(const unsigned char *in, uint32_t *out)
421 {
422         return decode_pfor_bitmap_interleaved_generic<128>(in, out);
423 }
424
425 #ifdef COULD_HAVE_SSE2
426 // Specialized version for SSE2.
427 __attribute__((target("sse2")))
428 const unsigned char *decode_pfor_bitmap_interleaved_128_32(const unsigned char *in, uint32_t *out)
429 {
430         constexpr unsigned BlockSize = 128;
431         using Docid = uint32_t;
432
433         memset(out, 0, BlockSize * sizeof(Docid));
434
435         const unsigned bit_width = *in++ & 0x3f;
436
437         in = decode_pfor_bitmap_exceptions(in, BlockSize, bit_width, out);
438         in = decode_bitmap_sse2<BlockSize, true>(in, bit_width, out);
439         delta_decode_sse2<BlockSize>(out);
440
441         return in;
442 }
443 #endif
444
445 // PFor block with variable-byte exceptions. Layout:
446 //
447 //  - Bit width (6 bits) | type << 6
448 //  - Number of exceptions (8 bits)
449 //  - Base values (<num> values of <bits> bits, rounded up to a byte)
450 //  - Exceptions:
451 //    - If first byte is 255, <num_exc> 32-bit values (does not include the 255 byte)
452 //    - Else, <num_exc> varbyte-encoded values (includes the non-255 byte)
453 //  - Indexes of exceptions (<num_exc> bytes).
454 template<unsigned BlockSize, class Docid>
455 const unsigned char *decode_pfor_vb(const unsigned char *in, unsigned num, Docid *out)
456 {
457         //fprintf(stderr, "in=%p out=%p num=%u\n", in, out, num);
458
459         const unsigned bit_width = *in++ & 0x3f;
460         unsigned num_exceptions = *in++;
461
462         // Decode the base values.
463         BitReader bs(in, bit_width);
464         for (unsigned i = 0; i < num; ++i) {
465                 out[i] = bs.read();
466         }
467         in += bytes_for_packed_bits(num, bit_width);
468
469         // Decode exceptions.
470         Docid exceptions[BlockSize];
471         if (*in == 255) {
472                 ++in;
473                 for (unsigned i = 0; i < num_exceptions; ++i) {
474                         exceptions[i] = read_le<Docid>(in);
475                         in += sizeof(Docid);
476                 }
477         } else {
478                 for (unsigned i = 0; i < num_exceptions; ++i) {
479                         in = read_vb(in, &exceptions[i]);
480                 }
481         }
482         // Apply exceptions.
483         for (unsigned i = 0; i < num_exceptions; ++i) {
484                 unsigned idx = *in++;
485                 out[idx] |= exceptions[i] << bit_width;
486         }
487
488         // Delta-decode.
489         Docid prev_val = out[-1];
490         for (unsigned i = 0; i < num; ++i) {
491                 out[i] = prev_val = out[i] + prev_val + 1;
492         }
493
494         return in;
495 }
496
497 // Like decode_pfor_vb(), but the base values are organized in four
498 // independent streams, for SIMD (presumably SSE2). Supports a whole block only.
499 template<unsigned BlockSize, class Docid>
500 const unsigned char *decode_pfor_vb_interleaved_generic(const unsigned char *in, Docid *out)
501 {
502         const unsigned bit_width = *in++ & 0x3f;
503         unsigned num_exceptions = *in++;
504
505         // Decode the base values.
506         InterleavedBitReader<4> bs0(in + 0 * sizeof(uint32_t), bit_width);
507         InterleavedBitReader<4> bs1(in + 1 * sizeof(uint32_t), bit_width);
508         InterleavedBitReader<4> bs2(in + 2 * sizeof(uint32_t), bit_width);
509         InterleavedBitReader<4> bs3(in + 3 * sizeof(uint32_t), bit_width);
510         for (unsigned i = 0; i < BlockSize / 4; ++i) {
511                 out[i * 4 + 0] = bs0.read();
512                 out[i * 4 + 1] = bs1.read();
513                 out[i * 4 + 2] = bs2.read();
514                 out[i * 4 + 3] = bs3.read();
515         }
516         in += bytes_for_packed_bits(BlockSize, bit_width);
517
518         // Decode exceptions.
519         Docid exceptions[BlockSize];
520         if (*in == 255) {
521                 ++in;
522                 for (unsigned i = 0; i < num_exceptions; ++i) {
523                         exceptions[i] = read_le<Docid>(in);
524                         in += sizeof(Docid);
525                 }
526         } else {
527                 for (unsigned i = 0; i < num_exceptions; ++i) {
528                         in = read_vb(in, &exceptions[i]);
529                 }
530         }
531
532         // Apply exceptions.
533         for (unsigned i = 0; i < num_exceptions; ++i) {
534                 unsigned idx = *in++;
535                 out[idx] |= exceptions[i] << bit_width;
536         }
537
538         // Delta-decode.
539         Docid prev_val = out[-1];
540         for (unsigned i = 0; i < BlockSize; ++i) {
541                 out[i] = prev_val = out[i] + prev_val + 1;
542         }
543
544         return in;
545 }
546
547 template<unsigned BlockSize, class Docid>
548 const unsigned char *decode_pfor_vb_interleaved(const unsigned char *in, Docid *out)
549 {
550         if constexpr (BlockSize == 128 && sizeof(Docid) == sizeof(uint32_t)) {
551                 return decode_pfor_vb_interleaved_128_32(in, out);
552         } else {
553                 return decode_pfor_vb_interleaved_generic(in, out);
554         }
555 }
556
557 __attribute__((target("default")))
558 const unsigned char *decode_pfor_vb_interleaved_128_32(const unsigned char *in, uint32_t *out)
559 {
560         return decode_pfor_vb_interleaved_generic<128>(in, out);
561 }
562
563 // Specialized version for SSE2.
564 __attribute__((target("sse2")))
565 const unsigned char *decode_pfor_vb_interleaved_128_32(const unsigned char *in, uint32_t *out)
566 {
567         constexpr unsigned BlockSize = 128;
568         using Docid = uint32_t;
569
570         const unsigned bit_width = *in++ & 0x3f;
571         unsigned num_exceptions = *in++;
572
573         // Decode the base values.
574         in = decode_bitmap_sse2<BlockSize, false>(in, bit_width, out);
575
576         // Decode exceptions.
577         Docid exceptions[BlockSize];
578         if (*in == 255) {
579                 ++in;
580                 for (unsigned i = 0; i < num_exceptions; ++i) {
581                         exceptions[i] = read_le<Docid>(in);
582                         in += sizeof(Docid);
583                 }
584         } else {
585                 for (unsigned i = 0; i < num_exceptions; ++i) {
586                         in = read_vb(in, &exceptions[i]);
587                 }
588         }
589
590         // Apply exceptions.
591         for (unsigned i = 0; i < num_exceptions; ++i) {
592                 unsigned idx = *in++;
593                 out[idx] |= exceptions[i] << bit_width;
594         }
595
596         delta_decode_sse2<BlockSize>(out);
597
598         return in;
599 }
600
601 enum BlockType {
602         FOR = 0,
603         PFOR_VB = 1,
604         PFOR_BITMAP = 2,
605         CONSTANT = 3
606 };
607
608 template<unsigned BlockSize, class Docid>
609 const unsigned char *decode_pfor_delta1(const unsigned char *in, unsigned num, bool interleaved, Docid *out)
610 {
611         if (num == 0) {
612                 return in;
613         }
614         in = read_baseval(in, out++);
615
616         for (unsigned i = 1; i < num; i += BlockSize, out += BlockSize) {
617                 const unsigned num_this_block = std::min<unsigned>(num - i, BlockSize);
618                 switch (in[0] >> 6) {
619                 case BlockType::FOR:
620                         if (interleaved && num_this_block == BlockSize) {
621                                 dprintf("%d+%d: blocktype=%d (for, interleaved), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
622                                 in = decode_for_interleaved<BlockSize>(in, out);
623                         } else {
624                                 dprintf("%d+%d: blocktype=%d (for), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
625                                 in = decode_for(in, num_this_block, out);
626                         }
627                         break;
628                 case BlockType::PFOR_VB:
629                         if (interleaved && num_this_block == BlockSize) {
630                                 dprintf("%d+%d: blocktype=%d (pfor + vb, interleaved), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
631                                 in = decode_pfor_vb_interleaved<BlockSize>(in, out);
632                         } else {
633                                 dprintf("%d+%d: blocktype=%d (pfor + vb), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
634                                 in = decode_pfor_vb<BlockSize>(in, num_this_block, out);
635                         }
636                         break;
637                 case BlockType::PFOR_BITMAP:
638                         if (interleaved && num_this_block == BlockSize) {
639                                 dprintf("%d+%d: blocktype=%d (pfor + bitmap, interleaved), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
640                                 in = decode_pfor_bitmap_interleaved<BlockSize>(in, out);
641                         } else {
642                                 dprintf("%d+%d: blocktype=%d (pfor + bitmap), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
643                                 in = decode_pfor_bitmap(in, num_this_block, out);
644                         }
645                         break;
646                 case BlockType::CONSTANT:
647                         dprintf("%d+%d: blocktype=%d (constant), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
648                         in = decode_constant(in, num_this_block, out);
649                         break;
650                 }
651         }
652
653         return in;
654 }
655
656 #endif  // !defined(_TURBOPFOR_H)