-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeflate.cpp
1229 lines (1129 loc) · 49.8 KB
/
deflate.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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* File: deflate.cpp
* Author: Yuxuan (Eric) Zheng
*
* Created on July 8, 2016, 10:52 AM
*/
#include "deflate.h"
/*
* Good references:
* 1. Data Compression The Complete Reference 4th edition by David Salomon
* 2. rfc1951 standard
* 3. http://www.gzip.org/algorithm.txt by gzip
* 4. Gzip on a Chip: High Performance Lossless Data Compression on FPGAs using OpenCL by Abdelfattah
*
* Notes:
*
* 1. Currently, the core only supports max offset = 4096 and max length = 32(LEN) for LZ77
* It's a tradeoff between compression ratio and speed.
* 2. Please note that the static Huffman encoding uses Little-Endian now.
* 3. The dynamic encoding part is commented out. Because building dynamic Huffman trees can be
* implemented using hardware or host CPU + PCIe. The commented code is the major part of
* hardware implementation.
*/
/*
* Endianness Clarification:
*
* Endianness is quite confusing in the standard and in the project.
* It's necessary to state the endianness details in the cores.
*
* Compression:
* 1. LZ77 core: no endianness issue.
*
* 2. Static Huffman Encoding Part:
* header bits: little-endian (correct)
* huffman codes: big-endian (correct)
* extra bits: big-endian (wrong, should change to little-endian if following standard)
* write to output stream: little-endian (correct)
* 3. Dynamic Huffman Encoding Part:
* All big-endian; extra bits should be little-endian
*
* Decompression:
* 1. Static Huffman Decoding Part:
* header bits: little-endian (correct)
* read from input stream: little-endian (correct)
* huffman codes: big-endian (correct)
* extra bits: big-endian (wrong, should be little-endian)
* 2. Dynamic Huffman Decoding Part:
* header bits: little-endian (correct)
* read from input stream: little-endian (correct)
* huffman codes: big-endian (correct)
* all other extra bits, HLIT codes, etc. : little-endian (correct)
*
* In summary, the extra bits endianness in static Huffman encoding/decoding is wrong.
* Other parts are correct. The Deflate and Inflate core matches each other.
*/
// Top level module for compression
void Deflate(hls::stream<uint32_t> &input, int size,
hls::stream<uint32_t> &output)
{
// temp array for connecting two cores
uint8_t LZ77_output[3000];
LZ77(input, size, LZ77_output);
// // Print out the compressed data - for testing
// int offset, length;
// int j = 0;
// cout << endl << "LZ77 compressed stream is:" << endl << endl;
// while (LZ77_output[j] != '\0') {
// if (LZ77_output[j] == '@') {
// offset = (LZ77_output[j + 1] * 128) + LZ77_output[j + 2];
// length = LZ77_output[j + 3];
// cout << "@(" << offset << "," << length << ")";
// j += 4;
// } else {
// cout << LZ77_output[j];
// j++;
// }
// }
//
// cout << endl << endl;
huffman(LZ77_output, output);
return;
}
/*
* The first part of DEFLATE Algorithm - LZ77
*
* Interface: hls_stream for input and output
*
* Notes:
*
* LZ77 searches max matching in the dictionaries stored before. If found
* a matching, the algorithm makes a pair of @(offset, length) on the data stream.
* The output of LZ77 is a data array containing the literals, offsets, and lengths.
* Then, the output is further compressed in the Huffman function below.
*
* At first, LZ77 takes one word from the hls_stream input. Calculate its hash value
* trying to find a match in dictionaries. Next, LZ77 compares the string in curr_window
* and comp_window to find the matching length. Note that this process is done
* for each string with length LEN starting at each char of the curr_window
* (totally, VEC strings with length LEN; called lazy evaluation). Then, the
* program tries to decide which matching to choose through the approach from
* Mohamed's paper. Next, the results are stored into the output array. Finally,
* the program updates the dictionaries using the data in curr_window.
*
*
* Also note that the function argument 'size' is not necessary if the outermost
* loop checks whether the input stream is empty each time.
*/
void LZ77(hls::stream<uint32_t> &input, int size, uint8_t output[3000])
{
/*************************** Initialization *******************************/
int iteration_count = size / VEC;
// Use current_index to indicate the start index of each set of processing data
int current_index = 0;
int output_position = 0;
uint8_t comp_window[NUM_DICT][VEC][LEN];
uint8_t dict[NUM_DICT][HASH_TABLE_SIZE][LEN];
// Record the information of where the string starts - in order to calculate offset
int dict_string_start_pos[NUM_DICT][HASH_TABLE_SIZE];
int compare_window_string_start_pos[NUM_DICT][VEC];
int hash_value, new_hash_value;
bool done[VEC];
int length[VEC];
match_pair bestlength[VEC];
int match_length;
int offset;
int start_match_position = 0;
int first_valid_position = 4;
int temp_valid_position = 0;
// For hls_stream input
uint8_t curr_window[VEC + LEN]; // a processing buffer containing all information to use
uint32_t input_word;
for (char i = 1; i < 9; i++)
{ // first time to fill in the processing buffer
input.read(input_word); // read one word from the input stream
curr_window[i * 4] = (input_word & 0xFF000000) >> 24;
curr_window[i * 4 + 1] = (input_word & 0x00FF0000) >> 16;
curr_window[i * 4 + 2] = (input_word & 0x0000FF00) >> 8;
curr_window[i * 4 + 3] = (input_word & 0x000000FF);
}
/************************** Main Loop *************************************/
CONTROL_LOOP:
for (int loop_count = 0; loop_count < iteration_count; loop_count++)
{
#pragma HLS loop_tripcount min = 635 max = 635
// 1. Dictionary Lookup and Update
// search each dictionary to find most similar sequence
// repeat for VEC sequence substrings; store data into memory
// Shift current window
for (char i = 0; i < LEN; i++)
{
#pragma HLS UNROLL
curr_window[i] = curr_window[VEC + i];
}
// Load in new data
if (!input.empty())
{
input.read(input_word);
curr_window[LEN] = (input_word & 0xFF000000) >> 24;
curr_window[LEN + 1] = (input_word & 0x00FF0000) >> 16;
curr_window[LEN + 2] = (input_word & 0x0000FF00) >> 8;
curr_window[LEN + 3] = (input_word & 0x000000FF);
}
first_valid_position -= VEC; // minus VEC since the buffer will be shifted to left
SUBSTRING_MATCHING:
for (int i = 0; i < VEC; i++)
{
#pragma HLS loop_tripcount min = 4 max = 4
#pragma HLS UNROLL
// for each substring
hash_value = (curr_window[i] << 3) ^ (curr_window[i + 1] << 2) ^ (curr_window[i + 2] << 1) ^ (curr_window[i + 3]);
/* The hash function is an important part for improving compression ratio.
* The current version is chosen by testing many possible cases. */
DICT_MATCHING:
for (int t = 0; t < NUM_DICT; t++)
{
#pragma HLS UNROLL
#pragma HLS loop_tripcount min = 4 max = 4
if (dict_string_start_pos[t][hash_value] != '\0')
{ // found a match
compare_window_string_start_pos[t][i] = dict_string_start_pos[t][hash_value];
COPY_LOOP_MATCHING:
for (int j = 0; j < LEN; j++)
{
#pragma HLS loop_tripcount min = 32 max = 32
#pragma HLS UNROLL
// copy the string to compare window
comp_window[t][i][j] = dict[t][hash_value][j];
}
}
}
}
// 2. Match Search and Reduction
// clear best length
CLEAN_BESTLENGTH:
for (int i = 0; i < VEC; i++)
{
#pragma HLS UNROLL
#pragma HLS loop_tripcount min = 4 max = 4
bestlength[i].string_start_pos = 0;
bestlength[i].length = 0;
}
// calculate the length of the same sequence
FIND_MATCHING_LENGTH:
for (int i = 0; i < VEC; i++)
{ // for different dictionary
#pragma HLS loop_tripcount min = 4 max = 4
#pragma HLS UNROLL
// clear done[]
CLEAN_LENGTH_AND_DONE:
for (int n = 0; n < VEC; n++)
{
#pragma HLS UNROLL
#pragma HLS loop_tripcount min = 4 max = 4
done[n] = false;
length[n] = 0;
}
SUBSTRING_MATCHING_LENGTH:
for (int j = 0; j < VEC; j++)
{ // for each substring (lazy evaluation)
#pragma HLS UNROLL
#pragma HLS loop_tripcount min = 4 max = 4
COMPARE_EACH_CHAR:
for (int k = 0; k < LEN; k++)
{ // for each char of each substring
#pragma HLS UNROLL
#pragma HLS loop_tripcount min = 32 max = 32
if (done[j])
break;
if (curr_window[j + k] == comp_window[i][j][k] && !done[j])
length[j]++;
else
done[j] = true;
//if (curr_window[j + k] == comp_window[k][i][j]) // possible way to improve area
//length_bool[i][j] |= 1 << k;
}
}
// update best length
// here, i is the index of dictionary
UPDATE_BESTLENGTH:
for (int m = 0; m < VEC; m++)
{
#pragma HLS UNROLL
#pragma HLS loop_tripcount min = 4 max = 4
if (length[m] > bestlength[m].length)
{
bestlength[m].length = length[m];
bestlength[m].string_start_pos = compare_window_string_start_pos[i][m];
}
}
}
// 3. Match Filtering
// choose which one to compress // huge modification for hls_stream
temp_valid_position = first_valid_position;
match_length = 0;
CHOOSE_MATCHING_STRING:
for (int i = first_valid_position; i < VEC; i++)
{ // go through bestlength
#pragma HLS UNROLL
#pragma HLS loop_tripcount min = 4 max = 4
if (bestlength[i].length >= 3)
{ // can be a candidate
if (i + bestlength[i].length > temp_valid_position)
{
// find a later match
temp_valid_position = i + bestlength[i].length;
match_length = bestlength[i].length;
start_match_position = i;
offset = (current_index + i) - bestlength[i].string_start_pos;
}
}
}
// 4. Fill In the Output Array
if (match_length > 0 && offset < 4096)
{ // some data was compressed
FILL_LOOP_LITERAL:
while (first_valid_position < start_match_position)
{
#pragma HLS loop_tripcount min = 0 max = 4
#pragma HLS PIPELINE
#pragma HLS UNROLL
// copy the literals
output[output_position] = curr_window[first_valid_position];
first_valid_position++;
output_position++;
}
output[output_position] = '@'; // record marker // There is a corner case here. If the input has '@', the marker is wrong.
output[output_position + 1] = offset >> 7; // record offset
output[output_position + 2] = (offset & 0x07F);
output[output_position + 3] = match_length; // record length
output_position += 4;
// update the first_valid_position
first_valid_position = temp_valid_position;
// when first valid position doesn't exceed next buffer frame
FILL_LOOP_UNTIL_NEXT_BUFFER:
while (first_valid_position < VEC)
{
#pragma HLS UNROLL
#pragma HLS PIPELINE
#pragma HLS loop_tripcount min = 0 max = 1
output[output_position] = curr_window[first_valid_position];
first_valid_position++;
output_position++;
}
}
else
{ // no data was compressed, or offset >= 4096
FILL_LOOP_NO_COMPRESSION:
while (first_valid_position < VEC)
{
#pragma HLS UNROLL
#pragma HLS PIPELINE
#pragma HLS loop_tripcount min = 0 max = 4
// copy the block to output
output[output_position] = curr_window[first_valid_position];
first_valid_position++;
output_position++;
}
}
// 5. Update Dictionaries
UPDATE_DICT:
for (int i = 0; i < VEC; i++)
{
#pragma HLS UNROLL
#pragma HLS loop_tripcount min = 4 max = 4
new_hash_value = (curr_window[i] << 3) ^ (curr_window[i + 1] << 2) ^ (curr_window[i + 2] << 1) ^ (curr_window[i + 3]);
UPDATE_COPY_STRING:
for (int j = 0; j < LEN; j++)
{
#pragma HLS UNROLL
#pragma HLS loop_tripcount min = 32 max = 32
dict[i][new_hash_value][j] = curr_window[i + j];
}
dict_string_start_pos[i][new_hash_value] = current_index + i;
}
// Move the current window index by VEC bytes
current_index += VEC;
}
// Fill the remaining bytes
FILL_LOOP_REMAINING_BYTES:
while (curr_window[first_valid_position] != '\0')
{
#pragma HLS UNROLL
#pragma HLS PIPELINE
#pragma HLS loop_tripcount min = 0 max = 4
output[output_position] = curr_window[first_valid_position];
first_valid_position++;
output_position++;
}
output[output_position] = '\0';
return;
}
/*
* The second part of DEFLATE Algorithm - Huffman encoding
*
* Input: an array of uint8_t, compressed by LZ77 algorithm
* Output: a hls_stream containing the Huffman encoding result
*
* Notes:
*
* There are a lot of details in the Huffman encoding process, especially for
* dynamic Huffman encoding. Basically, the algorithm uses shorter code to
* represent the symbols appear frequently. The static Huffman encoding mode is
* like hard-coding. The dynamic Huffman encoding is hard to implement through
* hardware. Therefore, some techniques can be used to store a dynamic tree on
* hardware or traverse the tree without recursion and stack/queue. However, in
* Mohamed's paper, the hardware needs not to build dynamic trees by itself,
* which could be another way to solve the problem.
*
* The static encoding part checks each input char and finds the corresponding code.
* The dynamic part is not completely finished because the Huffman core cannot
* get trees from CPU currently. However, all the essential parts for dynamic
* Huffman encoding were finished, including building the trees and getting codes
* through the trees. Some comments explain how to use hardware the build the
* entire core.
*
*/
void huffman(uint8_t input[3000], hls::stream<uint32_t> &output)
{
int input_pos = 0, output_pos = 0;
unsigned length_valid_bits_num, offset_valid_bits_num,
output_char_remaining_bits, remaining_bits_to_output;
unsigned offset;
uint8_t input_char, length;
uint8_t code_8_bits;
uint16_t code_9_bits;
uint16_t length_code, offset_code;
uint32_t length_and_offset;
uint8_t output_char_buffer;
// For hls_stream input
uint32_t output_word;
uint8_t output_buffer[3000]; // temp buffer for storing output data
short mode = 1;
// mode indicates which type of Huffman encoding is used
// mode = 0: no compression; mode = 1: static Huffman; mode = 2: dynamic Huffman
if (mode == 1)
{
// Static Huffman Encoding
// write the flag code of static Huffman encoding into output array
output_buffer[0] = 0xC0;
output_char_remaining_bits = 5;
// analyze the input
STATIC_HUFFMAN:
while (input[input_pos] != '\0')
{
input_char = input[input_pos];
if (input_char == '@')
{
// meet a match
length = input[input_pos + 3];
if (length >= 115 || length < 3)
{
// currently, impossible to enter here
cout << "The matching length is too long or too short! Wrong!"
<< endl;
}
else
{
// edoc: 257-279
if (length >= 3 && length <= 10)
{
// 257-264, no extra bit
length_code = length - 2;
length_valid_bits_num = 7;
}
else if (length == 11 || length == 12)
{
// 265-268, 1 extra bit
length_code = 0b00010010 + (length - 11);
length_valid_bits_num = 8;
}
else if (length == 13 || length == 14)
{
// 265-268, 1 extra bit
length_code = 0b00010100 + (length - 13);
length_valid_bits_num = 8;
}
else if (length == 15 || length == 16)
{
// 265-268, 1 extra bit
length_code = 0b00010110 + (length - 15);
length_valid_bits_num = 8;
}
else if (length == 17 || length == 18)
{
// 265-268, 1 extra bit
length_code = 0b00011000 + (length - 17);
length_valid_bits_num = 8;
}
else if (length >= 19 && length <= 22)
{
// 269-272, 2 extra bits
length_code = 0b000110100 + (length - 19);
length_valid_bits_num = 9;
}
else if (length >= 23 && length <= 26)
{
// 269-272, 2 extra bits
length_code = 0b000111000 + (length - 23);
length_valid_bits_num = 9;
}
else if (length >= 27 && length <= 30)
{
// 269-272, 2 extra bits
length_code = 0b000111100 + (length - 27);
length_valid_bits_num = 9;
}
else if (length >= 31 && length <= 34)
{
// 269-272, 2 extra bits
length_code = 0b001000000 + (length - 31);
length_valid_bits_num = 9;
}
else if (length >= 35 && length <= 42)
{
// 273-276, 3 extra bits
length_code = 0b0010001000 + (length - 35);
length_valid_bits_num = 10;
}
else if (length >= 43 && length <= 50)
{
// 273-276, 3 extra bits
length_code = 0b0010010000 + (length - 43);
length_valid_bits_num = 10;
}
else if (length >= 51 && length <= 58)
{
// 273-276, 3 extra bits
length_code = 0b0010011000 + (length - 51);
length_valid_bits_num = 10;
}
else if (length >= 59 && length <= 66)
{
// 273-276, 3 extra bits
length_code = 0b0010100000 + (length - 59);
length_valid_bits_num = 10;
}
else if (length >= 67 && length <= 82)
{
// 277-279, 4 extra bits
length_code = 0b00101010000 + (length - 67);
length_valid_bits_num = 11;
}
else if (length >= 83 && length <= 98)
{
// 277-279, 4 extra bits
length_code = 0b00101100000 + (length - 83);
length_valid_bits_num = 11;
}
else if (length >= 99 && length <= 114)
{
// 277-279, 4 extra bits
length_code = 0b00101110000 + (length - 99);
length_valid_bits_num = 11;
}
else
{
cout << "Wrong! Cannot find the correct length code!"
<< endl;
}
}
offset = input[input_pos + 1] * 128 + input[input_pos + 2];
if (offset >= 1 && offset <= 4)
{
offset_code = offset - 1;
offset_valid_bits_num = 5;
}
else if (offset >= 5 && offset <= 6)
{
offset_code = 0b001000 + (offset - 5);
offset_valid_bits_num = 6;
}
else if (offset >= 7 && offset <= 8)
{
offset_code = 0b001010 + (offset - 7);
offset_valid_bits_num = 6;
}
else if (offset >= 9 && offset <= 12)
{
offset_code = 0b0011000 + (offset - 9);
offset_valid_bits_num = 7;
}
else if (offset >= 13 && offset <= 16)
{
offset_code = 0b0011100 + (offset - 13);
offset_valid_bits_num = 7;
}
else if (offset >= 17 && offset <= 24)
{
offset_code = 0b01000000 + (offset - 17);
offset_valid_bits_num = 8;
}
else if (offset >= 25 && offset <= 32)
{
offset_code = 0b01001000 + (offset - 25);
offset_valid_bits_num = 8;
}
else if (offset >= 33 && offset <= 48)
{
offset_code = 0b010100000 + (offset - 33);
offset_valid_bits_num = 9;
}
else if (offset >= 49 && offset <= 64)
{
offset_code = 0b010110000 + (offset - 49);
offset_valid_bits_num = 9;
}
else if (offset >= 65 && offset <= 96)
{
offset_code = 0b0110000000 + (offset - 65);
offset_valid_bits_num = 10;
}
else if (offset >= 97 && offset <= 128)
{
offset_code = 0b0110100000 + (offset - 97);
offset_valid_bits_num = 10;
}
else if (offset >= 129 && offset <= 192)
{
offset_code = 0b01110000000 + (offset - 129);
offset_valid_bits_num = 11;
}
else if (offset >= 193 && offset <= 256)
{
offset_code = 0b01111000000 + (offset - 193);
offset_valid_bits_num = 11;
}
else if (offset >= 257 && offset <= 384)
{
offset_code = 0b100000000000 + (offset - 257);
offset_valid_bits_num = 12;
}
else if (offset >= 385 && offset <= 512)
{
offset_code = 0b100010000000 + (offset - 385);
offset_valid_bits_num = 12;
}
else if (offset >= 513 && offset <= 768)
{
offset_code = 0b1001000000000 + (offset - 513);
offset_valid_bits_num = 13;
}
else if (offset >= 769 && offset <= 1024)
{
offset_code = 0b1001100000000 + (offset - 769);
offset_valid_bits_num = 13;
}
else if (offset >= 1025 && offset <= 1536)
{
offset_code = 0b10100000000000 + (offset - 1025);
offset_valid_bits_num = 14;
}
else if (offset >= 1537 && offset <= 2048)
{
offset_code = 0b10101000000000 + (offset - 1537);
offset_valid_bits_num = 14;
}
else if (offset >= 2049 && offset <= 3072)
{
offset_code = 0b101100000000000 + (offset - 2049);
offset_valid_bits_num = 15;
}
else if (offset >= 3073 && offset <= 4096)
{
offset_code = 0b101110000000000 + (offset - 3073);
offset_valid_bits_num = 15;
}
else
{
cout << "Wrong! Offset is out of range!" << endl;
}
// write the length_code and offset_code into the output
// combine length and offset codes
remaining_bits_to_output = length_valid_bits_num + offset_valid_bits_num;
length_and_offset = (length_code << (32 - length_valid_bits_num)) | (offset_code << (32 - remaining_bits_to_output));
if (output_char_remaining_bits != 8)
{
// not a 8-bit boundary to start, move the 8-bit boundary
output_char_buffer = length_and_offset >> (32 - output_char_remaining_bits);
output_buffer[output_pos] = output_buffer[output_pos] | output_char_buffer;
length_and_offset <<= output_char_remaining_bits;
remaining_bits_to_output -= output_char_remaining_bits;
output_char_remaining_bits = 8;
output_pos++;
}
// starting at 8-bit boundary, iteration to write into the output array
STATIC_HUFFMAN_WRITE_OUTPUT:
while (remaining_bits_to_output >= 8)
{
// can write to the next whole char
output_buffer[output_pos++] = (length_and_offset & 0xFF000000) >> 24;
length_and_offset <<= 8;
remaining_bits_to_output -= 8;
}
if (remaining_bits_to_output == 0)
{
// do nothing
}
else
{
// follow-up steps: output the remaining bits (less than 8)
output_buffer[output_pos] = length_and_offset >> 24;
output_char_remaining_bits = 8 - remaining_bits_to_output;
}
input_pos += 4;
}
else
{
// normal literals
if (input_char <= 143)
{
// edoc: 0-143
code_8_bits = input_char + 0x30;
// write the literal code into the output
if (output_char_remaining_bits == 8)
{
// enough space to store the literal in this position
output_buffer[output_pos++] = code_8_bits;
}
else
{
// not enough to store the 8 bit literal code
output_char_buffer = code_8_bits >> (8 - output_char_remaining_bits);
output_buffer[output_pos] = output_buffer[output_pos] | output_char_buffer;
output_pos++;
output_buffer[output_pos] = code_8_bits << output_char_remaining_bits;
}
}
else
{
// edoc: 144-255
code_9_bits = input_char - 144 + 0x190;
// not enough to store the 9 bit literal code
output_char_buffer = code_9_bits >> (9 - output_char_remaining_bits);
output_buffer[output_pos] = output_buffer[output_pos] | output_char_buffer;
output_char_buffer = code_9_bits << output_char_remaining_bits;
output_buffer[output_pos + 1] = output_char_buffer;
output_char_remaining_bits--;
if (output_char_remaining_bits == 0)
{
output_pos += 2;
output_char_remaining_bits = 8;
}
else
{
output_pos++;
}
}
input_pos++;
}
}
// finish encoding, edoc: 256
if (output_char_remaining_bits == 8)
{
output_buffer[output_pos] = '\0';
}
else
{
output_buffer[output_pos + 1] = '\0';
}
// Store output_buffer data into output stream
output_pos++;
int copy_count = (output_pos + 1) / 4;
copy_count++;
for (int i = 0; i < copy_count; i++)
{
output_word = (reverse(output_buffer[i * 4], 8) << 24) | (reverse(output_buffer[i * 4 + 1], 8) << 16) | (reverse(output_buffer[i * 4 + 2], 8) << 8) | (reverse(output_buffer[i * 4 + 3], 8));
output.write(output_word);
}
}
else if (mode == 2)
{
// // Dynamic Huffman Encoding
//
// /*********************** Initialization *******************************/
//
// // Use fixed size array to store dynamic trees
// // 0-29: distances (leaf); 30-89: parent nodes in the distance Huffman tree
// tree_node distance_tree[90];
// // 0-279: literals and lengths (leaf); 280-599: parent nodes in the LIT Huffman tree
// tree_node literal_tree[600];
//
// smallest_node smallest_two_nodes[2];
// smallest_node lit_smallest_two_nodes[2];
//
// code_table_node dis_codes[30];
// code_table_node lit_codes[280];
//
// for (int i = 0; i < 90; i++) {
//#pragma HLS UNROLL
// distance_tree[i].no_parent = true;
// distance_tree[i].level = 0;
// distance_tree[i].weight = 0;
// }
//
// for (int i = 0; i < 280; i++) {
//#pragma HLS UNROLL
// literal_tree[i].no_parent = true;
// literal_tree[i].level = 0;
// literal_tree[i].weight = 0;
// }
// /**********************************************************************/
//
// // write the flag code of dynamic Huffman encoding into output array
// output[0] = 0xC0;
// output_char_remaining_bits = 5;
//
// while (input[input_pos] != '\0') {
// input_char = input[input_pos];
//
// // Get the count of each char, length, or offset
// // store the information into distance_count and literal_count
// if (input_char == '@') {
// // Found a length and distance
// offset = input[input_pos + 1] * 128 + input[input_pos + 2];
//
// if (offset >= 1 && offset <= 4) {
// distance_tree[offset - 1].weight++;
// } else if (offset >= 5 && offset <= 6) {
// distance_tree[4].weight++;
// } else if (offset >= 7 && offset <= 8) {
// distance_tree[5].weight++;
// } else if (offset >= 9 && offset <= 12) {
// distance_tree[6].weight++;
// } else if (offset >= 13 && offset <= 16) {
// distance_tree[7].weight++;
// } else if (offset >= 17 && offset <= 24) {
// distance_tree[8].weight++;
// } else if (offset >= 25 && offset <= 32) {
// distance_tree[9].weight++;
// } else if (offset >= 33 && offset <= 48) {
// distance_tree[10].weight++;
// } else if (offset >= 49 && offset <= 64) {
// distance_tree[11].weight++;
// } else if (offset >= 65 && offset <= 96) {
// distance_tree[12].weight++;
// } else if (offset >= 97 && offset <= 128) {
// distance_tree[13].weight++;
// } else if (offset >= 129 && offset <= 192) {
// distance_tree[14].weight++;
// } else if (offset >= 193 && offset <= 256) {
// distance_tree[15].weight++;
// } else if (offset >= 257 && offset <= 384) {
// distance_tree[16].weight++;
// } else if (offset >= 385 && offset <= 512) {
// distance_tree[17].weight++;
// } else if (offset >= 513 && offset <= 768) {
// distance_tree[18].weight++;
// } else if (offset >= 769 && offset <= 1024) {
// distance_tree[19].weight++;
// } else if (offset >= 1025 && offset <= 1536) {
// distance_tree[20].weight++;
// } else if (offset >= 1537 && offset <= 2048) {
// distance_tree[21].weight++;
// } else if (offset >= 2049 && offset <= 3072) {
// distance_tree[22].weight++;
// } else if (offset >= 3073 && offset <= 4096) {
// distance_tree[23].weight++;
// } else {
// cout << "Wrong! Offset is out of range!" << endl;
// }
//
// length = input[input_pos + 3];
//
// // edoc: 257-279
// if (length >= 3 && length <= 10) {
// // 257-264, no extra bit
// literal_tree[length + 254].weight++;
// } else if (length == 11 || length == 12) {
// // 265-268, 1 extra bit
// literal_tree[265].weight++;
// } else if (length == 13 || length == 14) {
// // 265-268, 1 extra bit
// literal_tree[266].weight++;
// } else if (length == 15 || length == 16) {
// // 265-268, 1 extra bit
// literal_tree[267].weight++;
// } else if (length == 17 || length == 18) {
// // 265-268, 1 extra bit
// literal_tree[268].weight++;
// } else if (length >= 19 && length <= 22) {
// // 269-272, 2 extra bits
// literal_tree[269].weight++;
// } else if (length >= 23 && length <= 26) {
// // 269-272, 2 extra bits
// literal_tree[270].weight++;
// } else if (length >= 27 && length <= 30) {
// // 269-272, 2 extra bits
// literal_tree[271].weight++;
// } else if (length >= 31 && length <= 34) {
// // 269-272, 2 extra bits
// literal_tree[272].weight++;
// } else if (length >= 35 && length <= 42) {
// // 273-276, 3 extra bits
// literal_tree[273].weight++;
// } else if (length >= 43 && length <= 50) {
// // 273-276, 3 extra bits
// literal_tree[274].weight++;
// } else if (length >= 51 && length <= 58) {
// // 273-276, 3 extra bits
// literal_tree[275].weight++;
// } else if (length >= 59 && length <= 66) {
// // 273-276, 3 extra bits
// literal_tree[276].weight++;
// } else if (length >= 67 && length <= 82) {
// // 277-279, 4 extra bits
// literal_tree[277].weight++;
// } else if (length >= 83 && length <= 98) {
// // 277-279, 4 extra bits
// literal_tree[278].weight++;
// } else if (length >= 99 && length <= 114) {
// // 277-279, 4 extra bits
// literal_tree[279].weight++;
// } else {
// // currently, impossible to enter here
// cout << "The matching length is too long or too short! Wrong!"
// << endl;
// }
// input_pos += 4;
// } else {
// // Found a literal
// literal_tree[input_char].weight++;
// input_pos++;
// }
// }
//
// /********************** Build the Huffman trees ***********************/
// /************************** Distance Tree *****************************/
// unsigned dis_max_parent_node_id = 30; // record the current position of parent nodes
// unsigned dis_element_count = 0; // the # of elements in the table
//
// // NEED TO REPEAT # ELEMENTS - 1 TIMES
// // (elements - 1) times to build the tree
// for (int i = 0; i < 30; i++) {
// if (distance_tree[i].weight != 0)
// dis_element_count++;
// }
// dis_element_count--;
//
// for (int repeat_count = 0; repeat_count < dis_element_count; repeat_count++) {
// // get the smallest two nodes
// smallest_two_nodes[0].node_weight = 30000;
// smallest_two_nodes[1].node_weight = 30000;
//
// for (int i = 0; i < 90; i++) { // may have error ...
// if (distance_tree[i].weight != 0 && distance_tree[i].no_parent == true) {
// // Can be considered to build a tree
// if (distance_tree[i].weight < smallest_two_nodes[0].node_weight) {
// smallest_two_nodes[1].node_weight = smallest_two_nodes[0].node_weight;
// smallest_two_nodes[1].node_id = smallest_two_nodes[0].node_id;
//
// smallest_two_nodes[0].node_weight = distance_tree[i].weight;
// smallest_two_nodes[0].node_id = i;
// } else if (distance_tree[i].weight < smallest_two_nodes[1].node_weight) {
// smallest_two_nodes[1].node_weight = distance_tree[i].weight;