-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05-dumb-length-limiting.cpp
502 lines (418 loc) · 17.5 KB
/
05-dumb-length-limiting.cpp
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
* Copyright (c) 2024 Romain BAILLY
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*
* Changes from previous file:
* - Length limiting using a simple algorithm
*/
#include "utils.h"
#include <algorithm>
#include <cassert>
#include <cstring>
#include <vector>
namespace
{
constexpr size_t maxSymbolCount = 256;
constexpr unsigned maxCodeLength = 32;
using Code = std::conditional<maxCodeLength <= 32u, uint32_t, uint64_t>::type;
using BitBuffer = Code;
static_assert(sizeof(BitBuffer) >= sizeof(Code), "");
void sort_symbols_by_weight(uint8_t symbols[maxSymbolCount], const size_t weights[]) noexcept
{
// 10 bits per round is a good tradeoff
constexpr unsigned bitsPerRound = 10;
constexpr uint32_t mask = (1 << bitsPerRound) - 1;
uint8_t histo[1 << bitsPerRound]; // 8 bits are enough in our case (256 symbols)
uint8_t tmp[256];
// Write to tmp in the first round: this requires one memcpy in single-round cases
// but none in dual-round cases. This is more balanced (and we assume more than 2
// rounds to be a rare occurrence)
uint8_t* src = symbols;
uint8_t* dst = tmp;
// Compute histogram of weight bits (and compute maxWeight on the fly)
size_t maxWeight = 0;
memset(histo, 0, sizeof(histo));
for (size_t symbol=0; symbol < maxSymbolCount; ++symbol)
{
size_t w = weights[symbol];
maxWeight = std::max(maxWeight, w);
histo[w & mask]++;
}
// Turn histogram into prefix sum
unsigned sum = 0;
for (size_t i=0; i < (1u << bitsPerRound); ++i)
{
unsigned h = histo[i];
histo[i] = uint8_t(sum);
sum += h;
}
// 1st round of LSD-first radix sort
for (size_t symbol=0; symbol < maxSymbolCount; ++symbol)
{
size_t bits = weights[symbol] & mask;
size_t dstIndex = histo[bits]++;
dst[dstIndex] = uint8_t(symbol);
}
// Some more rounds required, keep doing the same stuff
for (unsigned shift=bitsPerRound; (maxWeight >> shift) > 0u; shift+=bitsPerRound)
{
std::swap(src, dst);
// Compute histogram
memset(histo, 0, sizeof(histo));
for (size_t symbol=0; symbol < maxSymbolCount; ++symbol)
histo[(weights[symbol] >> shift) & mask]++;
// Turn histogram into prefix sum
sum = 0;
for (size_t i=0; i < (1u << bitsPerRound); ++i)
{
unsigned h = histo[i];
histo[i] = uint8_t(sum);
sum += h;
}
// LSD-first radix sort
for (size_t srcIndex=0; srcIndex < maxSymbolCount; ++srcIndex)
{
size_t symbol = src[srcIndex];
size_t bits = (weights[symbol] >> shift) & mask;
size_t dstIndex = histo[bits]++;
dst[dstIndex] = uint8_t(symbol);
}
}
if (dst != symbols)
memcpy(symbols, dst, maxSymbolCount);
}
union TreeItem
{
size_t weight;
TreeItem* parent;
uint8_t length;
};
void compute_huffman_lengths(uint8_t lengths[maxSymbolCount], const size_t weights[maxSymbolCount], const uint8_t symbolsSortedByWeight[maxSymbolCount]) noexcept
{
// Skip unused symbols
size_t unusedSymbolCount = 0;
while (unusedSymbolCount < maxSymbolCount && weights[symbolsSortedByWeight[unusedSymbolCount]] == 0u)
++unusedSymbolCount;
const size_t usedSymbolCount = maxSymbolCount - unusedSymbolCount;
// Create tree leaves
TreeItem tree[2*maxSymbolCount];
for (size_t i=0; i < usedSymbolCount; ++i)
{
const uint8_t symbol = symbolsSortedByWeight[unusedSymbolCount + i];
tree[i].weight = weights[symbol];
}
// Special case if we have less than two items
if (usedSymbolCount <= 1u)
{
if (usedSymbolCount == 1u)
lengths[symbolsSortedByWeight[unusedSymbolCount]] = 1;
return;
}
// Insert a sentinel between the leaves and the nodes
TreeItem* sentinel = tree + usedSymbolCount;
sentinel->weight = SIZE_MAX;
// Build initial node: its children are the first two leaves
TreeItem* lastNode = sentinel + 1;
lastNode->weight = tree[0].weight + tree[1].weight;
tree[0].parent = lastNode;
tree[1].parent = lastNode;
// Now, build the rest of the tree
TreeItem* oldestUnconsumedLeaf = tree + 2; // The first 2 leaves have just been consumed
TreeItem* oldestUnconsumedNode = lastNode; // The first node has not yet been consumed
for (size_t i=0; i < usedSymbolCount-2; ++i)
{
++lastNode;
TreeItem* child1 = (oldestUnconsumedLeaf->weight <= oldestUnconsumedNode->weight) ? oldestUnconsumedLeaf++ : oldestUnconsumedNode++;
TreeItem* child2 = (oldestUnconsumedLeaf->weight <= oldestUnconsumedNode->weight || oldestUnconsumedNode == lastNode) ? oldestUnconsumedLeaf++ : oldestUnconsumedNode++;
lastNode->weight = child1->weight + child2->weight;
child1->parent = lastNode;
child2->parent = lastNode;
}
// Compute the length (i.e. depth) of each node
TreeItem* root = lastNode;
root->length = 0;
TreeItem* item = root - 1;
do
item->length = item->parent->length + 1;
while (--item > sentinel);
// Assign each used symbol its length
for (size_t i=0; i < usedSymbolCount; ++i)
{
const size_t symbol = symbolsSortedByWeight[unusedSymbolCount + i];
lengths[symbol] = tree[i].parent->length + 1;
}
}
void limit_lengths(uint8_t lengths[maxSymbolCount], const uint8_t symbolsSortedByWeight[maxSymbolCount], uint8_t lengthLimit) noexcept
{
// Skip unused symbols
size_t unusedSymbolCount = 0;
while (lengths[symbolsSortedByWeight[unusedSymbolCount]] == 0u)
++unusedSymbolCount;
// Clamp lengths and compute the Kraft-McMillan sum
using Kraft = uint32_t;
assert(lengthLimit < std::numeric_limits<Kraft>::digits); // Otherwise overflow will occur
const Kraft kraftOne = Kraft(1) << lengthLimit;
Kraft kraftSum = 0;
for (size_t i=unusedSymbolCount; i<maxSymbolCount; ++i)
{
const size_t symbol = symbolsSortedByWeight[i];
uint8_t length = std::min(lengths[symbol], lengthLimit);
kraftSum += kraftOne >> length;
lengths[symbol] = length;
}
// If the sum is <= 1, it means we haven't shortened anything: we're done
if (kraftSum <= kraftOne)
return;
// Skip symbols whose length is already at the limit
size_t i = unusedSymbolCount;
while (lengths[symbolsSortedByWeight[i]] == lengthLimit)
++i;
const size_t firstBelowLimit = i;
// Lengthen symbols until Kraft inequality is fixed
do
{
uint8_t length = ++lengths[symbolsSortedByWeight[i++]];
kraftSum -= kraftOne >> length;
}
while (kraftSum > kraftOne);
// We might have fixed it a bit too much, let's try to un-fix some symbols
if (kraftSum < kraftOne)
{
while (--i >= firstBelowLimit)
{
uint8_t& length = lengths[symbolsSortedByWeight[i]];
Kraft kraftSum2 = kraftSum + (kraftOne >> length);
if (kraftSum2 <= kraftOne)
{
--length;
if (kraftSum2 == kraftOne)
break;
kraftSum = kraftSum2;
}
}
}
}
uint8_t* sort_symbols_by_length(uint8_t symbolsSortedByLength[maxSymbolCount], const uint8_t lengths[maxSymbolCount]) noexcept
{
// Build length histogram
uint8_t histo[maxCodeLength+1] = {};
for (size_t symbol=0; symbol < maxSymbolCount; ++symbol)
histo[lengths[symbol]]++;
// Turn histogram into prefix sum
unsigned sum = 0;
for (size_t i=0; i <= maxCodeLength; ++i)
{
unsigned h = histo[i];
histo[i] = uint8_t(sum);
sum += h;
}
// Single-pass radix sort (i.e. counting sort)
for (size_t i=0; i < maxSymbolCount; ++i)
symbolsSortedByLength[histo[lengths[i]]++] = uint8_t(i);
// Skip unused symbols
uint8_t* usedSymbols = symbolsSortedByLength;
while (!lengths[*usedSymbols])
++usedSymbols;
return usedSymbols;
}
void compute_canonical_codes(Code codes[], const uint8_t lengths[], const uint8_t symbolsSortedByLength[], size_t symbolCount) noexcept
{
Code nextCode = 0;
unsigned prevLength = 0;
for (size_t i=0; i<symbolCount; ++i)
{
size_t symbol = symbolsSortedByLength[i];
unsigned length = lengths[symbol];
assert(0u < length && length <= 64u);
Code code = nextCode << (length - prevLength);
codes[symbol] = code;
nextCode = code + 1;
prevLength = length;
}
}
struct DecodingEntry
{
BitBuffer firstCode;
uint8_t firstSymbolIndex;
};
void build_decoding_table(DecodingEntry decodingTable[maxCodeLength+2], const uint8_t usedSymbolsSortedByLength[], size_t usedSymbolCount, const uint8_t lengths[maxSymbolCount], const Code codes[maxSymbolCount]) noexcept
{
unsigned previousLength = 0;
for (size_t i=0; i<usedSymbolCount;)
{
size_t symbol = usedSymbolsSortedByLength[i];
unsigned length = lengths[symbol];
assert(0u < length && length <= maxCodeLength);
BitBuffer code = BitBuffer(codes[symbol]) << (8*sizeof(BitBuffer) - length); // Left align the code in the bit buffer
for (unsigned l=previousLength+1; l<=length; ++l)
decodingTable[l] = {code, uint8_t(i)};
while (i<usedSymbolCount && lengths[usedSymbolsSortedByLength[i]]==length)
++i;
previousLength = length;
}
decodingTable[previousLength+1] = {std::numeric_limits<Code>::max(), UINT8_MAX};
}
} // namespace
void encode(std::vector<uint8_t>& encodedData, const uint8_t* data, size_t dataSize)
{
const Clock::time_point t0 = Clock::now();
// Compute each symbol's weight
size_t weights[maxSymbolCount];
compute_histogram(weights, data, dataSize);
const Clock::time_point tHisto = Clock::now();
// Sort symbols by weight
uint8_t symbolsSortedByWeight[maxSymbolCount];
sort_symbols_by_weight(symbolsSortedByWeight, weights);
const Clock::time_point tSortByWeight = Clock::now();
// Compute the Huffman lengths
uint8_t lengths[maxSymbolCount] = {};
compute_huffman_lengths(lengths, weights, symbolsSortedByWeight);
const Clock::time_point tLengths = Clock::now();
// Apply the length limit
const unsigned lengthLimit = 12;
limit_lengths(lengths, symbolsSortedByWeight, lengthLimit);
const Clock::time_point tLimit = Clock::now();
// Sort symbols by length
uint8_t symbolsSortedByLength[maxSymbolCount];
const uint8_t* usedSymbols = sort_symbols_by_length(symbolsSortedByLength, lengths);
const size_t usedSymbolCount = maxSymbolCount - (usedSymbols - symbolsSortedByLength);
const Clock::time_point tSortByLength = Clock::now();
// Compute canonical codes
Code codes[maxSymbolCount] = {};
compute_canonical_codes(codes, lengths, usedSymbols, usedSymbolCount);
const Clock::time_point tCodes = Clock::now();
// Compute the size of the encoded data
size_t encodedDataSizeInBits = 0;
for (size_t i=0; i<dataSize; ++i)
encodedDataSizeInBits += lengths[data[i]];
const size_t headerSize = maxSymbolCount; // The length of each symbol on 1 byte
const size_t encodedDataSize = headerSize + (encodedDataSizeInBits + 7) / 8;
encodedData.resize(encodedDataSize);
// Write header
uint8_t* writePtr = encodedData.data();
memcpy(writePtr, lengths, headerSize);
writePtr += headerSize;
// Encode data
uint8_t bitBuffer = 0;
unsigned bitBufferSize = 0;
for (size_t i=0; i<dataSize; ++i)
{
// Retrieve code
size_t symbol = data[i];
Code code = codes[symbol];
unsigned length = lengths[symbol];
// Push code into the bit buffer
constexpr unsigned codeBits = sizeof(code) * 8;
code <<= codeBits - length; // Left align code for easier consumption by the bit buffer
do
{
unsigned count = std::min(8u-bitBufferSize, length);
bitBuffer = uint8_t((bitBuffer << count) | (code >> (codeBits-count)));
bitBufferSize += count;
length -= count;
code <<= count;
if (bitBufferSize == 8u)
{
*writePtr++ = bitBuffer;
bitBufferSize = 0;
}
}
while (length != 0u);
}
// Flush bit buffer
if (bitBufferSize != 0u)
{
bitBuffer <<= 8 - bitBufferSize;
*writePtr++ = bitBuffer;
}
const Clock::time_point tDone = Clock::now();
assert(size_t(writePtr - encodedData.data()) == encodedDataSize);
printf("Encoding: %5.3f ms\n", to_ms(t0, tDone));
printf(" Histogram: %10.3f us\n", to_us(t0, tHisto));
printf(" Sort by weight: %10.3f us\n", to_us(tHisto, tSortByWeight));
printf(" Compute lengths: %10.3f us\n", to_us(tSortByWeight, tLengths));
printf(" Limit lengths: %10.3f us\n", to_us(tLengths, tLimit));
printf(" Sort by length: %10.3f us\n", to_us(tLimit, tSortByLength));
printf(" Compute codes: %10.3f us\n", to_us(tSortByLength, tCodes));
printf(" Encoding loop: %10.3f us\n", to_us(tCodes, tDone));
printf("\n");
}
void decode(std::vector<uint8_t>& decodedData, const uint8_t* encodedData, size_t encodedDataSize)
{
(void)encodedDataSize; // Only used for assert
const Clock::time_point t0 = Clock::now();
constexpr unsigned bitBufferCapacity = sizeof(BitBuffer) * 8;
// Read header
const uint8_t* lengths = encodedData;
const uint8_t* readPtr = encodedData + maxSymbolCount;
// Sort symbols by length
uint8_t symbolsSortedByLength[maxSymbolCount];
const uint8_t* usedSymbols = sort_symbols_by_length(symbolsSortedByLength, lengths);
const size_t usedSymbolCount = maxSymbolCount - (usedSymbols - symbolsSortedByLength);
const Clock::time_point tSortByLength = Clock::now();
// Compute canonical codes
Code codes[maxSymbolCount] = {};
compute_canonical_codes(codes, lengths, usedSymbols, usedSymbolCount);
const Clock::time_point tCodes = Clock::now();
// Build decoding table
DecodingEntry decodingTable[maxSymbolCount+2];
build_decoding_table(decodingTable, usedSymbols, usedSymbolCount, lengths, codes);
const Clock::time_point tTable = Clock::now();
// Decode data
const unsigned minLength = lengths[usedSymbols[0]];
Code bitBuffer = 0;
unsigned bitBufferSize = 0;
uint8_t* writePtr = decodedData.data();
for (size_t i=0; i<decodedData.size(); ++i)
{
// Figure out symbol's length
unsigned length = minLength + 1;
for (;;)
{
while (length < bitBufferSize && bitBuffer >= decodingTable[length].firstCode)
++length;
if (length <= bitBufferSize)
break;
// Read some more bits and retry
assert(readPtr < encodedData+encodedDataSize);
bitBuffer |= *readPtr++ << (bitBufferCapacity - bitBufferSize - 8);
bitBufferSize += 8;
assert(bitBufferSize <= bitBufferCapacity);
}
--length;
// Decode symbol
const DecodingEntry& e = decodingTable[length];
unsigned shift = bitBufferCapacity - length; // To right align the code
Code code = bitBuffer >> shift;
Code firstCode = decodingTable[length].firstCode >> shift;
uint8_t symbol = usedSymbols[e.firstSymbolIndex + (code - firstCode)];
*writePtr++ = symbol;
bitBuffer <<= length;
bitBufferSize -= length;
}
const Clock::time_point tDone = Clock::now();
printf("Decoding: %5.3f ms\n", to_ms(t0, tDone));
printf(" Sort by length: %10.3f us\n", to_us(t0, tSortByLength));
printf(" Compute codes: %10.3f us\n", to_us(tSortByLength, tCodes));
printf(" Build table: %10.3f us\n", to_us(tCodes, tTable));
printf(" Decoding loop: %10.3f us\n", to_us(tTable, tDone));
printf("\n");
}