-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03-fast-huffman.cpp
436 lines (360 loc) · 15.6 KB
/
03-fast-huffman.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
/*
* 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: linear-time Huffman tree construction.
* For this we need two things:
* - Linear-time sorting of symbols by weight (achieved via radix sort)
* - Linear-time tree building from sorted symbols
*/
#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);
}
struct TreeItem
{
size_t weight;
uint8_t number;
const TreeItem* children[2] = {};
};
const TreeItem* build_huffman_tree(TreeItem tree[2*maxSymbolCount-1], 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
for (size_t i=0; i < usedSymbolCount; ++i)
{
const uint8_t symbol = symbolsSortedByWeight[unusedSymbolCount + i];
tree[i] = {weights[symbol], symbol};
}
// Special case if we have less than two items
if (usedSymbolCount <= 1u)
return (usedSymbolCount == 1u) ? tree : nullptr;
// Build initial node: its children are the first two leaves
TreeItem* lastNode = tree + usedSymbolCount;
*lastNode = {tree[0].weight + tree[1].weight, 0, {tree+0, tree+1}};
// 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
TreeItem* endOfLeaves = tree + usedSymbolCount;
for (size_t i=0; i < usedSymbolCount-2; ++i)
{
++lastNode;
// Select 1st child
if (oldestUnconsumedLeaf < endOfLeaves && oldestUnconsumedLeaf->weight <= oldestUnconsumedNode->weight)
lastNode->children[0] = oldestUnconsumedLeaf++;
else
lastNode->children[0] = oldestUnconsumedNode++;
// Select 2nd child
if ((oldestUnconsumedLeaf < endOfLeaves && oldestUnconsumedLeaf->weight <= oldestUnconsumedNode->weight) || oldestUnconsumedNode == lastNode)
lastNode->children[1] = oldestUnconsumedLeaf++;
else
lastNode->children[1] = oldestUnconsumedNode++;
lastNode->weight = lastNode->children[0]->weight + lastNode->children[1]->weight;
}
return lastNode; // Tree root
}
void get_code_Lengths(uint8_t lengths[maxSymbolCount], const TreeItem* item, unsigned depth=0) noexcept
{
if (item->children[0] == nullptr) // Leaf
{
lengths[item->number] = uint8_t(depth);
}
else // Node
{
get_code_Lengths(lengths, item->children[0], depth+1);
get_code_Lengths(lengths, item->children[1], depth+1);
}
}
uint8_t* sort_symbols_by_length(uint8_t symbolsSortedByLength[maxSymbolCount], const uint8_t lengths[maxSymbolCount]) noexcept
{
for (size_t i=0; i<maxSymbolCount; ++i)
symbolsSortedByLength[i] = uint8_t(i);
std::stable_sort(symbolsSortedByLength, symbolsSortedByLength+maxSymbolCount, [&](size_t a, size_t b) noexcept->bool
{
return (lengths[a] < lengths[b] || (lengths[a] == lengths[b] && a < b));
});
// 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();
// Build the Huffman tree
TreeItem tree[2*maxSymbolCount-1];
const TreeItem* treeRoot;
for (int i=0; i<100; ++i)
treeRoot = build_huffman_tree(tree, weights, symbolsSortedByWeight);
const Clock::time_point tHuffman = Clock::now();
// Retrieve symbols' lengths
uint8_t lengths[maxSymbolCount] = {};
get_code_Lengths(lengths, treeRoot);
const Clock::time_point tLengths = 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(" Huffman: %10.3f us\n", to_us(tSortByWeight, tHuffman));
printf(" Compute lengths: %10.3f us\n", to_us(tHuffman, tLengths));
printf(" Sort by length: %10.3f us\n", to_us(tLengths, 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");
}