-
Notifications
You must be signed in to change notification settings - Fork 3
/
huffman_canonize_tree.cpp
36 lines (33 loc) · 1.28 KB
/
huffman_canonize_tree.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
#include "huffman.h"
#include "assert.h"
void canonize_tree(
/* input */ Symbol sorted[INPUT_SYMBOL_SIZE],
/* input */ ap_uint<SYMBOL_BITS> num_symbols,
/* input */ ap_uint<SYMBOL_BITS> codeword_length_histogram[TREE_DEPTH],
/* output */ CodewordLength symbol_bits[INPUT_SYMBOL_SIZE] ) {
assert(num_symbols <= INPUT_SYMBOL_SIZE);
init_bits:
for(int i = 0; i < INPUT_SYMBOL_SIZE; i++) {
symbol_bits[i] = 0;
}
ap_uint<SYMBOL_BITS> length = TREE_DEPTH;
ap_uint<SYMBOL_BITS> count = 0;
// Iterate across the symbols from lowest frequency to highest
// Assign them largest bit length to smallest
process_symbols:
for(int k = 0; k < num_symbols; k++) {
#pragma HLS LOOP_TRIPCOUNT min = 64 max = 256
if (count == 0) {
//find the next non-zero bit length
do {
#pragma HLS LOOP_TRIPCOUNT min=1 avg=1 max=2
length--;
// n is the number of symbols with encoded length i
count = codeword_length_histogram[length];
}
while (count == 0);
}
symbol_bits[sorted[k].value] = length; //assign symbol k to have length bits
count--; //keep assigning i bits until we have counted off n symbols
}
}