-
Notifications
You must be signed in to change notification settings - Fork 25
/
block_codecs.hpp
351 lines (303 loc) · 10.8 KB
/
block_codecs.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#pragma once
#include "FastPFor/headers/VarIntG8IU.h"
#include "FastPFor/headers/optpfor.h"
#include "FastPFor/headers/variablebyte.h"
#include "interpolative_coding.hpp"
#include "qmx_codec.hpp"
#include "succinct/util.hpp"
#include "util.hpp"
namespace ds2i {
// workaround: VariableByte::decodeArray needs the buffer size, while we
// only know the number of values. It also pads to 32 bits. We need to
// rewrite
class TightVariableByte {
public:
template <uint32_t i> static uint8_t extract7bits(const uint32_t val) {
return static_cast<uint8_t>((val >> (7 * i)) & ((1U << 7) - 1));
}
template <uint32_t i>
static uint8_t extract7bitsmaskless(const uint32_t val) {
return static_cast<uint8_t>((val >> (7 * i)));
}
static void encode(const uint32_t *in, const size_t length, uint8_t *out,
size_t &nvalue) {
uint8_t *bout = out;
for (size_t k = 0; k < length; ++k) {
const uint32_t val(in[k]);
/**
* Code below could be shorter. Whether it could be faster
* depends on your compiler and machine.
*/
if (val < (1U << 7)) {
*bout = static_cast<uint8_t>(val | (1U << 7));
++bout;
} else if (val < (1U << 14)) {
*bout = extract7bits<0>(val);
++bout;
*bout = extract7bitsmaskless<1>(val) | (1U << 7);
++bout;
} else if (val < (1U << 21)) {
*bout = extract7bits<0>(val);
++bout;
*bout = extract7bits<1>(val);
++bout;
*bout = extract7bitsmaskless<2>(val) | (1U << 7);
++bout;
} else if (val < (1U << 28)) {
*bout = extract7bits<0>(val);
++bout;
*bout = extract7bits<1>(val);
++bout;
*bout = extract7bits<2>(val);
++bout;
*bout = extract7bitsmaskless<3>(val) | (1U << 7);
++bout;
} else {
*bout = extract7bits<0>(val);
++bout;
*bout = extract7bits<1>(val);
++bout;
*bout = extract7bits<2>(val);
++bout;
*bout = extract7bits<3>(val);
++bout;
*bout = extract7bitsmaskless<4>(val) | (1U << 7);
++bout;
}
}
nvalue = bout - out;
}
static void encode_single(uint32_t val, std::vector<uint8_t> &out) {
uint8_t buf[5];
size_t nvalue;
encode(&val, 1, buf, nvalue);
out.insert(out.end(), buf, buf + nvalue);
}
static uint8_t const *decode(const uint8_t *in, uint32_t *out, size_t n) {
const uint8_t *inbyte = in;
for (size_t i = 0; i < n; ++i) {
unsigned int shift = 0;
for (uint32_t v = 0;; shift += 7) {
uint8_t c = *inbyte++;
v += ((c & 127) << shift);
if ((c & 128)) {
*out++ = v;
break;
}
}
}
return inbyte;
}
};
struct interpolative_block {
static const uint64_t block_size = 128;
static const uint64_t overflow = 0;
static void encode(uint32_t const *in, uint32_t sum_of_values, size_t n,
std::vector<uint8_t> &out) {
assert(n <= block_size);
thread_local std::vector<uint32_t> inbuf(block_size);
thread_local std::vector<uint32_t> outbuf;
inbuf[0] = *in;
for (size_t i = 1; i < n; ++i) {
inbuf[i] = inbuf[i - 1] + in[i];
}
if (sum_of_values == uint32_t(-1)) {
sum_of_values = inbuf[n - 1];
TightVariableByte::encode_single(sum_of_values, out);
}
bit_writer bw(outbuf);
bw.write_interpolative(inbuf.data(), n - 1, 0, sum_of_values);
uint8_t const *bufptr = (uint8_t const *)outbuf.data();
out.insert(out.end(), bufptr,
bufptr + succinct::util::ceil_div(bw.size(), 8));
}
static uint8_t const *DS2I_NOINLINE decode(uint8_t const *in, uint32_t *out,
uint32_t sum_of_values, size_t n) {
assert(n <= block_size);
uint8_t const *inbuf = in;
if (sum_of_values == uint32_t(-1)) {
inbuf = TightVariableByte::decode(inbuf, &sum_of_values, 1);
}
out[n - 1] = sum_of_values;
size_t read_interpolative = 0;
if (n > 1) {
bit_reader br((uint32_t const *)inbuf);
br.read_interpolative(out, n - 1, 0, sum_of_values);
for (size_t i = n - 1; i > 0; --i) {
out[i] -= out[i - 1];
}
read_interpolative = succinct::util::ceil_div(br.position(), 8);
}
return inbuf + read_interpolative;
}
};
struct optpfor_block {
struct codec_type : FastPFor::OPTPFor<4, FastPFor::Simple16<false>> {
uint8_t const *force_b;
uint32_t findBestB(const uint32_t *in, uint32_t len) {
// trick to force the choice of b from a parameter
if (force_b) {
return *force_b;
}
// this is mostly a cut&paste from FastPFor, but we stop the
// optimization early as the b to test becomes larger than maxb
uint32_t b = 0;
uint32_t bsize = std::numeric_limits<uint32_t>::max();
const uint32_t mb = FastPFor::maxbits(in, in + len);
uint32_t i = 0;
while (mb > 28 + possLogs[i])
++i; // some schemes such as Simple16 don't code numbers greater than 28
for (; i < possLogs.size(); i++) {
if (possLogs[i] > mb && possLogs[i] >= mb)
break;
const uint32_t csize = tryB(possLogs[i], in, len);
if (csize <= bsize) {
b = possLogs[i];
bsize = csize;
}
}
return b;
}
};
static const uint64_t block_size = codec_type::BlockSize;
static const uint64_t overflow = 0;
static void encode(uint32_t const *in, uint32_t sum_of_values, size_t n,
std::vector<uint8_t> &out,
uint8_t const *b = nullptr) // if non-null forces b
{
thread_local codec_type optpfor_codec;
thread_local std::vector<uint8_t> buf(2 * 4 * block_size);
assert(n <= block_size);
if (n < block_size) {
interpolative_block::encode(in, sum_of_values, n, out);
return;
}
size_t out_len = buf.size();
optpfor_codec.force_b = b;
optpfor_codec.encodeBlock(in, reinterpret_cast<uint32_t *>(buf.data()),
out_len);
out_len *= 4;
out.insert(out.end(), buf.data(), buf.data() + out_len);
}
static uint8_t const *DS2I_NOINLINE decode(uint8_t const *in, uint32_t *out,
uint32_t sum_of_values, size_t n) {
thread_local codec_type optpfor_codec; // pfor decoding is *not* thread-safe
assert(n <= block_size);
if (DS2I_UNLIKELY(n < block_size)) {
return interpolative_block::decode(in, out, sum_of_values, n);
}
size_t out_len = block_size;
uint8_t const *ret;
ret = reinterpret_cast<uint8_t const *>(optpfor_codec.decodeBlock(
reinterpret_cast<uint32_t const *>(in), out, out_len));
assert(out_len == n);
return ret;
}
};
struct varint_G8IU_block {
static const uint64_t block_size = 128;
static const uint64_t overflow = 0;
struct codec_type : FastPFor::VarIntG8IU {
// rewritten version of decodeBlock optimized for when the output
// size is known rather than the input
// the buffers pointed by src and dst must be respectively at least
// 9 and 8 elements large
uint32_t decodeBlock(uint8_t const *&src, uint32_t *dst) const {
uint8_t desc = *src;
src += 1;
const __m128i data =
_mm_lddqu_si128(reinterpret_cast<__m128i const *>(src));
src += 8;
const __m128i result = _mm_shuffle_epi8(data, vecmask[desc][0]);
_mm_storeu_si128(reinterpret_cast<__m128i *>(dst), result);
int readSize = maskOutputSize[desc];
if (readSize > 4) {
const __m128i result2 = _mm_shuffle_epi8(
data, vecmask[desc][1]); //__builtin_ia32_pshufb128(data, shf2);
_mm_storeu_si128(
reinterpret_cast<__m128i *>(dst + 4),
result2); //__builtin_ia32_storedqu(dst + (16), result2);
}
return readSize;
}
};
static void encode(uint32_t const *in, uint32_t sum_of_values, size_t n,
std::vector<uint8_t> &out) {
thread_local codec_type varint_codec;
thread_local std::vector<uint8_t> buf(2 * 4 * block_size);
assert(n <= block_size);
if (n < block_size) {
interpolative_block::encode(in, sum_of_values, n, out);
return;
}
size_t out_len = buf.size();
const uint32_t *src = in;
unsigned char *dst = buf.data();
size_t srclen = n * 4;
size_t dstlen = out_len;
out_len = 0;
while (srclen > 0 && dstlen >= 9) {
out_len += varint_codec.encodeBlock(src, srclen, dst, dstlen);
}
assert(srclen == 0);
out.insert(out.end(), buf.data(), buf.data() + out_len);
}
// we only allow varint to be inlined (others have DS2I_NOILINE)
static uint8_t const *decode(uint8_t const *in, uint32_t *out,
uint32_t sum_of_values, size_t n) {
static codec_type varint_codec; // decodeBlock is thread-safe
assert(n <= block_size);
if (DS2I_UNLIKELY(n < block_size)) {
return interpolative_block::decode(in, out, sum_of_values, n);
}
size_t out_len = 0;
uint8_t const *src = in;
uint32_t *dst = out;
while (out_len <= (n - 8)) {
out_len += varint_codec.decodeBlock(src, dst + out_len);
}
// decodeBlock can overshoot, so we decode the last blocks in a
// local buffer
while (out_len < n) {
uint32_t buf[8];
size_t read = varint_codec.decodeBlock(src, buf);
size_t needed = std::min(read, n - out_len);
memcpy(dst + out_len, buf, needed * 4);
out_len += needed;
}
assert(out_len == n);
return src;
}
};
struct qmx_block {
static const uint64_t block_size = 128;
static const uint64_t overflow = 512; // qmx can potentially overshoot...
static void encode(uint32_t const *in, uint32_t sum_of_values, size_t n,
std::vector<uint8_t> &out) {
assert(n <= block_size);
if (n < block_size) {
interpolative_block::encode(in, sum_of_values, n, out);
return;
}
thread_local QMX::codec<block_size> qmx_codec;
thread_local std::vector<uint8_t> buf( (overflow*4) + 2 * 4 * block_size);
size_t out_len = buf.size();
out_len = qmx_codec.encode(buf.data(),in);
TightVariableByte::encode_single(out_len, out);
out.insert(out.end(), buf.data(), buf.data() + out_len);
}
// we only allow varint to be inlined (others have DS2I_NOILINE)
static uint8_t const *decode(uint8_t const *in, uint32_t *out,
uint32_t sum_of_values, size_t n) {
static QMX::codec<block_size> qmx_codec; // decodeBlock is thread-safe
assert(n <= block_size);
if (DS2I_UNLIKELY(n < block_size)) {
return interpolative_block::decode(in, out, sum_of_values, n);
}
uint32_t enc_len = 0;
in = TightVariableByte::decode(in, &enc_len, 1);
qmx_codec.decode(out,in, enc_len);
return in + enc_len;
}
};
}