-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharith_test.c
174 lines (151 loc) · 4.55 KB
/
arith_test.c
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
/*
* R&D in tree-structure friendly arithmetic coding techniques.
*
* Copyright (C) 2021, 2022 Jamie Lokier
*
* This file is licensed under either of "MIT license" or "Apache License,
* Version 2.0", at your option. Links to each license respectively:
* - <http://opensource.org/licenses/MIT>
* - <http://www.apache.org/licenses/LICENSE-2.0>.
*
* This file is provided without any warranty. Use at your own risk. It is
* intended that excerpts be used and changed in other programs, subject to the
* terms of the one or both of the above licenses.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>
#include <assert.h>
#include <time.h>
#include "arith.c"
static FILE *static_model_calculate(FILE *input, StaticModel *static_model)
{
char *mem_data = 0;
size_t mem_size = 0;
FILE *mem_file = open_memstream(&mem_data, &mem_size);
if (!mem_file)
exit_perror("open_memstream");
static_model_init(static_model);
while (1) {
int ch = getc(input);
if (ch == EOF)
break;
putc((byte)ch, mem_file);
byte b = (byte)ch ^ USE_BYTE_XOR;
static_model_update(static_model, b);
}
fseeko(mem_file, 0, SEEK_SET);
return mem_file;
}
static count_t compress(FILE *input, FILE *output, StaticModel *static_model,
int64_t *time_out)
{
Compress c;
compress_init(&c, output, static_model);
int64_t time_before = get_time();
count_t count_bytes = 0;
while (1) {
int ch = getc(input);
if (ch == EOF)
break;
count_bytes++;
byte b = (byte)ch ^ USE_BYTE_XOR;
compress_symbol(&c, b);
model_update(&c.model, b);
}
compress_finish(&c);
*time_out = get_time() - time_before;
return count_bytes;
}
static void decompress(count_t count_bytes, FILE *input, FILE *output,
StaticModel *static_model, int64_t *time_out)
{
Decompress d;
decompress_init(&d, input, static_model);
char *out = xmalloc(count_bytes);
int64_t time_before = get_time();
char *end = out;
while (count_bytes > 0) {
byte b = decompress_symbol(&d);
#if 0
if (feof(input) && !ferror(input))
break;
#endif
*end++ = b ^ USE_BYTE_XOR;
model_update(&d.model, b);
count_bytes--;
}
*time_out = get_time() - time_before;
if (output) {
for (char *ptr = out; ptr < end; ptr++)
putc(*ptr, output);
}
free(out);
}
int main(int argc, char **argv)
{
FILE *input = stdin;
if (argc > 1) {
input = fopen(argv[1], "r");
if (input == NULL)
exit_perror("fopen(input)");
}
fprintf(stderr, "Note: Reading input into memory for timing accuracy\n");
char *input_data = 0;
size_t input_size = 0;
FILE *input_mem_file = open_memstream(&input_data, &input_size);
if (!input_mem_file)
exit_perror("open_memstream(input_mem_file)");
while (1) {
int ch = getc(input);
if (ch == EOF)
break;
putc((byte)ch, input_mem_file);
}
if (ferror(input))
exit_perror("getc(input)");
if (fflush(input_mem_file) != 0)
exit_perror("fflush(input_mem_file)");
if (fseeko(input_mem_file, 0, SEEK_SET) != 0)
exit_perror("fseeko(input_mem_file, 0)");
if (input != stdin)
fclose(input);
input = input_mem_file;
char *compressed_data = 0;
size_t compressed_size = 0;
FILE *compressed_file = open_memstream(&compressed_data, &compressed_size);
if (!compressed_file)
exit_perror("open_memstream(compressed_file)");
#if USE_STATIC_MODEL
fprintf(stderr, "Note: Starting static_model_calculate\n");
StaticModel the_static_model, *static_model = &the_static_model;
input = static_model_calculate(input, static_model);
#else
StaticModel *static_model = NULL;
#endif
fprintf(stderr, "Note: Starting recip_table_init\n");
recip_table_init();
int64_t time;
fprintf(stderr, "Note: Starting compression\n");
count_t count_bytes = compress(input, compressed_file, static_model, &time);
fprintf(stderr, "Note: Compression time %.6f seconds, %.2f ns per byte\n",
(double)time / 1e9, (double)time / count_bytes);
count_t compressed_bytes = ftello(compressed_file);
fprintf(stderr, "Note: Uncompressed %llu bytes, compressed %llu bytes, removed %.3f %%\n",
(unsigned long long)count_bytes,
(unsigned long long)compressed_bytes,
(double)((int64_t)count_bytes - (int64_t)compressed_bytes) * 100 / count_bytes);
if (fseeko(compressed_file, 0, SEEK_SET) != 0)
exit_perror("fseeko(compressed_file, 0)");
fprintf(stderr, "Note: Starting decompression\n");
decompress(count_bytes, compressed_file, stdout, static_model, &time);
fprintf(stderr, "Note: Decompression time %.6f seconds, %.2f ns per byte\n",
(double)time / 1e9,
(double)time / count_bytes);
fclose(compressed_file);
if (input != stdin)
fclose(input);
return EXIT_SUCCESS;
}