-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrANS_test.c
204 lines (165 loc) · 4.61 KB
/
rANS_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
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <assert.h>
#include <sys/time.h>
#include "rANS_static.h"
#define ABS(a) ((a)>0?(a):-(a))
#ifndef BLK_SIZE
# define BLK_SIZE 1024*1024
#endif
// Room to allow for expanded BLK_SIZE on worst case compression.
#define BLK_SIZE2 ((int)(1.05*BLK_SIZE))
int main(int argc, char **argv) {
int opt, order = 0;
unsigned char in_buf[BLK_SIZE2+257*257*3];
int decode = 0, test = 0;
FILE *infp = stdin, *outfp = stdout;
struct timeval tv1, tv2, tv3, tv4;
size_t bytes = 0;
int renorm_size=0;
extern char *optarg;
extern int optind;
while ((opt = getopt(argc, argv, "o:dt186")) != -1) {
switch (opt) {
case 'o':
order = atoi(optarg);
break;
case 'd':
decode = 1;
break;
case 't':
test = 1;
break;
case '1': case '6': case '8':
renorm_size = renorm_size*10 + opt - '0';
break;
}
}
if (!renorm_size) renorm_size = 16;
printf("renorm_size=%d\n", renorm_size);
order = order ? 1 : 0; // Only support O(0) and O(1)
if (optind < argc) {
if (!(infp = fopen(argv[optind], "rb"))) {
perror(argv[optind]);
return 1;
}
optind++;
}
if (optind < argc) {
if (!(outfp = fopen(argv[optind], "wb"))) {
perror(argv[optind]);
return 1;
}
optind++;
}
gettimeofday(&tv1, NULL);
if (test) {
size_t len, in_sz = 0, out_sz = 0;
typedef struct {
unsigned char *blk;
uint32_t sz;
} blocks;
blocks *b = NULL, *bc = NULL, *bu = NULL;
int nb = 0, i;
while ((len = fread(in_buf, 1, BLK_SIZE, infp)) != 0) {
// inefficient, but it'll do for testing
b = realloc(b, (nb+1)*sizeof(*b));
bu = realloc(bu, (nb+1)*sizeof(*bu));
bc = realloc(bc, (nb+1)*sizeof(*bc));
b[nb].blk = malloc(len);
b[nb].sz = len;
memcpy(b[nb].blk, in_buf, len);
bc[nb].sz = rans_compress_bound(renorm_size, BLK_SIZE, order);
bc[nb].blk = malloc(bc[nb].sz);
bu[nb].sz = BLK_SIZE;
bu[nb].blk = malloc(BLK_SIZE);
nb++;
in_sz += len;
}
#ifndef NTRIALS
#define NTRIALS 10
#endif
int trials = NTRIALS;
while (trials--) {
// Warmup
for (i = 0; i < nb; i++) memset(bc[i].blk, 0, bc[i].sz);
gettimeofday(&tv1, NULL);
out_sz = 0;
for (i = 0; i < nb; i++) {
unsigned int csz = bc[i].sz;
bc[i].blk = rans_compress_to(renorm_size, b[i].blk, b[i].sz,
bc[i].blk, &csz, order);
assert(csz <= bc[i].sz);
out_sz += 5 + csz;
}
gettimeofday(&tv2, NULL);
// Warmup
for (i = 0; i < nb; i++) memset(bu[i].blk, 0, BLK_SIZE);
gettimeofday(&tv3, NULL);
for (i = 0; i < nb; i++)
bu[i].blk = rans_uncompress_to(renorm_size, bc[i].blk, bc[i].sz,
bu[i].blk, &bu[i].sz, order);
gettimeofday(&tv4, NULL);
for (i = 0; i < nb; i++) {
if (b[i].sz != bu[i].sz || memcmp(b[i].blk, bu[i].blk, b[i].sz))
fprintf(stderr, "Mismatch in block %d, sz %d/%d\n", i, b[i].sz, bu[i].sz);
//free(bc[i].blk);
//free(bu[i].blk);
}
fprintf(stderr, "%5.1f MB/s enc, %5.1f MB/s dec\t %lld bytes -> %lld bytes\n",
(double)in_sz / ((long)(tv2.tv_sec - tv1.tv_sec)*1000000 +
tv2.tv_usec - tv1.tv_usec),
(double)in_sz / ((long)(tv4.tv_sec - tv3.tv_sec)*1000000 +
tv4.tv_usec - tv3.tv_usec),
(long long)in_sz, (long long)out_sz);
}
exit(0);
}
if (decode) {
// Only used in some test implementations of RC_GetFreq()
//RC_init();
//RC_init2();
for (;;) {
uint32_t in_size, out_size;
unsigned char *out;
order = fgetc(infp);
if (4 != fread(&in_size, 1, 4, infp))
break;
if (in_size != fread(in_buf, 1, in_size, infp)) {
fprintf(stderr, "Truncated input\n");
exit(1);
}
out = rans_uncompress(renorm_size, in_buf, in_size, &out_size, order);
if (!out)
abort();
fwrite(out, 1, out_size, outfp);
free(out);
bytes += out_size;
}
} else {
for (;;) {
uint32_t in_size, out_size;
unsigned char *out;
in_size = fread(in_buf, 1, BLK_SIZE, infp);
if (in_size <= 0)
break;
out = rans_compress(renorm_size, in_buf, in_size, &out_size,
order && in_size >= 4);
fputc(order && in_size >= 4, outfp);
fwrite(&out_size, 1, 4, outfp);
fwrite(out, 1, out_size, outfp);
free(out);
bytes += in_size;
}
}
gettimeofday(&tv2, NULL);
fprintf(stderr, "Took %ld microseconds, %5.1f MB/s\n",
(long)(tv2.tv_sec - tv1.tv_sec)*1000000 +
tv2.tv_usec - tv1.tv_usec,
(double)bytes / ((long)(tv2.tv_sec - tv1.tv_sec)*1000000 +
tv2.tv_usec - tv1.tv_usec));
return 0;
}