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 1/3 of the performance), and do not support the same breadth of
7 // codecs (in particular, only delta-plus-1 is implemented, and only 32-bit
8 // docids are tested), but aim to be more portable and easier-to-understand.
9 // In particular, they will compile on x86 without SSE4.1 or AVX support.
11 // The main reference is https://michael.stapelberg.ch/posts/2019-02-05-turbopfor-analysis/,
12 // although some implementation details have been worked out by studying the
24 Docid read_le(const void *in)
27 memcpy(&val, in, sizeof(val));
28 if constexpr (sizeof(Docid) == 8) {
30 } else if constexpr (sizeof(Docid) == 4) {
32 } else if constexpr (sizeof(Docid) == 2) {
34 } else if constexpr (sizeof(Docid) == 1) {
41 // Reads a single value with an encoding that looks a bit like PrefixVarint.
42 // It's unclear why this doesn't use the varbyte encoding.
44 const unsigned char *read_baseval(const unsigned char *in, Docid *out)
46 //fprintf(stderr, "baseval: 0x%02x 0x%02x 0x%02x 0x%02x\n", in[0], in[1], in[2], in[3]);
50 } else if (*in < 192) {
51 *out = ((uint32_t(in[0]) << 8) | uint32_t(in[1])) & 0x3fff;
53 } else if (*in < 224) {
54 *out = ((uint32_t(in[0]) << 16) |
55 (uint32_t(in[2]) << 8) |
56 (uint32_t(in[1]))) & 0x1fffff;
59 assert(false); // Not implemented.
64 const unsigned char *read_vb(const unsigned char *in, Docid *out)
69 } else if (*in <= 240) {
70 *out = ((uint32_t(in[0] - 177) << 8) | uint32_t(in[1])) + 177;
72 } else if (*in <= 248) {
73 *out = ((uint32_t(in[0] - 241) << 16) | read_le<uint16_t>(in + 1)) + 16561;
75 } else if (*in == 249) {
76 *out = (uint32_t(in[1])) |
77 (uint32_t(in[2]) << 8) |
78 (uint32_t(in[3]) << 16);
80 } else if (*in == 250) {
81 *out = read_le<uint32_t>(in + 1);
90 BitReader(const unsigned char *in, unsigned bits)
91 : in(in), bits(bits), mask((1U << bits) - 1) {}
94 uint32_t val = (read_le<uint32_t>(in) >> bits_used) & mask;
104 const unsigned char *in;
107 unsigned bits_used = 0;
110 template<unsigned NumStreams>
111 struct InterleavedBitReader {
113 InterleavedBitReader(const unsigned char *in, unsigned bits)
114 : in(in), bits(bits), mask((1U << bits) - 1) {}
118 if (bits_used + bits > 32) {
119 val = (read_le<uint32_t>(in) >> bits_used) | (read_le<uint32_t>(in + Stride) << (32 - bits_used));
121 val = (read_le<uint32_t>(in) >> bits_used);
125 in += Stride * (bits_used / 32);
132 static constexpr unsigned Stride = NumStreams * sizeof(uint32_t);
133 const unsigned char *in;
136 unsigned bits_used = 0;
139 // Does not properly account for overflow.
140 inline unsigned div_round_up(unsigned val, unsigned div)
142 return (val + div - 1) / div;
145 inline unsigned bytes_for_packed_bits(unsigned num, unsigned bit_width)
147 return div_round_up(num * bit_width, CHAR_BIT);
150 // Constant block. Layout:
152 // - Bit width (6 bits) | type << 6
153 // - Base values (<bits> bits, rounded up to nearest byte)
154 template<class Docid>
155 const unsigned char *decode_constant(const unsigned char *in, unsigned num, Docid *out)
157 const unsigned bit_width = *in++ & 0x3f;
158 Docid val = read_le<Docid>(in);
159 if (bit_width < sizeof(Docid) * 8) {
160 val &= ((1U << bit_width) - 1);
163 Docid prev_val = out[-1];
164 for (unsigned i = 0; i < num; ++i) {
165 out[i] = prev_val = val + prev_val + 1;
167 return in + div_round_up(bit_width, 8);
170 // FOR block (ie., PFor without exceptions). Layout:
172 // - Bit width (6 bits) | type << 6
173 // - Base values (<num> values of <bits> bits, rounded up to a multiple of 32 values)
174 template<class Docid>
175 const unsigned char *decode_for(const unsigned char *in, unsigned num, Docid *out)
177 const unsigned bit_width = *in++ & 0x3f;
179 Docid prev_val = out[-1];
180 BitReader bs(in, bit_width);
181 for (unsigned i = 0; i < num; ++i) {
182 prev_val = out[i] = bs.read() + prev_val + 1;
184 return in + bytes_for_packed_bits(num, bit_width);
187 // Like decode_for(), but the values are organized in four independent streams,
188 // for SIMD (presumably SSE2). Supports a whole block only.
189 template<unsigned BlockSize, class Docid>
190 const unsigned char *decode_for_interleaved(const unsigned char *in, Docid *out)
192 const unsigned bit_width = *in++ & 0x3f;
194 InterleavedBitReader<4> bs0(in + 0 * sizeof(uint32_t), bit_width);
195 InterleavedBitReader<4> bs1(in + 1 * sizeof(uint32_t), bit_width);
196 InterleavedBitReader<4> bs2(in + 2 * sizeof(uint32_t), bit_width);
197 InterleavedBitReader<4> bs3(in + 3 * sizeof(uint32_t), bit_width);
198 for (unsigned i = 0; i < BlockSize / 4; ++i) {
199 out[i * 4 + 0] = bs0.read();
200 out[i * 4 + 1] = bs1.read();
201 out[i * 4 + 2] = bs2.read();
202 out[i * 4 + 3] = bs3.read();
204 Docid prev_val = out[-1];
205 for (unsigned i = 0; i < BlockSize; ++i) {
206 out[i] = prev_val = out[i] + prev_val + 1;
208 return in + bytes_for_packed_bits(BlockSize, bit_width);
211 template<class Docid>
212 const unsigned char *decode_pfor_bitmap_exceptions(const unsigned char *in, unsigned num, unsigned bit_width, Docid *out)
214 const unsigned exception_bit_width = *in++;
215 const uint64_t *exception_bitmap_ptr = reinterpret_cast<const uint64_t *>(in);
216 in += div_round_up(num, 8);
218 int num_exceptions = 0;
220 BitReader bs(in, exception_bit_width);
221 for (unsigned i = 0; i < num; i += 64, ++exception_bitmap_ptr) {
222 uint64_t exceptions = read_le<uint64_t>(exception_bitmap_ptr);
224 // We've read some bytes past the end, so clear out the junk bits.
225 exceptions &= (1ULL << (num - i)) - 1;
227 for (; exceptions != 0; exceptions &= exceptions - 1, ++num_exceptions) {
228 unsigned idx = (ffsll(exceptions) - 1) + i;
229 out[idx] = bs.read() << bit_width;
232 in += bytes_for_packed_bits(num_exceptions, exception_bit_width);
236 // PFor block with bitmap exceptions. Layout:
238 // - Bit width (6 bits) | type << 6
239 // - Exception bit width (8 bits)
240 // - Bitmap of which values have exceptions (<num> bits, rounded up to a byte)
241 // - Exceptions (<num_exc> values of <bits_exc> bits, rounded up to a byte)
242 // - Base values (<num> values of <bits> bits, rounded up to a byte)
243 template<class Docid>
244 const unsigned char *decode_pfor_bitmap(const unsigned char *in, unsigned num, Docid *out)
246 memset(out, 0, num * sizeof(Docid));
248 const unsigned bit_width = *in++ & 0x3f;
250 in = decode_pfor_bitmap_exceptions(in, num, bit_width, out);
252 // Decode the base values, and delta-decode.
253 Docid prev_val = out[-1];
254 BitReader bs(in, bit_width);
255 for (unsigned i = 0; i < num; ++i) {
256 out[i] = prev_val = (out[i] | bs.read()) + prev_val + 1;
258 return in + bytes_for_packed_bits(num, bit_width);
261 // Like decode_pfor_bitmap(), but the base values are organized in four
262 // independent streams, for SIMD (presumably SSE2). Supports a whole block only.
263 template<unsigned BlockSize, class Docid>
264 const unsigned char *decode_pfor_bitmap_interleaved(const unsigned char *in, Docid *out)
266 memset(out, 0, BlockSize * sizeof(Docid));
268 const unsigned bit_width = *in++ & 0x3f;
270 in = decode_pfor_bitmap_exceptions(in, BlockSize, bit_width, out);
272 // Decode the base values.
273 InterleavedBitReader<4> bs0(in + 0 * sizeof(uint32_t), bit_width);
274 InterleavedBitReader<4> bs1(in + 1 * sizeof(uint32_t), bit_width);
275 InterleavedBitReader<4> bs2(in + 2 * sizeof(uint32_t), bit_width);
276 InterleavedBitReader<4> bs3(in + 3 * sizeof(uint32_t), bit_width);
277 for (unsigned i = 0; i < BlockSize / 4; ++i) {
278 out[i * 4 + 0] |= bs0.read();
279 out[i * 4 + 1] |= bs1.read();
280 out[i * 4 + 2] |= bs2.read();
281 out[i * 4 + 3] |= bs3.read();
285 Docid prev_val = out[-1];
286 for (unsigned i = 0; i < BlockSize; ++i) {
287 out[i] = prev_val = out[i] + prev_val + 1;
289 return in + bytes_for_packed_bits(BlockSize, bit_width);
292 // PFor block with variable-byte exceptions. Layout:
294 // - Bit width (6 bits) | type << 6
295 // - Number of exceptions (8 bits)
296 // - Base values (<num> values of <bits> bits, rounded up to a byte)
298 // - If first byte is 255, <num_exc> 32-bit values (does not include the 255 byte)
299 // - Else, <num_exc> varbyte-encoded values (includes the non-255 byte)
300 // - Indexes of exceptions (<num_exc> bytes).
301 template<unsigned BlockSize, class Docid>
302 const unsigned char *decode_pfor_vb(const unsigned char *in, unsigned num, Docid *out)
304 //fprintf(stderr, "in=%p out=%p num=%u\n", in, out, num);
306 const unsigned bit_width = *in++ & 0x3f;
307 unsigned num_exceptions = *in++;
309 // Decode the base values.
310 BitReader bs(in, bit_width);
311 for (unsigned i = 0; i < num; ++i) {
314 in += bytes_for_packed_bits(num, bit_width);
316 // Decode exceptions.
317 Docid exceptions[BlockSize];
320 for (unsigned i = 0; i < num_exceptions; ++i) {
321 exceptions[i] = read_le<Docid>(in);
325 for (unsigned i = 0; i < num_exceptions; ++i) {
326 in = read_vb(in, &exceptions[i]);
330 for (unsigned i = 0; i < num_exceptions; ++i) {
331 unsigned idx = *in++;
332 out[idx] |= exceptions[i] << bit_width;
336 Docid prev_val = out[-1];
337 for (unsigned i = 0; i < num; ++i) {
338 out[i] = prev_val = out[i] + prev_val + 1;
344 // Like decode_pfor_vb(), but the base values are organized in four
345 // independent streams, for SIMD (presumably SSE2). Supports a whole block only.
346 template<unsigned BlockSize, class Docid>
347 const unsigned char *decode_pfor_vb_interleaved(const unsigned char *in, Docid *out)
349 const unsigned bit_width = *in++ & 0x3f;
350 unsigned num_exceptions = *in++;
352 // Decode the base values.
353 InterleavedBitReader<4> bs0(in + 0 * sizeof(uint32_t), bit_width);
354 InterleavedBitReader<4> bs1(in + 1 * sizeof(uint32_t), bit_width);
355 InterleavedBitReader<4> bs2(in + 2 * sizeof(uint32_t), bit_width);
356 InterleavedBitReader<4> bs3(in + 3 * sizeof(uint32_t), bit_width);
357 for (unsigned i = 0; i < BlockSize / 4; ++i) {
358 out[i * 4 + 0] = bs0.read();
359 out[i * 4 + 1] = bs1.read();
360 out[i * 4 + 2] = bs2.read();
361 out[i * 4 + 3] = bs3.read();
363 in += bytes_for_packed_bits(BlockSize, bit_width);
365 // Decode exceptions.
366 Docid exceptions[BlockSize];
369 for (unsigned i = 0; i < num_exceptions; ++i) {
370 exceptions[i] = read_le<Docid>(in);
374 for (unsigned i = 0; i < num_exceptions; ++i) {
375 in = read_vb(in, &exceptions[i]);
380 for (unsigned i = 0; i < num_exceptions; ++i) {
381 unsigned idx = *in++;
382 out[idx] |= exceptions[i] << bit_width;
386 Docid prev_val = out[-1];
387 for (unsigned i = 0; i < BlockSize; ++i) {
388 out[i] = prev_val = out[i] + prev_val + 1;
401 template<unsigned BlockSize, class Docid>
402 const unsigned char *decode_pfor_delta1(const unsigned char *in, unsigned num, bool interleaved, Docid *out)
407 in = read_baseval(in, out++);
409 for (unsigned i = 1; i < num; i += BlockSize, out += BlockSize) {
410 const unsigned num_this_block = std::min<unsigned>(num - i, BlockSize);
411 switch (in[0] >> 6) {
413 if (interleaved && num_this_block == BlockSize) {
414 dprintf("%d+%d: blocktype=%d (for, interleaved), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
415 in = decode_for_interleaved<BlockSize>(in, out);
417 dprintf("%d+%d: blocktype=%d (for), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
418 in = decode_for(in, num_this_block, out);
421 case BlockType::PFOR_VB:
422 if (interleaved && num_this_block == BlockSize) {
423 dprintf("%d+%d: blocktype=%d (pfor + vb, interleaved), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
424 in = decode_pfor_vb_interleaved<BlockSize>(in, out);
426 dprintf("%d+%d: blocktype=%d (pfor + vb), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
427 in = decode_pfor_vb<BlockSize>(in, num_this_block, out);
430 case BlockType::PFOR_BITMAP:
431 if (interleaved && num_this_block == BlockSize) {
432 dprintf("%d+%d: blocktype=%d (pfor + bitmap, interleaved), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
433 in = decode_pfor_bitmap_interleaved<BlockSize>(in, out);
435 dprintf("%d+%d: blocktype=%d (pfor + bitmap), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
436 in = decode_pfor_bitmap(in, num_this_block, out);
439 case BlockType::CONSTANT:
440 dprintf("%d+%d: blocktype=%d (constant), bitwidth=%d\n", i, num_this_block, in[0] >> 6, in[0] & 0x3f);
441 in = decode_constant(in, num_this_block, out);
449 #endif // !defined(_TURBOPFOR_H)