Repository navigation
Expand file tree
/
Copy pathdecode.c
More file actions
287 lines (257 loc) · 8.5 KB
/
Copy pathdecode.c
File metadata and controls
287 lines (257 loc) · 8.5 KB
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
#include <limits.h>
static void _t2(inv_xform, Int, DIMS)(Int* p);
/* private functions ------------------------------------------------------- */
/* inverse lifting transform of 4-vector */
static void
_t1(inv_lift, Int)(Int* p, ptrdiff_t s)
{
Int x, y, z, w;
x = *p; p += s;
y = *p; p += s;
z = *p; p += s;
w = *p; p += s;
/*
** non-orthogonal transform
** ( 4 6 -4 -1) (x)
** 1/4 * ( 4 2 4 5) (y)
** ( 4 -2 4 -5) (z)
** ( 4 -6 -4 1) (w)
*/
y += w >> 1; w -= y >> 1;
y += w; w <<= 1; w -= y;
z += x; x <<= 1; x -= z;
y += z; z <<= 1; z -= y;
w += x; x <<= 1; x -= w;
p -= s; *p = w;
p -= s; *p = z;
p -= s; *p = y;
p -= s; *p = x;
}
#if ZFP_ROUNDING_MODE == ZFP_ROUND_LAST
/* bias values such that truncation is equivalent to round to nearest */
static void
_t1(inv_round, UInt)(UInt* ublock, uint n, uint m, uint prec)
{
/* add 1/6 ulp to unbias errors */
if (prec < (uint)(CHAR_BIT * sizeof(UInt) - 1)) {
/* the first m values (0 <= m <= n) have one more bit of precision */
n -= m;
while (m--) *ublock++ += ((NBMASK >> 2) >> prec);
while (n--) *ublock++ += ((NBMASK >> 1) >> prec);
}
}
#endif
/* map two's complement signed integer to negabinary unsigned integer */
static Int
_t1(uint2int, UInt)(UInt x)
{
return (Int)((x ^ NBMASK) - NBMASK);
}
/* reorder unsigned coefficients and convert to signed integer */
static void
_t1(inv_order, Int)(const UInt* ublock, Int* iblock, const uchar* perm, uint n)
{
do
iblock[*perm++] = _t1(uint2int, UInt)(*ublock++);
while (--n);
}
/* decompress sequence of size <= 64 unsigned integers */
static uint
_t1(decode_few_ints, UInt)(bitstream* restrict_ stream, uint maxbits, uint maxprec, UInt* restrict_ data, uint size)
{
/* make a copy of bit stream to avoid aliasing */
bitstream s = *stream;
uint intprec = (uint)(CHAR_BIT * sizeof(UInt));
uint kmin = intprec > maxprec ? intprec - maxprec : 0;
uint bits = maxbits;
uint i, k, m, n;
uint64 x;
/* initialize data array to all zeros */
for (i = 0; i < size; i++)
data[i] = 0;
/* decode one bit plane at a time from MSB to LSB */
for (k = intprec, m = n = 0; bits && (m = 0, k-- > kmin);) {
/* step 1: decode first n bits of bit plane #k */
m = MIN(n, bits);
bits -= m;
x = stream_read_bits(&s, m);
/* step 2: unary run-length decode remainder of bit plane */
for (; bits && n < size; n++, m = n) {
bits--;
if (stream_read_bit(&s)) {
/* positive group test; scan for next one-bit */
for (; bits && n < size - 1; n++) {
bits--;
if (stream_read_bit(&s))
break;
}
/* set bit and continue decoding bit plane */
x += (uint64)1 << n;
}
else {
/* negative group test; done with bit plane */
m = size;
break;
}
}
/* step 3: deposit bit plane from x */
for (i = 0; x; i++, x >>= 1)
data[i] += (UInt)(x & 1u) << k;
}
#if ZFP_ROUNDING_MODE == ZFP_ROUND_LAST
/* bias values to achieve proper rounding */
_t1(inv_round, UInt)(data, size, m, intprec - k);
#endif
*stream = s;
return maxbits - bits;
}
/* decompress sequence of size > 64 unsigned integers */
static uint
_t1(decode_many_ints, UInt)(bitstream* restrict_ stream, uint maxbits, uint maxprec, UInt* restrict_ data, uint size)
{
/* make a copy of bit stream to avoid aliasing */
bitstream s = *stream;
uint intprec = (uint)(CHAR_BIT * sizeof(UInt));
uint kmin = intprec > maxprec ? intprec - maxprec : 0;
uint bits = maxbits;
uint i, k, m, n;
/* initialize data array to all zeros */
for (i = 0; i < size; i++)
data[i] = 0;
/* decode one bit plane at a time from MSB to LSB */
for (k = intprec, m = n = 0; bits && (m = 0, k-- > kmin);) {
/* step 1: decode first n bits of bit plane #k */
m = MIN(n, bits);
bits -= m;
for (i = 0; i < m; i++)
if (stream_read_bit(&s))
data[i] += (UInt)1 << k;
/* step 2: unary run-length decode remainder of bit plane */
for (; bits && n < size; n++, m = n) {
bits--;
if (stream_read_bit(&s)) {
/* positive group test; scan for next one-bit */
for (; bits && n < size - 1; n++) {
bits--;
if (stream_read_bit(&s))
break;
}
/* set bit and continue decoding bit plane */
data[n] += (UInt)1 << k;
}
else {
/* negative group test; done with bit plane */
m = size;
break;
}
}
}
#if ZFP_ROUNDING_MODE == ZFP_ROUND_LAST
/* bias values to achieve proper rounding */
_t1(inv_round, UInt)(data, size, m, intprec - k);
#endif
*stream = s;
return maxbits - bits;
}
/* decompress sequence of size <= 64 unsigned integers with no rate constraint */
static uint
_t1(decode_few_ints_prec, UInt)(bitstream* restrict_ stream, uint maxprec, UInt* restrict_ data, uint size)
{
/* make a copy of bit stream to avoid aliasing */
bitstream s = *stream;
size_t offset = stream_rtell(&s);
uint intprec = (uint)(CHAR_BIT * sizeof(UInt));
uint kmin = intprec > maxprec ? intprec - maxprec : 0;
uint i, k, n;
/* initialize data array to all zeros */
for (i = 0; i < size; i++)
data[i] = 0;
/* decode one bit plane at a time from MSB to LSB */
for (k = intprec, n = 0; k-- > kmin;) {
/* step 1: decode first n bits of bit plane #k */
uint64 x = stream_read_bits(&s, n);
/* step 2: unary run-length decode remainder of bit plane */
for (; n < size && stream_read_bit(&s); x += (uint64)1 << n, n++)
for (; n < size - 1 && !stream_read_bit(&s); n++)
;
/* step 3: deposit bit plane from x */
for (i = 0; x; i++, x >>= 1)
data[i] += (UInt)(x & 1u) << k;
}
#if ZFP_ROUNDING_MODE == ZFP_ROUND_LAST
/* bias values to achieve proper rounding */
_t1(inv_round, UInt)(data, size, 0, intprec - k);
#endif
*stream = s;
return (uint)(stream_rtell(&s) - offset);
}
/* decompress sequence of size > 64 unsigned integers with no rate constraint */
static uint
_t1(decode_many_ints_prec, UInt)(bitstream* restrict_ stream, uint maxprec, UInt* restrict_ data, uint size)
{
/* make a copy of bit stream to avoid aliasing */
bitstream s = *stream;
size_t offset = stream_rtell(&s);
uint intprec = (uint)(CHAR_BIT * sizeof(UInt));
uint kmin = intprec > maxprec ? intprec - maxprec : 0;
uint i, k, n;
/* initialize data array to all zeros */
for (i = 0; i < size; i++)
data[i] = 0;
/* decode one bit plane at a time from MSB to LSB */
for (k = intprec, n = 0; k-- > kmin;) {
/* step 1: decode first n bits of bit plane #k */
for (i = 0; i < n; i++)
if (stream_read_bit(&s))
data[i] += (UInt)1 << k;
/* step 2: unary run-length decode remainder of bit plane */
for (; n < size && stream_read_bit(&s); data[n] += (UInt)1 << k, n++)
for (; n < size - 1 && !stream_read_bit(&s); n++)
;
}
#if ZFP_ROUNDING_MODE == ZFP_ROUND_LAST
/* bias values to achieve proper rounding */
_t1(inv_round, UInt)(data, size, 0, intprec - k);
#endif
*stream = s;
return (uint)(stream_rtell(&s) - offset);
}
/* decompress sequence of size unsigned integers */
static uint
_t1(decode_ints, UInt)(bitstream* restrict_ stream, uint maxbits, uint maxprec, UInt* restrict_ data, uint size)
{
/* use fastest available decoder implementation */
if (with_maxbits(maxbits, maxprec, size)) {
/* rate constrained path: decode partial bit planes */
if (size <= 64)
return _t1(decode_few_ints, UInt)(stream, maxbits, maxprec, data, size); /* 1D, 2D, 3D blocks */
else
return _t1(decode_many_ints, UInt)(stream, maxbits, maxprec, data, size); /* 4D blocks */
}
else {
/* variable-rate path: decode whole bit planes */
if (size <= 64)
return _t1(decode_few_ints_prec, UInt)(stream, maxprec, data, size); /* 1D, 2D, 3D blocks */
else
return _t1(decode_many_ints_prec, UInt)(stream, maxprec, data, size); /* 4D blocks */
}
}
/* decode block of integers */
static uint
_t2(decode_block, Int, DIMS)(bitstream* stream, int minbits, int maxbits, int maxprec, Int* iblock)
{
int bits;
cache_align_(UInt ublock[BLOCK_SIZE]);
/* decode integer coefficients */
bits = _t1(decode_ints, UInt)(stream, maxbits, maxprec, ublock, BLOCK_SIZE);
/* read at least minbits bits */
if (bits < minbits) {
stream_skip(stream, minbits - bits);
bits = minbits;
}
/* reorder unsigned coefficients and convert to signed integer */
_t1(inv_order, Int)(ublock, iblock, PERM, BLOCK_SIZE);
/* perform decorrelating transform */
_t2(inv_xform, Int, DIMS)(iblock);
return bits;
}