-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhuffman_encoding.cpp
60 lines (52 loc) · 2.27 KB
/
huffman_encoding.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
#include "huffman.h"
#include "assert.h"
void huffman_encoding(
/* input */ Symbol symbol_histogram[INPUT_SYMBOL_SIZE],
/* output */ PackedCodewordAndLength encoding[INPUT_SYMBOL_SIZE],
/* output */ int *num_nonzero_symbols) {
#pragma HLS DATAFLOW
Symbol filtered[INPUT_SYMBOL_SIZE];
Symbol sorted[INPUT_SYMBOL_SIZE];
Symbol sorted_copy1[INPUT_SYMBOL_SIZE];
Symbol sorted_copy2[INPUT_SYMBOL_SIZE];
ap_uint<SYMBOL_BITS> parent[INPUT_SYMBOL_SIZE-1];
ap_uint<SYMBOL_BITS> left[INPUT_SYMBOL_SIZE-1];
ap_uint<SYMBOL_BITS> right[INPUT_SYMBOL_SIZE-1];
int n;
filter(symbol_histogram, filtered, &n);
sort(filtered, n, sorted);
ap_uint<SYMBOL_BITS> length_histogram[TREE_DEPTH];
ap_uint<SYMBOL_BITS> truncated_length_histogram1[TREE_DEPTH];
ap_uint<SYMBOL_BITS> truncated_length_histogram2[TREE_DEPTH];
CodewordLength symbol_bits[INPUT_SYMBOL_SIZE];
int previous_frequency = -1;
copy_sorted:
for(int i = 0; i < n; i++) {
#pragma HLS LOOP_TRIPCOUNT min = 64 max = 128
sorted_copy1[i].value = sorted[i].value;
sorted_copy1[i].frequency = sorted[i].frequency;
sorted_copy2[i].value = sorted[i].value;
sorted_copy2[i].frequency = sorted[i].frequency;
// std::cout << sorted[i].value << " " << sorted[i].frequency << "\n";
assert(previous_frequency <= (int)sorted[i].frequency);
previous_frequency = sorted[i].frequency;
}
create_tree(sorted_copy1, n, parent, left, right);
compute_bit_length(parent, left, right, n, length_histogram);
// #ifndef __SYNTHESIS__
// // Check the result of computing the tree histogram
// int codewords_in_tree = 0;
// merge_bit_length:
// for(int i = 0; i < TREE_DEPTH; i++) {
// #pragma HLS PIPELINE II=1
// if(length_histogram[i] > 0)
// std::cout << length_histogram[i] << " codewords with length " << i << "\n";
// codewords_in_tree += length_histogram[i];
// }
// assert(codewords_in_tree == n);
// #endif
truncate_tree(length_histogram, truncated_length_histogram1, truncated_length_histogram2);
canonize_tree(sorted_copy2, n, truncated_length_histogram1, symbol_bits);
create_codeword(symbol_bits, truncated_length_histogram2, encoding);
*num_nonzero_symbols = n;
}