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.
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
24 #if defined(__i386__) || defined(__x86_64__)
25 #define COULD_HAVE_SSE2
26 #include <immintrin.h>
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);
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);
48 Docid read_le(const void *in)
51 memcpy(&val, in, sizeof(val));
52 if constexpr (sizeof(Docid) == 8) {
54 } else if constexpr (sizeof(Docid) == 4) {
56 } else if constexpr (sizeof(Docid) == 2) {
58 } else if constexpr (sizeof(Docid) == 1) {
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.
68 const unsigned char *read_baseval(const unsigned char *in, Docid *out)
70 //fprintf(stderr, "baseval: 0x%02x 0x%02x 0x%02x 0x%02x\n", in[0], in[1], in[2], in[3]);
74 } else if (*in < 192) {
75 *out = ((uint32_t(in[0]) << 8) | uint32_t(in[1])) & 0x3fff;
77 } else if (*in < 224) {
78 *out = ((uint32_t(in[0]) << 16) |
79 (uint32_t(in[2]) << 8) |
80 (uint32_t(in[1]))) & 0x1fffff;
83 assert(false); // Not implemented.
88 const unsigned char *read_vb(const unsigned char *in, Docid *out)
93 } else if (*in <= 240) {
94 *out = ((uint32_t(in[0] - 177) << 8) | uint32_t(in[1])) + 177;
96 } else if (*in <= 248) {
97 *out = ((uint32_t(in[0] - 241) << 16) | read_le<uint16_t>(in + 1)) + 16561;
99 } else if (*in == 249) {
100 *out = (uint32_t(in[1])) |
101 (uint32_t(in[2]) << 8) |
102 (uint32_t(in[3]) << 16);
104 } else if (*in == 250) {
105 *out = read_le<uint32_t>(in + 1);
114 BitReader(const unsigned char *in, unsigned bits)
115 : in(in), bits(bits), mask((1U << bits) - 1) {}
118 uint32_t val = (read_le<uint32_t>(in) >> bits_used) & mask;
128 const unsigned char *in;
131 unsigned bits_used = 0;
134 template<unsigned NumStreams>
135 struct InterleavedBitReader {
137 InterleavedBitReader(const unsigned char *in, unsigned bits)
138 : in(in), bits(bits), mask((1U << bits) - 1) {}
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 InterleavedBitReaderSSE2(const unsigned char *in, unsigned bits)
167 : in(reinterpret_cast<const __m128i *>(in)), bits(bits), mask(_mm_set1_epi32((1U << bits) - 1)) {}
169 __m128i val = _mm_srli_epi32(_mm_loadu_si128(in), bits_used);
170 if (bits_used + bits > 32) {
171 __m128i val_upper = _mm_slli_epi32(_mm_loadu_si128(in + 1), 32 - bits_used);
172 val = _mm_or_si128(val, val_upper);
174 val = _mm_and_si128(val, mask);
177 in += bits_used / 32;
186 unsigned bits_used = 0;
190 // Does not properly account for overflow.
191 inline unsigned div_round_up(unsigned val, unsigned div)
193 return (val + div - 1) / div;
196 inline unsigned bytes_for_packed_bits(unsigned num, unsigned bit_width)
198 return div_round_up(num * bit_width, CHAR_BIT);
201 // Constant block. Layout:
203 // - Bit width (6 bits) | type << 6
204 // - Base values (<bits> bits, rounded up to nearest byte)
205 template<class Docid>
206 const unsigned char *decode_constant(const unsigned char *in, unsigned num, Docid *out)
208 const unsigned bit_width = *in++ & 0x3f;
209 Docid val = read_le<Docid>(in);
210 if (bit_width < sizeof(Docid) * 8) {
211 val &= ((1U << bit_width) - 1);
214 Docid prev_val = out[-1];
215 for (unsigned i = 0; i < num; ++i) {
216 out[i] = prev_val = val + prev_val + 1;
218 return in + div_round_up(bit_width, 8);
221 // FOR block (ie., PFor without exceptions). Layout:
223 // - Bit width (6 bits) | type << 6
224 // - Base values (<num> values of <bits> bits, rounded up to a multiple of 32 values)
225 template<class Docid>
226 const unsigned char *decode_for(const unsigned char *in, unsigned num, Docid *out)
228 const unsigned bit_width = *in++ & 0x3f;
230 Docid prev_val = out[-1];
231 BitReader bs(in, bit_width);
232 for (unsigned i = 0; i < num; ++i) {
233 prev_val = out[i] = bs.read() + prev_val + 1;
235 return in + bytes_for_packed_bits(num, bit_width);
238 #ifdef COULD_HAVE_SSE2
239 class DeltaDecoderSSE2 {
241 DeltaDecoderSSE2(uint32_t prev_val) : prev_val(_mm_set1_epi32(prev_val)) {}
242 __m128i decode(__m128i val) {
243 val = _mm_add_epi32(val, _mm_slli_si128(val, 4));
244 val = _mm_add_epi32(val, _mm_slli_si128(val, 8));
245 val = _mm_add_epi32(val, _mm_add_epi32(prev_val, delta));
246 prev_val = _mm_shuffle_epi32(val, _MM_SHUFFLE(3, 3, 3, 3));
251 // Use 4/3/2/1 as delta instead of fixed 1, so that we can do the prev_val + delta
252 // in parallel with something else.
253 const __m128i delta = _mm_set_epi32(4, 3, 2, 1);
258 template<unsigned BlockSize>
259 __attribute__((target("sse2")))
260 inline void delta_decode_sse2(uint32_t *out)
262 DeltaDecoderSSE2 delta(out[-1]);
263 __m128i *outvec = reinterpret_cast<__m128i *>(out);
264 for (unsigned i = 0; i < BlockSize / 4; ++i) {
265 __m128i val = _mm_loadu_si128(outvec + i);
266 _mm_storeu_si128(outvec + i, delta.decode(val));
270 template<unsigned BlockSize, bool OrWithExisting, bool DeltaDecode>
271 __attribute__((target("sse2")))
272 const unsigned char *decode_bitmap_sse2(const unsigned char *in, unsigned bit_width, uint32_t *out)
274 __m128i *outvec = reinterpret_cast<__m128i *>(out);
275 DeltaDecoderSSE2 delta(out[-1]);
276 InterleavedBitReaderSSE2 bs(in, bit_width);
277 for (unsigned i = 0; i < BlockSize / 4; ++i) {
278 __m128i val = bs.read();
279 if constexpr (OrWithExisting) {
280 val = _mm_or_si128(val, _mm_loadu_si128(outvec + i));
282 if constexpr (DeltaDecode) {
283 val = delta.decode(val);
285 _mm_storeu_si128(outvec + i, val);
287 in += bytes_for_packed_bits(BlockSize, bit_width);
292 // Like decode_for(), but the values are organized in four independent streams,
293 // for SIMD (presumably SSE2). Supports a whole block only.
294 template<unsigned BlockSize, class Docid>
295 const unsigned char *decode_for_interleaved_generic(const unsigned char *in, Docid *out)
297 const unsigned bit_width = *in++ & 0x3f;
299 InterleavedBitReader<4> bs0(in + 0 * sizeof(uint32_t), bit_width);
300 InterleavedBitReader<4> bs1(in + 1 * sizeof(uint32_t), bit_width);
301 InterleavedBitReader<4> bs2(in + 2 * sizeof(uint32_t), bit_width);
302 InterleavedBitReader<4> bs3(in + 3 * sizeof(uint32_t), bit_width);
303 for (unsigned i = 0; i < BlockSize / 4; ++i) {
304 out[i * 4 + 0] = bs0.read();
305 out[i * 4 + 1] = bs1.read();
306 out[i * 4 + 2] = bs2.read();
307 out[i * 4 + 3] = bs3.read();
309 Docid prev_val = out[-1];
310 for (unsigned i = 0; i < BlockSize; ++i) {
311 out[i] = prev_val = out[i] + prev_val + 1;
313 return in + bytes_for_packed_bits(BlockSize, bit_width);
316 template<unsigned BlockSize, class Docid>
317 const unsigned char *decode_for_interleaved(const unsigned char *in, Docid *out)
319 if constexpr (BlockSize == 128 && sizeof(Docid) == sizeof(uint32_t)) {
320 return decode_for_interleaved_128_32(in, out);
322 return decode_for_interleaved_generic(in, out);
326 __attribute__((target("default")))
327 const unsigned char *decode_for_interleaved_128_32(const unsigned char *in, uint32_t *out)
329 return decode_for_interleaved_generic<128>(in, out);
332 #ifdef COULD_HAVE_SSE2
333 // Specialized version for SSE2.
334 __attribute__((target("sse2")))
335 const unsigned char *decode_for_interleaved_128_32(const unsigned char *in, uint32_t *out)
337 constexpr unsigned BlockSize = 128;
339 const unsigned bit_width = *in++ & 0x3f;
341 in = decode_bitmap_sse2<BlockSize, /*OrWithExisting=*/false, /*DeltaDecode=*/true>(in, bit_width, out);
347 template<class Docid>
348 const unsigned char *decode_pfor_bitmap_exceptions(const unsigned char *in, unsigned num, unsigned bit_width, Docid *out)
350 const unsigned exception_bit_width = *in++;
351 const uint64_t *exception_bitmap_ptr = reinterpret_cast<const uint64_t *>(in);
352 in += div_round_up(num, 8);
354 int num_exceptions = 0;
356 BitReader bs(in, exception_bit_width);
357 for (unsigned i = 0; i < num; i += 64, ++exception_bitmap_ptr) {
358 uint64_t exceptions = read_le<uint64_t>(exception_bitmap_ptr);
360 // We've read some bytes past the end, so clear out the junk bits.
361 exceptions &= (1ULL << (num - i)) - 1;
363 for (; exceptions != 0; exceptions &= exceptions - 1, ++num_exceptions) {
364 unsigned idx = (ffsll(exceptions) - 1) + i;
365 out[idx] = bs.read() << bit_width;
368 in += bytes_for_packed_bits(num_exceptions, exception_bit_width);
372 // PFor block with bitmap exceptions. Layout:
374 // - Bit width (6 bits) | type << 6
375 // - Exception bit width (8 bits)
376 // - Bitmap of which values have exceptions (<num> bits, rounded up to a byte)
377 // - Exceptions (<num_exc> values of <bits_exc> bits, rounded up to a byte)
378 // - Base values (<num> values of <bits> bits, rounded up to a byte)
379 template<class Docid>
380 const unsigned char *decode_pfor_bitmap(const unsigned char *in, unsigned num, Docid *out)
382 memset(out, 0, num * sizeof(Docid));
384 const unsigned bit_width = *in++ & 0x3f;
386 in = decode_pfor_bitmap_exceptions(in, num, bit_width, out);
388 // Decode the base values, and delta-decode.
389 Docid prev_val = out[-1];
390 BitReader bs(in, bit_width);
391 for (unsigned i = 0; i < num; ++i) {
392 out[i] = prev_val = (out[i] | bs.read()) + prev_val + 1;
394 return in + bytes_for_packed_bits(num, bit_width);
397 // Like decode_pfor_bitmap(), but the base values are organized in four
398 // independent streams, for SIMD (presumably SSE2). Supports a whole block only.
399 template<unsigned BlockSize, class Docid>
400 const unsigned char *decode_pfor_bitmap_interleaved_generic(const unsigned char *in, Docid *out)
402 memset(out, 0, BlockSize * sizeof(Docid));
404 const unsigned bit_width = *in++ & 0x3f;
406 in = decode_pfor_bitmap_exceptions(in, BlockSize, bit_width, out);
408 // Decode the base values.
409 InterleavedBitReader<4> bs0(in + 0 * sizeof(uint32_t), bit_width);
410 InterleavedBitReader<4> bs1(in + 1 * sizeof(uint32_t), bit_width);
411 InterleavedBitReader<4> bs2(in + 2 * sizeof(uint32_t), bit_width);
412 InterleavedBitReader<4> bs3(in + 3 * sizeof(uint32_t), bit_width);
413 for (unsigned i = 0; i < BlockSize / 4; ++i) {
414 out[i * 4 + 0] |= bs0.read();
415 out[i * 4 + 1] |= bs1.read();
416 out[i * 4 + 2] |= bs2.read();
417 out[i * 4 + 3] |= bs3.read();
421 Docid prev_val = out[-1];
422 for (unsigned i = 0; i < BlockSize; ++i) {
423 out[i] = prev_val = out[i] + prev_val + 1;
425 return in + bytes_for_packed_bits(BlockSize, bit_width);
428 template<unsigned BlockSize, class Docid>
429 const unsigned char *decode_pfor_bitmap_interleaved(const unsigned char *in, Docid *out)
431 if constexpr (BlockSize == 128 && sizeof(Docid) == sizeof(uint32_t)) {
432 return decode_pfor_bitmap_interleaved_128_32(in, out);
434 return decode_pfor_bitmap_interleaved_generic(in, out);
438 __attribute__((target("default")))
439 const unsigned char *decode_pfor_bitmap_interleaved_128_32(const unsigned char *in, uint32_t *out)
441 return decode_pfor_bitmap_interleaved_generic<128>(in, out);
444 #ifdef COULD_HAVE_SSE2
445 // Specialized version for SSE2.
446 __attribute__((target("sse2")))
447 const unsigned char *decode_pfor_bitmap_interleaved_128_32(const unsigned char *in, uint32_t *out)
449 constexpr unsigned BlockSize = 128;
450 using Docid = uint32_t;
452 memset(out, 0, BlockSize * sizeof(Docid));
454 const unsigned bit_width = *in++ & 0x3f;
456 in = decode_pfor_bitmap_exceptions(in, BlockSize, bit_width, out);
457 in = decode_bitmap_sse2<BlockSize, /*OrWithExisting=*/true, /*DeltaDecode=*/true>(in, bit_width, out);
463 // PFor block with variable-byte exceptions. Layout:
465 // - Bit width (6 bits) | type << 6
466 // - Number of exceptions (8 bits)
467 // - Base values (<num> values of <bits> bits, rounded up to a byte)
469 // - If first byte is 255, <num_exc> 32-bit values (does not include the 255 byte)
470 // - Else, <num_exc> varbyte-encoded values (includes the non-255 byte)
471 // - Indexes of exceptions (<num_exc> bytes).
472 template<unsigned BlockSize, class Docid>
473 const unsigned char *decode_pfor_vb(const unsigned char *in, unsigned num, Docid *out)
475 //fprintf(stderr, "in=%p out=%p num=%u\n", in, out, num);
477 const unsigned bit_width = *in++ & 0x3f;
478 unsigned num_exceptions = *in++;
480 // Decode the base values.
481 BitReader bs(in, bit_width);
482 for (unsigned i = 0; i < num; ++i) {
485 in += bytes_for_packed_bits(num, bit_width);
487 // Decode exceptions.
488 Docid exceptions[BlockSize];
491 for (unsigned i = 0; i < num_exceptions; ++i) {
492 exceptions[i] = read_le<Docid>(in);
496 for (unsigned i = 0; i < num_exceptions; ++i) {
497 in = read_vb(in, &exceptions[i]);
501 for (unsigned i = 0; i < num_exceptions; ++i) {
502 unsigned idx = *in++;
503 out[idx] |= exceptions[i] << bit_width;
507 Docid prev_val = out[-1];
508 for (unsigned i = 0; i < num; ++i) {
509 out[i] = prev_val = out[i] + prev_val + 1;
515 // Like decode_pfor_vb(), but the base values are organized in four
516 // independent streams, for SIMD (presumably SSE2). Supports a whole block only.
517 template<unsigned BlockSize, class Docid>
518 const unsigned char *decode_pfor_vb_interleaved_generic(const unsigned char *in, Docid *out)
520 const unsigned bit_width = *in++ & 0x3f;
521 unsigned num_exceptions = *in++;
523 // Decode the base values.
524 InterleavedBitReader<4> bs0(in + 0 * sizeof(uint32_t), bit_width);
525 InterleavedBitReader<4> bs1(in + 1 * sizeof(uint32_t), bit_width);
526 InterleavedBitReader<4> bs2(in + 2 * sizeof(uint32_t), bit_width);
527 InterleavedBitReader<4> bs3(in + 3 * sizeof(uint32_t), bit_width);
528 for (unsigned i = 0; i < BlockSize / 4; ++i) {
529 out[i * 4 + 0] = bs0.read();
530 out[i * 4 + 1] = bs1.read();
531 out[i * 4 + 2] = bs2.read();
532 out[i * 4 + 3] = bs3.read();
534 in += bytes_for_packed_bits(BlockSize, bit_width);
536 // Decode exceptions.
537 Docid exceptions[BlockSize];
540 for (unsigned i = 0; i < num_exceptions; ++i) {
541 exceptions[i] = read_le<Docid>(in);
545 for (unsigned i = 0; i < num_exceptions; ++i) {
546 in = read_vb(in, &exceptions[i]);
551 for (unsigned i = 0; i < num_exceptions; ++i) {
552 unsigned idx = *in++;
553 out[idx] |= exceptions[i] << bit_width;
557 Docid prev_val = out[-1];
558 for (unsigned i = 0; i < BlockSize; ++i) {
559 out[i] = prev_val = out[i] + prev_val + 1;
565 template<unsigned BlockSize, class Docid>
566 const unsigned char *decode_pfor_vb_interleaved(const unsigned char *in, Docid *out)
568 if constexpr (BlockSize == 128 && sizeof(Docid) == sizeof(uint32_t)) {
569 return decode_pfor_vb_interleaved_128_32(in, out);
571 return decode_pfor_vb_interleaved_generic(in, out);
575 __attribute__((target("default")))
576 const unsigned char *decode_pfor_vb_interleaved_128_32(const unsigned char *in, uint32_t *out)
578 return decode_pfor_vb_interleaved_generic<128>(in, out);
581 // Specialized version for SSE2.
582 __attribute__((target("sse2")))
583 const unsigned char *decode_pfor_vb_interleaved_128_32(const unsigned char *in, uint32_t *out)
585 constexpr unsigned BlockSize = 128;
586 using Docid = uint32_t;
588 const unsigned bit_width = *in++ & 0x3f;
589 unsigned num_exceptions = *in++;
591 // Decode the base values.
592 in = decode_bitmap_sse2<BlockSize, /*OrWithExisting=*/false, /*DeltaDecode=*/false>(in, bit_width, out);
594 // Decode exceptions.
595 Docid exceptions[BlockSize];
598 for (unsigned i = 0; i < num_exceptions; ++i) {
599 exceptions[i] = read_le<Docid>(in);
603 for (unsigned i = 0; i < num_exceptions; ++i) {
604 in = read_vb(in, &exceptions[i]);
609 for (unsigned i = 0; i < num_exceptions; ++i) {
610 unsigned idx = *in++;
611 out[idx] |= exceptions[i] << bit_width;
614 delta_decode_sse2<BlockSize>(out);
626 template<unsigned BlockSize, class Docid>
627 const unsigned char *decode_pfor_delta1(const unsigned char *in, unsigned num, bool interleaved, Docid *out)
632 in = read_baseval(in, out++);
634 for (unsigned i = 1; i < num; i += BlockSize, out += BlockSize) {
635 const unsigned num_this_block = std::min<unsigned>(num - i, BlockSize);
636 switch (in[0] >> 6) {
638 if (interleaved && num_this_block == BlockSize) {
639 dprintf("%d+%d: blocktype=%d (for, interleaved), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
640 in = decode_for_interleaved<BlockSize>(in, out);
642 dprintf("%d+%d: blocktype=%d (for), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
643 in = decode_for(in, num_this_block, out);
646 case BlockType::PFOR_VB:
647 if (interleaved && num_this_block == BlockSize) {
648 dprintf("%d+%d: blocktype=%d (pfor + vb, interleaved), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
649 in = decode_pfor_vb_interleaved<BlockSize>(in, out);
651 dprintf("%d+%d: blocktype=%d (pfor + vb), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
652 in = decode_pfor_vb<BlockSize>(in, num_this_block, out);
655 case BlockType::PFOR_BITMAP:
656 if (interleaved && num_this_block == BlockSize) {
657 dprintf("%d+%d: blocktype=%d (pfor + bitmap, interleaved), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
658 in = decode_pfor_bitmap_interleaved<BlockSize>(in, out);
660 dprintf("%d+%d: blocktype=%d (pfor + bitmap), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
661 in = decode_pfor_bitmap(in, num_this_block, out);
664 case BlockType::CONSTANT:
665 dprintf("%d+%d: blocktype=%d (constant), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
666 in = decode_constant(in, num_this_block, out);
674 #endif // !defined(_TURBOPFOR_H)