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 80% 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 // Unlike the reference code, only GCC and GCC-compatible compilers
12 // (e.g. Clang) are supported.
14 // The main reference is https://michael.stapelberg.ch/posts/2019-02-05-turbopfor-analysis/,
15 // although some implementation details have been worked out by studying the
18 // The decoder, like the reference implementation, is not robust against
19 // malicious of corrupted. Several functions (again like the reference
20 // implementation) can read N bytes past the end, so you need to have some slop
21 // in the input buffers; this is documented for each function (unlike
22 // the reference implementation), but the documented slop assumes a
23 // non-malicious encoder.
32 #if defined(__i386__) || defined(__x86_64__)
33 #define COULD_HAVE_SSE2
34 #include <immintrin.h>
37 #include "turbopfor-common.h"
39 // Forward declarations to declare to the template code below that they exist.
40 // (These must seemingly be non-templates for function multiversioning to work.)
41 __attribute__((target("default")))
43 decode_for_interleaved_128_32(const unsigned char *in, uint32_t *out);
44 __attribute__((target("default")))
46 decode_pfor_bitmap_interleaved_128_32(const unsigned char *in, uint32_t *out);
47 __attribute__((target("default")))
49 decode_pfor_vb_interleaved_128_32(const unsigned char *in, uint32_t *out);
51 #ifdef COULD_HAVE_SSE2
52 __attribute__((target("sse2")))
54 decode_for_interleaved_128_32(const unsigned char *in, uint32_t *out);
55 __attribute__((target("sse2")))
57 decode_pfor_bitmap_interleaved_128_32(const unsigned char *in, uint32_t *out);
58 __attribute__((target("sse2")))
60 decode_pfor_vb_interleaved_128_32(const unsigned char *in, uint32_t *out);
64 Docid read_le(const void *in)
67 memcpy(&val, in, sizeof(val));
68 if constexpr (sizeof(Docid) == 8) {
70 } else if constexpr (sizeof(Docid) == 4) {
72 } else if constexpr (sizeof(Docid) == 2) {
74 } else if constexpr (sizeof(Docid) == 1) {
81 // Reads a single value with an encoding that looks a bit like PrefixVarint.
82 // It's unclear why this doesn't use the varbyte encoding.
84 const unsigned char *read_baseval(const unsigned char *in, Docid *out)
86 //fprintf(stderr, "baseval: 0x%02x 0x%02x 0x%02x 0x%02x\n", in[0], in[1], in[2], in[3]);
90 } else if (*in < 192) {
91 *out = ((uint32_t(in[0]) << 8) | uint32_t(in[1])) & 0x3fff;
93 } else if (*in < 224) {
94 *out = ((uint32_t(in[0]) << 16) |
95 (uint32_t(in[2]) << 8) |
96 (uint32_t(in[1]))) & 0x1fffff;
99 assert(false); // Not implemented.
103 // Does not read past the end of the input.
104 template<class Docid>
105 const unsigned char *read_vb(const unsigned char *in, Docid *out)
110 } else if (*in <= 240) {
111 *out = ((uint32_t(in[0] - 177) << 8) | uint32_t(in[1])) + 177;
113 } else if (*in <= 248) {
114 *out = ((uint32_t(in[0] - 241) << 16) | read_le<uint16_t>(in + 1)) + 16561;
116 } else if (*in == 249) {
117 *out = (uint32_t(in[1])) |
118 (uint32_t(in[2]) << 8) |
119 (uint32_t(in[3]) << 16);
121 } else if (*in == 250) {
122 *out = read_le<uint32_t>(in + 1);
131 BitReader(const unsigned char *in, unsigned bits)
132 : in(in), bits(bits), mask(mask_for_bits(bits)) {}
134 // Can read 4 bytes past the end of the input (if bits_used == 0).
137 uint32_t val = (read_le<uint32_t>(in) >> bits_used) & mask;
147 const unsigned char *in;
150 unsigned bits_used = 0;
153 template<unsigned NumStreams>
154 struct InterleavedBitReader {
156 InterleavedBitReader(const unsigned char *in, unsigned bits)
157 : in(in), bits(bits), mask(mask_for_bits(bits)) {}
159 // Can read 4 bytes past the end of the input (if bit_width == 0).
163 if (bits_used + bits > 32) {
164 val = (read_le<uint32_t>(in) >> bits_used) | (read_le<uint32_t>(in + Stride) << (32 - bits_used));
166 val = (read_le<uint32_t>(in) >> bits_used);
170 in += Stride * (bits_used / 32);
177 static constexpr unsigned Stride = NumStreams * sizeof(uint32_t);
178 const unsigned char *in;
181 unsigned bits_used = 0;
184 #ifdef COULD_HAVE_SSE2
185 struct InterleavedBitReaderSSE2 {
187 __attribute__((target("sse2")))
188 InterleavedBitReaderSSE2(const unsigned char *in, unsigned bits)
189 : in(reinterpret_cast<const __m128i *>(in)), bits(bits), mask(_mm_set1_epi32(mask_for_bits(bits))) {}
191 // Can read 16 bytes past the end of the input (if bit_width == 0).
192 __attribute__((target("sse2")))
196 __m128i val = _mm_srli_epi32(_mm_loadu_si128(in), bits_used);
197 if (bits_used + bits > 32) {
198 __m128i val_upper = _mm_slli_epi32(_mm_loadu_si128(in + 1), 32 - bits_used);
199 val = _mm_or_si128(val, val_upper);
201 val = _mm_and_si128(val, mask);
204 in += bits_used / 32;
213 unsigned bits_used = 0;
217 // Constant block. Layout:
219 // - Bit width (6 bits) | type << 6
220 // - Base values (<bits> bits, rounded up to nearest byte)
222 // Can read 4 bytes past the end of the input (if bit_width == 0).
223 template<class Docid>
224 const unsigned char *decode_constant(const unsigned char *in, unsigned num, Docid *out)
226 const unsigned bit_width = *in++ & 0x3f;
227 Docid val = read_le<Docid>(in);
228 if (bit_width < sizeof(Docid) * 8) {
229 val &= mask_for_bits(bit_width);
232 Docid prev_val = out[-1];
233 for (unsigned i = 0; i < num; ++i) {
234 out[i] = prev_val = val + prev_val + 1;
236 return in + div_round_up(bit_width, 8);
239 // FOR block (ie., PFor without exceptions). Layout:
241 // - Bit width (6 bits) | type << 6
242 // - Base values (<num> values of <bits> bits, rounded up to a multiple of 32 values)
244 // Can read 4 bytes past the end of the input (inherit from BitReader).
245 template<class Docid>
246 const unsigned char *decode_for(const unsigned char *in, unsigned num, Docid *out)
248 const unsigned bit_width = *in++ & 0x3f;
250 Docid prev_val = out[-1];
251 BitReader bs(in, bit_width);
252 for (unsigned i = 0; i < num; ++i) {
253 prev_val = out[i] = bs.read() + prev_val + 1;
255 return in + bytes_for_packed_bits(num, bit_width);
258 #ifdef COULD_HAVE_SSE2
259 class DeltaDecoderSSE2 {
261 __attribute__((target("sse2")))
262 DeltaDecoderSSE2(uint32_t prev_val)
263 : prev_val(_mm_set1_epi32(prev_val)) {}
265 __attribute__((target("sse2")))
269 val = _mm_add_epi32(val, _mm_slli_si128(val, 4));
270 val = _mm_add_epi32(val, _mm_slli_si128(val, 8));
271 val = _mm_add_epi32(val, _mm_add_epi32(prev_val, delta));
272 prev_val = _mm_shuffle_epi32(val, _MM_SHUFFLE(3, 3, 3, 3));
277 // Use 4/3/2/1 as delta instead of fixed 1, so that we can do the prev_val + delta
278 // in parallel with something else.
279 const __m128i delta = _mm_set_epi32(4, 3, 2, 1);
284 template<unsigned BlockSize>
285 __attribute__((target("sse2"))) inline void delta_decode_sse2(uint32_t *out)
287 DeltaDecoderSSE2 delta(out[-1]);
288 __m128i *outvec = reinterpret_cast<__m128i *>(out);
289 for (unsigned i = 0; i < BlockSize / 4; ++i) {
290 __m128i val = _mm_loadu_si128(outvec + i);
291 _mm_storeu_si128(outvec + i, delta.decode(val));
295 // Can read 16 bytes past the end of its input (inherit from InterleavedBitReaderSSE2).
296 template<unsigned BlockSize, bool OrWithExisting, bool DeltaDecode, unsigned bit_width>
297 __attribute__((target("sse2")))
298 const unsigned char *
299 decode_bitmap_sse2_unrolled(const unsigned char *in, uint32_t *out)
301 __m128i *outvec = reinterpret_cast<__m128i *>(out);
302 DeltaDecoderSSE2 delta(out[-1]);
303 InterleavedBitReaderSSE2 bs(in, bit_width);
304 #pragma GCC unroll 32
305 for (unsigned i = 0; i < BlockSize / 4; ++i) {
306 __m128i val = bs.read();
307 if constexpr (OrWithExisting) {
308 val = _mm_or_si128(val, _mm_slli_epi32(_mm_loadu_si128(outvec + i), bit_width));
310 if constexpr (DeltaDecode) {
311 val = delta.decode(val);
313 _mm_storeu_si128(outvec + i, val);
315 in += bytes_for_packed_bits(BlockSize, bit_width);
319 // Can read 16 bytes past the end of its input (inherit from InterleavedBitReaderSSE2).
320 template<unsigned BlockSize, bool OrWithExisting, bool DeltaDecode>
321 __attribute__((target("sse2")))
322 const unsigned char *
323 decode_bitmap_sse2(const unsigned char *in, unsigned bit_width, uint32_t *out)
327 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 0>(in, out);
329 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 1>(in, out);
331 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 2>(in, out);
333 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 3>(in, out);
335 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 4>(in, out);
337 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 5>(in, out);
339 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 6>(in, out);
341 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 7>(in, out);
343 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 8>(in, out);
345 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 9>(in, out);
347 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 10>(in, out);
349 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 11>(in, out);
351 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 12>(in, out);
353 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 13>(in, out);
355 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 14>(in, out);
357 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 15>(in, out);
359 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 16>(in, out);
361 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 17>(in, out);
363 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 18>(in, out);
365 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 19>(in, out);
367 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 20>(in, out);
369 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 21>(in, out);
371 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 22>(in, out);
373 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 23>(in, out);
375 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 24>(in, out);
377 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 25>(in, out);
379 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 26>(in, out);
381 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 27>(in, out);
383 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 28>(in, out);
385 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 29>(in, out);
387 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 30>(in, out);
389 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 31>(in, out);
391 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 32>(in, out);
397 // Like decode_for(), but the values are organized in four independent streams,
398 // for SIMD (presumably SSE2). Supports a whole block only.
400 // Can read 16 bytes past the end of its input (inherit from InterleavedBitReader).
401 template<unsigned BlockSize, class Docid>
402 const unsigned char *decode_for_interleaved_generic(const unsigned char *in, Docid *out)
404 const unsigned bit_width = *in++ & 0x3f;
406 InterleavedBitReader<4> bs0(in + 0 * sizeof(uint32_t), bit_width);
407 InterleavedBitReader<4> bs1(in + 1 * sizeof(uint32_t), bit_width);
408 InterleavedBitReader<4> bs2(in + 2 * sizeof(uint32_t), bit_width);
409 InterleavedBitReader<4> bs3(in + 3 * sizeof(uint32_t), bit_width);
410 for (unsigned i = 0; i < BlockSize / 4; ++i) {
411 out[i * 4 + 0] = bs0.read();
412 out[i * 4 + 1] = bs1.read();
413 out[i * 4 + 2] = bs2.read();
414 out[i * 4 + 3] = bs3.read();
416 Docid prev_val = out[-1];
417 for (unsigned i = 0; i < BlockSize; ++i) {
418 out[i] = prev_val = out[i] + prev_val + 1;
420 return in + bytes_for_packed_bits(BlockSize, bit_width);
423 // Does not read past the end of the input.
424 template<unsigned BlockSize, class Docid>
425 const unsigned char *decode_for_interleaved(const unsigned char *in, Docid *out)
427 if constexpr (BlockSize == 128 && sizeof(Docid) == sizeof(uint32_t)) {
428 return decode_for_interleaved_128_32(in, out);
430 return decode_for_interleaved_generic(in, out);
434 // Does not read past the end of the input.
435 __attribute__((target("default")))
436 const unsigned char *
437 decode_for_interleaved_128_32(const unsigned char *in, uint32_t *out)
439 return decode_for_interleaved_generic<128>(in, out);
442 #ifdef COULD_HAVE_SSE2
443 // Specialized version for SSE2.
444 // Can read 16 bytes past the end of the input (inherit from decode_bitmap_sse2()).
445 __attribute__((target("sse2")))
446 const unsigned char *
447 decode_for_interleaved_128_32(const unsigned char *in, uint32_t *out)
449 constexpr unsigned BlockSize = 128;
451 const unsigned bit_width = *in++ & 0x3f;
453 in = decode_bitmap_sse2<BlockSize, /*OrWithExisting=*/false, /*DeltaDecode=*/true>(in, bit_width, out);
459 // Can read 4 bytes past the end of the input (inherit from BitReader).
460 template<class Docid>
461 const unsigned char *decode_pfor_bitmap_exceptions(const unsigned char *in, unsigned num, Docid *out)
463 const unsigned exception_bit_width = *in++;
464 const uint64_t *exception_bitmap_ptr = reinterpret_cast<const uint64_t *>(in);
465 in += div_round_up(num, 8);
467 int num_exceptions = 0;
469 BitReader bs(in, exception_bit_width);
470 for (unsigned i = 0; i < num; i += 64, ++exception_bitmap_ptr) {
471 uint64_t exceptions = read_le<uint64_t>(exception_bitmap_ptr);
473 // We've read some bytes past the end, so clear out the junk bits.
474 exceptions &= (1ULL << (num - i)) - 1;
476 for (; exceptions != 0; exceptions &= exceptions - 1, ++num_exceptions) {
477 unsigned idx = (ffsll(exceptions) - 1) + i;
478 out[idx] = bs.read();
481 in += bytes_for_packed_bits(num_exceptions, exception_bit_width);
485 // PFor block with bitmap exceptions. Layout:
487 // - Bit width (6 bits) | type << 6
488 // - Exception bit width (8 bits)
489 // - Bitmap of which values have exceptions (<num> bits, rounded up to a byte)
490 // - Exceptions (<num_exc> values of <bits_exc> bits, rounded up to a byte)
491 // - Base values (<num> values of <bits> bits, rounded up to a byte)
493 // Can read 4 bytes past the end of the input (inherit from BitReader).
494 template<class Docid>
495 const unsigned char *decode_pfor_bitmap(const unsigned char *in, unsigned num, Docid *out)
497 memset(out, 0, num * sizeof(Docid));
499 const unsigned bit_width = *in++ & 0x3f;
501 in = decode_pfor_bitmap_exceptions(in, num, out);
503 // Decode the base values, and delta-decode.
504 Docid prev_val = out[-1];
505 BitReader bs(in, bit_width);
506 for (unsigned i = 0; i < num; ++i) {
507 out[i] = prev_val = ((out[i] << bit_width) | bs.read()) + prev_val + 1;
509 return in + bytes_for_packed_bits(num, bit_width);
512 // Like decode_pfor_bitmap(), but the base values are organized in four
513 // independent streams, for SIMD (presumably SSE2). Supports a whole block only.
515 // Can read 16 bytes past the end of the input (inherit from InterleavedBitReader
516 // and decode_pfor_bitmap_exceptions()).
517 template<unsigned BlockSize, class Docid>
518 const unsigned char *decode_pfor_bitmap_interleaved_generic(const unsigned char *in, Docid *out)
520 memset(out, 0, BlockSize * sizeof(Docid));
522 const unsigned bit_width = *in++ & 0x3f;
524 in = decode_pfor_bitmap_exceptions(in, BlockSize, out);
526 // Decode the base values.
527 InterleavedBitReader<4> bs0(in + 0 * sizeof(uint32_t), bit_width);
528 InterleavedBitReader<4> bs1(in + 1 * sizeof(uint32_t), bit_width);
529 InterleavedBitReader<4> bs2(in + 2 * sizeof(uint32_t), bit_width);
530 InterleavedBitReader<4> bs3(in + 3 * sizeof(uint32_t), bit_width);
531 for (unsigned i = 0; i < BlockSize / 4; ++i) {
532 out[i * 4 + 0] = bs0.read() | (out[i * 4 + 0] << bit_width);
533 out[i * 4 + 1] = bs1.read() | (out[i * 4 + 1] << bit_width);
534 out[i * 4 + 2] = bs2.read() | (out[i * 4 + 2] << bit_width);
535 out[i * 4 + 3] = bs3.read() | (out[i * 4 + 3] << bit_width);
539 Docid prev_val = out[-1];
540 for (unsigned i = 0; i < BlockSize; ++i) {
541 out[i] = prev_val = out[i] + prev_val + 1;
543 return in + bytes_for_packed_bits(BlockSize, bit_width);
546 // Can read 16 bytes past the end of the input (inherit from decode_pfor_bitmap_interleaved_generic()).
547 template<unsigned BlockSize, class Docid>
548 const unsigned char *decode_pfor_bitmap_interleaved(const unsigned char *in, Docid *out)
550 if constexpr (BlockSize == 128 && sizeof(Docid) == sizeof(uint32_t)) {
551 return decode_pfor_bitmap_interleaved_128_32(in, out);
553 return decode_pfor_bitmap_interleaved_generic(in, out);
557 __attribute__((target("default")))
558 const unsigned char *
559 decode_pfor_bitmap_interleaved_128_32(const unsigned char *in, uint32_t *out)
561 return decode_pfor_bitmap_interleaved_generic<128>(in, out);
564 #ifdef COULD_HAVE_SSE2
565 // Specialized version for SSE2.
567 // Can read 16 bytes past the end of the input (inherit from InterleavedBitReaderSSE2
568 // and decode_pfor_bitmap_exceptions()).
569 __attribute__((target("sse2")))
570 const unsigned char *
571 decode_pfor_bitmap_interleaved_128_32(const unsigned char *in, uint32_t *out)
573 constexpr unsigned BlockSize = 128;
575 // Set all output values to zero, before the exceptions are filled in.
577 for (unsigned i = 0; i < BlockSize / 4; ++i) {
578 _mm_storeu_si128(reinterpret_cast<__m128i *>(out) + i, _mm_setzero_si128());
581 const unsigned bit_width = *in++ & 0x3f;
583 in = decode_pfor_bitmap_exceptions(in, BlockSize, out);
584 in = decode_bitmap_sse2<BlockSize, /*OrWithExisting=*/true, /*DeltaDecode=*/true>(in, bit_width, out);
590 // PFor block with variable-byte exceptions. Layout:
592 // - Bit width (6 bits) | type << 6
593 // - Number of exceptions (8 bits)
594 // - Base values (<num> values of <bits> bits, rounded up to a byte)
596 // - If first byte is 255, <num_exc> 32-bit values (does not include the 255 byte)
597 // - Else, <num_exc> varbyte-encoded values (includes the non-255 byte)
598 // - Indexes of exceptions (<num_exc> bytes).
600 // Can read 4 bytes past the end of the input (inherit from BitReader,
601 // assuming zero exceptions).
602 template<unsigned BlockSize, class Docid>
603 const unsigned char *decode_pfor_vb(const unsigned char *in, unsigned num, Docid *out)
605 //fprintf(stderr, "in=%p out=%p num=%u\n", in, out, num);
607 const unsigned bit_width = *in++ & 0x3f;
608 unsigned num_exceptions = *in++;
610 // Decode the base values.
611 BitReader bs(in, bit_width);
612 for (unsigned i = 0; i < num; ++i) {
615 in += bytes_for_packed_bits(num, bit_width);
617 // Decode exceptions.
618 Docid exceptions[BlockSize];
621 for (unsigned i = 0; i < num_exceptions; ++i) {
622 exceptions[i] = read_le<Docid>(in);
626 for (unsigned i = 0; i < num_exceptions; ++i) {
627 in = read_vb(in, &exceptions[i]);
631 for (unsigned i = 0; i < num_exceptions; ++i) {
632 unsigned idx = *in++;
633 out[idx] |= exceptions[i] << bit_width;
637 Docid prev_val = out[-1];
638 for (unsigned i = 0; i < num; ++i) {
639 out[i] = prev_val = out[i] + prev_val + 1;
645 // Like decode_pfor_vb(), but the base values are organized in four
646 // independent streams, for SIMD (presumably SSE2). Supports a whole block only.
647 // Can read 16 bytes past the end of its input (inherit from InterleavedBitReader).
648 template<unsigned BlockSize, class Docid>
649 const unsigned char *decode_pfor_vb_interleaved_generic(const unsigned char *in, Docid *out)
651 const unsigned bit_width = *in++ & 0x3f;
652 unsigned num_exceptions = *in++;
654 // Decode the base values.
655 InterleavedBitReader<4> bs0(in + 0 * sizeof(uint32_t), bit_width);
656 InterleavedBitReader<4> bs1(in + 1 * sizeof(uint32_t), bit_width);
657 InterleavedBitReader<4> bs2(in + 2 * sizeof(uint32_t), bit_width);
658 InterleavedBitReader<4> bs3(in + 3 * sizeof(uint32_t), bit_width);
659 for (unsigned i = 0; i < BlockSize / 4; ++i) {
660 out[i * 4 + 0] = bs0.read();
661 out[i * 4 + 1] = bs1.read();
662 out[i * 4 + 2] = bs2.read();
663 out[i * 4 + 3] = bs3.read();
665 in += bytes_for_packed_bits(BlockSize, bit_width);
667 // Decode exceptions.
668 Docid exceptions[BlockSize];
671 for (unsigned i = 0; i < num_exceptions; ++i) {
672 exceptions[i] = read_le<Docid>(in);
676 for (unsigned i = 0; i < num_exceptions; ++i) {
677 in = read_vb(in, &exceptions[i]);
682 for (unsigned i = 0; i < num_exceptions; ++i) {
683 unsigned idx = *in++;
684 out[idx] |= exceptions[i] << bit_width;
688 Docid prev_val = out[-1];
689 for (unsigned i = 0; i < BlockSize; ++i) {
690 out[i] = prev_val = out[i] + prev_val + 1;
696 // Can read 16 bytes past the end of its input (inherit from decode_pfor_vb_interleaved_generic()).
697 template<unsigned BlockSize, class Docid>
698 const unsigned char *decode_pfor_vb_interleaved(const unsigned char *in, Docid *out)
700 if constexpr (BlockSize == 128 && sizeof(Docid) == sizeof(uint32_t)) {
701 return decode_pfor_vb_interleaved_128_32(in, out);
703 return decode_pfor_vb_interleaved_generic(in, out);
707 __attribute__((target("default")))
708 const unsigned char *
709 decode_pfor_vb_interleaved_128_32(const unsigned char *in, uint32_t *out)
711 return decode_pfor_vb_interleaved_generic<128>(in, out);
714 // Specialized version for SSE2.
715 // Can read 16 bytes past the end of the input (inherit from decode_bitmap_sse2()).
716 __attribute__((target("sse2")))
717 const unsigned char *
718 decode_pfor_vb_interleaved_128_32(const unsigned char *in, uint32_t *out)
720 constexpr unsigned BlockSize = 128;
721 using Docid = uint32_t;
723 const unsigned bit_width = *in++ & 0x3f;
724 unsigned num_exceptions = *in++;
726 // Decode the base values.
727 in = decode_bitmap_sse2<BlockSize, /*OrWithExisting=*/false, /*DeltaDecode=*/false>(in, bit_width, out);
729 // Decode exceptions.
730 Docid exceptions[BlockSize];
733 for (unsigned i = 0; i < num_exceptions; ++i) {
734 exceptions[i] = read_le<Docid>(in);
738 for (unsigned i = 0; i < num_exceptions; ++i) {
739 in = read_vb(in, &exceptions[i]);
744 for (unsigned i = 0; i < num_exceptions; ++i) {
745 unsigned idx = *in++;
746 out[idx] |= exceptions[i] << bit_width;
749 delta_decode_sse2<BlockSize>(out);
754 // Can read 16 bytes past the end of the input (inherit from several functions).
755 template<unsigned BlockSize, class Docid>
756 const unsigned char *decode_pfor_delta1(const unsigned char *in, unsigned num, bool interleaved, Docid *out)
761 in = read_baseval(in, out++);
763 for (unsigned i = 1; i < num; i += BlockSize, out += BlockSize) {
764 const unsigned num_this_block = std::min<unsigned>(num - i, BlockSize);
765 switch (in[0] >> 6) {
767 if (interleaved && num_this_block == BlockSize) {
768 dprintf("%d+%d: blocktype=%d (for, interleaved), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
769 in = decode_for_interleaved<BlockSize>(in, out);
771 dprintf("%d+%d: blocktype=%d (for), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
772 in = decode_for(in, num_this_block, out);
775 case BlockType::PFOR_VB:
776 if (interleaved && num_this_block == BlockSize) {
777 dprintf("%d+%d: blocktype=%d (pfor + vb, interleaved), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
778 in = decode_pfor_vb_interleaved<BlockSize>(in, out);
780 dprintf("%d+%d: blocktype=%d (pfor + vb), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
781 in = decode_pfor_vb<BlockSize>(in, num_this_block, out);
784 case BlockType::PFOR_BITMAP:
785 if (interleaved && num_this_block == BlockSize) {
786 dprintf("%d+%d: blocktype=%d (pfor + bitmap, interleaved), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
787 in = decode_pfor_bitmap_interleaved<BlockSize>(in, out);
789 dprintf("%d+%d: blocktype=%d (pfor + bitmap), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
790 in = decode_pfor_bitmap(in, num_this_block, out);
793 case BlockType::CONSTANT:
794 dprintf("%d+%d: blocktype=%d (constant), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
795 in = decode_constant(in, num_this_block, out);
803 #endif // !defined(_TURBOPFOR_H)