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 // Does not properly account for overflow.
164 inline unsigned div_round_up(unsigned val, unsigned div)
166 return (val + div - 1) / div;
169 inline unsigned bytes_for_packed_bits(unsigned num, unsigned bit_width)
171 return div_round_up(num * bit_width, CHAR_BIT);
174 // Constant block. Layout:
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)
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);
187 Docid prev_val = out[-1];
188 for (unsigned i = 0; i < num; ++i) {
189 out[i] = prev_val = val + prev_val + 1;
191 return in + div_round_up(bit_width, 8);
194 // FOR block (ie., PFor without exceptions). Layout:
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)
201 const unsigned bit_width = *in++ & 0x3f;
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;
208 return in + bytes_for_packed_bits(num, bit_width);
211 #ifdef COULD_HAVE_SSE2
212 template<unsigned BlockSize>
213 __attribute__((target("sse2")))
214 inline void delta_decode_sse2(uint32_t *out)
216 // Use 4/3/2/1 as delta instead of fixed 1, so that we can do the prev_val + delta
217 // in parallel with something else.
218 const __m128i delta = _mm_set_epi32(4, 3, 2, 1);
219 __m128i prev_val = _mm_set1_epi32(out[-1]);
220 __m128i *outvec = reinterpret_cast<__m128i *>(out);
221 for (unsigned i = 0; i < BlockSize / 4; ++i) {
222 __m128i val = _mm_loadu_si128(outvec + i);
223 val = _mm_add_epi32(val, _mm_slli_si128(val, 4));
224 val = _mm_add_epi32(val, _mm_slli_si128(val, 8));
225 val = _mm_add_epi32(val, _mm_add_epi32(prev_val, delta));
226 _mm_storeu_si128(outvec + i, val);
228 prev_val = _mm_shuffle_epi32(val, _MM_SHUFFLE(3, 3, 3, 3));
232 template<unsigned BlockSize, bool OrWithExisting>
233 __attribute__((target("sse2")))
234 const unsigned char *decode_bitmap_sse2(const unsigned char *in, unsigned bit_width, uint32_t *out)
236 const __m128i *invec = reinterpret_cast<const __m128i *>(in);
237 __m128i *outvec = reinterpret_cast<__m128i *>(out);
238 const __m128i mask = _mm_set1_epi32((1U << bit_width) - 1);
239 unsigned bits_used = 0;
240 for (unsigned i = 0; i < BlockSize / 4; ++i) {
241 __m128i val = _mm_srli_epi32(_mm_loadu_si128(invec), bits_used);
242 if (bits_used + bit_width > 32) {
243 __m128i val_upper = _mm_slli_epi32(_mm_loadu_si128(invec + 1), 32 - bits_used);
244 val = _mm_or_si128(val, val_upper);
246 val = _mm_and_si128(val, mask);
247 if constexpr (OrWithExisting) {
248 val = _mm_or_si128(val, _mm_loadu_si128(outvec + i));
250 _mm_storeu_si128(outvec + i, val);
252 bits_used += bit_width;
253 invec += bits_used / 32;
256 in += bytes_for_packed_bits(BlockSize, bit_width);
261 // Like decode_for(), but the values are organized in four independent streams,
262 // for SIMD (presumably SSE2). Supports a whole block only.
263 template<unsigned BlockSize, class Docid>
264 const unsigned char *decode_for_interleaved_generic(const unsigned char *in, Docid *out)
266 const unsigned bit_width = *in++ & 0x3f;
268 InterleavedBitReader<4> bs0(in + 0 * sizeof(uint32_t), bit_width);
269 InterleavedBitReader<4> bs1(in + 1 * sizeof(uint32_t), bit_width);
270 InterleavedBitReader<4> bs2(in + 2 * sizeof(uint32_t), bit_width);
271 InterleavedBitReader<4> bs3(in + 3 * sizeof(uint32_t), bit_width);
272 for (unsigned i = 0; i < BlockSize / 4; ++i) {
273 out[i * 4 + 0] = bs0.read();
274 out[i * 4 + 1] = bs1.read();
275 out[i * 4 + 2] = bs2.read();
276 out[i * 4 + 3] = bs3.read();
278 Docid prev_val = out[-1];
279 for (unsigned i = 0; i < BlockSize; ++i) {
280 out[i] = prev_val = out[i] + prev_val + 1;
282 return in + bytes_for_packed_bits(BlockSize, bit_width);
285 template<unsigned BlockSize, class Docid>
286 const unsigned char *decode_for_interleaved(const unsigned char *in, Docid *out)
288 if constexpr (BlockSize == 128 && sizeof(Docid) == sizeof(uint32_t)) {
289 return decode_for_interleaved_128_32(in, out);
291 return decode_for_interleaved_generic(in, out);
295 __attribute__((target("default")))
296 const unsigned char *decode_for_interleaved_128_32(const unsigned char *in, uint32_t *out)
298 return decode_for_interleaved_generic<128>(in, out);
301 #ifdef COULD_HAVE_SSE2
302 // Specialized version for SSE2.
303 __attribute__((target("sse2")))
304 const unsigned char *decode_for_interleaved_128_32(const unsigned char *in, uint32_t *out)
306 constexpr unsigned BlockSize = 128;
308 const unsigned bit_width = *in++ & 0x3f;
310 in = decode_bitmap_sse2<BlockSize, false>(in, bit_width, out);
311 delta_decode_sse2<BlockSize>(out);
317 template<class Docid>
318 const unsigned char *decode_pfor_bitmap_exceptions(const unsigned char *in, unsigned num, unsigned bit_width, Docid *out)
320 const unsigned exception_bit_width = *in++;
321 const uint64_t *exception_bitmap_ptr = reinterpret_cast<const uint64_t *>(in);
322 in += div_round_up(num, 8);
324 int num_exceptions = 0;
326 BitReader bs(in, exception_bit_width);
327 for (unsigned i = 0; i < num; i += 64, ++exception_bitmap_ptr) {
328 uint64_t exceptions = read_le<uint64_t>(exception_bitmap_ptr);
330 // We've read some bytes past the end, so clear out the junk bits.
331 exceptions &= (1ULL << (num - i)) - 1;
333 for (; exceptions != 0; exceptions &= exceptions - 1, ++num_exceptions) {
334 unsigned idx = (ffsll(exceptions) - 1) + i;
335 out[idx] = bs.read() << bit_width;
338 in += bytes_for_packed_bits(num_exceptions, exception_bit_width);
342 // PFor block with bitmap exceptions. Layout:
344 // - Bit width (6 bits) | type << 6
345 // - Exception bit width (8 bits)
346 // - Bitmap of which values have exceptions (<num> bits, rounded up to a byte)
347 // - Exceptions (<num_exc> values of <bits_exc> bits, rounded up to a byte)
348 // - Base values (<num> values of <bits> bits, rounded up to a byte)
349 template<class Docid>
350 const unsigned char *decode_pfor_bitmap(const unsigned char *in, unsigned num, Docid *out)
352 memset(out, 0, num * sizeof(Docid));
354 const unsigned bit_width = *in++ & 0x3f;
356 in = decode_pfor_bitmap_exceptions(in, num, bit_width, out);
358 // Decode the base values, and delta-decode.
359 Docid prev_val = out[-1];
360 BitReader bs(in, bit_width);
361 for (unsigned i = 0; i < num; ++i) {
362 out[i] = prev_val = (out[i] | bs.read()) + prev_val + 1;
364 return in + bytes_for_packed_bits(num, bit_width);
367 // Like decode_pfor_bitmap(), but the base values are organized in four
368 // independent streams, for SIMD (presumably SSE2). Supports a whole block only.
369 template<unsigned BlockSize, class Docid>
370 const unsigned char *decode_pfor_bitmap_interleaved_generic(const unsigned char *in, Docid *out)
372 memset(out, 0, BlockSize * sizeof(Docid));
374 const unsigned bit_width = *in++ & 0x3f;
376 in = decode_pfor_bitmap_exceptions(in, BlockSize, bit_width, out);
378 // Decode the base values.
379 InterleavedBitReader<4> bs0(in + 0 * sizeof(uint32_t), bit_width);
380 InterleavedBitReader<4> bs1(in + 1 * sizeof(uint32_t), bit_width);
381 InterleavedBitReader<4> bs2(in + 2 * sizeof(uint32_t), bit_width);
382 InterleavedBitReader<4> bs3(in + 3 * sizeof(uint32_t), bit_width);
383 for (unsigned i = 0; i < BlockSize / 4; ++i) {
384 out[i * 4 + 0] |= bs0.read();
385 out[i * 4 + 1] |= bs1.read();
386 out[i * 4 + 2] |= bs2.read();
387 out[i * 4 + 3] |= bs3.read();
391 Docid prev_val = out[-1];
392 for (unsigned i = 0; i < BlockSize; ++i) {
393 out[i] = prev_val = out[i] + prev_val + 1;
395 return in + bytes_for_packed_bits(BlockSize, bit_width);
398 template<unsigned BlockSize, class Docid>
399 const unsigned char *decode_pfor_bitmap_interleaved(const unsigned char *in, Docid *out)
401 if constexpr (BlockSize == 128 && sizeof(Docid) == sizeof(uint32_t)) {
402 return decode_pfor_bitmap_interleaved_128_32(in, out);
404 return decode_pfor_bitmap_interleaved_generic(in, out);
408 __attribute__((target("default")))
409 const unsigned char *decode_pfor_bitmap_interleaved_128_32(const unsigned char *in, uint32_t *out)
411 return decode_pfor_bitmap_interleaved_generic<128>(in, out);
414 #ifdef COULD_HAVE_SSE2
415 // Specialized version for SSE2.
416 __attribute__((target("sse2")))
417 const unsigned char *decode_pfor_bitmap_interleaved_128_32(const unsigned char *in, uint32_t *out)
419 constexpr unsigned BlockSize = 128;
420 using Docid = uint32_t;
422 memset(out, 0, BlockSize * sizeof(Docid));
424 const unsigned bit_width = *in++ & 0x3f;
426 in = decode_pfor_bitmap_exceptions(in, BlockSize, bit_width, out);
427 in = decode_bitmap_sse2<BlockSize, true>(in, bit_width, out);
428 delta_decode_sse2<BlockSize>(out);
434 // PFor block with variable-byte exceptions. Layout:
436 // - Bit width (6 bits) | type << 6
437 // - Number of exceptions (8 bits)
438 // - Base values (<num> values of <bits> bits, rounded up to a byte)
440 // - If first byte is 255, <num_exc> 32-bit values (does not include the 255 byte)
441 // - Else, <num_exc> varbyte-encoded values (includes the non-255 byte)
442 // - Indexes of exceptions (<num_exc> bytes).
443 template<unsigned BlockSize, class Docid>
444 const unsigned char *decode_pfor_vb(const unsigned char *in, unsigned num, Docid *out)
446 //fprintf(stderr, "in=%p out=%p num=%u\n", in, out, num);
448 const unsigned bit_width = *in++ & 0x3f;
449 unsigned num_exceptions = *in++;
451 // Decode the base values.
452 BitReader bs(in, bit_width);
453 for (unsigned i = 0; i < num; ++i) {
456 in += bytes_for_packed_bits(num, bit_width);
458 // Decode exceptions.
459 Docid exceptions[BlockSize];
462 for (unsigned i = 0; i < num_exceptions; ++i) {
463 exceptions[i] = read_le<Docid>(in);
467 for (unsigned i = 0; i < num_exceptions; ++i) {
468 in = read_vb(in, &exceptions[i]);
472 for (unsigned i = 0; i < num_exceptions; ++i) {
473 unsigned idx = *in++;
474 out[idx] |= exceptions[i] << bit_width;
478 Docid prev_val = out[-1];
479 for (unsigned i = 0; i < num; ++i) {
480 out[i] = prev_val = out[i] + prev_val + 1;
486 // Like decode_pfor_vb(), but the base values are organized in four
487 // independent streams, for SIMD (presumably SSE2). Supports a whole block only.
488 template<unsigned BlockSize, class Docid>
489 const unsigned char *decode_pfor_vb_interleaved_generic(const unsigned char *in, Docid *out)
491 const unsigned bit_width = *in++ & 0x3f;
492 unsigned num_exceptions = *in++;
494 // Decode the base values.
495 InterleavedBitReader<4> bs0(in + 0 * sizeof(uint32_t), bit_width);
496 InterleavedBitReader<4> bs1(in + 1 * sizeof(uint32_t), bit_width);
497 InterleavedBitReader<4> bs2(in + 2 * sizeof(uint32_t), bit_width);
498 InterleavedBitReader<4> bs3(in + 3 * sizeof(uint32_t), bit_width);
499 for (unsigned i = 0; i < BlockSize / 4; ++i) {
500 out[i * 4 + 0] = bs0.read();
501 out[i * 4 + 1] = bs1.read();
502 out[i * 4 + 2] = bs2.read();
503 out[i * 4 + 3] = bs3.read();
505 in += bytes_for_packed_bits(BlockSize, bit_width);
507 // Decode exceptions.
508 Docid exceptions[BlockSize];
511 for (unsigned i = 0; i < num_exceptions; ++i) {
512 exceptions[i] = read_le<Docid>(in);
516 for (unsigned i = 0; i < num_exceptions; ++i) {
517 in = read_vb(in, &exceptions[i]);
522 for (unsigned i = 0; i < num_exceptions; ++i) {
523 unsigned idx = *in++;
524 out[idx] |= exceptions[i] << bit_width;
528 Docid prev_val = out[-1];
529 for (unsigned i = 0; i < BlockSize; ++i) {
530 out[i] = prev_val = out[i] + prev_val + 1;
536 template<unsigned BlockSize, class Docid>
537 const unsigned char *decode_pfor_vb_interleaved(const unsigned char *in, Docid *out)
539 if constexpr (BlockSize == 128 && sizeof(Docid) == sizeof(uint32_t)) {
540 return decode_pfor_vb_interleaved_128_32(in, out);
542 return decode_pfor_vb_interleaved_generic(in, out);
546 __attribute__((target("default")))
547 const unsigned char *decode_pfor_vb_interleaved_128_32(const unsigned char *in, uint32_t *out)
549 return decode_pfor_vb_interleaved_generic<128>(in, out);
552 // Specialized version for SSE2.
553 __attribute__((target("sse2")))
554 const unsigned char *decode_pfor_vb_interleaved_128_32(const unsigned char *in, uint32_t *out)
556 constexpr unsigned BlockSize = 128;
557 using Docid = uint32_t;
559 const unsigned bit_width = *in++ & 0x3f;
560 unsigned num_exceptions = *in++;
562 // Decode the base values.
563 in = decode_bitmap_sse2<BlockSize, false>(in, bit_width, out);
565 // Decode exceptions.
566 Docid exceptions[BlockSize];
569 for (unsigned i = 0; i < num_exceptions; ++i) {
570 exceptions[i] = read_le<Docid>(in);
574 for (unsigned i = 0; i < num_exceptions; ++i) {
575 in = read_vb(in, &exceptions[i]);
580 for (unsigned i = 0; i < num_exceptions; ++i) {
581 unsigned idx = *in++;
582 out[idx] |= exceptions[i] << bit_width;
585 delta_decode_sse2<BlockSize>(out);
597 template<unsigned BlockSize, class Docid>
598 const unsigned char *decode_pfor_delta1(const unsigned char *in, unsigned num, bool interleaved, Docid *out)
603 in = read_baseval(in, out++);
605 for (unsigned i = 1; i < num; i += BlockSize, out += BlockSize) {
606 const unsigned num_this_block = std::min<unsigned>(num - i, BlockSize);
607 switch (in[0] >> 6) {
609 if (interleaved && num_this_block == BlockSize) {
610 dprintf("%d+%d: blocktype=%d (for, interleaved), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
611 in = decode_for_interleaved<BlockSize>(in, out);
613 dprintf("%d+%d: blocktype=%d (for), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
614 in = decode_for(in, num_this_block, out);
617 case BlockType::PFOR_VB:
618 if (interleaved && num_this_block == BlockSize) {
619 dprintf("%d+%d: blocktype=%d (pfor + vb, interleaved), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
620 in = decode_pfor_vb_interleaved<BlockSize>(in, out);
622 dprintf("%d+%d: blocktype=%d (pfor + vb), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
623 in = decode_pfor_vb<BlockSize>(in, num_this_block, out);
626 case BlockType::PFOR_BITMAP:
627 if (interleaved && num_this_block == BlockSize) {
628 dprintf("%d+%d: blocktype=%d (pfor + bitmap, interleaved), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
629 in = decode_pfor_bitmap_interleaved<BlockSize>(in, out);
631 dprintf("%d+%d: blocktype=%d (pfor + bitmap), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
632 in = decode_pfor_bitmap(in, num_this_block, out);
635 case BlockType::CONSTANT:
636 dprintf("%d+%d: blocktype=%d (constant), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
637 in = decode_constant(in, num_this_block, out);
645 #endif // !defined(_TURBOPFOR_H)