-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathread.cpp
313 lines (291 loc) · 8 KB
/
read.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
#include <cstdio>
#include <cstdlib>
#include "pcdecoder.h"
#include "rcqsmodel.h"
#include "front.h"
#include "fpzip.h"
#include "codec.h"
#include "read.h"
// array meta data and decoder
struct FPZinput : public FPZ {
RCdecoder* rd;
};
// allocate input stream
static FPZinput*
allocate_input()
{
FPZinput* stream = new FPZinput;
stream->type = FPZIP_TYPE_FLOAT;
stream->prec = 0;
stream->nx = stream->ny = stream->nz = stream->nf = 1;
stream->rd = 0;
return stream;
}
#if FPZIP_FP == FPZIP_FP_FAST || FPZIP_FP == FPZIP_FP_SAFE
// decompress 3D array at specified precision using floating-point arithmetic
template <typename T, uint bits>
static void
decompress3d(
RCdecoder* rd, // entropy decoder
T* data, // flattened 3D array to decompress to
uint nx, // number of x samples
uint ny, // number of y samples
uint nz // number of z samples
)
{
// initialize decompressor
typedef PCmap<T, bits> Map;
RCmodel* rm = new RCqsmodel(false, PCdecoder<T, Map>::symbols);
PCdecoder<T, Map>* fd = new PCdecoder<T, Map>(rd, &rm);
Front<T> f(nx, ny);
// decode difference between predicted (p) and actual (a) value
uint x, y, z;
for (z = 0, f.advance(0, 0, 1); z < nz; z++)
for (y = 0, f.advance(0, 1, 0); y < ny; y++)
for (x = 0, f.advance(1, 0, 0); x < nx; x++) {
#if FPZIP_FP == FPZIP_FP_SAFE
volatile T p = f(1, 1, 1);
p += f(1, 0, 0);
p -= f(0, 1, 1);
p += f(0, 1, 0);
p -= f(1, 0, 1);
p += f(0, 0, 1);
p -= f(1, 1, 0);
#else
T p = f(1, 0, 0) - f(0, 1, 1) +
f(0, 1, 0) - f(1, 0, 1) +
f(0, 0, 1) - f(1, 1, 0) +
f(1, 1, 1);
#endif
T a = fd->decode(p);
*data++ = a;
f.push(a);
}
delete fd;
delete rm;
}
#elif FPZIP_FP == FPZIP_FP_EMUL
#include "fpe.h"
// decompress 3D array at specified precision using floating-point emulation
template <typename T, uint bits>
static void
decompress3d(
RCdecoder* rd, // entropy encoder
T* data, // flattened 3D array to decompress to
uint nx, // number of x samples
uint ny, // number of y samples
uint nz // number of z samples
)
{
// initialize decompressor
typedef PCmap<T, bits> Map;
typedef FPE<T> Float;
RCmodel* rm = new RCqsmodel(false, PCdecoder<T, Map>::symbols);
PCdecoder<T, Map>* fd = new PCdecoder<T, Map>(rd, &rm);
Front<Float> f(nx, ny);
// decode difference between predicted (p) and actual (a) value
uint x, y, z;
for (z = 0, f.advance(0, 0, 1); z < nz; z++)
for (y = 0, f.advance(0, 1, 0); y < ny; y++)
for (x = 0, f.advance(1, 0, 0); x < nx; x++) {
Float p = f(1, 0, 0) - f(0, 1, 1) +
f(0, 1, 0) - f(1, 0, 1) +
f(0, 0, 1) - f(1, 1, 0) +
f(1, 1, 1);
T a = fd->decode(T(p));
*data++ = a;
f.push(a);
}
delete fd;
delete rm;
}
#else // FPZIP_FP_INT
// decompress 3D array at specified precision using integer arithmetic
template <typename T, uint bits>
static void
decompress3d(
RCdecoder* rd, // entropy decoder
T* data, // flattened 3D array to decompress to
uint nx, // number of x samples
uint ny, // number of y samples
uint nz // number of z samples
)
{
// initialize decompressor
typedef PCmap<T, bits> TMap;
typedef typename TMap::Range U;
typedef PCmap<U, bits, U> UMap;
RCmodel* rm = new RCqsmodel(false, PCdecoder<U, UMap>::symbols);
PCdecoder<U, UMap>* fd = new PCdecoder<U, UMap>(rd, &rm);
TMap map;
Front<U> f(nx, ny, map.forward(0));
// decode difference between predicted (p) and actual (a) value
uint x, y, z;
for (z = 0, f.advance(0, 0, 1); z < nz; z++)
for (y = 0, f.advance(0, 1, 0); y < ny; y++)
for (x = 0, f.advance(1, 0, 0); x < nx; x++) {
U p = f(1, 0, 0) - f(0, 1, 1) +
f(0, 1, 0) - f(1, 0, 1) +
f(0, 0, 1) - f(1, 1, 0) +
f(1, 1, 1);
U a = fd->decode(p);
*data++ = map.inverse(a);
f.push(a);
}
delete fd;
delete rm;
}
#endif
// decompress p-bit float, 2p-bit double
#define decompress_case(p)\
case subsize(T, p):\
decompress3d<T, subsize(T, p)>(stream->rd, data, stream->nx, stream->ny, stream->nz);\
break
// decompress 4D array
template <typename T>
static bool
decompress4d(
FPZinput* stream, // input stream
T* data // flattened 4D array to decompress to
)
{
// decompress one field at a time
for (int i = 0; i < stream->nf; i++) {
int bits = stream->prec ? stream->prec : (int)(CHAR_BIT * sizeof(T));
switch (bits) {
decompress_case( 2);
decompress_case( 3);
decompress_case( 4);
decompress_case( 5);
decompress_case( 6);
decompress_case( 7);
decompress_case( 8);
decompress_case( 9);
decompress_case(10);
decompress_case(11);
decompress_case(12);
decompress_case(13);
decompress_case(14);
decompress_case(15);
decompress_case(16);
decompress_case(17);
decompress_case(18);
decompress_case(19);
decompress_case(20);
decompress_case(21);
decompress_case(22);
decompress_case(23);
decompress_case(24);
decompress_case(25);
decompress_case(26);
decompress_case(27);
decompress_case(28);
decompress_case(29);
decompress_case(30);
decompress_case(31);
decompress_case(32);
default:
fpzip_errno = fpzipErrorBadPrecision;
return false;
}
data += stream->nx * stream->ny * stream->nz;
}
return true;
}
// read compressed stream from file
FPZ*
fpzip_read_from_file(
FILE* file // binary input stream
)
{
fpzip_errno = fpzipSuccess;
FPZinput* stream = allocate_input();
stream->rd = new RCfiledecoder(file);
stream->rd->init();
return static_cast<FPZ*>(stream);
}
// read compressed stream from memory buffer
FPZ*
fpzip_read_from_buffer(
const void* buffer // pointer to compressed data
)
{
fpzip_errno = fpzipSuccess;
FPZinput* stream = allocate_input();
stream->rd = new RCmemdecoder(buffer);
stream->rd->init();
return static_cast<FPZ*>(stream);
}
// close stream for reading and clean up
void
fpzip_read_close(
FPZ* fpz // stream handle
)
{
FPZinput* stream = static_cast<FPZinput*>(fpz);
delete stream->rd;
delete stream;
}
// read meta data
int
fpzip_read_header(
FPZ* fpz // stream handle
)
{
fpzip_errno = fpzipSuccess;
FPZinput* stream = static_cast<FPZinput*>(fpz);
RCdecoder* rd = stream->rd;
// magic
if (rd->decode<uint>(8) != 'f' ||
rd->decode<uint>(8) != 'p' ||
rd->decode<uint>(8) != 'z' ||
rd->decode<uint>(8) != '\0') {
fpzip_errno = fpzipErrorBadFormat;
return 0;
}
// format version
if (rd->decode<uint>(16) != FPZ_MAJ_VERSION ||
rd->decode<uint>(8) != FPZ_MIN_VERSION) {
fpzip_errno = fpzipErrorBadVersion;
return 0;
}
// type and precision
stream->type = rd->decode<uint>(1);
stream->prec = rd->decode<uint>(7);
// array dimensions
stream->nx = rd->decode<uint>(32);
stream->ny = rd->decode<uint>(32);
stream->nz = rd->decode<uint>(32);
stream->nf = rd->decode<uint>(32);
return 1;
}
// decompress a single- or double-precision 4D array
size_t
fpzip_read(
FPZ* fpz, // stream handle
void* data // array to read
)
{
fpzip_errno = fpzipSuccess;
size_t bytes = 0;
try {
FPZinput* stream = static_cast<FPZinput*>(fpz);
bool success = (stream->type == FPZIP_TYPE_FLOAT
? decompress4d(stream, static_cast<float*>(data))
: decompress4d(stream, static_cast<double*>(data)));
if (success) {
RCdecoder* rd = stream->rd;
if (rd->error) {
if (fpzip_errno == fpzipSuccess)
fpzip_errno = fpzipErrorReadStream;
}
else
bytes = rd->bytes();
}
}
catch (...) {
// exceptions indicate unrecoverable internal errors
fpzip_errno = fpzipErrorInternal;
}
return bytes;
}