8 #if defined(__i386__) || defined(__x86_64__)
9 #define COULD_HAVE_SSE2
10 #include <immintrin.h>
13 #include "turbopfor-common.h"
16 //#define dprintf(...) fprintf(stderr, __VA_ARGS__);
18 // Forward declarations to declare to the template code below that they exist.
19 // (These must seemingly be non-templates for function multiversioning to work.)
20 __attribute__((target("default")))
22 decode_for_interleaved_128_32(const unsigned char *in, uint32_t *out);
23 __attribute__((target("default")))
25 decode_pfor_bitmap_interleaved_128_32(const unsigned char *in, uint32_t *out);
26 __attribute__((target("default")))
28 decode_pfor_vb_interleaved_128_32(const unsigned char *in, uint32_t *out);
30 #ifdef COULD_HAVE_SSE2
31 __attribute__((target("sse2")))
33 decode_for_interleaved_128_32(const unsigned char *in, uint32_t *out);
34 __attribute__((target("sse2")))
36 decode_pfor_bitmap_interleaved_128_32(const unsigned char *in, uint32_t *out);
37 __attribute__((target("sse2")))
39 decode_pfor_vb_interleaved_128_32(const unsigned char *in, uint32_t *out);
43 Docid read_le(const void *in)
46 memcpy(&val, in, sizeof(val));
47 if constexpr (sizeof(Docid) == 8) {
49 } else if constexpr (sizeof(Docid) == 4) {
51 } else if constexpr (sizeof(Docid) == 2) {
53 } else if constexpr (sizeof(Docid) == 1) {
60 // Reads a single value with an encoding that looks a bit like PrefixVarint.
61 // It's unclear why this doesn't use the varbyte encoding.
63 const unsigned char *read_baseval(const unsigned char *in, Docid *out)
65 //fprintf(stderr, "baseval: 0x%02x 0x%02x 0x%02x 0x%02x\n", in[0], in[1], in[2], in[3]);
69 } else if (*in < 192) {
70 *out = ((uint32_t(in[0]) << 8) | uint32_t(in[1])) & 0x3fff;
72 } else if (*in < 224) {
73 *out = ((uint32_t(in[0]) << 16) |
74 (uint32_t(in[2]) << 8) |
75 (uint32_t(in[1]))) & 0x1fffff;
78 assert(false); // Not implemented.
82 // Does not read past the end of the input.
84 const unsigned char *read_vb(const unsigned char *in, Docid *out)
89 } else if (*in <= 240) {
90 *out = ((uint32_t(in[0] - 177) << 8) | uint32_t(in[1])) + 177;
92 } else if (*in <= 248) {
93 *out = ((uint32_t(in[0] - 241) << 16) | read_le<uint16_t>(in + 1)) + 16561;
95 } else if (*in == 249) {
96 *out = (uint32_t(in[1])) |
97 (uint32_t(in[2]) << 8) |
98 (uint32_t(in[3]) << 16);
100 } else if (*in == 250) {
101 *out = read_le<uint32_t>(in + 1);
110 BitReader(const unsigned char *in, unsigned bits)
111 : in(in), bits(bits), mask(mask_for_bits(bits)) {}
113 // Can read 4 bytes past the end of the input (if bits_used == 0).
116 uint32_t val = (read_le<uint32_t>(in) >> bits_used) & mask;
126 const unsigned char *in;
129 unsigned bits_used = 0;
132 template<unsigned NumStreams>
133 struct InterleavedBitReader {
135 InterleavedBitReader(const unsigned char *in, unsigned bits)
136 : in(in), bits(bits), mask(mask_for_bits(bits)) {}
138 // Can read 4 bytes past the end of the input (if bit_width == 0).
142 if (bits_used + bits > 32) {
143 val = (read_le<uint32_t>(in) >> bits_used) | (read_le<uint32_t>(in + Stride) << (32 - bits_used));
145 val = (read_le<uint32_t>(in) >> bits_used);
149 in += Stride * (bits_used / 32);
156 static constexpr unsigned Stride = NumStreams * sizeof(uint32_t);
157 const unsigned char *in;
160 unsigned bits_used = 0;
163 #ifdef COULD_HAVE_SSE2
164 struct InterleavedBitReaderSSE2 {
166 __attribute__((target("sse2")))
167 InterleavedBitReaderSSE2(const unsigned char *in, unsigned bits)
168 : in(reinterpret_cast<const __m128i *>(in)), bits(bits), mask(_mm_set1_epi32(mask_for_bits(bits))) {}
170 // Can read 16 bytes past the end of the input (if bit_width == 0).
171 __attribute__((target("sse2")))
175 __m128i val = _mm_srli_epi32(_mm_loadu_si128(in), bits_used);
176 if (bits_used + bits > 32) {
177 __m128i val_upper = _mm_slli_epi32(_mm_loadu_si128(in + 1), 32 - bits_used);
178 val = _mm_or_si128(val, val_upper);
180 val = _mm_and_si128(val, mask);
183 in += bits_used / 32;
192 unsigned bits_used = 0;
196 // Constant block. Layout:
198 // - Bit width (6 bits) | type << 6
199 // - Base values (<bits> bits, rounded up to nearest byte)
201 // Can read 4 bytes past the end of the input (if bit_width == 0).
202 template<class Docid>
203 const unsigned char *decode_constant(const unsigned char *in, unsigned num, Docid *out)
205 const unsigned bit_width = *in++ & 0x3f;
206 Docid val = read_le<Docid>(in);
207 if (bit_width < sizeof(Docid) * 8) {
208 val &= mask_for_bits(bit_width);
211 Docid prev_val = out[-1];
212 for (unsigned i = 0; i < num; ++i) {
213 out[i] = prev_val = val + prev_val + 1;
215 return in + div_round_up(bit_width, 8);
218 // FOR block (ie., PFor without exceptions). Layout:
220 // - Bit width (6 bits) | type << 6
221 // - Base values (<num> values of <bits> bits, rounded up to a multiple of 32 values)
223 // Can read 4 bytes past the end of the input (inherit from BitReader).
224 template<class Docid>
225 const unsigned char *decode_for(const unsigned char *in, unsigned num, Docid *out)
227 const unsigned bit_width = *in++ & 0x3f;
229 Docid prev_val = out[-1];
230 BitReader bs(in, bit_width);
231 for (unsigned i = 0; i < num; ++i) {
232 prev_val = out[i] = bs.read() + prev_val + 1;
234 return in + bytes_for_packed_bits(num, bit_width);
237 #ifdef COULD_HAVE_SSE2
238 class DeltaDecoderSSE2 {
240 __attribute__((target("sse2")))
241 DeltaDecoderSSE2(uint32_t prev_val)
242 : prev_val(_mm_set1_epi32(prev_val)) {}
244 __attribute__((target("sse2")))
248 val = _mm_add_epi32(val, _mm_slli_si128(val, 4));
249 val = _mm_add_epi32(val, _mm_slli_si128(val, 8));
250 val = _mm_add_epi32(val, _mm_add_epi32(prev_val, delta));
251 prev_val = _mm_shuffle_epi32(val, _MM_SHUFFLE(3, 3, 3, 3));
256 // Use 4/3/2/1 as delta instead of fixed 1, so that we can do the prev_val + delta
257 // in parallel with something else.
258 const __m128i delta = _mm_set_epi32(4, 3, 2, 1);
263 template<unsigned BlockSize>
264 __attribute__((target("sse2"))) inline void delta_decode_sse2(uint32_t *out)
266 DeltaDecoderSSE2 delta(out[-1]);
267 __m128i *outvec = reinterpret_cast<__m128i *>(out);
268 for (unsigned i = 0; i < BlockSize / 4; ++i) {
269 __m128i val = _mm_loadu_si128(outvec + i);
270 _mm_storeu_si128(outvec + i, delta.decode(val));
274 // Can read 16 bytes past the end of its input (inherit from InterleavedBitReaderSSE2).
275 template<unsigned BlockSize, bool OrWithExisting, bool DeltaDecode, unsigned bit_width>
276 __attribute__((target("sse2")))
277 const unsigned char *
278 decode_bitmap_sse2_unrolled(const unsigned char *in, uint32_t *out)
280 __m128i *outvec = reinterpret_cast<__m128i *>(out);
281 DeltaDecoderSSE2 delta(out[-1]);
282 InterleavedBitReaderSSE2 bs(in, bit_width);
283 #pragma GCC unroll 32
284 for (unsigned i = 0; i < BlockSize / 4; ++i) {
285 __m128i val = bs.read();
286 if constexpr (OrWithExisting) {
287 val = _mm_or_si128(val, _mm_slli_epi32(_mm_loadu_si128(outvec + i), bit_width));
289 if constexpr (DeltaDecode) {
290 val = delta.decode(val);
292 _mm_storeu_si128(outvec + i, val);
294 in += bytes_for_packed_bits(BlockSize, bit_width);
298 // Can read 16 bytes past the end of its input (inherit from InterleavedBitReaderSSE2).
299 template<unsigned BlockSize, bool OrWithExisting, bool DeltaDecode>
300 __attribute__((target("sse2")))
301 const unsigned char *
302 decode_bitmap_sse2(const unsigned char *in, unsigned bit_width, uint32_t *out)
306 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 0>(in, out);
308 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 1>(in, out);
310 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 2>(in, out);
312 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 3>(in, out);
314 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 4>(in, out);
316 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 5>(in, out);
318 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 6>(in, out);
320 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 7>(in, out);
322 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 8>(in, out);
324 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 9>(in, out);
326 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 10>(in, out);
328 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 11>(in, out);
330 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 12>(in, out);
332 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 13>(in, out);
334 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 14>(in, out);
336 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 15>(in, out);
338 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 16>(in, out);
340 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 17>(in, out);
342 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 18>(in, out);
344 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 19>(in, out);
346 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 20>(in, out);
348 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 21>(in, out);
350 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 22>(in, out);
352 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 23>(in, out);
354 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 24>(in, out);
356 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 25>(in, out);
358 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 26>(in, out);
360 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 27>(in, out);
362 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 28>(in, out);
364 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 29>(in, out);
366 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 30>(in, out);
368 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 31>(in, out);
370 return decode_bitmap_sse2_unrolled<BlockSize, OrWithExisting, DeltaDecode, 32>(in, out);
376 // Like decode_for(), but the values are organized in four independent streams,
377 // for SIMD (presumably SSE2). Supports a whole block only.
379 // Can read 16 bytes past the end of its input (inherit from InterleavedBitReader).
380 template<unsigned BlockSize, class Docid>
381 const unsigned char *decode_for_interleaved_generic(const unsigned char *in, Docid *out)
383 const unsigned bit_width = *in++ & 0x3f;
385 InterleavedBitReader<4> bs0(in + 0 * sizeof(uint32_t), bit_width);
386 InterleavedBitReader<4> bs1(in + 1 * sizeof(uint32_t), bit_width);
387 InterleavedBitReader<4> bs2(in + 2 * sizeof(uint32_t), bit_width);
388 InterleavedBitReader<4> bs3(in + 3 * sizeof(uint32_t), bit_width);
389 for (unsigned i = 0; i < BlockSize / 4; ++i) {
390 out[i * 4 + 0] = bs0.read();
391 out[i * 4 + 1] = bs1.read();
392 out[i * 4 + 2] = bs2.read();
393 out[i * 4 + 3] = bs3.read();
395 Docid prev_val = out[-1];
396 for (unsigned i = 0; i < BlockSize; ++i) {
397 out[i] = prev_val = out[i] + prev_val + 1;
399 return in + bytes_for_packed_bits(BlockSize, bit_width);
402 // Does not read past the end of the input.
403 template<unsigned BlockSize, class Docid>
404 const unsigned char *decode_for_interleaved(const unsigned char *in, Docid *out)
406 if constexpr (BlockSize == 128 && sizeof(Docid) == sizeof(uint32_t)) {
407 return decode_for_interleaved_128_32(in, out);
409 return decode_for_interleaved_generic(in, out);
413 // Does not read past the end of the input.
414 __attribute__((target("default")))
415 const unsigned char *
416 decode_for_interleaved_128_32(const unsigned char *in, uint32_t *out)
418 return decode_for_interleaved_generic<128>(in, out);
421 #ifdef COULD_HAVE_SSE2
422 // Specialized version for SSE2.
423 // Can read 16 bytes past the end of the input (inherit from decode_bitmap_sse2()).
424 __attribute__((target("sse2")))
425 const unsigned char *
426 decode_for_interleaved_128_32(const unsigned char *in, uint32_t *out)
428 constexpr unsigned BlockSize = 128;
430 const unsigned bit_width = *in++ & 0x3f;
432 in = decode_bitmap_sse2<BlockSize, /*OrWithExisting=*/false, /*DeltaDecode=*/true>(in, bit_width, out);
438 // Can read 4 bytes past the end of the input (inherit from BitReader).
439 template<class Docid>
440 const unsigned char *decode_pfor_bitmap_exceptions(const unsigned char *in, unsigned num, Docid *out)
442 const unsigned exception_bit_width = *in++;
443 const uint64_t *exception_bitmap_ptr = reinterpret_cast<const uint64_t *>(in);
444 in += div_round_up(num, 8);
446 int num_exceptions = 0;
448 BitReader bs(in, exception_bit_width);
449 for (unsigned i = 0; i < num; i += 64, ++exception_bitmap_ptr) {
450 uint64_t exceptions = read_le<uint64_t>(exception_bitmap_ptr);
452 // We've read some bytes past the end, so clear out the junk bits.
453 exceptions &= (1ULL << (num - i)) - 1;
455 for (; exceptions != 0; exceptions &= exceptions - 1, ++num_exceptions) {
456 unsigned idx = (ffsll(exceptions) - 1) + i;
457 out[idx] = bs.read();
460 in += bytes_for_packed_bits(num_exceptions, exception_bit_width);
464 // PFor block with bitmap exceptions. Layout:
466 // - Bit width (6 bits) | type << 6
467 // - Exception bit width (8 bits)
468 // - Bitmap of which values have exceptions (<num> bits, rounded up to a byte)
469 // - Exceptions (<num_exc> values of <bits_exc> bits, rounded up to a byte)
470 // - Base values (<num> values of <bits> bits, rounded up to a byte)
472 // Can read 4 bytes past the end of the input (inherit from BitReader).
473 template<class Docid>
474 const unsigned char *decode_pfor_bitmap(const unsigned char *in, unsigned num, Docid *out)
476 memset(out, 0, num * sizeof(Docid));
478 const unsigned bit_width = *in++ & 0x3f;
480 in = decode_pfor_bitmap_exceptions(in, num, out);
482 // Decode the base values, and delta-decode.
483 Docid prev_val = out[-1];
484 BitReader bs(in, bit_width);
485 for (unsigned i = 0; i < num; ++i) {
486 out[i] = prev_val = ((out[i] << bit_width) | bs.read()) + prev_val + 1;
488 return in + bytes_for_packed_bits(num, bit_width);
491 // Like decode_pfor_bitmap(), but the base values are organized in four
492 // independent streams, for SIMD (presumably SSE2). Supports a whole block only.
494 // Can read 16 bytes past the end of the input (inherit from InterleavedBitReader
495 // and decode_pfor_bitmap_exceptions()).
496 template<unsigned BlockSize, class Docid>
497 const unsigned char *decode_pfor_bitmap_interleaved_generic(const unsigned char *in, Docid *out)
499 memset(out, 0, BlockSize * sizeof(Docid));
501 const unsigned bit_width = *in++ & 0x3f;
503 in = decode_pfor_bitmap_exceptions(in, BlockSize, out);
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() | (out[i * 4 + 0] << bit_width);
512 out[i * 4 + 1] = bs1.read() | (out[i * 4 + 1] << bit_width);
513 out[i * 4 + 2] = bs2.read() | (out[i * 4 + 2] << bit_width);
514 out[i * 4 + 3] = bs3.read() | (out[i * 4 + 3] << bit_width);
518 Docid prev_val = out[-1];
519 for (unsigned i = 0; i < BlockSize; ++i) {
520 out[i] = prev_val = out[i] + prev_val + 1;
522 return in + bytes_for_packed_bits(BlockSize, bit_width);
525 // Can read 16 bytes past the end of the input (inherit from decode_pfor_bitmap_interleaved_generic()).
526 template<unsigned BlockSize, class Docid>
527 const unsigned char *decode_pfor_bitmap_interleaved(const unsigned char *in, Docid *out)
529 if constexpr (BlockSize == 128 && sizeof(Docid) == sizeof(uint32_t)) {
530 return decode_pfor_bitmap_interleaved_128_32(in, out);
532 return decode_pfor_bitmap_interleaved_generic(in, out);
536 __attribute__((target("default")))
537 const unsigned char *
538 decode_pfor_bitmap_interleaved_128_32(const unsigned char *in, uint32_t *out)
540 return decode_pfor_bitmap_interleaved_generic<128>(in, out);
543 #ifdef COULD_HAVE_SSE2
544 // Specialized version for SSE2.
546 // Can read 16 bytes past the end of the input (inherit from InterleavedBitReaderSSE2
547 // and decode_pfor_bitmap_exceptions()).
548 __attribute__((target("sse2")))
549 const unsigned char *
550 decode_pfor_bitmap_interleaved_128_32(const unsigned char *in, uint32_t *out)
552 constexpr unsigned BlockSize = 128;
554 // Set all output values to zero, before the exceptions are filled in.
556 for (unsigned i = 0; i < BlockSize / 4; ++i) {
557 _mm_storeu_si128(reinterpret_cast<__m128i *>(out) + i, _mm_setzero_si128());
560 const unsigned bit_width = *in++ & 0x3f;
562 in = decode_pfor_bitmap_exceptions(in, BlockSize, out);
563 in = decode_bitmap_sse2<BlockSize, /*OrWithExisting=*/true, /*DeltaDecode=*/true>(in, bit_width, out);
569 // PFor block with variable-byte exceptions. Layout:
571 // - Bit width (6 bits) | type << 6
572 // - Number of exceptions (8 bits)
573 // - Base values (<num> values of <bits> bits, rounded up to a byte)
575 // - If first byte is 255, <num_exc> 32-bit values (does not include the 255 byte)
576 // - Else, <num_exc> varbyte-encoded values (includes the non-255 byte)
577 // - Indexes of exceptions (<num_exc> bytes).
579 // Can read 4 bytes past the end of the input (inherit from BitReader,
580 // assuming zero exceptions).
581 template<unsigned BlockSize, class Docid>
582 const unsigned char *decode_pfor_vb(const unsigned char *in, unsigned num, Docid *out)
584 //fprintf(stderr, "in=%p out=%p num=%u\n", in, out, num);
586 const unsigned bit_width = *in++ & 0x3f;
587 unsigned num_exceptions = *in++;
589 // Decode the base values.
590 BitReader bs(in, bit_width);
591 for (unsigned i = 0; i < num; ++i) {
594 in += bytes_for_packed_bits(num, bit_width);
596 // Decode exceptions.
597 Docid exceptions[BlockSize];
600 for (unsigned i = 0; i < num_exceptions; ++i) {
601 exceptions[i] = read_le<Docid>(in);
605 for (unsigned i = 0; i < num_exceptions; ++i) {
606 in = read_vb(in, &exceptions[i]);
610 for (unsigned i = 0; i < num_exceptions; ++i) {
611 unsigned idx = *in++;
612 out[idx] |= exceptions[i] << bit_width;
616 Docid prev_val = out[-1];
617 for (unsigned i = 0; i < num; ++i) {
618 out[i] = prev_val = out[i] + prev_val + 1;
624 // Like decode_pfor_vb(), but the base values are organized in four
625 // independent streams, for SIMD (presumably SSE2). Supports a whole block only.
626 // Can read 16 bytes past the end of its input (inherit from InterleavedBitReader).
627 template<unsigned BlockSize, class Docid>
628 const unsigned char *decode_pfor_vb_interleaved_generic(const unsigned char *in, Docid *out)
630 const unsigned bit_width = *in++ & 0x3f;
631 unsigned num_exceptions = *in++;
633 // Decode the base values.
634 InterleavedBitReader<4> bs0(in + 0 * sizeof(uint32_t), bit_width);
635 InterleavedBitReader<4> bs1(in + 1 * sizeof(uint32_t), bit_width);
636 InterleavedBitReader<4> bs2(in + 2 * sizeof(uint32_t), bit_width);
637 InterleavedBitReader<4> bs3(in + 3 * sizeof(uint32_t), bit_width);
638 for (unsigned i = 0; i < BlockSize / 4; ++i) {
639 out[i * 4 + 0] = bs0.read();
640 out[i * 4 + 1] = bs1.read();
641 out[i * 4 + 2] = bs2.read();
642 out[i * 4 + 3] = bs3.read();
644 in += bytes_for_packed_bits(BlockSize, bit_width);
646 // Decode exceptions.
647 Docid exceptions[BlockSize];
650 for (unsigned i = 0; i < num_exceptions; ++i) {
651 exceptions[i] = read_le<Docid>(in);
655 for (unsigned i = 0; i < num_exceptions; ++i) {
656 in = read_vb(in, &exceptions[i]);
661 for (unsigned i = 0; i < num_exceptions; ++i) {
662 unsigned idx = *in++;
663 out[idx] |= exceptions[i] << bit_width;
667 Docid prev_val = out[-1];
668 for (unsigned i = 0; i < BlockSize; ++i) {
669 out[i] = prev_val = out[i] + prev_val + 1;
675 // Can read 16 bytes past the end of its input (inherit from decode_pfor_vb_interleaved_generic()).
676 template<unsigned BlockSize, class Docid>
677 const unsigned char *decode_pfor_vb_interleaved(const unsigned char *in, Docid *out)
679 if constexpr (BlockSize == 128 && sizeof(Docid) == sizeof(uint32_t)) {
680 return decode_pfor_vb_interleaved_128_32(in, out);
682 return decode_pfor_vb_interleaved_generic(in, out);
686 __attribute__((target("default")))
687 const unsigned char *
688 decode_pfor_vb_interleaved_128_32(const unsigned char *in, uint32_t *out)
690 return decode_pfor_vb_interleaved_generic<128>(in, out);
693 // Specialized version for SSE2.
694 // Can read 16 bytes past the end of the input (inherit from decode_bitmap_sse2()).
695 __attribute__((target("sse2")))
696 const unsigned char *
697 decode_pfor_vb_interleaved_128_32(const unsigned char *in, uint32_t *out)
699 constexpr unsigned BlockSize = 128;
700 using Docid = uint32_t;
702 const unsigned bit_width = *in++ & 0x3f;
703 unsigned num_exceptions = *in++;
705 // Decode the base values.
706 in = decode_bitmap_sse2<BlockSize, /*OrWithExisting=*/false, /*DeltaDecode=*/false>(in, bit_width, out);
708 // Decode exceptions.
709 Docid exceptions[BlockSize];
712 for (unsigned i = 0; i < num_exceptions; ++i) {
713 exceptions[i] = read_le<Docid>(in);
717 for (unsigned i = 0; i < num_exceptions; ++i) {
718 in = read_vb(in, &exceptions[i]);
723 for (unsigned i = 0; i < num_exceptions; ++i) {
724 unsigned idx = *in++;
725 out[idx] |= exceptions[i] << bit_width;
728 delta_decode_sse2<BlockSize>(out);
733 // Can read 16 bytes past the end of the input (inherit from several functions).
734 template<unsigned BlockSize, class Docid>
735 const unsigned char *decode_pfor_delta1(const unsigned char *in, unsigned num, bool interleaved, Docid *out)
740 in = read_baseval(in, out++);
742 for (unsigned i = 1; i < num; i += BlockSize, out += BlockSize) {
743 const unsigned num_this_block = std::min<unsigned>(num - i, BlockSize);
744 switch (in[0] >> 6) {
746 if (interleaved && num_this_block == BlockSize) {
747 dprintf("%d+%d: blocktype=%d (for, interleaved), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
748 in = decode_for_interleaved<BlockSize>(in, out);
750 dprintf("%d+%d: blocktype=%d (for), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
751 in = decode_for(in, num_this_block, out);
754 case BlockType::PFOR_VB:
755 if (interleaved && num_this_block == BlockSize) {
756 dprintf("%d+%d: blocktype=%d (pfor + vb, interleaved), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
757 in = decode_pfor_vb_interleaved<BlockSize>(in, out);
759 dprintf("%d+%d: blocktype=%d (pfor + vb), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
760 in = decode_pfor_vb<BlockSize>(in, num_this_block, out);
763 case BlockType::PFOR_BITMAP:
764 if (interleaved && num_this_block == BlockSize) {
765 dprintf("%d+%d: blocktype=%d (pfor + bitmap, interleaved), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
766 in = decode_pfor_bitmap_interleaved<BlockSize>(in, out);
768 dprintf("%d+%d: blocktype=%d (pfor + bitmap), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
769 in = decode_pfor_bitmap(in, num_this_block, out);
772 case BlockType::CONSTANT:
773 dprintf("%d+%d: blocktype=%d (constant), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
774 in = decode_constant(in, num_this_block, out);
782 const unsigned char *decode_pfor_delta1_128(const unsigned char *in, unsigned num, bool interleaved, uint32_t *out)
784 return decode_pfor_delta1<128>(in, num, interleaved, out);